/** * 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); }
@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; }
@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; }
@Override public DocumentEntity insert(final String collectionName, final Object value, final DocumentCreateOptions options) throws DataAccessException { potentiallyEmitEvent(new BeforeSaveEvent<>(value)); final DocumentEntity result; try { result = _collection(collectionName).insertDocument(toVPack(value), options); } catch (final ArangoDBException e) { throw exceptionTranslator.translateExceptionIfPossible(e); } updateDBFields(value, result); potentiallyEmitEvent(new AfterSaveEvent<>(value)); return result; }
@Override public DocumentEntity insert(final String collectionName, final Object value, final DocumentCreateOptions options) throws DataAccessException { potentiallyEmitEvent(new BeforeSaveEvent<>(value)); final DocumentEntity result; try { result = _collection(collectionName).insertDocument(toVPack(value), options); } catch (final ArangoDBException e) { throw exceptionTranslator.translateExceptionIfPossible(e); } updateDBFields(value, result); potentiallyEmitEvent(new AfterSaveEvent<>(value)); return result; }
@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; }