summaryrefslogtreecommitdiffstats
path: root/document/src/main
diff options
context:
space:
mode:
authorfreva <valerijf@yahoo-inc.com>2017-02-09 09:31:45 +0100
committerfreva <valerijf@yahoo-inc.com>2017-02-09 09:31:45 +0100
commitd9e75e756f7081c14a136f6096356565e173d81e (patch)
tree1abccaffae898c7283e055d7603b9138a5b526bc /document/src/main
parentaf1c45368eb07f510b5d495b0c995d017acf92e0 (diff)
Created VespaJsonDocumentReader
Diffstat (limited to 'document/src/main')
-rw-r--r--document/src/main/java/com/yahoo/document/json/JsonReader.java75
-rw-r--r--document/src/main/java/com/yahoo/document/json/VespaJsonDocumentReader.java122
-rw-r--r--document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java1
3 files changed, 131 insertions, 67 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/JsonReader.java b/document/src/main/java/com/yahoo/document/json/JsonReader.java
index 22db31103c4..4af429340e4 100644
--- a/document/src/main/java/com/yahoo/document/json/JsonReader.java
+++ b/document/src/main/java/com/yahoo/document/json/JsonReader.java
@@ -250,7 +250,7 @@ public class JsonReader {
expectArrayStart(parser.currentToken());
parser.nextToken();
do {
- FieldPathUpdate fieldPathUpdate = parseFieldPathUpdate(documentType, parser);
+ FieldPathUpdate fieldPathUpdate = parseFieldPathUpdate(documentType, parser);
documentParseInfo.addFieldPathUpdates(fieldPathUpdate);
} while (parser.nextToken() != JsonToken.END_ARRAY);
}
@@ -263,76 +263,17 @@ public class JsonReader {
return documentParseInfo;
}
- private static FieldPathUpdate parseFieldPathUpdate(DocumentType type, JsonParser parser) throws IOException, ParseException {
- Map<String, Object> map = parseMap(parser);
- String operation = (String) map.get("operation");
- String fieldPath = (String) map.get("fieldpath");
- Optional<String> where = Optional.ofNullable((String) map.get("where")).filter(s -> !s.isEmpty());
- FieldPathUpdate fieldPathUpdate;
-
- Preconditions.checkArgument(fieldPath != null && !fieldPath.isEmpty(), "fieldpath argument must be set");
- switch (operation.toLowerCase()) {
- case UPDATE_ADD:
- fieldPathUpdate = new AddFieldPathUpdate(type, fieldPath, null);
- break;
-
- case UPDATE_ASSIGN:
- fieldPathUpdate = new AssignFieldPathUpdate(type, fieldPath, null);
-
- Optional.ofNullable((Boolean) map.get("removeifzero"))
- .ifPresent(((AssignFieldPathUpdate) fieldPathUpdate)::setRemoveIfZero);
- Optional.ofNullable((Boolean) map.get("createmissingpath"))
- .ifPresent(((AssignFieldPathUpdate) fieldPathUpdate)::setCreateMissingPath);
-
- DataType dt = fieldPathUpdate.getFieldPath().getResultingDataType();
- if (dt instanceof NumericDataType) {
- ((AssignFieldPathUpdate) fieldPathUpdate).setExpression(String.valueOf(map.get("value")));
- } else {
- FieldValue fv = dt.createFieldValue();
- fv.assign(map.get("value"));
- ((AssignFieldPathUpdate) fieldPathUpdate).setNewValue(fv);
- }
- break;
-
- case UPDATE_REMOVE:
- fieldPathUpdate = new RemoveFieldPathUpdate(type, fieldPath);
- break;
-
- default:
- throw new RuntimeException("Unsupported fieldpath operation: " + operation);
- }
-
- if (where.isPresent()) {
- fieldPathUpdate.setWhereClause(where.get());
- }
-
- return fieldPathUpdate;
- }
-
- private static Map<String, Object> parseMap(JsonParser parser) throws IOException {
- Map<String, Object> map = new HashMap<>();
+ private static FieldPathUpdate parseFieldPathUpdate(DocumentType documentType, JsonParser parser) throws IOException, ParseException {
assert parser.isExpectedStartObjectToken();
parser.nextToken();
- do {
- String key = parser.getValueAsString();
-
- parser.nextToken();
- Object value = null;
- if (parser.currentToken().isBoolean()) {
- value = parser.getBooleanValue();
- } else if (parser.currentToken().isNumeric()) {
- value = parser.getNumberValue();
- } else if (parser.currentToken().isScalarValue()) { // Non-structured value
- value = parser.getValueAsString();
- } else if (parser.isExpectedStartArrayToken()) {
- // TODO: Her må vi parse array for 'add'
- }
- Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
- map.put(key, value);
- } while (! parser.nextToken().isStructEnd());
+ FieldPathUpdate.Type operation = FieldPathUpdate.Type.valueOf(parser.getValueAsString().toUpperCase());
+ parser.nextToken();
+
+ VespaJsonDocumentReader jsonDocumentReader = new VespaJsonDocumentReader(parser);
+ FieldPathUpdate fieldPathUpdate = FieldPathUpdate.create(operation, documentType, jsonDocumentReader);
- return map;
+ return fieldPathUpdate;
}
private void verifyEndState() {
diff --git a/document/src/main/java/com/yahoo/document/json/VespaJsonDocumentReader.java b/document/src/main/java/com/yahoo/document/json/VespaJsonDocumentReader.java
new file mode 100644
index 00000000000..d3991d0ab4f
--- /dev/null
+++ b/document/src/main/java/com/yahoo/document/json/VespaJsonDocumentReader.java
@@ -0,0 +1,122 @@
+package com.yahoo.document.json;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.google.common.base.Preconditions;
+import com.yahoo.document.DataType;
+import com.yahoo.document.DocumentId;
+import com.yahoo.document.DocumentType;
+import com.yahoo.document.DocumentUpdate;
+import com.yahoo.document.NumericDataType;
+import com.yahoo.document.datatypes.FieldValue;
+import com.yahoo.document.fieldpathupdate.AddFieldPathUpdate;
+import com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate;
+import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
+import com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate;
+import com.yahoo.document.select.parser.ParseException;
+import com.yahoo.document.serialization.DocumentUpdateReader;
+import com.yahoo.document.update.FieldUpdate;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * @author valerijf
+ */
+public class VespaJsonDocumentReader implements DocumentUpdateReader {
+ private final JsonParser parser;
+ private final Map<String, Object> operationAttributes = new HashMap<>();
+
+ VespaJsonDocumentReader(JsonParser parser) {
+ this.parser = parser;
+ }
+
+ @Override
+ public void read(DocumentUpdate update) {
+
+ }
+
+ @Override
+ public void read(FieldUpdate update) {
+
+ }
+
+ @Override
+ public void read(FieldPathUpdate update) {
+ assert parser.isExpectedStartObjectToken();
+
+ try {
+ parser.nextToken();
+ update.setFieldPath(parser.getValueAsString());
+ parser.nextToken();
+
+ assert parser.isExpectedStartObjectToken();
+ parser.nextToken();
+ do {
+ String key = parser.getValueAsString();
+ Preconditions.checkState(key != null && !key.isEmpty(), "Missing attribute key for operation");
+ parser.nextToken();
+
+ System.out.println(update.getFieldPath().getResultingDataType());
+ if ("where".equals(key)) {
+ update.setWhereClause(parser.getValueAsString());
+ } else {
+ Object value = null;
+ if ("value".equals(key)) {
+ value = parser.getEmbeddedObject();
+ } else if (parser.currentToken().isBoolean()) {
+ value = parser.getBooleanValue();
+ } else if (parser.currentToken().isNumeric()) {
+ value = parser.getNumberValue();
+ } else if (parser.currentToken().isScalarValue()) { // Non-structured value
+ value = parser.getValueAsString();
+ }
+
+ operationAttributes.put(key, value);
+ }
+ } while (! parser.nextToken().isStructEnd());
+ } catch (IOException | ParseException e) {
+ throw new RuntimeException("Failed to parse JSON", e);
+ }
+ }
+
+
+ @Override
+ public void read(AssignFieldPathUpdate update) {
+ Optional.ofNullable((Boolean) operationAttributes.get("removeifzero"))
+ .ifPresent(update::setRemoveIfZero);
+ Optional.ofNullable((Boolean) operationAttributes.get("createmissingpath"))
+ .ifPresent(update::setCreateMissingPath);
+
+ DataType dt = update.getFieldPath().getResultingDataType();
+ if (dt instanceof NumericDataType) {
+ update.setExpression(String.valueOf(operationAttributes.get("value")));
+ } else {
+ FieldValue fv = dt.createFieldValue();
+ fv.assign(operationAttributes.get("value"));
+ update.setNewValue(fv);
+ }
+
+ }
+
+ @Override
+ public void read(AddFieldPathUpdate update) {
+
+ }
+
+ @Override
+ public void read(RemoveFieldPathUpdate update) {
+
+ }
+
+ @Override
+ public DocumentId readDocumentId() {
+ return null;
+ }
+
+ @Override
+ public DocumentType readDocumentType() {
+ return null;
+ }
+}
diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java
index a4d334848d5..5513ae88021 100644
--- a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java
+++ b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java
@@ -338,6 +338,7 @@ public class VespaXMLUpdateReader extends VespaXMLFieldReader implements Documen
}
}
DataType dt = update.getFieldPath().getResultingDataType();
+ dt.
if (dt instanceof NumericDataType) {
update.setExpression(reader.getElementText());