aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java
diff options
context:
space:
mode:
authorfreva <valerijf@yahoo-inc.com>2017-02-17 15:31:29 +0100
committerfreva <valerijf@yahoo-inc.com>2017-02-17 15:31:29 +0100
commita50b07f39599001772d3f8075425151622b8306a (patch)
tree9bfaf0bd80d7a9932b5c87c441979099fe788826 /document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java
parent9859b67db5f08cc25d10a6960173d15c1fca4465 (diff)
Added JSON arithmetic fieldpath operations
Diffstat (limited to 'document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java')
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java
index 2f8826354f3..067513b24dc 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/SingleValueReader.java
@@ -11,6 +11,12 @@ import com.yahoo.document.json.TokenBuffer;
import com.yahoo.document.update.ValueUpdate;
import org.apache.commons.codec.binary.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
public class SingleValueReader {
public static final String UPDATE_ASSIGN = "assign";
public static final String UPDATE_INCREMENT = "increment";
@@ -18,6 +24,22 @@ public class SingleValueReader {
public static final String UPDATE_MULTIPLY = "multiply";
public static final String UPDATE_DIVIDE = "divide";
+ public static final Map<String, String> UPDATE_OPERATION_TO_ARITHMETIC_SIGN = new HashMap<>();
+ public static final Map<String, String> ARITHMETIC_SIGN_TO_UPDATE_OPERATION;
+ private static final Pattern arithmeticExpressionPattern;
+
+ static {
+ UPDATE_OPERATION_TO_ARITHMETIC_SIGN.put(UPDATE_INCREMENT, "+");
+ UPDATE_OPERATION_TO_ARITHMETIC_SIGN.put(UPDATE_DECREMENT, "-");
+ UPDATE_OPERATION_TO_ARITHMETIC_SIGN.put(UPDATE_MULTIPLY, "*");
+ UPDATE_OPERATION_TO_ARITHMETIC_SIGN.put(UPDATE_DIVIDE, "/");
+ ARITHMETIC_SIGN_TO_UPDATE_OPERATION = UPDATE_OPERATION_TO_ARITHMETIC_SIGN.entrySet().stream()
+ .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
+
+ String validSigns = Pattern.quote(String.join("", UPDATE_OPERATION_TO_ARITHMETIC_SIGN.values()));
+ arithmeticExpressionPattern = Pattern.compile("^\\$\\w+\\s*([" + validSigns + "])\\s*(\\d+(.\\d+)?)$");
+ }
+
public static FieldValue readSingleValue(TokenBuffer buffer, DataType expectedType) {
if (buffer.currentToken().isScalarValue()) {
return readAtomic(buffer.currentText(), expectedType);
@@ -57,6 +79,10 @@ public class SingleValueReader {
return update;
}
+ public static Matcher matchArithmeticOperation(String expression) {
+ return arithmeticExpressionPattern.matcher(expression.trim());
+ }
+
public static FieldValue readAtomic(String field, DataType expectedType) {
if (expectedType.equals(DataType.RAW)) {
return expectedType.createFieldValue(new Base64().decode(field));