diff options
Diffstat (limited to 'document/src/main')
-rw-r--r-- | document/src/main/java/com/yahoo/document/DocumentUpdate.java | 38 | ||||
-rw-r--r-- | document/src/main/java/com/yahoo/document/datatypes/Raw.java | 13 |
2 files changed, 42 insertions, 9 deletions
diff --git a/document/src/main/java/com/yahoo/document/DocumentUpdate.java b/document/src/main/java/com/yahoo/document/DocumentUpdate.java index 2a9ab9e6169..70c5410534e 100644 --- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java +++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java @@ -1,11 +1,14 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document; +import com.yahoo.document.datatypes.FieldValue; import com.yahoo.document.fieldpathupdate.FieldPathUpdate; import com.yahoo.document.serialization.DocumentSerializerFactory; import com.yahoo.document.serialization.DocumentUpdateReader; import com.yahoo.document.serialization.DocumentUpdateWriter; +import com.yahoo.document.update.AssignValueUpdate; import com.yahoo.document.update.FieldUpdate; +import com.yahoo.document.update.ValueUpdate; import com.yahoo.io.GrowableByteBuffer; import java.util.ArrayList; @@ -94,6 +97,12 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP docId = id; } + private void verifyType(Document doc) { + if (!documentType.equals(doc.getDataType())) { + throw new IllegalArgumentException( + "Document " + doc.getId() + " with type " + doc.getDataType() + " must have same type as update, which is type " + documentType); + } + } /** * Applies this document update. * @@ -102,10 +111,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @throws IllegalArgumentException if the document does not have the same document type as this update */ public DocumentUpdate applyTo(Document doc) { - if (!documentType.equals(doc.getDataType())) { - throw new IllegalArgumentException( - "Document " + doc + " must have same type as update, which is type " + documentType); - } + verifyType(doc); for (FieldUpdate fieldUpdate : fieldUpdates) { fieldUpdate.applyTo(doc); @@ -117,6 +123,30 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP } /** + * Prune away any field update that will not modify any field in the document. + * @param doc document to check against + * @return a reference to itself + * @throws IllegalArgumentException if the document does not have the same document type as this update + */ + public DocumentUpdate prune(Document doc) { + verifyType(doc); + + for (Iterator<FieldUpdate> iter = fieldUpdates.iterator(); iter.hasNext();) { + FieldUpdate update = iter.next(); + if (!update.isEmpty()) { + ValueUpdate last = update.getValueUpdate(update.size() - 1); + if (last instanceof AssignValueUpdate) { + FieldValue currentValue = doc.getFieldValue(update.getField()); + if ((currentValue != null) && (currentValue.compareTo(last.getValue()) == 0)) { + iter.remove(); + } + } + } + } + return this; + } + + /** * Get an unmodifiable list of all field updates that this document update specifies. * * @return a list of all FieldUpdates in this DocumentUpdate diff --git a/document/src/main/java/com/yahoo/document/datatypes/Raw.java b/document/src/main/java/com/yahoo/document/datatypes/Raw.java index 2a5383705df..23ed0cee23e 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Raw.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Raw.java @@ -17,15 +17,16 @@ import java.util.Arrays; /** * FieldValue which encapsulates a Raw value * - * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a> + * @author Einar M R Rosenvinge */ public final class Raw extends FieldValue { + private static class Factory extends PrimitiveDataType.Factory { public FieldValue create() { return new Raw(); } } - public static PrimitiveDataType.Factory getFactory() { return new Factory(); } + public static final int classId = registerClass(Ids.document + 16, Raw.class); private ByteBuffer value; @@ -42,6 +43,8 @@ public final class Raw extends FieldValue { value.position(0); } + public static PrimitiveDataType.Factory getFactory() { return new Factory(); } + public ByteBuffer getByteBuffer() { return value; } @@ -136,11 +139,11 @@ public final class Raw extends FieldValue { } /* (non-Javadoc) - * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) - */ - + * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader) + */ @Override public void deserialize(Field field, FieldReader reader) { reader.read(field, this); } + } |