1616package org .eclipse .jnosql .databases .arangodb .communication ;
1717
1818
19+ import com .arangodb .ArangoCollection ;
1920import com .arangodb .ArangoDB ;
20- import com .arangodb .entity .CollectionEntity ;
21+ import com .arangodb .entity .CollectionType ;
22+ import com .arangodb .model .CollectionCreateOptions ;
2123import jakarta .json .Json ;
2224import jakarta .json .JsonArray ;
2325import jakarta .json .JsonArrayBuilder ;
3032import org .eclipse .jnosql .communication .semistructured .CommunicationEntity ;
3133import org .eclipse .jnosql .communication .semistructured .Element ;
3234
33- import java .util .Collection ;
34- import java .util .Collections ;
35- import java .util .List ;
36- import java .util .Map ;
37- import java .util .Objects ;
35+ import java .util .*;
3836import java .util .logging .Level ;
3937import java .util .logging .Logger ;
38+ import java .util .stream .Collectors ;
4039
4140import static java .util .stream .Collectors .toList ;
4241import static java .util .stream .StreamSupport .stream ;
@@ -49,12 +48,17 @@ public final class ArangoDBUtil {
4948 public static final String KEY = "_key" ;
5049 public static final String ID = "_id" ;
5150 public static final String REV = "_rev" ;
51+ public static final String FROM = "_from" ;
52+ public static final String TO = "_to" ;
53+ private static final Set <String > META_FIELDS = Collections .unmodifiableSet (new HashSet <>(Arrays .asList (
54+ ID , KEY , REV , FROM , TO
55+ )));
56+
5257 private static final Logger LOGGER = Logger .getLogger (ArangoDBUtil .class .getName ());
5358
5459 private ArangoDBUtil () {
5560 }
5661
57-
5862 static void checkDatabase (String database , ArangoDB arangoDB ) {
5963 Objects .requireNonNull (database , "database is required" );
6064 try {
@@ -67,14 +71,21 @@ static void checkDatabase(String database, ArangoDB arangoDB) {
6771 }
6872 }
6973
70- public static void checkCollection (String bucketName , ArangoDB arangoDB , String namespace ) {
71- checkDatabase (bucketName , arangoDB );
72- List <String > collections = arangoDB .db (bucketName )
73- .getCollections ().stream ()
74- .map (CollectionEntity ::getName )
75- .toList ();
76- if (!collections .contains (namespace )) {
77- arangoDB .db (bucketName ).createCollection (namespace );
74+ public static void checkCollection (String dbName , ArangoDB arangoDB , String collectionName ) {
75+ checkDatabase (dbName , arangoDB );
76+ ArangoCollection collection = arangoDB .db (dbName ).collection (collectionName );
77+ if (!collection .exists ()) {
78+ collection .create ();
79+ }
80+ }
81+
82+ public static void checkEdgeCollection (String dbName , ArangoDB arangoDB , String collectionName ) {
83+ checkDatabase (dbName , arangoDB );
84+ ArangoCollection collection = arangoDB .db (dbName ).collection (collectionName );
85+ if (!collection .exists ()) {
86+ collection .create (new CollectionCreateOptions ().type (CollectionType .EDGES ));
87+ } else if (collection .getInfo ().getType () != CollectionType .EDGES ) {
88+ throw new IllegalStateException (String .format ("The collection %s is not an edge collection" , collectionName ));
7889 }
7990 }
8091
@@ -85,10 +96,27 @@ static CommunicationEntity toEntity(JsonObject jsonObject) {
8596 documents .add (Element .of (KEY , jsonObject .getString (KEY )));
8697 documents .add (Element .of (ID , id ));
8798 documents .add (Element .of (REV , jsonObject .getString (REV )));
99+ if (jsonObject .containsKey (FROM )) {
100+ documents .add (Element .of (FROM , jsonObject .getString (FROM )));
101+ }
102+ if (jsonObject .containsKey (TO )) {
103+ documents .add (Element .of (TO , jsonObject .getString (TO )));
104+ }
88105 String collection = id .split ("/" )[0 ];
89106 return CommunicationEntity .of (collection , documents );
90107 }
91108
109+ static ArangoDBCommunicationEdge toEdge (JsonObject edge , JsonObject from , JsonObject to ) {
110+ String id = edge .getString (ID );
111+ String label = id .split ("/" )[0 ];
112+ Map <String , Object > properties = edge .entrySet ().stream ()
113+ .filter (e -> !META_FIELDS .contains (e .getKey ()))
114+ .map (e -> new AbstractMap .SimpleEntry <>(e .getKey (), toDocuments (e .getValue ())))
115+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
116+
117+ return new ArangoDBCommunicationEdge (id , toEntity (edge ), toEntity (from ), label , properties );
118+ }
119+
92120 static JsonObject toJsonObject (CommunicationEntity entity ) {
93121 return toJsonObject (entity .elements ());
94122 }
0 commit comments