diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-04-25 20:53:07 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-04-25 20:53:07 +0200 |
commit | 34d3074847f2182e1ba7922207e5f8790b9bd581 (patch) | |
tree | 04c2c1264da848238882a8f6c2a07a6555d842ad /document | |
parent | 1547d20ff3b8ec57fbd62fcd7348758b8c103232 (diff) |
Replace the multipurpose VespaXMLFeeder.Operation with more to the point classes with proper final members.
Diffstat (limited to 'document')
8 files changed, 104 insertions, 112 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java b/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java index f773173e89a..be4aa6cfb41 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java +++ b/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java @@ -9,9 +9,11 @@ import com.yahoo.document.DocumentPut; import com.yahoo.document.DocumentRemove; import com.yahoo.document.DocumentTypeManager; import com.yahoo.document.DocumentUpdate; +import com.yahoo.vespaxmlparser.DocumentFeedOperation; +import com.yahoo.vespaxmlparser.DocumentUpdateFeedOperation; import com.yahoo.vespaxmlparser.FeedOperation; import com.yahoo.vespaxmlparser.FeedReader; -import com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation; +import com.yahoo.vespaxmlparser.RemoveFeedOperation; /** @@ -37,25 +39,21 @@ public class JsonFeedReader implements FeedReader { @Override public FeedOperation read() throws Exception { DocumentOperation documentOperation = reader.next(); - Operation operation = new Operation(); if (documentOperation == null) { stream.close(); - return operation; + return FeedOperation.INVALID; } if (documentOperation instanceof DocumentUpdate) { - operation.setDocumentUpdate((DocumentUpdate) documentOperation); + return new DocumentUpdateFeedOperation((DocumentUpdate) documentOperation, documentOperation.getCondition()); } else if (documentOperation instanceof DocumentRemove) { - operation.setRemove(documentOperation.getId()); + return new RemoveFeedOperation(documentOperation.getId(), documentOperation.getCondition()); } else if (documentOperation instanceof DocumentPut) { - operation.setDocument(((DocumentPut) documentOperation).getDocument()); + return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition()); } else { throw new IllegalStateException("Got unknown class from JSON reader: " + documentOperation.getClass().getName()); } - - operation.setCondition(documentOperation.getCondition()); - return operation; } } diff --git a/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java b/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java index 8012ebafb13..28aa9ed1d8d 100644 --- a/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java +++ b/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java @@ -7,7 +7,9 @@ import com.yahoo.document.DocumentPut; import com.yahoo.document.DocumentTypeManager; import com.yahoo.document.DocumentUpdate; import com.yahoo.document.json.document.DocumentParser; -import com.yahoo.vespaxmlparser.VespaXMLFeedReader; +import com.yahoo.vespaxmlparser.DocumentFeedOperation; +import com.yahoo.vespaxmlparser.DocumentUpdateFeedOperation; +import com.yahoo.vespaxmlparser.FeedOperation; import java.io.IOException; import java.io.InputStream; @@ -25,32 +27,26 @@ public class SingleDocumentParser { this.docMan = docMan; } - public VespaXMLFeedReader.Operation parsePut(InputStream inputStream, String docId) { + public FeedOperation parsePut(InputStream inputStream, String docId) { return parse(inputStream, docId, DocumentParser.SupportedOperation.PUT); } - public VespaXMLFeedReader.Operation parseUpdate(InputStream inputStream, String docId) { + public FeedOperation parseUpdate(InputStream inputStream, String docId) { return parse(inputStream, docId, DocumentParser.SupportedOperation.UPDATE); } - private VespaXMLFeedReader.Operation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) { + private FeedOperation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) { final JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); final DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); - VespaXMLFeedReader.Operation operation = new VespaXMLFeedReader.Operation(); try { inputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } if (supportedOperation == DocumentParser.SupportedOperation.PUT) { - operation.setDocument(((DocumentPut) documentOperation).getDocument()); + return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition()); } else { - operation.setDocumentUpdate((DocumentUpdate) documentOperation); + return new DocumentUpdateFeedOperation((DocumentUpdate) documentOperation, documentOperation.getCondition()); } - - // (A potentially empty) test-and-set condition is always set by JsonReader - operation.setCondition(documentOperation.getCondition()); - - return operation; } } diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/ConditionalFeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/ConditionalFeedOperation.java index c512c004efb..e7a06560532 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/ConditionalFeedOperation.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/ConditionalFeedOperation.java @@ -1,4 +1,21 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespaxmlparser; -public class ConditionalFeedOperation { +import com.yahoo.document.TestAndSetCondition; + +public class ConditionalFeedOperation extends FeedOperation { + private final TestAndSetCondition condition; + protected ConditionalFeedOperation(Type type) { + super(type); + this.condition = TestAndSetCondition.NOT_PRESENT_CONDITION; + } + protected ConditionalFeedOperation(Type type, TestAndSetCondition condition) { + super(type); + this.condition = condition; + } + + @Override + public TestAndSetCondition getCondition() { + return condition; + } } diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/DocumentFeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/DocumentFeedOperation.java index 6efdfc2547a..f3ddbc9196c 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/DocumentFeedOperation.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/DocumentFeedOperation.java @@ -1,4 +1,23 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespaxmlparser; -public class DocumentFeedOperation { +import com.yahoo.document.Document; +import com.yahoo.document.TestAndSetCondition; + +public class DocumentFeedOperation extends ConditionalFeedOperation { + private final Document document; + public DocumentFeedOperation(Document document) { + super(Type.DOCUMENT); + this.document = document; + } + + public DocumentFeedOperation(Document document, TestAndSetCondition condition) { + super(Type.DOCUMENT, condition); + this.document = document; + } + + @Override + public Document getDocument() { + return document; + } } diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/DocumentUpdateFeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/DocumentUpdateFeedOperation.java index 91cfcb955fe..af20d72a4e2 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/DocumentUpdateFeedOperation.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/DocumentUpdateFeedOperation.java @@ -1,4 +1,22 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespaxmlparser; -public class DocumentUpdateFeedOperation { +import com.yahoo.document.DocumentUpdate; +import com.yahoo.document.TestAndSetCondition; + +public class DocumentUpdateFeedOperation extends ConditionalFeedOperation { + private final DocumentUpdate update; + public DocumentUpdateFeedOperation(DocumentUpdate update) { + super(Type.UPDATE); + this.update = update; + } + public DocumentUpdateFeedOperation(DocumentUpdate update, TestAndSetCondition condition) { + super(Type.UPDATE, condition); + this.update = update; + } + + @Override + public DocumentUpdate getDocumentUpdate() { + return update; + } } diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java index 58371faeced..9da3408da61 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/FeedOperation.java @@ -1,3 +1,4 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespaxmlparser; import com.yahoo.document.Document; @@ -5,13 +6,11 @@ import com.yahoo.document.DocumentId; import com.yahoo.document.DocumentUpdate; import com.yahoo.document.TestAndSetCondition; -public abstract class FeedOperation { +public class FeedOperation { public enum Type {DOCUMENT, REMOVE, UPDATE, INVALID} + public static final FeedOperation INVALID = new FeedOperation(Type.INVALID); private Type type; - protected FeedOperation() { - this(Type.INVALID); - } protected FeedOperation(Type type) { this.type = type; } @@ -20,9 +19,9 @@ public abstract class FeedOperation { this.type = type; } - public abstract Document getDocument(); - public abstract DocumentUpdate getDocumentUpdate(); - public abstract DocumentId getRemove(); + public Document getDocument() { return null; } + public DocumentUpdate getDocumentUpdate() { return null; } + public DocumentId getRemove() { return null; } public TestAndSetCondition getCondition() { return TestAndSetCondition.NOT_PRESENT_CONDITION; diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/RemoveFeedOperation.java b/document/src/main/java/com/yahoo/vespaxmlparser/RemoveFeedOperation.java index 8acd8506d0b..782a6295ee1 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/RemoveFeedOperation.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/RemoveFeedOperation.java @@ -1,4 +1,22 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespaxmlparser; -public class RemoveFeedOperation { +import com.yahoo.document.DocumentId; +import com.yahoo.document.TestAndSetCondition; + +public class RemoveFeedOperation extends ConditionalFeedOperation { + private final DocumentId documentId; + public RemoveFeedOperation(DocumentId documentId) { + super(Type.REMOVE); + this.documentId = documentId; + } + public RemoveFeedOperation(DocumentId documentId, TestAndSetCondition condition) { + super(Type.REMOVE, condition); + this.documentId = documentId; + } + + @Override + public DocumentId getRemove() { + return documentId; + } } diff --git a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java index e34f3e7e4e4..7bc0cc871ca 100644 --- a/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java +++ b/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLFeedReader.java @@ -75,70 +75,6 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader { } /** - * 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 extends FeedOperation { - - private Document doc; - private DocumentId remove; - private DocumentUpdate docUpdate; - private TestAndSetCondition condition = TestAndSetCondition.NOT_PRESENT_CONDITION; - - public Operation() { - setInvalid(); - } - - public void setInvalid() { - doc = null; - remove = null; - docUpdate = null; - condition = TestAndSetCondition.NOT_PRESENT_CONDITION; - } - - @Override - public Document getDocument() { - return doc; - } - - public void setDocument(Document doc) { - setType(Type.DOCUMENT); - this.doc = doc; - } - - @Override - public DocumentId getRemove() { - return remove; - } - - public void setRemove(DocumentId remove) { - setType(Type.REMOVE); - this.remove = remove; - } - @Override - public DocumentUpdate getDocumentUpdate() { - return docUpdate; - } - - public void setDocumentUpdate(DocumentUpdate docUpdate) { - setType(Type.UPDATE); - this.docUpdate = docUpdate; - } - - public void setCondition(TestAndSetCondition condition) { - this.condition = condition; - } - - @Override - public TestAndSetCondition getCondition() { - return condition; - } - - } - - /** * <p>Reads all operations from the XML stream and puts into a list. Note * that if the XML stream is large, this may cause out of memory errors, so * make sure to use this only with small streams.</p> @@ -162,7 +98,6 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader { */ @Override public FeedOperation read() throws Exception { - Operation operation = new Operation(); String startTag = null; try { while (reader.hasNext()) { @@ -174,36 +109,28 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader { if ("document".equals(startTag)) { VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(reader, docTypeManager); Document document = new Document(documentReader); - operation.setDocument(document); - operation.setCondition(TestAndSetCondition.fromConditionString(documentReader.getCondition())); - return operation; + return new DocumentFeedOperation(document, TestAndSetCondition.fromConditionString(documentReader.getCondition())); } 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 operation; + return new DocumentUpdateFeedOperation(update, TestAndSetCondition.fromConditionString(updateReader.getCondition())); } else if ("remove".equals(startTag)) { - boolean documentIdFound = false; + DocumentId documentId = null; Optional<String> condition = Optional.empty(); for (int i = 0; i < reader.getAttributeCount(); i++) { final String attributeName = reader.getAttributeName(i).toString(); if ("documentid".equals(attributeName) || "id".equals(attributeName)) { - operation.setRemove(new DocumentId(reader.getAttributeValue(i))); - documentIdFound = true; + documentId = new DocumentId(reader.getAttributeValue(i)); } else if ("condition".equals(attributeName)) { condition = Optional.of(reader.getAttributeValue(i)); } } - if (!documentIdFound) { + if (documentId == null) { throw newDeserializeException("Missing \"documentid\" attribute for remove operation"); } - - operation.setCondition(TestAndSetCondition.fromConditionString(condition)); - - return operation; + return new RemoveFeedOperation(documentId, TestAndSetCondition.fromConditionString(condition)); } else { throw newDeserializeException("Element \"" + startTag + "\" not allowed in this context"); } @@ -222,7 +149,7 @@ public class VespaXMLFeedReader extends VespaXMLReader implements FeedReader { throw(e); } - return operation; + return FeedOperation.INVALID; } } |