diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2018-09-10 11:17:16 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2018-09-10 11:17:16 +0200 |
commit | 5a85fa0ff10c21e67eac08179898fea874aa8288 (patch) | |
tree | 1aa8403891d69aac609a0d60107ef25f846d0d71 /document | |
parent | af0574fa2162488f8aaa9aaf359b53fd0a312ab3 (diff) |
Also prune away ClearValue updates if possible.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/main/java/com/yahoo/document/DocumentUpdate.java | 12 | ||||
-rw-r--r-- | document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java | 42 |
2 files changed, 54 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 3374714e3a7..ad93942c1c0 100644 --- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java +++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java @@ -7,6 +7,7 @@ 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.ClearValueUpdate; import com.yahoo.document.update.FieldUpdate; import com.yahoo.document.update.ValueUpdate; import com.yahoo.io.GrowableByteBuffer; @@ -140,6 +141,17 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP if ((currentValue != null) && currentValue.equals(last.getValue())) { iter.remove(); } + } else if (last instanceof ClearValueUpdate) { + FieldValue currentValue = doc.getFieldValue(update.getField()); + if (currentValue == null) { + iter.remove(); + } else { + FieldValue copy = currentValue.clone(); + copy.clear(); + if (currentValue.equals(copy)) { + iter.remove(); + } + } } } } diff --git a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java index 4f3d7d3b820..15319985591 100644 --- a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java @@ -709,6 +709,48 @@ public class DocumentUpdateTestCase { assertEquals(expected, doc.getFieldValue(field).getWrappedValue()); } + @Test + public void testThatClearCanBePrunedIfNoneExisting() { + Field field = docType.getField("strfoo"); + Document doc = createDocument(); + StringFieldValue expected = new StringFieldValue("some value"); + expected.clear(); + doc.setFieldValue(field, expected); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createClearField(field)); + update.prune(doc); + assertEquals(0, update.size()); + update.applyTo(doc); + assertEquals(expected, doc.getFieldValue(field)); + } + + @Test + public void testThatClearCanBePrunedIfEmpty() { + Field field = docType.getField("strfoo"); + String expected = ""; + Document doc = createDocument(); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createClearField(field)); + update.prune(doc); + assertEquals(0, update.size()); + update.applyTo(doc); + assertNull(doc.getFieldValue(field)); + } + + @Test + public void testThatClearCanBePrunedIfNoneExistingAndLast() { + Field field = docType.getField("strfoo"); + String expected = ""; + Document doc = createDocument(); + DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId)); + update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue("some value"))); + update.addFieldUpdate(FieldUpdate.createClearField(field)); + update.prune(doc); + assertEquals(0, update.size()); + update.applyTo(doc); + assertNull(doc.getFieldValue(field)); + } + private static TensorFieldValue createTensorFieldValue(String tensor) { return new TensorFieldValue(Tensor.from(tensor)); } |