Skip to content

Commit

Permalink
feat: update query using new arangodb api
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Jul 19, 2023
1 parent 7ed1960 commit 368e337
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


import com.arangodb.ArangoDB;
import com.arangodb.DbName;

import com.arangodb.entity.BaseDocument;
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
Expand Down Expand Up @@ -73,10 +73,10 @@ public <K, V> void put(K key, V value) throws NullPointerException {
BaseDocument baseDocument = new BaseDocument();
baseDocument.setKey(key.toString());
baseDocument.addAttribute(VALUE, JSONB.toJson(value));
if (arangoDB.db(DbName.of(bucketName)).collection(namespace).documentExists(key.toString())) {
arangoDB.db(DbName.of(bucketName)).collection(namespace).deleteDocument(key.toString());
if (arangoDB.db(bucketName).collection(namespace).documentExists(key.toString())) {
arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString());
}
arangoDB.db(DbName.of(bucketName)).collection(namespace)
arangoDB.db(bucketName).collection(namespace)
.insertDocument(baseDocument);
}

Expand All @@ -95,7 +95,7 @@ public void put(Iterable<KeyValueEntity> keyValueEntities) throws NullPointerEx
@Override
public <K> Optional<Value> get(K key) throws NullPointerException {
Objects.requireNonNull(key, "Key is required");
BaseDocument entity = arangoDB.db(DbName.of(bucketName)).collection(namespace)
BaseDocument entity = arangoDB.db(bucketName).collection(namespace)
.getDocument(key.toString(), BaseDocument.class);

return ofNullable(entity)
Expand All @@ -108,7 +108,7 @@ public <K> Optional<Value> get(K key) throws NullPointerException {
public <K> Iterable<Value> get(Iterable<K> keys) throws NullPointerException {
return stream(keys.spliterator(), false)
.map(Object::toString)
.map(k -> arangoDB.db(DbName.of(bucketName)).collection(namespace)
.map(k -> arangoDB.db(bucketName).collection(namespace)
.getDocument(k, BaseDocument.class))
.filter(Objects::nonNull)
.map(TO_JSON)
Expand All @@ -118,14 +118,14 @@ public <K> Iterable<Value> get(Iterable<K> keys) throws NullPointerException {

@Override
public <K> void delete(K key) throws NullPointerException {
arangoDB.db(DbName.of(bucketName)).collection(namespace).deleteDocument(key.toString());
arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString());
}

@Override
public <K> void delete(Iterable<K> keys) throws NullPointerException {
Objects.requireNonNull(keys, "Keys is required");

arangoDB.db(DbName.of(bucketName)).collection(namespace)
arangoDB.db(bucketName).collection(namespace)
.deleteDocuments(stream(keys.spliterator(), false)
.map(Object::toString).collect(toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@


import com.arangodb.ArangoDB;
import com.arangodb.DbName;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import org.eclipse.jnosql.communication.Value;
Expand Down Expand Up @@ -68,7 +67,7 @@ static void checkDatabase(String database, ArangoDB arangoDB) {
try {
Collection<String> databases = arangoDB.getDatabases();
if (!databases.contains(database)) {
arangoDB.createDatabase(DbName.of(database));
arangoDB.createDatabase(database);
}
} catch (ArangoDBException e) {
LOGGER.log(Level.WARNING, "Failed to create database: " + database, e);
Expand All @@ -77,12 +76,12 @@ static void checkDatabase(String database, ArangoDB arangoDB) {

public static void checkCollection(String bucketName, ArangoDB arangoDB, String namespace) {
checkDatabase(bucketName, arangoDB);
List<String> collections = arangoDB.db(DbName.of(bucketName))
List<String> collections = arangoDB.db(bucketName)
.getCollections().stream()
.map(CollectionEntity::getName)
.collect(toList());
if (!collections.contains(namespace)) {
arangoDB.db(DbName.of(bucketName)).createCollection(namespace);
arangoDB.db(bucketName).createCollection(namespace);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDB;
import com.arangodb.DbName;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.entity.DocumentUpdateEntity;
Expand Down Expand Up @@ -63,7 +62,7 @@ public DocumentEntity insert(DocumentEntity entity) throws NullPointerException
String collectionName = entity.name();
checkCollection(collectionName);
BaseDocument baseDocument = ArangoDBUtil.getBaseDocument(entity);
DocumentCreateEntity<BaseDocument> arandoDocument = arangoDB.db(DbName.of(database))
DocumentCreateEntity<Void> arandoDocument = arangoDB.db(database)
.collection(collectionName).insertDocument(baseDocument);
updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev());
return entity;
Expand All @@ -78,7 +77,7 @@ public DocumentEntity update(DocumentEntity entity) {
" the _id column"));
feedKey(entity, id);
BaseDocument baseDocument = ArangoDBUtil.getBaseDocument(entity);
DocumentUpdateEntity<BaseDocument> arandoDocument = arangoDB.db(DbName.of(database))
DocumentUpdateEntity<Void> arandoDocument = arangoDB.db(database)
.collection(collectionName).updateDocument(baseDocument.getKey(), baseDocument);
updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev());
return entity;
Expand All @@ -101,17 +100,18 @@ public void delete(DocumentDeleteQuery query) {
}

AQLQueryResult delete = QueryAQLConverter.delete(query);
arangoDB.db(DbName.of(database)).query(delete.getQuery(), delete.getValues(),
null, BaseDocument.class);
arangoDB.db(database).query(delete.getQuery(), BaseDocument.class, delete.getValues(),
null);
}

@Override
public Stream<DocumentEntity> select(DocumentQuery query) throws NullPointerException {
requireNonNull(query, "query is required");

AQLQueryResult result = QueryAQLConverter.select(query);
ArangoCursor<BaseDocument> documents = arangoDB.db(DbName.of(database)).query(result.getQuery(),
result.getValues(), null, BaseDocument.class);
ArangoCursor<BaseDocument> documents = arangoDB.db(database).query(result.getQuery(),
BaseDocument.class,
result.getValues(), null);

return StreamSupport.stream(documents.spliterator(), false)
.map(ArangoDBUtil::toEntity);
Expand All @@ -121,7 +121,7 @@ public Stream<DocumentEntity> select(DocumentQuery query) throws NullPointerExce
public long count(String documentCollection) {
Objects.requireNonNull(documentCollection, "document collection is required");
String aql = "RETURN LENGTH(" + documentCollection + ")";
ArangoCursor<Object> query = arangoDB.db(DbName.of(database)).query(aql, emptyMap(), null, Object.class);
ArangoCursor<Object> query = arangoDB.db(database).query(aql, Object.class, emptyMap(), null);
return StreamSupport.stream(query.spliterator(), false).findFirst().map(Long.class::cast).orElse(0L);
}

Expand All @@ -130,7 +130,7 @@ public long count(String documentCollection) {
public Stream<DocumentEntity> aql(String query, Map<String, Object> values) throws NullPointerException {
requireNonNull(query, "query is required");
requireNonNull(values, "values is required");
ArangoCursor<BaseDocument> result = arangoDB.db(DbName.of(database)).query(query, values, null, BaseDocument.class);
ArangoCursor<BaseDocument> result = arangoDB.db(database).query(query,BaseDocument.class, values, null);
return StreamSupport.stream(result.spliterator(), false)
.map(ArangoDBUtil::toEntity);

Expand All @@ -141,15 +141,15 @@ public <T> Stream<T> aql(String query, Map<String, Object> values, Class<T> type
requireNonNull(query, "query is required");
requireNonNull(values, "values is required");
requireNonNull(typeClass, "typeClass is required");
ArangoCursor<T> result = arangoDB.db(DbName.of(database)).query(query, values, null, typeClass);
ArangoCursor<T> result = arangoDB.db(database).query(query, typeClass, values, null);
return StreamSupport.stream(result.spliterator(), false);
}

@Override
public <T> Stream<T> aql(String query, Class<T> typeClass) {
requireNonNull(query, "query is required");
requireNonNull(typeClass, "typeClass is required");
ArangoCursor<T> result = arangoDB.db(DbName.of(database)).query(query, emptyMap(), null, typeClass);
ArangoCursor<T> result = arangoDB.db(database).query(query,typeClass, emptyMap(), null);
return StreamSupport.stream(result.spliterator(), false);
}

Expand Down

0 comments on commit 368e337

Please sign in to comment.