summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2018-07-20 13:28:05 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2018-07-20 13:28:05 +0200
commit127b0d5c0eaa2701bfe3d2e8899251a6270314ee (patch)
tree7c124666e89ca0d8e75aa6ffb6980ffdaf3031e3 /document
parent2f7f5678fb8e21e99b4d5ceeb64da4fcb4300260 (diff)
Add a prune method that can be used to remove updates that will have no effect when applied.
Currently will only affect ASSIGN updates.
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/DocumentUpdate.java32
-rw-r--r--document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java58
2 files changed, 90 insertions, 0 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..ef5fdc290c7 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;
@@ -117,6 +120,35 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
}
/**
+ * Will prune away any field update that will not modify any field in the document.
+ * @param doc
+ * @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) {
+ if ( ! fieldPathUpdates.isEmpty()) return this;
+
+ if (!documentType.equals(doc.getDataType())) {
+ throw new IllegalArgumentException(
+ "Document " + doc + " must have same type as update, which is type " + documentType);
+ }
+
+ 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));
}