From 40744a1a30cccff9c4b4b9f67ae6891956509ee3 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Mon, 4 Jul 2022 22:47:21 +0200 Subject: Add option to ignore undefined fields --- .../src/main/java/com/yahoo/document/DataType.java | 2 +- .../com/yahoo/document/DocumentTypeManager.java | 27 +++++++---------- .../document/DocumentTypeManagerConfigurer.java | 6 ++-- .../java/com/yahoo/document/json/JsonReader.java | 4 +-- .../document/json/readers/AddRemoveCreator.java | 19 ++++++------ .../yahoo/document/json/readers/ArrayReader.java | 9 +++--- .../document/json/readers/CompositeReader.java | 10 +++---- .../com/yahoo/document/json/readers/MapReader.java | 35 ++++++++++++---------- .../document/json/readers/SingleValueReader.java | 8 ++--- .../yahoo/document/json/readers/StructReader.java | 20 ++++++------- .../json/readers/VespaJsonDocumentReader.java | 28 ++++++++++------- 11 files changed, 87 insertions(+), 81 deletions(-) (limited to 'document/src/main/java') diff --git a/document/src/main/java/com/yahoo/document/DataType.java b/document/src/main/java/com/yahoo/document/DataType.java index b185ff53c87..e47c0c3a81e 100644 --- a/document/src/main/java/com/yahoo/document/DataType.java +++ b/document/src/main/java/com/yahoo/document/DataType.java @@ -34,7 +34,7 @@ import java.util.List; public abstract class DataType extends Identifiable implements Comparable { // The global class identifier shared with C++. - public static int classId = registerClass(Ids.document + 50, DataType.class); + public static final int classId = registerClass(Ids.document + 50, DataType.class); // NOTE: These types are also defined in // document/src/vespa/document/datatype/datatype.h diff --git a/document/src/main/java/com/yahoo/document/DocumentTypeManager.java b/document/src/main/java/com/yahoo/document/DocumentTypeManager.java index b5c9e54939e..379e0836a01 100644 --- a/document/src/main/java/com/yahoo/document/DocumentTypeManager.java +++ b/document/src/main/java/com/yahoo/document/DocumentTypeManager.java @@ -2,7 +2,6 @@ package com.yahoo.document; import com.yahoo.component.annotation.Inject; -import com.yahoo.config.subscription.ConfigSubscriber; import com.yahoo.document.annotation.AnnotationType; import com.yahoo.document.annotation.AnnotationTypeRegistry; import com.yahoo.document.annotation.AnnotationTypes; @@ -21,12 +20,11 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.logging.Logger; /** * The DocumentTypeManager keeps track of the document types registered in * the Vespa common repository. - *

+ * * The DocumentTypeManager is also responsible for registering a FieldValue * factory for each data type a field can have. The Document object * uses this factory to serialize and deserialize the various datatypes. @@ -38,9 +36,6 @@ import java.util.logging.Logger; */ public class DocumentTypeManager { - private final static Logger log = Logger.getLogger(DocumentTypeManager.class.getName()); - - // *Configured data types* (not built-in/primitive) indexed by their id // // *Primitive* data types are always available and have a single id. @@ -54,6 +49,7 @@ public class DocumentTypeManager { private Map dataTypes = new LinkedHashMap<>(); private Map documentTypes = new LinkedHashMap<>(); private AnnotationTypeRegistry annotationTypeRegistry = new AnnotationTypeRegistry(); + private boolean ignoreUndefinedFields = false; public DocumentTypeManager() { registerDefaultDataTypes(); @@ -103,22 +99,12 @@ public class DocumentTypeManager { } } - boolean hasDataTypeInternal(String name) { - if (name.startsWith("tensor(")) return true; // built-in dynamic: Always present - for (DataType type : dataTypes.values()) { - if (type.getName().equalsIgnoreCase(name)) { - return true; - } - } - return false; - } - /** * For internal use only, avoid whenever possible. * Use constants and factories in DataType instead. * For structs, use getStructType() in DocumentType. * For annotation payloads, use getDataType() in AnnotationType. - **/ + */ DataType getDataTypeInternal(String name) { if (name.startsWith("tensor(")) // built-in dynamic return new TensorDataType(TensorType.fromSpec(name)); @@ -150,6 +136,13 @@ public class DocumentTypeManager { return foundTypes.get(0); } + /** + * Returns true if we should ignore attempts to set a field not defined in the document type, + * rather than (by default) throwing an exception. + */ + public boolean getIgnoreUndefinedFields() { return ignoreUndefinedFields; } + public void setIgnoreUndefinedFields(boolean ignoreUndefinedFields) { this.ignoreUndefinedFields = ignoreUndefinedFields; } + /** * Return a data type instance * diff --git a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java index d6833a482f1..518a80ca058 100644 --- a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java +++ b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java @@ -60,9 +60,8 @@ public class DocumentTypeManagerConfigurer implements ConfigSubscriber.SingleSub /** One-shot configuration; should be called on a newly constructed manager */ static void configureNewManager(DocumentmanagerConfig config, DocumentTypeManager manager) { - if (config == null) { - return; - } + if (config == null) return; + manager.setIgnoreUndefinedFields(config.ignoreundefinedfields()); new Apply(config, manager); if (config.datatype().size() == 0 && config.annotationtype().size() == 0) { new ApplyNewDoctypeConfig(config, manager); @@ -328,6 +327,7 @@ public class DocumentTypeManagerConfigurer implements ConfigSubscriber.SingleSub public ApplyNewDoctypeConfig(DocumentmanagerConfig config, DocumentTypeManager manager) { this.manager = manager; this.usev8geopositions = config.usev8geopositions(); + apply(config); } diff --git a/document/src/main/java/com/yahoo/document/json/JsonReader.java b/document/src/main/java/com/yahoo/document/json/JsonReader.java index 009fcb3de28..94ce986fc81 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonReader.java +++ b/document/src/main/java/com/yahoo/document/json/JsonReader.java @@ -71,7 +71,7 @@ public class JsonReader { throw new IllegalArgumentException(e); } documentParseInfo.operationType = operationType; - VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(); + VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(typeManager.getIgnoreUndefinedFields()); DocumentOperation operation = vespaJsonDocumentReader.createDocumentOperation( getDocumentTypeFromString(documentParseInfo.documentId.getDocType(), typeManager), documentParseInfo); operation.setCondition(TestAndSetCondition.fromConditionString(documentParseInfo.condition)); @@ -103,7 +103,7 @@ public class JsonReader { state = END_OF_FEED; return null; } - VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(); + VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(typeManager.getIgnoreUndefinedFields()); DocumentOperation operation = vespaJsonDocumentReader.createDocumentOperation( getDocumentTypeFromString(documentParseInfo.get().documentId.getDocType(), typeManager), documentParseInfo.get()); diff --git a/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java b/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java index b0882c3ab03..bc214f18776 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java +++ b/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java @@ -21,22 +21,22 @@ public class AddRemoveCreator { // yes, this suppresswarnings ugliness is by intention, the code relies on // the contracts in the builders - @SuppressWarnings({ "cast", "rawtypes", "unchecked" }) - public static void createAdds(TokenBuffer buffer, Field field, FieldUpdate update) { - createAddsOrRemoves(buffer, field, update, false); + @SuppressWarnings("cast") + public static void createAdds(TokenBuffer buffer, Field field, FieldUpdate update, boolean ignoreUndefinedFields) { + createAddsOrRemoves(buffer, field, update, false, ignoreUndefinedFields); } // yes, this suppresswarnings ugliness is by intention, the code relies on // the contracts in the builders - @SuppressWarnings({ "cast", "rawtypes", "unchecked" }) - public static void createRemoves(TokenBuffer buffer, Field field, FieldUpdate update) { - createAddsOrRemoves(buffer, field, update, true); + @SuppressWarnings("cast") + public static void createRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean ignoreUndefinedFields) { + createAddsOrRemoves(buffer, field, update, true, ignoreUndefinedFields); } // yes, this suppresswarnings ugliness is by intention, the code relies on // the contracts in the builders @SuppressWarnings({ "cast", "rawtypes", "unchecked" }) - private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) { + private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove, boolean ignoreUndefinedFields) { FieldValue container = field.getDataType().createFieldValue(); FieldUpdate singleUpdate; int initNesting = buffer.nesting(); @@ -45,10 +45,9 @@ public class AddRemoveCreator { if (container instanceof CollectionFieldValue) { buffer.next(); DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType(); - if (container instanceof WeightedSet) { + if (container instanceof WeightedSet weightedSet) { // these are objects with string keys (which are the nested // types) and values which are the weight - WeightedSet weightedSet = (WeightedSet) container; fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet); if (isRemove) { singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet); @@ -58,7 +57,7 @@ public class AddRemoveCreator { } } else { List arrayContents = new ArrayList<>(); - ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents); + ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents, ignoreUndefinedFields); if (buffer.currentToken() != JsonToken.END_ARRAY) { throw new IllegalArgumentException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'."); } diff --git a/document/src/main/java/com/yahoo/document/json/readers/ArrayReader.java b/document/src/main/java/com/yahoo/document/json/readers/ArrayReader.java index 9c27b1eb9d0..e8c4ae85356 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/ArrayReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/ArrayReader.java @@ -15,22 +15,23 @@ import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue; public class ArrayReader { - public static void fillArrayUpdate(TokenBuffer buffer, int initNesting, DataType valueType, List arrayContents) { + public static void fillArrayUpdate(TokenBuffer buffer, int initNesting, DataType valueType, + List arrayContents, boolean ignoreUndefinedFields) { while (buffer.nesting() >= initNesting) { Preconditions.checkArgument(buffer.currentToken() != JsonToken.VALUE_NULL, "Illegal null value for array entry"); - arrayContents.add(readSingleValue(buffer, valueType)); + arrayContents.add(readSingleValue(buffer, valueType, ignoreUndefinedFields)); buffer.next(); } } @SuppressWarnings({ "unchecked", "rawtypes" }) - public static void fillArray(TokenBuffer buffer, CollectionFieldValue parent, DataType valueType) { + public static void fillArray(TokenBuffer buffer, CollectionFieldValue parent, DataType valueType, boolean ignoreUndefinedFields) { int initNesting = buffer.nesting(); expectArrayStart(buffer.currentToken()); buffer.next(); while (buffer.nesting() >= initNesting) { Preconditions.checkArgument(buffer.currentToken() != JsonToken.VALUE_NULL, "Illegal null value for array entry"); - parent.add(readSingleValue(buffer, valueType)); + parent.add(readSingleValue(buffer, valueType, ignoreUndefinedFields)); buffer.next(); } } diff --git a/document/src/main/java/com/yahoo/document/json/readers/CompositeReader.java b/document/src/main/java/com/yahoo/document/json/readers/CompositeReader.java index 91274144710..a2dd91b90a0 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/CompositeReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/CompositeReader.java @@ -19,10 +19,10 @@ import static com.yahoo.document.json.readers.WeightedSetReader.fillWeightedSet; public class CompositeReader { - // TODO createComposite is extremely similar to add/remove, refactor + // TODO: reateComposite is extremely similar to add/remove, refactor // yes, this suppresswarnings ugliness is by intention, the code relies on the contracts in the builders @SuppressWarnings({ "cast", "rawtypes" }) - public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue) { + public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue, boolean ignoreUndefinedFields) { JsonToken token = buffer.currentToken(); if ((token != JsonToken.START_OBJECT) && (token != JsonToken.START_ARRAY)) { throw new IllegalArgumentException("Expected '[' or '{'. Got '" + token + "'."); @@ -32,14 +32,14 @@ public class CompositeReader { if (fieldValue instanceof WeightedSet) { fillWeightedSet(buffer, valueType, (WeightedSet) fieldValue); } else { - fillArray(buffer, (CollectionFieldValue) fieldValue, valueType); + fillArray(buffer, (CollectionFieldValue) fieldValue, valueType, ignoreUndefinedFields); } } else if (fieldValue instanceof MapFieldValue) { - MapReader.fillMap(buffer, (MapFieldValue) fieldValue); + MapReader.fillMap(buffer, (MapFieldValue) fieldValue, ignoreUndefinedFields); } else if (PositionDataType.INSTANCE.equals(fieldValue.getDataType())) { GeoPositionReader.fillGeoPosition(buffer, fieldValue); } else if (fieldValue instanceof StructuredFieldValue) { - StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue); + StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue, ignoreUndefinedFields); } else if (fieldValue instanceof TensorFieldValue) { TensorReader.fillTensor(buffer, (TensorFieldValue) fieldValue); } else { diff --git a/document/src/main/java/com/yahoo/document/json/readers/MapReader.java b/document/src/main/java/com/yahoo/document/json/readers/MapReader.java index fa8f9bfd6c5..1d4cb85d130 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/MapReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/MapReader.java @@ -25,21 +25,22 @@ import static com.yahoo.document.json.readers.SingleValueReader.readSingleUpdate import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue; public class MapReader { + public static final String MAP_KEY = "key"; public static final String MAP_VALUE = "value"; public static final String UPDATE_ELEMENT = "element"; public static final String UPDATE_MATCH = "match"; - public static void fillMap(TokenBuffer buffer, MapFieldValue parent) { + public static void fillMap(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) { if (buffer.currentToken() == JsonToken.START_ARRAY) { - MapReader.fillMapFromArray(buffer, parent); + MapReader.fillMapFromArray(buffer, parent, ignoreUndefinedFields); } else { - MapReader.fillMapFromObject(buffer, parent); + MapReader.fillMapFromObject(buffer, parent, ignoreUndefinedFields); } } @SuppressWarnings({ "rawtypes", "cast", "unchecked" }) - public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent) { + public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) { JsonToken token = buffer.currentToken(); int initNesting = buffer.nesting(); expectArrayStart(token); @@ -53,9 +54,9 @@ public class MapReader { token = buffer.next(); for (int i = 0; i < 2; ++i) { if (MAP_KEY.equals(buffer.currentName())) { - key = readSingleValue(buffer, keyType); + key = readSingleValue(buffer, keyType, ignoreUndefinedFields); } else if (MAP_VALUE.equals(buffer.currentName())) { - value = readSingleValue(buffer, valueType); + value = readSingleValue(buffer, valueType, ignoreUndefinedFields); } token = buffer.next(); } @@ -68,7 +69,7 @@ public class MapReader { } @SuppressWarnings({ "rawtypes", "cast", "unchecked" }) - public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) { + public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) { JsonToken token = buffer.currentToken(); int initNesting = buffer.nesting(); expectObjectStart(token); @@ -77,7 +78,7 @@ public class MapReader { DataType valueType = parent.getDataType().getValueType(); while (buffer.nesting() >= initNesting) { FieldValue key = readAtomic(buffer.currentName(), keyType); - FieldValue value = readSingleValue(buffer, valueType); + FieldValue value = readSingleValue(buffer, valueType, ignoreUndefinedFields); Preconditions.checkState(key != null && value != null, "Missing key or value for map entry."); parent.put(key, value); @@ -87,7 +88,11 @@ public class MapReader { } @SuppressWarnings({ "rawtypes", "unchecked" }) - public static ValueUpdate createMapUpdate(TokenBuffer buffer, DataType currentLevel, FieldValue keyParent, FieldValue topLevelKey) { + public static ValueUpdate createMapUpdate(TokenBuffer buffer, + DataType currentLevel, + FieldValue keyParent, + FieldValue topLevelKey, + boolean ignoreUndefinedFields) { TokenBuffer.Token element = buffer.prefetchScalar(UPDATE_ELEMENT); if (UPDATE_ELEMENT.equals(buffer.currentName())) { buffer.next(); @@ -102,24 +107,24 @@ public class MapReader { if (!UPDATE_MATCH.equals(buffer.currentName())) { // we have reached an action... if (topLevelKey == null) { - return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName())); + return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName(), ignoreUndefinedFields)); } else { - return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName())); + return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName(), ignoreUndefinedFields)); } } else { // next level of matching if (topLevelKey == null) { - return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key); + return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key, ignoreUndefinedFields); } else { - return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey); + return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey, ignoreUndefinedFields); } } } @SuppressWarnings("rawtypes") - public static ValueUpdate createMapUpdate(TokenBuffer buffer, Field field) { + public static ValueUpdate createMapUpdate(TokenBuffer buffer, Field field, boolean ignoreUndefinedFields) { buffer.next(); - MapValueUpdate m = (MapValueUpdate) MapReader.createMapUpdate(buffer, field.getDataType(), null, null); + MapValueUpdate m = (MapValueUpdate) MapReader.createMapUpdate(buffer, field.getDataType(), null, null, ignoreUndefinedFields); buffer.next(); // must generate the field value in parallell with the actual return m; diff --git a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java index 0ccdbdaa9d5..1747e739bbf 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java @@ -41,25 +41,25 @@ public class SingleValueReader { arithmeticExpressionPattern = Pattern.compile("^\\$\\w+\\s*([" + validSigns + "])\\s*(\\d+(.\\d+)?)$"); } - public static FieldValue readSingleValue(TokenBuffer buffer, DataType expectedType) { + public static FieldValue readSingleValue(TokenBuffer buffer, DataType expectedType, boolean ignoreUndefinedFields) { if (buffer.currentToken().isScalarValue()) { return readAtomic(buffer.currentText(), expectedType); } else { FieldValue fieldValue = expectedType.createFieldValue(); - CompositeReader.populateComposite(buffer, fieldValue); + CompositeReader.populateComposite(buffer, fieldValue, ignoreUndefinedFields); return fieldValue; } } @SuppressWarnings("rawtypes") - public static ValueUpdate readSingleUpdate(TokenBuffer buffer, DataType expectedType, String action) { + public static ValueUpdate readSingleUpdate(TokenBuffer buffer, DataType expectedType, String action, boolean ignoreUndefinedFields) { ValueUpdate update; switch (action) { case UPDATE_ASSIGN: update = (buffer.currentToken() == JsonToken.VALUE_NULL) ? ValueUpdate.createClear() - : ValueUpdate.createAssign(readSingleValue(buffer, expectedType)); + : ValueUpdate.createAssign(readSingleValue(buffer, expectedType, ignoreUndefinedFields)); break; // double is silly, but it's what is used internally anyway case UPDATE_INCREMENT: diff --git a/document/src/main/java/com/yahoo/document/json/readers/StructReader.java b/document/src/main/java/com/yahoo/document/json/readers/StructReader.java index 54591134d4c..b9eaf0d8ec6 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/StructReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/StructReader.java @@ -12,31 +12,31 @@ import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue; public class StructReader { - public static void fillStruct(TokenBuffer buffer, StructuredFieldValue parent) { + public static void fillStruct(TokenBuffer buffer, StructuredFieldValue parent, boolean ignoreUndefinedFields) { // do note the order of initializing initNesting and token is relevant for empty docs int initNesting = buffer.nesting(); buffer.next(); while (buffer.nesting() >= initNesting) { - Field f = getField(buffer, parent); + Field field = getField(buffer, parent, ignoreUndefinedFields); try { - // skip fields set to null - if (buffer.currentToken() != JsonToken.VALUE_NULL) { - FieldValue v = readSingleValue(buffer, f.getDataType()); - parent.setFieldValue(f, v); + if (field != null && buffer.currentToken() != JsonToken.VALUE_NULL) { + FieldValue v = readSingleValue(buffer, field.getDataType(), ignoreUndefinedFields); + parent.setFieldValue(field, v); } buffer.next(); } catch (IllegalArgumentException e) { - throw new JsonReaderException(f, e); + throw new JsonReaderException(field, e); } } } - public static Field getField(TokenBuffer buffer, StructuredFieldValue parent) { + private static Field getField(TokenBuffer buffer, StructuredFieldValue parent, boolean ignoreUndefinedFields) { Field field = parent.getField(buffer.currentName()); - if (field == null) { + if (field == null && ! ignoreUndefinedFields) { throw new IllegalArgumentException("No field '" + buffer.currentName() + "' in the structure of type '" + - parent.getDataType().getDataTypeName() + "', which has the fields:" + parent.getDataType().getFields()); + parent.getDataType().getDataTypeName() + + "', which has the fields: " + parent.getDataType().getFields()); } return field; } diff --git a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java index fb160a9bb44..7bc462ec73a 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java @@ -44,6 +44,12 @@ public class VespaJsonDocumentReader { private static final String UPDATE_REMOVE = "remove"; private static final String UPDATE_ADD = "add"; + private final boolean ignoreUndefinedFields; + + public VespaJsonDocumentReader(boolean ignoreUndefinedFields) { + this.ignoreUndefinedFields = ignoreUndefinedFields; + } + public DocumentOperation createDocumentOperation(DocumentType documentType, DocumentParseInfo documentParseInfo) { final DocumentOperation documentOperation; try { @@ -82,7 +88,7 @@ public class VespaJsonDocumentReader { try { if (buffer.isEmpty()) // no "fields" map throw new IllegalArgumentException(put + " is missing a 'fields' map"); - populateComposite(buffer, put.getDocument()); + populateComposite(buffer, put.getDocument(), ignoreUndefinedFields); } catch (JsonReaderException e) { throw JsonReaderException.addDocId(e, put.getId()); } @@ -129,25 +135,25 @@ public class VespaJsonDocumentReader { if (isTensorField(field)) { fieldUpdate.addValueUpdate(createTensorRemoveUpdate(buffer, field)); } else { - createRemoves(buffer, field, fieldUpdate); + createRemoves(buffer, field, fieldUpdate, ignoreUndefinedFields); } break; case UPDATE_ADD: if (isTensorField(field)) { fieldUpdate.addValueUpdate(createTensorAddUpdate(buffer, field)); } else { - createAdds(buffer, field, fieldUpdate); + createAdds(buffer, field, fieldUpdate, ignoreUndefinedFields); } break; case UPDATE_MATCH: - fieldUpdate.addValueUpdate(createMapUpdate(buffer, field)); + fieldUpdate.addValueUpdate(createMapUpdate(buffer, field, ignoreUndefinedFields)); break; case UPDATE_MODIFY: fieldUpdate.addValueUpdate(createModifyUpdate(buffer, field)); break; default: String action = buffer.currentName(); - fieldUpdate.addValueUpdate(readSingleUpdate(buffer, field.getDataType(), action)); + fieldUpdate.addValueUpdate(readSingleUpdate(buffer, field.getDataType(), action, ignoreUndefinedFields)); } buffer.next(); } @@ -183,14 +189,16 @@ public class VespaJsonDocumentReader { private AssignFieldPathUpdate readAssignFieldPathUpdate(DocumentType documentType, String fieldPath, TokenBuffer buffer) { AssignFieldPathUpdate fieldPathUpdate = new AssignFieldPathUpdate(documentType, fieldPath); - FieldValue fv = SingleValueReader.readSingleValue(buffer, fieldPathUpdate.getFieldPath().getResultingDataType()); + FieldValue fv = SingleValueReader.readSingleValue(buffer, fieldPathUpdate.getFieldPath().getResultingDataType(), + ignoreUndefinedFields); fieldPathUpdate.setNewValue(fv); return fieldPathUpdate; } private AddFieldPathUpdate readAddFieldPathUpdate(DocumentType documentType, String fieldPath, TokenBuffer buffer) { AddFieldPathUpdate fieldPathUpdate = new AddFieldPathUpdate(documentType, fieldPath); - FieldValue fv = SingleValueReader.readSingleValue(buffer, fieldPathUpdate.getFieldPath().getResultingDataType()); + FieldValue fv = SingleValueReader.readSingleValue(buffer, fieldPathUpdate.getFieldPath().getResultingDataType(), + ignoreUndefinedFields); fieldPathUpdate.setNewValues((Array) fv); return fieldPathUpdate; } @@ -200,8 +208,8 @@ public class VespaJsonDocumentReader { return new RemoveFieldPathUpdate(documentType, fieldPath); } - private AssignFieldPathUpdate readArithmeticFieldPathUpdate( - DocumentType documentType, String fieldPath, TokenBuffer buffer, String fieldPathOperation) { + private AssignFieldPathUpdate readArithmeticFieldPathUpdate(DocumentType documentType, String fieldPath, + TokenBuffer buffer, String fieldPathOperation) { AssignFieldPathUpdate fieldPathUpdate = new AssignFieldPathUpdate(documentType, fieldPath); String arithmeticSign = SingleValueReader.UPDATE_OPERATION_TO_ARITHMETIC_SIGN.get(fieldPathOperation); double value = Double.valueOf(buffer.currentText()); @@ -217,7 +225,7 @@ public class VespaJsonDocumentReader { private static void verifyEndState(TokenBuffer buffer, JsonToken expectedFinalToken) { Preconditions.checkState(buffer.currentToken() == expectedFinalToken, - "Expected end of JSON struct (%s), got %s", expectedFinalToken, buffer.currentToken()); + "Expected end of JSON struct (%s), got %s", expectedFinalToken, buffer.currentToken()); Preconditions.checkState(buffer.nesting() == 0, "Nesting not zero at end of operation"); Preconditions.checkState(buffer.next() == null, "Dangling data at end of operation"); Preconditions.checkState(buffer.size() == 0, "Dangling data at end of operation"); -- cgit v1.2.3