aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/vespaxmlparser
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-04-25 19:57:57 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2019-04-25 19:57:57 +0200
commit907158959c159904c3f4d20274c565806746ac15 (patch)
treef4db3e3d2fc81cf061cb1811b5bc3af4ca91695f /document/src/main/java/com/yahoo/vespaxmlparser
parent249fe76c9437d0f1a033294df98d8d8101baef2c (diff)
Refactor to allow for lazy decode.
Diffstat (limited to 'document/src/main/java/com/yahoo/vespaxmlparser')
-rw-r--r--document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java40
-rw-r--r--document/src/main/java/com/yahoo/vespaxmlparser/FeedReader.java7
-rw-r--r--document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java56
3 files changed, 60 insertions, 43 deletions
diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java
new file mode 100644
index 00000000000..58371faeced
--- /dev/null
+++ b/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java
@@ -0,0 +1,40 @@
+package com.yahoo.vespaxmlparser;
+
+import com.yahoo.document.Document;
+import com.yahoo.document.DocumentId;
+import com.yahoo.document.DocumentUpdate;
+import com.yahoo.document.TestAndSetCondition;
+
+public abstract class FeedOperation {
+ public enum Type {DOCUMENT, REMOVE, UPDATE, INVALID}
+
+ private Type type;
+ protected FeedOperation() {
+ this(Type.INVALID);
+ }
+ protected FeedOperation(Type type) {
+ this.type = type;
+ }
+ public final Type getType() { return type; }
+ protected final void setType(Type type) {
+ this.type = type;
+ }
+
+ public abstract Document getDocument();
+ public abstract DocumentUpdate getDocumentUpdate();
+ public abstract DocumentId getRemove();
+
+ public TestAndSetCondition getCondition() {
+ return TestAndSetCondition.NOT_PRESENT_CONDITION;
+ }
+ @Override
+ public String toString() {
+ return "Operation{" +
+ "type=" + getType() +
+ ", doc=" + getDocument() +
+ ", remove=" + getRemove() +
+ ", docUpdate=" + getDocumentUpdate() +
+ " testandset=" + getCondition() +
+ '}';
+ }
+} \ No newline at end of file
diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/FeedReader.java b/document/src/main/java/com/yahoo/vespaxmlparser/FeedReader.java
index 2c130cae782..c993d5a5153 100644
--- a/document/src/main/java/com/yahoo/vespaxmlparser/FeedReader.java
+++ b/document/src/main/java/com/yahoo/vespaxmlparser/FeedReader.java
@@ -1,8 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaxmlparser;
-import com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation;
-
/**
* Minimal interface for reading operations from a stream for a feeder.
*
@@ -14,8 +12,7 @@ public interface FeedReader {
/**
* Reads the next operation from the stream.
- * @param operation The operation to fill in. Operation is unchanged if none was found.
+ * @return operation, possibly invalid if none was found.
*/
- void read(Operation operation) throws Exception;
-
+ FeedOperation read() throws Exception;
}
diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java
index 14f4fd9484c..e34f3e7e4e4 100644
--- a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java
+++ b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java
@@ -74,22 +74,14 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
}
}
- public enum OperationType {
- DOCUMENT,
- REMOVE,
- UPDATE,
- INVALID
- }
-
/**
* Represents a feed operation found by the parser. Can be one of the following types:
* - getType() == DOCUMENT: getDocument() is valid.
* - getType() == REMOVE: getRemove() is valid.
* - getType() == UPDATE: getUpdate() is valid.
*/
- public static class Operation {
+ public static class Operation extends FeedOperation {
- private OperationType type;
private Document doc;
private DocumentId remove;
private DocumentUpdate docUpdate;
@@ -100,41 +92,38 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
}
public void setInvalid() {
- type = OperationType.INVALID;
doc = null;
remove = null;
docUpdate = null;
condition = TestAndSetCondition.NOT_PRESENT_CONDITION;
}
- public OperationType getType() {
- return type;
- }
-
+ @Override
public Document getDocument() {
return doc;
}
public void setDocument(Document doc) {
- this.type = OperationType.DOCUMENT;
+ setType(Type.DOCUMENT);
this.doc = doc;
}
+ @Override
public DocumentId getRemove() {
return remove;
}
public void setRemove(DocumentId remove) {
- this.type = OperationType.REMOVE;
+ setType(Type.REMOVE);
this.remove = remove;
}
-
+ @Override
public DocumentUpdate getDocumentUpdate() {
return docUpdate;
}
public void setDocumentUpdate(DocumentUpdate docUpdate) {
- this.type = OperationType.UPDATE;
+ setType(Type.UPDATE);
this.docUpdate = docUpdate;
}
@@ -142,19 +131,11 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
this.condition = condition;
}
+ @Override
public TestAndSetCondition getCondition() {
return condition;
}
- @Override
- public String toString() {
- return "Operation{" +
- "type=" + type +
- ", doc=" + doc +
- ", remove=" + remove +
- ", docUpdate=" + docUpdate +
- '}';
- }
}
/**
@@ -164,12 +145,11 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
*
* @return The list of all read operations.
*/
- public List<Operation> readAll() throws Exception {
- List<Operation> list = new ArrayList<>();
+ public List<FeedOperation> readAll() throws Exception {
+ List<FeedOperation> list = new ArrayList<>();
while (true) {
- Operation op = new Operation();
- read(op);
- if (op.getType() == OperationType.INVALID) {
+ FeedOperation op = read();
+ if (op.getType() == FeedOperation.Type.INVALID) {
return list;
} else {
list.add(op);
@@ -181,10 +161,9 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
* @see com.yahoo.vespaxmlparser.FeedReader#read(com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation)
*/
@Override
- public void read(Operation operation) throws Exception {
+ public FeedOperation read() throws Exception {
+ Operation operation = new Operation();
String startTag = null;
- operation.setInvalid();
-
try {
while (reader.hasNext()) {
int type = reader.next();
@@ -197,13 +176,13 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
Document document = new Document(documentReader);
operation.setDocument(document);
operation.setCondition(TestAndSetCondition.fromConditionString(documentReader.getCondition()));
- return;
+ return operation;
} else if ("update".equals(startTag)) {
VespaXMLUpdateReader updateReader = new VespaXMLUpdateReader(reader, docTypeManager);
DocumentUpdate update = new DocumentUpdate(updateReader);
operation.setDocumentUpdate(update);
operation.setCondition(TestAndSetCondition.fromConditionString(updateReader.getCondition()));
- return;
+ return operation;
} else if ("remove".equals(startTag)) {
boolean documentIdFound = false;
@@ -224,7 +203,7 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
operation.setCondition(TestAndSetCondition.fromConditionString(condition));
- return;
+ return operation;
} else {
throw newDeserializeException("Element \"" + startTag + "\" not allowed in this context");
}
@@ -243,6 +222,7 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader {
throw(e);
}
+ return operation;
}
}