summaryrefslogtreecommitdiffstats
path: root/vespa-documentgen-plugin
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2016-11-15 00:07:46 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2016-11-15 00:07:46 +0100
commit2126b575120e3d8166ce668341886abde62fde07 (patch)
tree5ec5662a8d555a2c2c6cd479aa219e30aaf3e053 /vespa-documentgen-plugin
parent43ef97313b369a484ec983b814e397720bb80a13 (diff)
Use constant time hash lookups for non-struct and non-string fields for getFieldValue() and getField().
Diffstat (limited to 'vespa-documentgen-plugin')
-rw-r--r--vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java43
1 files changed, 22 insertions, 21 deletions
diff --git a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
index db9a2dbb062..39552452a8a 100644
--- a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
+++ b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
@@ -440,7 +440,7 @@ public class DocumentGenMojo extends AbstractMojo {
exportStructTypeGetter(docType.getName()+".header", docType.allHeader().getFields(), out, 1, "getHeaderStructType", "com.yahoo.document.StructDataType");
exportStructTypeGetter(docType.getName()+".body", docType.allBody().getFields(), out, 1, "getBodyStructType", "com.yahoo.document.StructDataType");
- exportStructTypeGetter(docType.getName(), docType.getAllFields(), out, 1, "getDocumentType", "com.yahoo.document.DocumentType");
+ exportExtendedStructTypeGetter(docType.getName(), docType.getAllFields(), out, 1, "getDocumentType", "com.yahoo.document.DocumentType");
exportCopyConstructor(className, docType.getAllFields(), out, 1, true);
exportFieldsAndAccessors(className, "com.yahoo.document.Document".equals(superType) ? docType.getAllFields() : docType.getFields(), out, 1, true);
exportDocumentMethods(docType.getAllFields(), out, 1);
@@ -527,6 +527,17 @@ public class DocumentGenMojo extends AbstractMojo {
out.write(ind(ind+1)+"return ret;\n");
out.write(ind(ind)+"}\n\n");
}
+ private static void exportExtendedStructTypeGetter(String name, Collection<Field> fields, Writer out, int ind, String methodName, String retType) throws IOException {
+ out.write(ind(ind)+"private static "+retType+" "+methodName+"() {\n" +
+ ind(ind+1)+retType+" ret = new "+retType+"(\""+name+"\");\n");
+ for (Field f : fields) {
+ out.write(ind(ind+1)+"ret.addField(new com.yahoo.document.ExtendedField(\""+f.getName()+"\", "+ toJavaReference(f.getDataType())+
+ ", new com.yahoo.document.ExtendedField.Extract() { Object get(com.yahoo.document.Document doc) {return doc." + getter(f.getName()) + "(); } }));\n" );
+
+ }
+ out.write(ind(ind+1)+"return ret;\n");
+ out.write(ind(ind)+"}\n\n");
+ }
private static void exportOverriddenStructGetter(Collection<Field> fields, Writer out, int ind, String methodName, String structType) throws IOException {
out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.Struct "+methodName+"() {\n" +
@@ -542,8 +553,7 @@ public class DocumentGenMojo extends AbstractMojo {
/**
* Exports the necessary overridden methods from Document/StructuredFieldValue
*/
- private static void exportDocumentMethods(Collection<Field> fieldSet, Writer out,
- int ind) throws IOException {
+ private static void exportDocumentMethods(Collection<Field> fieldSet, Writer out, int ind) throws IOException {
exportGetFieldCount(fieldSet, out, ind);
exportGetField(fieldSet, out, ind);
exportGetFieldValue(fieldSet, out, ind);
@@ -671,8 +681,7 @@ public class DocumentGenMojo extends AbstractMojo {
ind(ind)+"}\n\n");
}
- private static void exportGetFieldValue(Collection<Field> fieldSet, Writer out,
- int ind) throws IOException {
+ private static void exportGetFieldValue(Collection<Field> fieldSet, Writer out, int ind) throws IOException {
out.write(ind(ind)+"@Override public com.yahoo.document.datatypes.FieldValue getFieldValue(com.yahoo.document.Field field) {\n");
out.write(ind(ind+1)+"if (field==null) return null;\n");
for (Field field: fieldSet) {
@@ -689,27 +698,19 @@ public class DocumentGenMojo extends AbstractMojo {
ind(ind+1)+"}\n");
} else if (field.getDataType() instanceof StructDataType) {
out.write(ind(ind+1)+"if (\""+name+"\".equals(field.getName())) return "+name+";\n");
- } else {
- out.write(
- ind(ind+1)+"if (\""+name+"\".equals(field.getName())) { if ("+getter(name)+"()==null) return null; return field.getDataType().createFieldValue("+getter(name)+"()); }\n");
}
}
- out.write(
- ind(ind+1)+"return super.getFieldValue(field);\n" +
- ind(ind)+"}\n\n");
+
+ out.write(ind(ind+1)+"if (field instanceof ExtendedField) return ((ExtendedField)field).extractFieldValue(this);\n");
+ out.write(ind(ind+1)+"return super.getFieldValue(field);\n");
+ out.write(ind(ind)+"}\n\n");
}
private static void exportGetField(Collection<Field> fieldSet, Writer out, int ind) throws IOException {
- out.write(ind(ind)+"@Override public com.yahoo.document.Field getField(String fieldName) {\n");
- out.write(ind(ind+1)+"if (fieldName==null) return null;\n");
- for (Field field: fieldSet) {
- String name = field.getName();
- out.write(
- ind(ind+1)+"if (\""+name+"\".equals(fieldName) ) { return new com.yahoo.document.Field(fieldName, "+toJavaReference(field.getDataType())+"); }\n"); // TODO what now...
- }
- out.write(
- ind(ind+1)+"return null;\n" +
- ind(ind)+"}\n\n");
+ out.write(ind(ind) + "@Override public com.yahoo.document.Field getField(String fieldName) {\n");
+ out.write(ind(ind+1) + "if (fieldName==null) return null;\n");
+ out.write(ind(ind+1) + "return type.getField(fieldName);\n");
+ out.write(ind(ind)+"}\n\n");
}
/**