From 8cff380e5d0f7d164365beb270713080dddba8a1 Mon Sep 17 00:00:00 2001 From: rashtao Date: Tue, 20 Aug 2019 10:14:48 +0200 Subject: [PATCH] GraphReadOptions (#303) * GraphReadOptions * changelog upd --- ChangeLog.md | 2 +- docs/Drivers/Java/Reference/Graph/Edges.md | 4 +- docs/Drivers/Java/Reference/Graph/Vertices.md | 4 +- .../com/arangodb/ArangoEdgeCollection.java | 8 +- .../com/arangodb/ArangoVertexCollection.java | 8 +- .../internal/ArangoEdgeCollectionImpl.java | 10 +- .../internal/ArangoVertexCollectionImpl.java | 10 +- .../InternalArangoEdgeCollection.java | 10 +- .../InternalArangoVertexCollection.java | 10 +- .../model/GraphDocumentReadOptions.java | 96 +++++++++++++++++++ .../arangodb/ArangoEdgeCollectionTest.java | 14 +-- .../arangodb/ArangoVertexCollectionTest.java | 14 +-- 12 files changed, 127 insertions(+), 63 deletions(-) create mode 100644 src/main/java/com/arangodb/model/GraphDocumentReadOptions.java diff --git a/ChangeLog.md b/ChangeLog.md index e04b582f8..df801561a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -8,13 +8,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ### Added +- split `GraphDocumentReadOptions` from `DocumentReadOptions` (breaking change) - added `ArangoCollection#getResponsibleShard(Object)` - added support for Analyzers - added support for Stream Transactions - added support for named indices - added support for TTL indices - added minReplicationAttribute for collections and graphs -- added batched thread support in ArangoCollection.importDocuments (by @rkhaja) ## [5.0.7] - 2019-07-19 diff --git a/docs/Drivers/Java/Reference/Graph/Edges.md b/docs/Drivers/Java/Reference/Graph/Edges.md index 3d30c8a82..21673ebdb 100644 --- a/docs/Drivers/Java/Reference/Graph/Edges.md +++ b/docs/Drivers/Java/Reference/Graph/Edges.md @@ -2,7 +2,7 @@ ## ArangoEdgeCollection.getEdge -`ArangoEdgeCollection.getEdge(String key, Class type, DocumentReadOptions options) : T` +`ArangoEdgeCollection.getEdge(String key, Class type, GraphDocumentReadOptions options) : T` Retrieves the edge document with the given `key` from the collection. @@ -16,7 +16,7 @@ Retrieves the edge document with the given `key` from the collection. The type of the edge-document (POJO class, `VPackSlice` or `String` for JSON) -- **options**: `DocumentReadOptions` +- **options**: `GraphDocumentReadOptions` - **ifNoneMatch**: `String` diff --git a/docs/Drivers/Java/Reference/Graph/Vertices.md b/docs/Drivers/Java/Reference/Graph/Vertices.md index ced694362..3dc5a0ba5 100644 --- a/docs/Drivers/Java/Reference/Graph/Vertices.md +++ b/docs/Drivers/Java/Reference/Graph/Vertices.md @@ -2,7 +2,7 @@ ## ArangoVertexCollection.getVertex -`ArangoVertexCollection.getVertex(String key, Class type, DocumentReadOptions options) : T` +`ArangoVertexCollection.getVertex(String key, Class type, GraphDocumentReadOptions options) : T` Retrieves the vertex document with the given `key` from the collection. @@ -16,7 +16,7 @@ Retrieves the vertex document with the given `key` from the collection. The type of the vertex-document (POJO class, `VPackSlice` or `String` for JSON) -- **options**: `DocumentReadOptions` +- **options**: `GraphDocumentReadOptions` - **ifNoneMatch**: `String` diff --git a/src/main/java/com/arangodb/ArangoEdgeCollection.java b/src/main/java/com/arangodb/ArangoEdgeCollection.java index 01252c444..610403bf3 100644 --- a/src/main/java/com/arangodb/ArangoEdgeCollection.java +++ b/src/main/java/com/arangodb/ArangoEdgeCollection.java @@ -22,11 +22,7 @@ import com.arangodb.entity.EdgeEntity; import com.arangodb.entity.EdgeUpdateEntity; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.EdgeCreateOptions; -import com.arangodb.model.EdgeDeleteOptions; -import com.arangodb.model.EdgeReplaceOptions; -import com.arangodb.model.EdgeUpdateOptions; +import com.arangodb.model.*; /** * Interface for operations on ArangoDB edge collection level. @@ -100,7 +96,7 @@ public interface ArangoEdgeCollection extends ArangoSerializationAccessor { * @return the edge identified by the key * @throws ArangoDBException */ - T getEdge(String key, Class type, DocumentReadOptions options) throws ArangoDBException; + T getEdge(String key, Class type, GraphDocumentReadOptions options) throws ArangoDBException; /** * Replaces the edge with key with the one in the body, provided there is such a edge and no precondition is diff --git a/src/main/java/com/arangodb/ArangoVertexCollection.java b/src/main/java/com/arangodb/ArangoVertexCollection.java index f872e51ae..b119c858b 100644 --- a/src/main/java/com/arangodb/ArangoVertexCollection.java +++ b/src/main/java/com/arangodb/ArangoVertexCollection.java @@ -22,11 +22,7 @@ import com.arangodb.entity.VertexEntity; import com.arangodb.entity.VertexUpdateEntity; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.VertexCreateOptions; -import com.arangodb.model.VertexDeleteOptions; -import com.arangodb.model.VertexReplaceOptions; -import com.arangodb.model.VertexUpdateOptions; +import com.arangodb.model.*; /** * Interface for operations on ArangoDB vertex collection level. @@ -110,7 +106,7 @@ public interface ArangoVertexCollection extends ArangoSerializationAccessor { * @return the vertex identified by the key * @throws ArangoDBException */ - T getVertex(String key, Class type, DocumentReadOptions options) throws ArangoDBException; + T getVertex(String key, Class type, GraphDocumentReadOptions options) throws ArangoDBException; /** * Replaces the vertex with key with the one in the body, provided there is such a vertex and no precondition is diff --git a/src/main/java/com/arangodb/internal/ArangoEdgeCollectionImpl.java b/src/main/java/com/arangodb/internal/ArangoEdgeCollectionImpl.java index 793e2e401..b8a894ef2 100644 --- a/src/main/java/com/arangodb/internal/ArangoEdgeCollectionImpl.java +++ b/src/main/java/com/arangodb/internal/ArangoEdgeCollectionImpl.java @@ -20,6 +20,7 @@ package com.arangodb.internal; +import com.arangodb.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,11 +28,6 @@ import com.arangodb.ArangoEdgeCollection; import com.arangodb.entity.EdgeEntity; import com.arangodb.entity.EdgeUpdateEntity; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.EdgeCreateOptions; -import com.arangodb.model.EdgeDeleteOptions; -import com.arangodb.model.EdgeReplaceOptions; -import com.arangodb.model.EdgeUpdateOptions; /** * @author Mark Vollmary @@ -61,7 +57,7 @@ public EdgeEntity insertEdge(final T value, final EdgeCreateOptions options) @Override public T getEdge(final String key, final Class type) throws ArangoDBException { try { - return executor.execute(getEdgeRequest(key, new DocumentReadOptions()), getEdgeResponseDeserializer(type)); + return executor.execute(getEdgeRequest(key, new GraphDocumentReadOptions()), getEdgeResponseDeserializer(type)); } catch (final ArangoDBException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); @@ -71,7 +67,7 @@ public T getEdge(final String key, final Class type) throws ArangoDBExcep } @Override - public T getEdge(final String key, final Class type, final DocumentReadOptions options) + public T getEdge(final String key, final Class type, final GraphDocumentReadOptions options) throws ArangoDBException { try { return executor.execute(getEdgeRequest(key, options), getEdgeResponseDeserializer(type)); diff --git a/src/main/java/com/arangodb/internal/ArangoVertexCollectionImpl.java b/src/main/java/com/arangodb/internal/ArangoVertexCollectionImpl.java index 56f807c89..a8d41f862 100644 --- a/src/main/java/com/arangodb/internal/ArangoVertexCollectionImpl.java +++ b/src/main/java/com/arangodb/internal/ArangoVertexCollectionImpl.java @@ -20,6 +20,7 @@ package com.arangodb.internal; +import com.arangodb.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,11 +28,6 @@ import com.arangodb.ArangoVertexCollection; import com.arangodb.entity.VertexEntity; import com.arangodb.entity.VertexUpdateEntity; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.VertexCreateOptions; -import com.arangodb.model.VertexDeleteOptions; -import com.arangodb.model.VertexReplaceOptions; -import com.arangodb.model.VertexUpdateOptions; /** * @author Mark Vollmary @@ -66,7 +62,7 @@ public VertexEntity insertVertex(final T value, final VertexCreateOptions op @Override public T getVertex(final String key, final Class type) throws ArangoDBException { try { - return executor.execute(getVertexRequest(key, new DocumentReadOptions()), + return executor.execute(getVertexRequest(key, new GraphDocumentReadOptions()), getVertexResponseDeserializer(type)); } catch (final ArangoDBException e) { if (LOGGER.isDebugEnabled()) { @@ -77,7 +73,7 @@ public T getVertex(final String key, final Class type) throws ArangoDBExc } @Override - public T getVertex(final String key, final Class type, final DocumentReadOptions options) + public T getVertex(final String key, final Class type, final GraphDocumentReadOptions options) throws ArangoDBException { try { return executor.execute(getVertexRequest(key, options), getVertexResponseDeserializer(type)); diff --git a/src/main/java/com/arangodb/internal/InternalArangoEdgeCollection.java b/src/main/java/com/arangodb/internal/InternalArangoEdgeCollection.java index 573478386..77c6fbc0a 100644 --- a/src/main/java/com/arangodb/internal/InternalArangoEdgeCollection.java +++ b/src/main/java/com/arangodb/internal/InternalArangoEdgeCollection.java @@ -30,11 +30,7 @@ import com.arangodb.internal.util.ArangoSerializationFactory.Serializer; import com.arangodb.internal.util.DocumentUtil; import com.arangodb.internal.util.RequestUtils; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.EdgeCreateOptions; -import com.arangodb.model.EdgeDeleteOptions; -import com.arangodb.model.EdgeReplaceOptions; -import com.arangodb.model.EdgeUpdateOptions; +import com.arangodb.model.*; import com.arangodb.util.ArangoSerializer; import com.arangodb.velocypack.VPackSlice; import com.arangodb.velocypack.exception.VPackException; @@ -94,10 +90,10 @@ public EdgeEntity deserialize(final Response response) throws VPackException { }; } - protected Request getEdgeRequest(final String key, final DocumentReadOptions options) { + protected Request getEdgeRequest(final String key, final GraphDocumentReadOptions options) { final Request request = request(graph.db().name(), RequestType.GET, PATH_API_GHARIAL, graph.name(), EDGE, DocumentUtil.createDocumentHandle(name, key)); - final DocumentReadOptions params = (options != null ? options : new DocumentReadOptions()); + final GraphDocumentReadOptions params = (options != null ? options : new GraphDocumentReadOptions()); request.putHeaderParam(ArangoRequestParam.IF_NONE_MATCH, params.getIfNoneMatch()); request.putHeaderParam(ArangoRequestParam.IF_MATCH, params.getIfMatch()); if (params.getAllowDirtyRead() == Boolean.TRUE) { diff --git a/src/main/java/com/arangodb/internal/InternalArangoVertexCollection.java b/src/main/java/com/arangodb/internal/InternalArangoVertexCollection.java index 76c8141b8..15874f789 100644 --- a/src/main/java/com/arangodb/internal/InternalArangoVertexCollection.java +++ b/src/main/java/com/arangodb/internal/InternalArangoVertexCollection.java @@ -30,11 +30,7 @@ import com.arangodb.internal.util.ArangoSerializationFactory.Serializer; import com.arangodb.internal.util.DocumentUtil; import com.arangodb.internal.util.RequestUtils; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.VertexCreateOptions; -import com.arangodb.model.VertexDeleteOptions; -import com.arangodb.model.VertexReplaceOptions; -import com.arangodb.model.VertexUpdateOptions; +import com.arangodb.model.*; import com.arangodb.util.ArangoSerializer; import com.arangodb.velocypack.VPackSlice; import com.arangodb.velocypack.exception.VPackException; @@ -98,10 +94,10 @@ public VertexEntity deserialize(final Response response) throws VPackException { }; } - protected Request getVertexRequest(final String key, final DocumentReadOptions options) { + protected Request getVertexRequest(final String key, final GraphDocumentReadOptions options) { final Request request = request(graph.db().name(), RequestType.GET, PATH_API_GHARIAL, graph.name(), VERTEX, DocumentUtil.createDocumentHandle(name, key)); - final DocumentReadOptions params = (options != null ? options : new DocumentReadOptions()); + final GraphDocumentReadOptions params = (options != null ? options : new GraphDocumentReadOptions()); request.putHeaderParam(ArangoRequestParam.IF_NONE_MATCH, params.getIfNoneMatch()); request.putHeaderParam(ArangoRequestParam.IF_MATCH, params.getIfMatch()); if (params.getAllowDirtyRead() == Boolean.TRUE) { diff --git a/src/main/java/com/arangodb/model/GraphDocumentReadOptions.java b/src/main/java/com/arangodb/model/GraphDocumentReadOptions.java new file mode 100644 index 000000000..aaa4ef1ba --- /dev/null +++ b/src/main/java/com/arangodb/model/GraphDocumentReadOptions.java @@ -0,0 +1,96 @@ +/* + * DISCLAIMER + * + * Copyright 2016 ArangoDB GmbH, Cologne, Germany + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.model; + +import com.arangodb.velocypack.annotations.Expose; + +/** + * @author Mark Vollmary + */ +public class GraphDocumentReadOptions { + + private String ifNoneMatch; + private String ifMatch; + private boolean catchException; + @Expose(serialize = false) + private Boolean allowDirtyRead; + + public GraphDocumentReadOptions() { + super(); + catchException = true; + } + + public String getIfNoneMatch() { + return ifNoneMatch; + } + + /** + * @param ifNoneMatch document revision must not contain If-None-Match + * @return options + */ + public GraphDocumentReadOptions ifNoneMatch(final String ifNoneMatch) { + this.ifNoneMatch = ifNoneMatch; + return this; + } + + public String getIfMatch() { + return ifMatch; + } + + /** + * @param ifMatch document revision must contain If-Match + * @return options + */ + public GraphDocumentReadOptions ifMatch(final String ifMatch) { + this.ifMatch = ifMatch; + return this; + } + + public boolean isCatchException() { + return catchException; + } + + /** + * @param catchException whether or not catch possible thrown exceptions + * @return options + */ + public GraphDocumentReadOptions catchException(final boolean catchException) { + this.catchException = catchException; + return this; + } + + /** + * @param allowDirtyRead Set to {@code true} allows reading from followers in an active-failover setup. + * @return options + * @see API + * Documentation + * @since ArangoDB 3.4.0 + */ + public GraphDocumentReadOptions allowDirtyRead(final Boolean allowDirtyRead) { + this.allowDirtyRead = allowDirtyRead; + return this; + } + + public Boolean getAllowDirtyRead() { + return allowDirtyRead; + } + +} diff --git a/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java b/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java index 6f26e4a83..6e4b1b9f3 100644 --- a/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Collection; +import com.arangodb.model.*; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -45,11 +46,6 @@ import com.arangodb.entity.EdgeEntity; import com.arangodb.entity.EdgeUpdateEntity; import com.arangodb.entity.VertexEntity; -import com.arangodb.model.CollectionCreateOptions; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.EdgeDeleteOptions; -import com.arangodb.model.EdgeReplaceOptions; -import com.arangodb.model.EdgeUpdateOptions; /** * @author Mark Vollmary @@ -148,7 +144,7 @@ public void getEdge() { public void getEdgeIfMatch() { final BaseEdgeDocument value = createEdgeValue(); final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch(edge.getRev()); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifMatch(edge.getRev()); final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseDocument.class, options); assertThat(document, is(notNullValue())); @@ -159,7 +155,7 @@ public void getEdgeIfMatch() { public void getEdgeIfMatchFail() { final BaseEdgeDocument value = createEdgeValue(); final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifMatch("no"); final BaseEdgeDocument edge2 = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseEdgeDocument.class, options); assertThat(edge2, is(nullValue())); @@ -169,7 +165,7 @@ public void getEdgeIfMatchFail() { public void getEdgeIfNoneMatch() { final BaseEdgeDocument value = createEdgeValue(); final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifNoneMatch("no"); final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseDocument.class, options); assertThat(document, is(notNullValue())); @@ -180,7 +176,7 @@ public void getEdgeIfNoneMatch() { public void getEdgeIfNoneMatchFail() { final BaseEdgeDocument value = createEdgeValue(); final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(edge.getRev()); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifNoneMatch(edge.getRev()); final BaseEdgeDocument edge2 = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseEdgeDocument.class, options); assertThat(edge2, is(nullValue())); diff --git a/src/test/java/com/arangodb/ArangoVertexCollectionTest.java b/src/test/java/com/arangodb/ArangoVertexCollectionTest.java index 2d2120131..01ac91580 100644 --- a/src/test/java/com/arangodb/ArangoVertexCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoVertexCollectionTest.java @@ -32,6 +32,7 @@ import java.util.Collection; import java.util.UUID; +import com.arangodb.model.*; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,11 +42,6 @@ import com.arangodb.entity.BaseDocument; import com.arangodb.entity.VertexEntity; import com.arangodb.entity.VertexUpdateEntity; -import com.arangodb.model.DocumentReadOptions; -import com.arangodb.model.GraphCreateOptions; -import com.arangodb.model.VertexDeleteOptions; -import com.arangodb.model.VertexReplaceOptions; -import com.arangodb.model.VertexUpdateOptions; /** * @author Mark Vollmary @@ -145,7 +141,7 @@ public void getVertex() { public void getVertexIfMatch() { final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) .insertVertex(new BaseDocument(), null); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch(vertex.getRev()); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifMatch(vertex.getRev()); final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class, options); assertThat(document, is(notNullValue())); @@ -156,7 +152,7 @@ public void getVertexIfMatch() { public void getVertexIfMatchFail() { final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) .insertVertex(new BaseDocument(), null); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifMatch("no"); final BaseDocument vertex2 = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class, options); assertThat(vertex2, is(nullValue())); @@ -166,7 +162,7 @@ public void getVertexIfMatchFail() { public void getVertexIfNoneMatch() { final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) .insertVertex(new BaseDocument(), null); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifNoneMatch("no"); final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class, options); assertThat(document, is(notNullValue())); @@ -177,7 +173,7 @@ public void getVertexIfNoneMatch() { public void getVertexIfNoneMatchFail() { final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) .insertVertex(new BaseDocument(), null); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(vertex.getRev()); + final GraphDocumentReadOptions options = new GraphDocumentReadOptions().ifNoneMatch(vertex.getRev()); final BaseDocument vertex2 = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class, options); assertThat(vertex2, is(nullValue()));