/** * Delete a record from the database. * * @param table * The name of the table * @param key * The record key of the record to delete. * @return Zero on success, a non-zero error code on error. See the * {@link DB} class's description for a discussion of error codes. */ @Override public Status delete(String table, String key) { try { arangoDB.db(databaseName).collection(table).deleteDocument(key); return Status.OK; } catch (ArangoDBException e) { logger.error("Exception while trying delete {} {} with ex {}", table, key, e.toString()); } return Status.ERROR; }
/** * Read a record from the database. Each field/value pair from the result * will be stored in a HashMap. * * @param table * The name of the table * @param key * The record key of the record to read. * @param fields * The list of fields to read, or null for all of them * @param result * A HashMap of field/value pairs for the result * @return Zero on success, a non-zero error code on error or "not found". */ @Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { try { VPackSlice document = arangoDB.db(databaseName).collection(table).getDocument(key, VPackSlice.class, null); if (!this.fillMap(result, document, fields)) { return Status.ERROR; } return Status.OK; } catch (ArangoDBException e) { logger.error("Exception while trying read {} {} with ex {}", table, key, e.toString()); } return Status.ERROR; }
/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key. * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See the * {@link DB} class's description for a discussion of error codes. */ @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { BaseDocument toInsert = new BaseDocument(key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { toInsert.addAttribute(entry.getKey(), byteIteratorToString(entry.getValue())); } DocumentCreateOptions options = new DocumentCreateOptions().waitForSync(waitForSync); arangoDB.db(databaseName).collection(table).insertDocument(toInsert, options); return Status.OK; } catch (ArangoDBException e) { logger.error("Exception while trying insert {} {} with ex {}", table, key, e.toString()); } return Status.ERROR; }
updateDoc.addAttribute(field.getKey(), byteIteratorToString(field.getValue())); arangoDB.db(databaseName).collection(table).updateDocument(key, updateDoc); return Status.OK; } else { options.writeCollections(table); options.params(createDocumentHandle(table, key)); arangoDB.db(databaseName).transaction(transactionAction, Void.class, options); return Status.OK;
cursor = arangoDB.db(databaseName).query(aqlQuery, bindVars, null, VPackSlice.class); while (cursor.hasNext()) { VPackSlice aDocument = cursor.next();
@Override public <K> void remove(Iterable<K> keys) throws NullPointerException { Objects.requireNonNull(keys, "Keys is required"); arangoDB.db(bucketName).collection(namespace) .deleteDocuments(stream(keys.spliterator(), false) .map(Object::toString).collect(toList())); }
@Override public List<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(database).query(query, values, null, BaseDocument.class); return StreamSupport.stream(result.spliterator(), false) .map(ArangoDBUtil::toEntity) .collect(toList()); }
public static void checkCollection(String bucketName, ArangoDB arangoDB, String namespace) { checkDatabase(bucketName, arangoDB); List<String> collections = arangoDB.db(bucketName) .getCollections().stream() .map(CollectionEntity::getName) .collect(toList()); if (!collections.contains(namespace)) { arangoDB.db(bucketName).createCollection(namespace); } }
@Override public <K> Iterable<Value> get(Iterable<K> keys) throws NullPointerException { return stream(keys.spliterator(), false) .map(Object::toString) .map(k -> arangoDB.db(bucketName).collection(namespace) .getDocument(k, BaseDocument.class)) .filter(Objects::nonNull) .map(TO_JSON) .map(ValueJSON::of) .collect(toList()); }
@Override public long count(String documentCollection) { Objects.requireNonNull(documentCollection, "document collection is required"); String aql = "RETURN LENGTH(" + documentCollection + ")"; ArangoCursor<Object> query = arangoDB.db(database).query(aql, Collections.emptyMap(), null, Object.class); return StreamSupport.stream(query.spliterator(), false).findFirst().map(Long.class::cast).orElse(0L); }
@Override public <K> Optional<Value> get(K key) throws NullPointerException { Objects.requireNonNull(key, "Key is required"); BaseDocument entity = arangoDB.db(bucketName).collection(namespace) .getDocument(key.toString(), BaseDocument.class); return ofNullable(entity) .map(TO_JSON) .map(ValueJSON::of); }
private ArangoDatabase db() { final String key = databaseExpression != null ? databaseExpression.getValue(context, String.class) : databaseName; return databaseCache.computeIfAbsent(key, name -> { final ArangoDatabase db = arango.db(name); if (!db.exists()) { db.create(); } return db; }); }
private ArangoDatabase db() { final String key = databaseExpression != null ? databaseExpression.getValue(context, String.class) : databaseName; return databaseCache.computeIfAbsent(key, name -> { final ArangoDatabase db = arango.db(name); if (!db.exists()) { db.create(); } return db; }); }
@Override public <K> void remove(K key) throws NullPointerException { arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString()); }
@Override public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException { requireNonNull(query, "query is required"); AQLQueryResult result = QueryAQLConverter.select(query); ArangoCursor<BaseDocument> documents = arangoDB.db(database).query(result.getQuery(), result.getValues(), null, BaseDocument.class); return StreamSupport.stream(documents.spliterator(), false) .map(ArangoDBUtil::toEntity) .collect(toList()); }
@Override public <K, V> void put(K key, V value) throws NullPointerException { Objects.requireNonNull(key, "Key is required"); Objects.requireNonNull(value, "value is required"); BaseDocument baseDocument = new BaseDocument(); baseDocument.setKey(key.toString()); baseDocument.addAttribute(VALUE, JSONB.toJson(value)); if (arangoDB.db(bucketName).collection(namespace).documentExists(key.toString())) { arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString()); } arangoDB.db(bucketName).collection(namespace) .insertDocument(baseDocument); }
@Override public void delete(DocumentDeleteQuery query) { requireNonNull(query, "query is required"); if (checkCondition(query.getCondition())) { return; } AQLQueryResult delete = QueryAQLConverter.delete(query); arangoDB.db(database).query(delete.getQuery(), delete.getValues(), null, BaseDocument.class); }
@Override public DocumentEntity insert(DocumentEntity entity) throws NullPointerException { String collectionName = entity.getName(); checkCollection(collectionName); BaseDocument baseDocument = getBaseDocument(entity); DocumentCreateEntity<BaseDocument> arandoDocument = arangoDB.db(database).collection(collectionName).insertDocument(baseDocument); updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev()); return entity; }
@Override public DocumentEntity update(DocumentEntity entity) { String collectionName = entity.getName(); checkCollection(collectionName); BaseDocument baseDocument = getBaseDocument(entity); DocumentUpdateEntity<BaseDocument> arandoDocument = arangoDB.db(database) .collection(collectionName).updateDocument(baseDocument.getKey(), baseDocument); updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev()); return entity; }