diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2018-07-20 14:21:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-20 14:21:25 +0200 |
commit | a2b96031d9dd0df98169ad3d2ad6ce3b1710eefc (patch) | |
tree | 32568f2d85a2662eca8b46f936ea9077428e9097 | |
parent | d93895b036a8bd09b02f947324fb1713a9488edd (diff) | |
parent | e2dff4c7357ea5fcafb68a1677035c19bdde8f8a (diff) |
Merge pull request #6441 from vespa-engine/balder/add-prune-interface-to-minimise-update
Add a prune method that can be used to remove updates that will have …
-rw-r--r-- | document/src/main/java/com/yahoo/document/DocumentUpdate.java | 38 | ||||
-rw-r--r-- | document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java | 58 |
2 files changed, 92 insertions, 4 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/test/java/com/yahoo/document/DocumentUpdateTestCase.java b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java index 4392ea932b2..4f3d7d3b820 100644 --- a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java @@ -651,6 +651,64 @@ public class DocumentUpdateTestCase { assertNull(doc.getFieldValue(tensorField)); } + @Test + public void testThatNonIdenticalAssignCanNotBePrunedAway() { + Field field = docType.getField("strfoo"); + String expected = "some other value"; + Document doc = createDocument(); + doc.setFieldValue(field, "some value"); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected))); + update.prune(doc); + assertEquals(1, update.size()); + update.applyTo(doc); + assertEquals(expected, doc.getFieldValue(field).getWrappedValue()); + } + + @Test + public void testThatIdenticalAssignCanBePrunedAway() { + Field field = docType.getField("strfoo"); + String expected = "some value"; + Document doc = createDocument(); + doc.setFieldValue(field, "some value"); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createAssign(field,new StringFieldValue(expected))); + update.prune(doc); + assertEquals(0, update.size()); + update.applyTo(doc); + assertEquals(expected, doc.getFieldValue(field).getWrappedValue()); + } + + @Test + public void testThatIdenticalAssignCanBePrunedAwayIfLast() { + Field field = docType.getField("strfoo"); + String expected = "some value"; + Document doc = createDocument(); + doc.setFieldValue(field, "some value"); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createClearField(field)); + update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected))); + update.prune(doc); + assertEquals(0, update.size()); + update.applyTo(doc); + assertEquals(expected, doc.getFieldValue(field).getWrappedValue()); + } + + @Test + public void testThatIdenticalAssignCanNotBePrunedAwayIfNotLast() { + Field field = docType.getField("strfoo"); + String expected = "some random value"; + Document doc = createDocument(); + doc.setFieldValue(field, "some value"); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue("some value"))); + update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected))); + update.prune(doc); + assertEquals(1, update.size()); + update.applyTo(doc); + assertEquals(expected, doc.getFieldValue(field).getWrappedValue()); + } + private static TensorFieldValue createTensorFieldValue(String tensor) { return new TensorFieldValue(Tensor.from(tensor)); } |