/** * 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 <T> MultiDocumentEntity<? extends DocumentEntity> insert( final Iterable<T> values, final Class<T> entityClass) throws DataAccessException { return insert(values, entityClass, new DocumentCreateOptions()); }
protected <T> Request insertDocumentRequest(final T value, final DocumentCreateOptions options) { final Request request = request(db.name(), RequestType.POST, PATH_API_DOCUMENT, name); final DocumentCreateOptions params = (options != null ? options : new DocumentCreateOptions()); request.putQueryParam(ArangoRequestParam.WAIT_FOR_SYNC, params.getWaitForSync()); request.putQueryParam(RETURN_NEW, params.getReturnNew()); request.putQueryParam(RETURN_OLD, params.getReturnOld()); request.putQueryParam(SILENT, params.getSilent()); request.putQueryParam(OVERWRITE, params.getOverwrite()); request.setBody(util(Serializer.CUSTOM).serialize(value)); return request; }
@Override public <T> void repsert(final T value) throws DataAccessException { insert(value, new DocumentCreateOptions().overwrite(true)); }
throws VPackException { Class<T> type = null; if (Boolean.TRUE == params.getReturnNew()) { if (!values.isEmpty()) { type = (Class<T>) values.iterator().next().getClass();
@SuppressWarnings("unchecked") @Override public DocumentCreateEntity<T> deserialize(final Response response) throws VPackException { final VPackSlice body = response.getBody(); final DocumentCreateEntity<T> doc = util().deserialize(body, DocumentCreateEntity.class); final VPackSlice newDoc = body.get(NEW); if (newDoc.isObject()) { doc.setNew((T) util(Serializer.CUSTOM).deserialize(newDoc, value.getClass())); } final VPackSlice oldDoc = body.get(OLD); if (oldDoc.isObject()) { doc.setOld((T) util(Serializer.CUSTOM).deserialize(oldDoc, value.getClass())); } if (options == null || Boolean.TRUE != options.getSilent()) { final Map<DocumentField.Type, String> values = new HashMap<DocumentField.Type, String>(); values.put(DocumentField.Type.ID, doc.getId()); values.put(DocumentField.Type.KEY, doc.getKey()); values.put(DocumentField.Type.REV, doc.getRev()); executor.documentCache().setValues(value, values); } return doc; } };
@Override public <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertDocuments(final Collection<T> values) throws ArangoDBException { return insertDocuments(values, new DocumentCreateOptions()); }
protected <T> Request insertDocumentsRequest(final Collection<T> values, final DocumentCreateOptions params) { final Request request = request(db.name(), RequestType.POST, PATH_API_DOCUMENT, name); request.putQueryParam(ArangoRequestParam.WAIT_FOR_SYNC, params.getWaitForSync()); request.putQueryParam(RETURN_NEW, params.getReturnNew()); request.putQueryParam(RETURN_OLD, params.getReturnOld()); request.putQueryParam(SILENT, params.getSilent()); request.putQueryParam(OVERWRITE, params.getOverwrite()); request.setBody(util(Serializer.CUSTOM).serialize(values, new ArangoSerializer.Options().serializeNullValues(false).stringAsJson(true))); return request; }
@Override public <T> void repsert(final Iterable<T> value, final Class<T> entityClass) throws DataAccessException { insert(value, entityClass, new DocumentCreateOptions().overwrite(true)); }
@Override public <T> MultiDocumentEntity<? extends DocumentEntity> insert( final Iterable<T> values, final Class<T> entityClass) throws DataAccessException { return insert(values, entityClass, new DocumentCreateOptions()); }
@Override public <T> void repsert(final T value) throws DataAccessException { insert(value, new DocumentCreateOptions().overwrite(true)); }
@Override public DocumentEntity insert(final Object value) throws DataAccessException { return insert(value, new DocumentCreateOptions()); }
@Override public <T> void repsert(final Iterable<T> value, final Class<T> entityClass) throws DataAccessException { insert(value, entityClass, new DocumentCreateOptions().overwrite(true)); }
@Override public DocumentEntity insert(final String collectionName, final Object value) throws DataAccessException { return insert(collectionName, value, new DocumentCreateOptions()); }
@Override public <T> DocumentCreateEntity<T> insertDocument(final T value) throws ArangoDBException { return insertDocument(value, new DocumentCreateOptions()); }
@Override public DocumentEntity insert(final String collectionName, final Object value) throws DataAccessException { return insert(collectionName, value, new DocumentCreateOptions()); }
@Override public DocumentEntity insert(final Object value) throws DataAccessException { return insert(value, new DocumentCreateOptions()); }
@Override public <T> CompletableFuture<MultiDocumentEntity<DocumentCreateEntity<T>>> insertDocuments( final Collection<T> values, final DocumentCreateOptions options) { final DocumentCreateOptions params = (options != null ? options : new DocumentCreateOptions()); return executor.execute(insertDocumentsRequest(values, params), insertDocumentsResponseDeserializer(values, params)); }
@Override public <T> CompletableFuture<MultiDocumentEntity<DocumentCreateEntity<T>>> insertDocuments( final Collection<T> values) { final DocumentCreateOptions params = new DocumentCreateOptions(); return executor.execute(insertDocumentsRequest(values, params), insertDocumentsResponseDeserializer(values, params)); }
@Override public <T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(final T value) { final DocumentCreateOptions options = new DocumentCreateOptions(); return executor.execute(insertDocumentRequest(value, options), insertDocumentResponseDeserializer(value, options)); }