/** * 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; }
/** * 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; }
/** * 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; }
@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); }
updateDoc.addAttribute(field.getKey(), byteIteratorToString(field.getValue())); arangoDB.db(databaseName).collection(table).updateDocument(key, updateDoc); return Status.OK; } else {
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 <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 void dropIndex(final String id) throws DataAccessException { try { collection.deleteIndex(id); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } }
@Override public boolean exists(final Object id, final Class<?> entityClass) throws DataAccessException { try { return _collection(entityClass).documentExists(determineDocumentKeyFromId(id)); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } }
@Override public DocumentEntity update(final Object id, final Object value, final DocumentUpdateOptions options) throws DataAccessException { potentiallyEmitEvent(new BeforeSaveEvent<>(value)); final DocumentEntity result; try { result = _collection(value.getClass(), id).updateDocument(determineDocumentKeyFromId(id), toVPack(value), options); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } updateDBFields(value, result); potentiallyEmitEvent(new AfterSaveEvent<>(value)); return result; }
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 MultiDocumentEntity<? extends DocumentEntity> delete( final Iterable<Object> values, final Class<?> entityClass, final DocumentDeleteOptions options) throws DataAccessException { potentiallyEmitBeforeDeleteEvent(values, entityClass); MultiDocumentEntity<? extends DocumentEntity> result; try { result = _collection(entityClass).deleteDocuments(toVPackCollection(values), entityClass, options); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } potentiallyEmitAfterDeleteEvent(values, entityClass, result); return result; }
@Override public void dropIndex(final String id) throws DataAccessException { try { collection.deleteIndex(id); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } }
@Override public boolean exists(final Object id, final Class<?> entityClass) throws DataAccessException { try { return _collection(entityClass).documentExists(determineDocumentKeyFromId(id)); } catch (final ArangoDBException e) { throw translateExceptionIfPossible(e); } }
@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 DocumentEntity insert(final Object value, final DocumentCreateOptions options) throws DataAccessException { potentiallyEmitEvent(new BeforeSaveEvent<>(value)); final DocumentEntity result; try { result = _collection(value.getClass()).insertDocument(toVPack(value), options); } catch (final ArangoDBException e) { throw exceptionTranslator.translateExceptionIfPossible(e); } updateDBFields(value, result); potentiallyEmitEvent(new AfterSaveEvent<>(value)); return result; }