diff options
author | Vegard Sjonfjell <vegard@yahoo-inc.com> | 2016-10-31 17:00:09 +0100 |
---|---|---|
committer | Vegard Sjonfjell <vegard@yahoo-inc.com> | 2016-10-31 17:00:09 +0100 |
commit | ccac92f520e18c6f485c140c6e4a11f03025259f (patch) | |
tree | 01df9ff8593538d37b71a06df5bee462e7c01e48 | |
parent | fef7efff52ea4bcebb9b6512cac0035998757d18 (diff) |
Implement document update serializing of create if non existent field
3 files changed, 45 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 0cfaa601b21..5101c41faa6 100644 --- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java +++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Optional; /** * <p>Specifies one or more field updates to a document.</p> <p>A document update contains a list of {@link @@ -41,7 +42,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP private List<FieldUpdate> fieldUpdates; private List<FieldPathUpdate> fieldPathUpdates; private DocumentType documentType; - private boolean createIfNonExistent; + private Optional<Boolean> createIfNonExistent; /** * Creates a DocumentUpdate. @@ -334,7 +335,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP if (fieldPathUpdates != null ? !fieldPathUpdates.equals(that.fieldPathUpdates) : that.fieldPathUpdates != null) return false; if (fieldUpdates != null ? !fieldUpdates.equals(that.fieldUpdates) : that.fieldUpdates != null) return false; - if (createIfNonExistent != that.createIfNonExistent) return false; + if (this.getCreateIfNonExistent() != ((DocumentUpdate) o).getCreateIfNonExistent()) return false; return true; } @@ -355,7 +356,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP string.append(docId); string.append("': "); string.append("create-if-non-existent="); - string.append(createIfNonExistent ? "true" : "false"); + string.append(createIfNonExistent.orElse(false)); string.append(": "); string.append("["); @@ -400,7 +401,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @param value Whether the document it updates should be created. */ public void setCreateIfNonExistent(boolean value) { - createIfNonExistent = value; + createIfNonExistent = Optional.of(value); } /** @@ -410,6 +411,10 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @return Whether the document it updates should be created. */ public boolean getCreateIfNonExistent() { + return createIfNonExistent.orElse(false); + } + + public Optional<Boolean> getOptionalCreateIfNonExistent() { return createIfNonExistent; } } diff --git a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java index a059df01ad5..58b2cac1b17 100644 --- a/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java +++ b/document/src/main/java/com/yahoo/document/json/DocumentUpdateJsonSerializer.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Optional; import static com.yahoo.document.json.JsonSerializationHelper.*; @@ -88,6 +89,11 @@ public class DocumentUpdateJsonSerializer generator.writeStringField("condition", update.getCondition().getSelection()); } + Optional<Boolean> createIfNotExistent = update.getOptionalCreateIfNonExistent(); + if (createIfNotExistent.isPresent()) { + generator.writeBooleanField("create", createIfNotExistent.get()); + } + generator.writeObjectFieldStart("fields"); for (FieldUpdate up : update.getFieldUpdates()) { up.serialize(this); diff --git a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java index de483186d6c..3c853c31323 100644 --- a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java +++ b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java @@ -340,4 +340,34 @@ public class DocumentUpdateJsonSerializerTest { "}" )); } + + @Test + public void testCreateIfNotExistTrue() { + deSerializeAndSerializeJsonAndMatch(inputJson( + "{", + " 'update': 'DOCUMENT_ID',", + " 'create': true,", + " 'fields': {", + " 'int_field': {", + " 'assign': 42", + " }", + " }", + "}" + )); + } + + @Test + public void testCreateIfNotExistFalse() { + deSerializeAndSerializeJsonAndMatch(inputJson( + "{", + " 'update': 'DOCUMENT_ID',", + " 'create': false,", + " 'fields': {", + " 'int_field': {", + " 'assign': 42", + " }", + " }", + "}" + )); + } } |