aboutsummaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2023-01-12 23:36:38 +0100
committerJon Bratseth <bratseth@gmail.com>2023-01-12 23:36:38 +0100
commitcb10bc9b8d66abdfb9b8c5d92314d3b6afce9164 (patch)
tree83793782c070a6f66bf1528fa6e4ce6e857f085b /document
parent2229a4d2e3141010850fa23f5ad731c9038052a8 (diff)
Simplify by using a list
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/json/TokenBuffer.java75
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/TensorReader.java3
-rw-r--r--document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java2
3 files changed, 39 insertions, 41 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/TokenBuffer.java b/document/src/main/java/com/yahoo/document/json/TokenBuffer.java
index e1214920296..f4573adc41e 100644
--- a/document/src/main/java/com/yahoo/document/json/TokenBuffer.java
+++ b/document/src/main/java/com/yahoo/document/json/TokenBuffer.java
@@ -2,9 +2,9 @@
package com.yahoo.document.json;
import java.io.IOException;
-import java.util.ArrayDeque;
-import java.util.Deque;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
@@ -17,72 +17,68 @@ import com.google.common.base.Preconditions;
*/
public class TokenBuffer {
- private final Deque<Token> buffer;
+ private final List<Token> tokens;
+ private int position = 0;
private int nesting = 0;
- private Token previousToken;
public TokenBuffer() {
- this(new ArrayDeque<>());
+ this(new ArrayList<>());
}
- public TokenBuffer(Deque<Token> buffer) {
- this.buffer = buffer;
- if (buffer.size() > 0) {
- updateNesting(buffer.peekFirst().token);
- }
+ public TokenBuffer(List<Token> tokens) {
+ this.tokens = tokens;
+ if (tokens.size() > 0)
+ updateNesting(tokens.get(position).token);
}
/** Returns whether any tokens are available in this */
- public boolean isEmpty() { return size() == 0; }
+ public boolean isEmpty() { return tokens.isEmpty(); }
public JsonToken next() {
- previousToken = buffer.removeFirst();
- Token t = buffer.peekFirst();
- if (t == null) {
- return null;
- }
- updateNesting(t.token);
- return t.token;
+ position++;
+ JsonToken token = currentToken();
+ updateNesting(token);
+ return token;
}
/** Goes one token back. Repeated calls to this method will *not* go back further. */
public JsonToken previous() {
- if (previousToken == null) return null;
updateNestingGoingBackwards(currentToken());
- Token newCurrent = previousToken;
- previousToken = null;
- buffer.push(newCurrent);
- return newCurrent.token;
+ position--;
+ return currentToken();
}
/** Returns the current token without changing position, or null if none */
public JsonToken currentToken() {
- Token token = buffer.peekFirst();
+ if (position >= tokens.size()) return null;
+ Token token = tokens.get(position);
if (token == null) return null;
return token.token;
}
/** Returns the current token name without changing position, or null if none */
public String currentName() {
- Token token = buffer.peekFirst();
+ if (position >= tokens.size()) return null;
+ Token token = tokens.get(position);
if (token == null) return null;
return token.name;
}
/** Returns the current token text without changing position, or null if none */
public String currentText() {
- Token token = buffer.peekFirst();
+ if (position >= tokens.size()) return null;
+ Token token = tokens.get(position);
if (token == null) return null;
return token.text;
}
public int size() {
- return buffer.size();
+ return tokens.size() - position;
}
private void add(JsonToken token, String name, String text) {
- buffer.addLast(new Token(token, name, text));
+ tokens.add(tokens.size(), new Token(token, name, text));
}
public void bufferObject(JsonToken first, JsonParser tokens) {
@@ -110,10 +106,11 @@ public class TokenBuffer {
return nesting + nestingOffset(t);
}
- private int nestingOffset(JsonToken t) {
- if (t.isStructStart()) {
+ private int nestingOffset(JsonToken token) {
+ if (token == null) return 0;
+ if (token.isStructStart()) {
return 1;
- } else if (t.isStructEnd()) {
+ } else if (token.isStructEnd()) {
return -1;
} else {
return 0;
@@ -138,12 +135,12 @@ public class TokenBuffer {
}
}
- private void updateNesting(JsonToken t) {
- nesting += nestingOffset(t);
+ private void updateNesting(JsonToken token) {
+ nesting += nestingOffset(token);
}
- private void updateNestingGoingBackwards(JsonToken t) {
- nesting -= nestingOffset(t);
+ private void updateNestingGoingBackwards(JsonToken token) {
+ nesting -= nestingOffset(token);
}
public int nesting() {
@@ -157,9 +154,9 @@ public class TokenBuffer {
Iterator<Token> i;
if (name.equals(currentName()) && currentToken().isScalarValue()) {
- toReturn = buffer.peekFirst();
+ toReturn = tokens.get(position);
} else {
- i = buffer.iterator();
+ i = tokens.iterator();
i.next(); // just ignore the first value, as we know it's not what
// we're looking for, and it's nesting effect is already
// included
@@ -185,8 +182,8 @@ public class TokenBuffer {
} while ( nesting() > initialNesting + relativeNesting);
}
- public Deque<Token> rest() {
- return buffer;
+ public List<Token> rest() {
+ return tokens.subList(position, tokens.size());
}
public static final class Token {
diff --git a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
index 4b2da912c37..fc9f5d0995d 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/TensorReader.java
@@ -14,6 +14,7 @@ import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import java.util.ArrayDeque;
+import java.util.ArrayList;
import java.util.Deque;
import static com.yahoo.document.json.readers.JsonParserHelpers.*;
@@ -40,7 +41,7 @@ public class TensorReader {
expectOneOf(buffer.currentToken(), JsonToken.START_OBJECT, JsonToken.START_ARRAY);
int initNesting = buffer.nesting();
for (buffer.next(); buffer.nesting() >= initNesting; buffer.next()) {
- if (TENSOR_CELLS.equals(buffer.currentName()) && ! primitiveContent(new TokenBuffer(new ArrayDeque<>(buffer.rest())))) {
+ if (TENSOR_CELLS.equals(buffer.currentName()) && ! primitiveContent(new TokenBuffer(new ArrayList<>(buffer.rest())))) {
readTensorCells(buffer, builder);
}
else if (TENSOR_VALUES.equals(buffer.currentName()) && builder.type().dimensions().stream().allMatch(d -> d.isIndexed())) {
diff --git a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java
index 22a9e7a1119..33f47beb01e 100644
--- a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java
+++ b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java
@@ -219,7 +219,7 @@ public class VespaJsonDocumentReader {
TokenBuffer buffer, String fieldPathOperation) {
AssignFieldPathUpdate fieldPathUpdate = new AssignFieldPathUpdate(documentType, fieldPath);
String arithmeticSign = SingleValueReader.UPDATE_OPERATION_TO_ARITHMETIC_SIGN.get(fieldPathOperation);
- double value = Double.valueOf(buffer.currentText());
+ double value = Double.parseDouble(buffer.currentText());
String expression = String.format("$value %s %s", arithmeticSign, value);
fieldPathUpdate.setExpression(expression);
return fieldPathUpdate;