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/src/main/java/com/yahoo/vespaxmlparser | |
parent | 1547d20ff3b8ec57fbd62fcd7348758b8c103232 (diff) |
Replace the multipurpose VespaXMLFeeder.Operation with more to the point classes with proper final members.
Diffstat (limited to 'document/src/main/java/com/yahoo/vespaxmlparser')
6 files changed, 89 insertions, 91 deletions
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; } } |