2222
2323import com .arangodb .config .ArangoConfigProperties ;
2424import com .arangodb .entity .*;
25+ import com .arangodb .model .*;
26+ import com .arangodb .tinkerpop .gremlin .structure .*;
2527import org .apache .tinkerpop .gremlin .structure .Direction ;
2628import org .apache .tinkerpop .gremlin .structure .Graph ;
2729import org .slf4j .Logger ;
3335import com .arangodb .ArangoDBException ;
3436import com .arangodb .ArangoDatabase ;
3537import com .arangodb .ArangoGraph ;
36- import com .arangodb .model .AqlQueryOptions ;
37- import com .arangodb .model .GraphCreateOptions ;
3838import com .arangodb .tinkerpop .gremlin .client .ArangoDBQueryBuilder .UniqueVertices ;
39- import com .arangodb .tinkerpop .gremlin .structure .ArangoDBEdge ;
40- import com .arangodb .tinkerpop .gremlin .structure .ArangoDBGraph ;
41- import com .arangodb .tinkerpop .gremlin .structure .ArangoDBGraphVariables ;
42- import com .arangodb .tinkerpop .gremlin .structure .ArangoDBVertex ;
4339import com .arangodb .tinkerpop .gremlin .utils .ArangoDBUtil ;
4440
4541/**
@@ -380,36 +376,6 @@ public void updateDocument(ArangoDBBaseDocument document) {
380376document ._rev (vertexEntity .getRev ());
381377}
382378
383- /**
384- * Get an edge from the graph.
385- *
386- * @param <V> the value type
387- * @param id the id (name) of the edge
388- * @param collection the collection from which the edge is retrieved
389- * @param edgeClass the edge's specialised class
390- * @return the edge
391- * @throws ArangoDBGraphException If there was an error retrieving the edge
392- */
393-
394- public <V extends ArangoDBBaseEdge > V getEdge (
395- String id ,
396- String collection ,
397- Class <V > edgeClass ) {
398- logger .debug ("Get edge {} from {}:{}" , id , graph .name (), graph .getPrefixedCollectioName (collection ));
399- V result ;
400- try {
401- result = db .graph (graph .name ())
402- .edgeCollection (graph .getPrefixedCollectioName (collection ))
403- .getEdge (id , edgeClass );
404- } catch (ArangoDBException e ) {
405- logger .error ("Failed to retrieve edge: {}" , e .getErrorMessage ());
406- throw ArangoDBExceptions .getArangoDBException (e );
407- }
408- result .collection (collection );
409- result .graph (graph );
410- return result ;
411- }
412-
413379/**
414380 * Insert an edge in the graph. The edge is updated with the id, rev and name (if not
415381 * present)
@@ -437,46 +403,6 @@ public void insertEdge(ArangoDBBaseEdge edge) {
437403edge ._rev (insertEntity .getRev ());
438404edge .setPaired (true );
439405}
440-
441- /**
442- * Delete an edge from the graph.
443- * @param edge the edge
444- * @throws ArangoDBGraphException If there was an error deleting the edge
445- */
446-
447- public void deleteEdge (ArangoDBBaseEdge edge ) {
448- logger .debug ("Delete edge {} in {}" , edge , graph .name ());
449- try {
450- db .graph (graph .name ())
451- .edgeCollection (edge .collection ())
452- .deleteEdge (edge ._key ());
453- } catch (ArangoDBException e ) {
454- logger .error ("Failed to delete vertex: {}" , e .getErrorMessage ());
455- throw ArangoDBExceptions .getArangoDBException (e );
456- }
457- edge .setPaired (false );
458- }
459-
460- /**
461- * Update the edge in the graph.
462- * @param edge the edge
463- * @throws ArangoDBGraphException If there was an error updating the edge
464- */
465-
466- public void updateEdge (ArangoDBBaseEdge edge ) {
467- logger .debug ("Update edge {} in {}" , edge , graph .name ());
468- EdgeUpdateEntity edgeEntity ;
469- try {
470- edgeEntity = db .graph (graph .name ())
471- .edgeCollection (edge .collection ())
472- .updateEdge (edge ._key (), edge );
473- } catch (ArangoDBException e ) {
474- logger .error ("Failed to update vertex: {}" , e .getErrorMessage ());
475- throw ArangoDBExceptions .getArangoDBException (e );
476- }
477- logger .info ("Edge updated, new rev {}" , edgeEntity .getRev ());
478- edge ._rev (edgeEntity .getRev ());
479- }
480406
481407public ArangoDBGraphVariables getGraphVariables () {
482408logger .debug ("Get graph variables" );
@@ -569,7 +495,7 @@ public void updateGraphVariables(ArangoDBGraphVariables document) {
569495}
570496
571497/**
572- * Create a query to get all the edges of a vertex.
498+ * Create a query to get all the edges of a vertex.
573499 *
574500 * @param vertex the vertex
575501 * @param edgeLabels a list of edge labels to follow, empty if all type of edges
@@ -578,27 +504,27 @@ public void updateGraphVariables(ArangoDBGraphVariables document) {
578504 * @throws ArangoDBException if there is an error executing the query
579505 */
580506
581- public ArangoCursor <ArangoDBEdge > getVertexEdges (
582- ArangoDBVertex vertex ,
583- List <String > edgeLabels ,
584- Direction direction )
585- throws ArangoDBException {
507+ public ArangoCursor <ArangoDBEdgeDocument > getVertexEdges (
508+ ArangoDBVertex vertex ,
509+ List <String > edgeLabels ,
510+ Direction direction )
511+ throws ArangoDBException {
586512logger .debug ("Get Vertex's {}:{} Edges, in {}, from collections {}" , vertex , direction , graph .name (), edgeLabels );
587513Map <String , Object > bindVars = new HashMap <>();
588514ArangoDBQueryBuilder queryBuilder = new ArangoDBQueryBuilder ();
589515ArangoDBQueryBuilder .Direction arangoDirection = ArangoDBUtil .getArangoDirectionFromGremlinDirection (direction );
590516logger .debug ("Creating query" );
591517queryBuilder .iterateGraph (graph .name (), "v" , Optional .of ("e" ),
592- Optional .empty (), Optional .empty (), Optional .empty (),
593- arangoDirection , vertex ._id (), bindVars )
594- .graphOptions (Optional .of (UniqueVertices .NONE ), Optional .empty (), true )
595- .filterSameCollections ("e" , edgeLabels , bindVars )
596- .ret ("e" );
597-
518+ Optional .empty (), Optional .empty (), Optional .empty (),
519+ arangoDirection , vertex ._id (), bindVars )
520+ .graphOptions (Optional .of (UniqueVertices .NONE ), Optional .empty (), true )
521+ .filterSameCollections ("e" , edgeLabels , bindVars )
522+ .ret ("e" );
523+
598524String query = queryBuilder .toString ();
599- return executeAqlQuery (query , bindVars , null , ArangoDBEdge .class );
525+ return executeAqlQuery (query , bindVars , null , ArangoDBEdgeDocument .class );
600526}
601-
527+
602528/**
603529 * Get all neighbours of a document.
604530 *
@@ -708,13 +634,9 @@ public ArangoCursor<ArangoDBVertex> getGraphVertices(
708634 * Get edges of a graph. If no ids are provided, get all edges.
709635 *
710636 * @param ids the ids to match
711- * @param collections the collections to search within
712637 * @return ArangoDBBaseQuery the query object
713638 */
714-
715- public ArangoCursor <ArangoDBEdge > getGraphEdges (
716- List <String > ids ,
717- List <String > collections ) {
639+ public ArangoCursor <ArangoDBEdgeDocument > getGraphEdges (List <String > ids ) {
718640logger .debug ("Get all {} graph edges, filtered by ids: {}" , graph .name (), ids );
719641Map <String , Object > bindVars = new HashMap <>();
720642ArangoDBQueryBuilder queryBuilder = new ArangoDBQueryBuilder ();
@@ -725,21 +647,15 @@ public ArangoCursor<ArangoDBEdge> getGraphEdges(
725647} else {
726648queryBuilder .iterateCollection ("e" , prefixedColNames .get (0 ), bindVars );
727649}
728- }
729- else {
730- if (!collections .isEmpty ()) {
731- prefixedColNames = collections .stream ().map (graph ::getPrefixedCollectioName ).collect (Collectors .toList ());
732- }
733- queryBuilder .with (prefixedColNames , bindVars )
734- .documentsById (ids , "e" , bindVars );
735-
650+ } else {
651+ queryBuilder .with (prefixedColNames , bindVars ).documentsById (ids , "e" , bindVars );
736652}
737653queryBuilder .ret ("e" );
738654String query = queryBuilder .toString ();
739655logger .debug ("AQL {}" , query );
740- return executeAqlQuery (query , bindVars , null , ArangoDBEdge .class );
656+ return executeAqlQuery (query , bindVars , null , ArangoDBEdgeDocument .class );
741657}
742-
658+
743659/**
744660 * Gets the edge vertices.
745661 *
@@ -1182,4 +1098,63 @@ private void insertDocument(ArangoDBBaseDocument document, String colName) {
11821098// }
11831099// }
11841100
1101+ public void insertEdge (ArangoDBEdgeDocument edge ) {
1102+ logger .debug ("Insert edge {} in {} " , edge , graph .name ());
1103+ EdgeEntity insertEntity ;
1104+ String collection = graph .getPrefixedCollectioName (edge .getLabel ());
1105+ try {
1106+ insertEntity = db .graph (graph .name ())
1107+ .edgeCollection (collection )
1108+ .insertEdge (edge );
1109+ } catch (ArangoDBException e ) {
1110+ logger .error ("Failed to insert edge: {}" , e .getErrorMessage ());
1111+ ArangoDBGraphException arangoDBException = ArangoDBExceptions .getArangoDBException (e );
1112+ if (arangoDBException .getErrorCode () == 1210 ) {
1113+ throw Graph .Exceptions .edgeWithIdAlreadyExists (collection + "/" + edge .getKey ());
1114+ }
1115+ throw arangoDBException ;
1116+ }
1117+ edge .setKey (insertEntity .getKey ());
1118+ edge .setRev (insertEntity .getRev ());
1119+ }
1120+
1121+ // public TinkerEdgeDocument getEdge(String key, String collection) {
1122+ // logger.debug("Get edge {} from {}:{}", key, graph.name(), graph.getPrefixedCollectioName(collection));
1123+ // try {
1124+ // return db.graph(graph.name())
1125+ // .edgeCollection(graph.getPrefixedCollectioName(collection))
1126+ // .getEdge(key, TinkerEdgeDocument.class);
1127+ // } catch (ArangoDBException e) {
1128+ // logger.error("Failed to retrieve edge: {}", e.getErrorMessage());
1129+ // throw ArangoDBExceptions.getArangoDBException(e);
1130+ // }
1131+ // }
1132+
1133+ public void deleteEdge (ArangoDBEdgeDocument edge ) {
1134+ logger .debug ("Delete edge {} in {}" , edge , graph .name ());
1135+ try {
1136+ db .graph (graph .name ())
1137+ .edgeCollection (graph .getPrefixedCollectioName (edge .getLabel ()))
1138+ .deleteEdge (edge .getKey ());
1139+ } catch (ArangoDBException e ) {
1140+ logger .error ("Failed to delete vertex: {}" , e .getErrorMessage ());
1141+ throw ArangoDBExceptions .getArangoDBException (e );
1142+ }
1143+ }
1144+
1145+ public void updateEdge (ArangoDBEdgeDocument edge ) {
1146+ logger .debug ("Update edge {} in {}" , edge , graph .name ());
1147+ EdgeUpdateEntity updateEntity ;
1148+ try {
1149+ updateEntity = db .graph (graph .name ())
1150+ .edgeCollection (graph .getPrefixedCollectioName (edge .getLabel ()))
1151+ .replaceEdge (edge .getKey (), edge );
1152+ } catch (ArangoDBException e ) {
1153+ logger .error ("Failed to update vertex: {}" , e .getErrorMessage ());
1154+ throw ArangoDBExceptions .getArangoDBException (e );
1155+ }
1156+ logger .info ("Edge updated, new rev {}" , updateEntity .getRev ());
1157+ edge .setKey (updateEntity .getKey ());
1158+ edge .setRev (updateEntity .getRev ());
1159+ }
11851160}
0 commit comments