/** * 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 {
@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 <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 <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); }
@Override public <K> void remove(K key) throws NullPointerException { arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString()); }
@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); }
/** * Delete collection. * * @param name the name * @return true, if successful */ public boolean deleteCollection(String name) { ArangoCollection collection = db.collection(name); if (collection.exists()) { collection.drop(); return collection.exists(); } return false; }
for (String definitionName : edgeDefinitions) { String collectioName = definitionName; if (db.collection(collectioName).exists()) { db.collection(collectioName).drop(); if (db.collection(collectioName).exists()) { db.collection(collectioName).drop();
private ArangoCollection _collection( final String name, final ArangoPersistentEntity<?> persistentEntity, final CollectionCreateOptions options) { final ArangoDatabase db = db(); final Class<?> entityClass = persistentEntity != null ? persistentEntity.getType() : null; final CollectionCacheValue value = collectionCache.computeIfAbsent(new CollectionCacheKey(db.name(), name), key -> { final ArangoCollection collection = db.collection(name); if (!collection.exists()) { collection.create(options); } return new CollectionCacheValue(collection); }); final Collection<Class<?>> entities = value.getEntities(); final ArangoCollection collection = value.getCollection(); if (persistentEntity != null && !entities.contains(entityClass)) { value.addEntityClass(entityClass); ensureCollectionIndexes(collection(collection), persistentEntity); } return collection; }
@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; }
private ArangoCollection _collection( final String name, final ArangoPersistentEntity<?> persistentEntity, final CollectionCreateOptions options) { final ArangoDatabase db = db(); final Class<?> entityClass = persistentEntity != null ? persistentEntity.getType() : null; final CollectionCacheValue value = collectionCache.computeIfAbsent(new CollectionCacheKey(db.name(), name), key -> { final ArangoCollection collection = db.collection(name); if (!collection.exists()) { collection.create(options); } return new CollectionCacheValue(collection); }); final Collection<Class<?>> entities = value.getEntities(); final ArangoCollection collection = value.getCollection(); if (persistentEntity != null && !entities.contains(entityClass)) { value.addEntityClass(entityClass); ensureCollectionIndexes(collection(collection), persistentEntity); } return collection; }
@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; }