3737import com .arangodb .entity .GraphGetCollectionsResultEntity ;
3838import com .arangodb .entity .GraphsEntity ;
3939import com .arangodb .entity .marker .VertexEntity ;
40+ import com .arangodb .http .BatchHttpManager ;
4041import com .arangodb .http .HttpManager ;
4142import com .arangodb .http .HttpResponseEntity ;
4243import com .arangodb .util .CollectionUtils ;
5455public class InternalGraphDriverImpl extends BaseArangoDriverWithCursorImpl
5556 implements com .arangodb .InternalGraphDriver {
5657
58+ private static final String UNKNOWN_ERROR = "unknown error" ;
59+ private static final String VERTEX = "/vertex" ;
60+ private static final String EDGE = "/edge" ;
61+
5762 InternalGraphDriverImpl (ArangoConfigure configure , InternalCursorDriver cursorDriver , HttpManager httpManager ) {
5863 super (configure , cursorDriver , httpManager );
5964 }
6065
6166 @ Override
6267 public GraphEntity createGraph (String databaseName , String graphName , Boolean waitForSync ) throws ArangoException {
63- HttpResponseEntity response = httpManager .doPost (createEndpointUrl (databaseName , "/_api/gharial" ),
68+ HttpResponseEntity response = httpManager .doPost (createGharialEndpointUrl (databaseName ),
6469 new MapBuilder ().put ("waitForSync" , waitForSync ).get (),
6570 EntityFactory .toJsonString (new MapBuilder ().put ("name" , graphName ).get ()));
6671 return createEntity (response , GraphEntity .class );
@@ -74,10 +79,11 @@ public GraphEntity createGraph(
7479 List <String > orphanCollections ,
7580 Boolean waitForSync ) throws ArangoException {
7681
77- HttpResponseEntity response = httpManager .doPost (createEndpointUrl (databaseName , "/_api/gharial" ),
78- new MapBuilder ().put ("waitForSync" , waitForSync ).get (),
79- EntityFactory .toJsonString (new MapBuilder ().put ("name" , graphName ).put ("edgeDefinitions" , edgeDefinitions )
80- .put ("orphanCollections" , orphanCollections ).get ()));
82+ HttpResponseEntity response = httpManager
83+ .doPost (createGharialEndpointUrl (databaseName ), new MapBuilder ().put ("waitForSync" , waitForSync ).get (),
84+ EntityFactory .toJsonString (
85+ new MapBuilder ().put ("name" , graphName ).put ("edgeDefinitions" , edgeDefinitions )
86+ .put ("orphanCollections" , orphanCollections ).get ()));
8187 return createEntity (response , GraphEntity .class );
8288 }
8389
@@ -99,7 +105,7 @@ public GraphsEntity getGraphs(String databaseName) throws ArangoException {
99105
100106 @ Override
101107 public List <String > getGraphList (String databaseName ) throws ArangoException {
102- HttpResponseEntity res = httpManager .doGet (createEndpointUrl (databaseName , "/_api/gharial" ));
108+ HttpResponseEntity res = httpManager .doGet (createGharialEndpointUrl (databaseName ));
103109 GraphsEntity graphsEntity = createEntity (res , GraphsEntity .class );
104110 List <String > graphList = new ArrayList <String >();
105111 List <GraphEntity > graphs = graphsEntity .getGraphs ();
@@ -115,8 +121,7 @@ public List<String> getGraphList(String databaseName) throws ArangoException {
115121 public GraphEntity getGraph (String databaseName , String graphName ) throws ArangoException {
116122 validateCollectionName (graphName ); // ??
117123 HttpResponseEntity res = httpManager .doGet (
118- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName )), new MapBuilder ().get (),
119- null );
124+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName )), new MapBuilder ().get (), null );
120125 return createEntity (res , GraphEntity .class );
121126
122127 }
@@ -126,31 +131,39 @@ public DeletedEntity deleteGraph(String databaseName, String graphName, Boolean
126131 throws ArangoException {
127132 validateCollectionName (graphName ); // ??
128133 HttpResponseEntity res = httpManager .doDelete (
129- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName )), new MapBuilder ().get (),
134+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName )), new MapBuilder ().get (),
130135 new MapBuilder ().put ("dropCollections" , dropCollections ).get ());
131136
132- if (! res . isJsonResponse ( )) {
133- throw new ArangoException ("unknown error" );
137+ if (wrongResult ( res )) {
138+ throw new ArangoException (UNKNOWN_ERROR );
134139 }
135140
136- DeletedEntity result = createEntity (res , DeletedEntity .class , null , true );
141+ DeletedEntity result ;
142+ if (isInBatchMode ()) {
143+ result = new DeletedEntity ();
144+ } else {
145+ result = createEntity (res , DeletedEntity .class , null , true );
146+ }
137147
138148 return result ;
139-
140149 }
141150
142151 @ Override
143152 public List <String > getVertexCollections (String databaseName , String graphName ) throws ArangoException {
144153 validateCollectionName (graphName );
145154 HttpResponseEntity res = httpManager
146- .doGet (createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/vertex" ));
155+ .doGet (createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), VERTEX ));
147156
148- if (! res . isJsonResponse ( )) {
149- throw new ArangoException ("unknown error" );
157+ if (wrongResult ( res )) {
158+ throw new ArangoException (UNKNOWN_ERROR );
150159 }
151160
152- GraphGetCollectionsResultEntity result = createEntity (res , GraphGetCollectionsResultEntity .class , null , true );
153-
161+ GraphGetCollectionsResultEntity result ;
162+ if (isInBatchMode ()) {
163+ result = new GraphGetCollectionsResultEntity ();
164+ } else {
165+ result = createEntity (res , GraphGetCollectionsResultEntity .class , null , true );
166+ }
154167 return result .getCollections ();
155168 }
156169
@@ -175,14 +188,22 @@ public DeletedEntity deleteVertexCollection(
175188 validateCollectionName (graphName );
176189
177190 HttpResponseEntity res = httpManager .doDelete (
178- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/vertex" ,
191+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), VERTEX ,
179192 StringUtils .encodeUrl (collectionName )),
180193 new MapBuilder ().get (), new MapBuilder ().put ("dropCollection" , dropCollection ).get ());
181194
182- DeletedEntity result = createEntity (res , DeletedEntity .class , null , true );
195+ if (wrongResult (res )) {
196+ throw new ArangoException (UNKNOWN_ERROR );
197+ }
183198
184- return result ;
199+ DeletedEntity result ;
200+ if (isInBatchMode ()) {
201+ result = new DeletedEntity ();
202+ } else {
203+ result = createEntity (res , DeletedEntity .class , null , true );
204+ }
185205
206+ return result ;
186207 }
187208
188209 @ Override
@@ -192,14 +213,19 @@ public GraphEntity createVertexCollection(String databaseName, String graphName,
192213 validateCollectionName (collectionName );
193214
194215 HttpResponseEntity res = httpManager .doPost (
195- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/vertex" ), null ,
216+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), VERTEX ), null ,
196217 EntityFactory .toJsonString (new MapBuilder ().put ("collection" , collectionName ).get ()));
197218
198- if (! res . isJsonResponse ( )) {
199- throw new ArangoException ("unknown error" );
219+ if (wrongResult ( res )) {
220+ throw new ArangoException (UNKNOWN_ERROR );
200221 }
201222
202- GraphEntity result = createEntity (res , GraphEntity .class , null , true );
223+ GraphEntity result ;
224+ if (isInBatchMode ()) {
225+ result = new GraphEntity ();
226+ } else {
227+ result = createEntity (res , GraphEntity .class , null , true );
228+ }
203229
204230 return result ;
205231 }
@@ -208,13 +234,19 @@ public GraphEntity createVertexCollection(String databaseName, String graphName,
208234 public List <String > getEdgeCollections (String databaseName , String graphName ) throws ArangoException {
209235 validateCollectionName (graphName );
210236 HttpResponseEntity res = httpManager
211- .doGet (createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/edge" ));
237+ .doGet (createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), EDGE ));
212238
213- if (!res .isJsonResponse ()) {
214- throw new ArangoException ("unknown error" );
239+ if (wrongResult (res )) {
240+ throw new ArangoException (UNKNOWN_ERROR );
241+ }
242+
243+ GraphGetCollectionsResultEntity result ;
244+ if (isInBatchMode ()) {
245+ result = new GraphGetCollectionsResultEntity ();
246+ } else {
247+ result = createEntity (res , GraphGetCollectionsResultEntity .class , null , true );
215248 }
216249
217- GraphGetCollectionsResultEntity result = createEntity (res , GraphGetCollectionsResultEntity .class , null , true );
218250 return result .getCollections ();
219251 }
220252
@@ -228,14 +260,18 @@ public GraphEntity createEdgeDefinition(String databaseName, String graphName, E
228260 String edgeDefinitionJson = this .convertToString (edgeDefinition );
229261
230262 HttpResponseEntity res = httpManager .doPost (
231- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/edge" ), null ,
232- edgeDefinitionJson );
263+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), EDGE ), null , edgeDefinitionJson );
233264
234- if (! res . isJsonResponse ( )) {
235- throw new ArangoException ("unknown error" );
265+ if (wrongResult ( res )) {
266+ throw new ArangoException (UNKNOWN_ERROR );
236267 }
237268
238- GraphEntity result = createEntity (res , GraphEntity .class , null , true );
269+ GraphEntity result ;
270+ if (isInBatchMode ()) {
271+ result = new GraphEntity ();
272+ } else {
273+ result = createEntity (res , GraphEntity .class , null , true );
274+ }
239275
240276 return result ;
241277 }
@@ -252,13 +288,19 @@ public GraphEntity replaceEdgeDefinition(
252288
253289 String edgeDefinitionJson = this .convertToString (edgeDefinition );
254290
255- HttpResponseEntity res = httpManager .doPut (createEndpointUrl (databaseName , "/_api/gharial" ,
256- StringUtils .encodeUrl (graphName ), "/edge" , StringUtils .encodeUrl (edgeName )), null , edgeDefinitionJson );
257- if (!res .isJsonResponse ()) {
258- throw new ArangoException ("unknown error" );
291+ HttpResponseEntity res = httpManager .doPut (createGharialEndpointUrl (databaseName ,
292+ StringUtils .encodeUrl (graphName ), EDGE , StringUtils .encodeUrl (edgeName )), null , edgeDefinitionJson );
293+
294+ if (wrongResult (res )) {
295+ throw new ArangoException (UNKNOWN_ERROR );
259296 }
260297
261- GraphEntity result = createEntity (res , GraphEntity .class , null , true );
298+ GraphEntity result ;
299+ if (isInBatchMode ()) {
300+ result = new GraphEntity ();
301+ } else {
302+ result = createEntity (res , GraphEntity .class , null , true );
303+ }
262304
263305 return result ;
264306
@@ -273,14 +315,20 @@ public GraphEntity deleteEdgeDefinition(
273315 validateCollectionName (graphName );
274316 validateCollectionName (edgeName );
275317
276- HttpResponseEntity res = httpManager .doDelete (createEndpointUrl (databaseName , "/_api/gharial" ,
277- StringUtils .encodeUrl (graphName ), "/edge" , StringUtils .encodeUrl (edgeName )),
318+ HttpResponseEntity res = httpManager .doDelete (createGharialEndpointUrl (databaseName ,
319+ StringUtils .encodeUrl (graphName ), EDGE , StringUtils .encodeUrl (edgeName )),
278320 new MapBuilder ().put ("dropCollection" , dropCollection ).get ());
279- if (!res .isJsonResponse ()) {
280- throw new ArangoException ("unknown error" );
321+
322+ if (wrongResult (res )) {
323+ throw new ArangoException (UNKNOWN_ERROR );
281324 }
282325
283- GraphEntity result = createEntity (res , GraphEntity .class , null , true );
326+ GraphEntity result ;
327+ if (isInBatchMode ()) {
328+ result = new GraphEntity ();
329+ } else {
330+ result = createEntity (res , GraphEntity .class , null , true );
331+ }
284332
285333 return result ;
286334 }
@@ -322,23 +370,43 @@ public <T> VertexEntity<T> createVertex(
322370
323371 validateCollectionName (graphName );
324372 HttpResponseEntity res = httpManager .doPost (
325- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "vertex" ,
373+ createGharialEndpointUrl (database , StringUtils .encodeUrl (graphName ), VERTEX ,
326374 StringUtils .encodeUrl (collectionName )),
327375 new MapBuilder ().put ("waitForSync" , waitForSync ).get (), EntityFactory .toJsonString (obj ));
328376
329- if (! res . isJsonResponse ( )) {
330- throw new ArangoException ("unknown error" );
377+ if (wrongResult ( res )) {
378+ throw new ArangoException (UNKNOWN_ERROR );
331379 }
332380
333- VertexEntity <T > result = createEntity (res , VertexEntity .class , vertex .getClass ());
334- if (vertex != null ) {
381+ VertexEntity <T > result ;
382+ if (isInBatchMode ()) {
383+ result = new VertexEntity <T >();
384+ result .setEntity (vertex );
385+ } else {
386+ result = createEntity (res , VertexEntity .class , vertex .getClass ());
335387 result .setEntity (vertex );
336388 annotationHandler .updateDocumentAttributes (result .getEntity (), result .getDocumentRevision (),
337389 result .getDocumentHandle (), result .getDocumentKey ());
338390 }
339391 return result ;
340392 }
341393
394+ private boolean wrongResult (HttpResponseEntity res ) {
395+ if (res .isJsonResponse ()) {
396+ return false ;
397+ }
398+ if (httpManager instanceof BatchHttpManager && ((BatchHttpManager ) httpManager ).isBatchModeActive ()) {
399+ // we are in batch mode
400+ return false ;
401+ }
402+
403+ return true ;
404+ }
405+
406+ private boolean isInBatchMode () {
407+ return httpManager instanceof BatchHttpManager && ((BatchHttpManager ) httpManager ).isBatchModeActive ();
408+ }
409+
342410 @ SuppressWarnings ("unchecked" )
343411 @ Override
344412 public <T > VertexEntity <T > getVertex (
@@ -352,8 +420,8 @@ public <T> VertexEntity<T> getVertex(
352420
353421 validateCollectionName (graphName );
354422 HttpResponseEntity res = httpManager .doGet (
355- createEndpointUrl ( StringUtils . encodeUrl ( databaseName ), "/_api/gharial" , StringUtils .encodeUrl (graphName ),
356- "vertex" , StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
423+ createGharialEndpointUrl ( databaseName , StringUtils .encodeUrl (graphName ), VERTEX ,
424+ StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
357425 new MapBuilder ().put ("If-Match" , ifMatchRevision , true ).put ("If-None-Match" , ifNoneMatchRevision , true )
358426 .get (),
359427 new MapBuilder ().get ());
@@ -375,8 +443,8 @@ public <T> VertexEntity<T> replaceVertex(
375443
376444 validateCollectionName (graphName );
377445 HttpResponseEntity res = httpManager .doPut (
378- createEndpointUrl ( StringUtils . encodeUrl ( databaseName ), "/_api/gharial" , StringUtils .encodeUrl (graphName ),
379- "vertex" , StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
446+ createGharialEndpointUrl ( databaseName , StringUtils .encodeUrl (graphName ), VERTEX ,
447+ StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
380448 new MapBuilder ().put ("If-Match" , ifMatchRevision , true ).put ("If-None-Match" , ifNoneMatchRevision , true )
381449 .get (),
382450 new MapBuilder ().put ("waitForSync" , waitForSync ).get (), EntityFactory .toJsonString (vertex ));
@@ -405,7 +473,7 @@ public <T> VertexEntity<T> updateVertex(
405473
406474 validateCollectionName (graphName );
407475 HttpResponseEntity res = httpManager .doPatch (
408- createEndpointUrl (databaseName , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "vertex" ,
476+ createGharialEndpointUrl (databaseName , StringUtils .encodeUrl (graphName ), VERTEX ,
409477 StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
410478 new MapBuilder ().put ("If-Match" , ifMatchRevision , true ).put ("If-None-Match" , ifNoneMatchRevision , true )
411479 .get (),
@@ -433,8 +501,8 @@ public DeletedEntity deleteVertex(
433501
434502 validateCollectionName (graphName );
435503 HttpResponseEntity res = httpManager .doDelete (
436- createEndpointUrl ( StringUtils . encodeUrl ( databaseName ), "/_api/gharial" , StringUtils .encodeUrl (graphName ),
437- "vertex" , StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
504+ createGharialEndpointUrl ( databaseName , StringUtils .encodeUrl (graphName ), VERTEX ,
505+ StringUtils .encodeUrl (collectionName ), StringUtils .encodeUrl (key )),
438506 new MapBuilder ().put ("If-Match" , ifMatchRevision , true ).put ("If-None-Match" , ifNoneMatchRevision , true )
439507 .get (),
440508 new MapBuilder ().put ("waitForSync" , waitForSync ).get ());
@@ -473,7 +541,7 @@ public <T> EdgeEntity<T> createEdge(
473541
474542 validateCollectionName (graphName );
475543 HttpResponseEntity res = httpManager .doPost (
476- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/edge" ,
544+ createGharialEndpointUrl (database , StringUtils .encodeUrl (graphName ), EDGE ,
477545 StringUtils .encodeUrl (edgeCollectionName )),
478546 new MapBuilder ().put ("waitForSync" , waitForSync ).get (), EntityFactory .toJsonString (obj ));
479547
@@ -502,7 +570,7 @@ public <T> EdgeEntity<T> getEdge(
502570
503571 validateCollectionName (graphName );
504572 HttpResponseEntity res = httpManager .doGet (
505- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "edge" ,
573+ createGharialEndpointUrl (database , StringUtils .encodeUrl (graphName ), EDGE ,
506574 StringUtils .encodeUrl (edgeCollectionName ), StringUtils .encodeUrl (key )),
507575 new MapBuilder ().put ("If-None-Match" , ifNoneMatchRevision , true ).put ("If-Match" , ifMatchRevision , true )
508576 .get (),
@@ -523,7 +591,7 @@ public DeletedEntity deleteEdge(
523591
524592 validateCollectionName (graphName );
525593 HttpResponseEntity res = httpManager .doDelete (
526- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "edge" ,
594+ createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), EDGE ,
527595 StringUtils .encodeUrl (edgeCollectionName ), StringUtils .encodeUrl (key )),
528596 new MapBuilder ().put ("If-None-Match" , ifNoneMatchRevision , true ).put ("If-Match" , ifMatchRevision , true )
529597 .get (),
@@ -547,7 +615,7 @@ public <T> EdgeEntity<T> replaceEdge(
547615
548616 validateCollectionName (graphName );
549617 HttpResponseEntity res = httpManager .doPut (
550- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/edge" ,
618+ createGharialEndpointUrl (database , StringUtils .encodeUrl (graphName ), EDGE ,
551619 StringUtils .encodeUrl (edgeCollectionName ), StringUtils .encodeUrl (key )),
552620 new MapBuilder ().put ("If-None-Match" , ifNoneMatchRevision , true ).put ("If-Match" , ifMatchRevision , true )
553621 .get (),
@@ -578,7 +646,7 @@ public <T> EdgeEntity<T> updateEdge(
578646
579647 validateCollectionName (graphName );
580648 HttpResponseEntity res = httpManager .doPatch (
581- createEndpointUrl (database , "/_api/gharial" , StringUtils .encodeUrl (graphName ), "/edge" ,
649+ createGharialEndpointUrl (database , StringUtils .encodeUrl (graphName ), EDGE ,
582650 StringUtils .encodeUrl (edgeCollectionName ), StringUtils .encodeUrl (key )),
583651 new MapBuilder ().put ("If-None-Match" , ifNoneMatchRevision , true ).put ("If-Match" , ifMatchRevision , true )
584652 .get (),
0 commit comments