Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
updateDocument with different return type
  • Loading branch information
rashtao committed May 29, 2020
commit 9d2f78794a83dae31f4c3a3f744c6b243d22ee98
17 changes: 17 additions & 0 deletions src/main/java/com/arangodb/ArangoCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceDocuments(
<T> DocumentUpdateEntity<T> updateDocument(String key, T value, DocumentUpdateOptions options)
throws ArangoDBException;

/**
* Partially updates the document identified by document-key. The value must contain a document with the attributes
* to patch (the patch document). All attributes from the patch document will be added to the existing document if
* they do not yet exist, and overwritten in the existing document if they do exist there.
*
* @param key The key of the document
* @param value A representation of a single document (POJO, VPackSlice or String for JSON)
* @param options Additional options, can be null
* @param returnType Type of the returned newDocument and/or oldDocument
* @return information about the document
* @throws ArangoDBException
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-document">API
* Documentation</a>
*/
<T, U> DocumentUpdateEntity<U> updateDocument(String key, T value, DocumentUpdateOptions options, Class<U> returnType)
throws ArangoDBException;

/**
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/arangodb/async/ArangoCollectionAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(
final T value,
final DocumentUpdateOptions options);

/**
* Partially updates the document identified by document-key. The value must contain a document with the attributes
* to patch (the patch document). All attributes from the patch document will be added to the existing document if
* they do not yet exist, and overwritten in the existing document if they do exist there.
*
* @param key The key of the document
* @param value A representation of a single document (POJO, VPackSlice or String for Json)
* @param options Additional options, can be null
* @param returnType Type of the returned newDocument and/or oldDocument
* @return information about the document
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-document">API
* Documentation</a>
*/
<T, U> CompletableFuture<DocumentUpdateEntity<U>> updateDocument(
final String key,
final T value,
final DocumentUpdateOptions options,
final Class<U> returnType);

/**
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,25 @@ public <T> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<T>>> repla

@Override
public <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(final String key, final T value) {
final DocumentUpdateOptions options = new DocumentUpdateOptions();
return executor.execute(updateDocumentRequest(key, value, options),
updateDocumentResponseDeserializer(value, options));
return updateDocument(key, value, new DocumentUpdateOptions());
}

@Override
public <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(
final String key,
final T value,
final DocumentUpdateOptions options) {
return updateDocument(key, value, options, (Class<T>) value.getClass());
}

@Override
public <T, U> CompletableFuture<DocumentUpdateEntity<U>> updateDocument(
final String key,
final T value,
final DocumentUpdateOptions options,
final Class<U> returnType) {
return executor.execute(updateDocumentRequest(key, value, options),
updateDocumentResponseDeserializer(value, options));
updateDocumentResponseDeserializer(value, options, returnType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ public <T> DocumentUpdateEntity<T> updateDocument(final String key, final T valu
@Override
public <T> DocumentUpdateEntity<T> updateDocument(
final String key, final T value, final DocumentUpdateOptions options) throws ArangoDBException {
return updateDocument(key, value, options, (Class<T>) value.getClass());
}

@Override
public <T, U> DocumentUpdateEntity<U> updateDocument(
final String key, final T value, final DocumentUpdateOptions options, final Class<U> returnType) throws ArangoDBException {
return executor.execute(updateDocumentRequest(key, value, options),
updateDocumentResponseDeserializer(value, options));
updateDocumentResponseDeserializer(value, options, returnType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,18 @@ protected <T> Request updateDocumentRequest(final String key, final T value, fin
return request;
}

protected <T> ResponseDeserializer<DocumentUpdateEntity<T>> updateDocumentResponseDeserializer(
final T value, final DocumentUpdateOptions options) {
protected <T, U> ResponseDeserializer<DocumentUpdateEntity<U>> updateDocumentResponseDeserializer(
final T value, final DocumentUpdateOptions options, final Class<U> returnType) {
return response -> {
final VPackSlice body = response.getBody();
final DocumentUpdateEntity<T> doc = util().deserialize(body, DocumentUpdateEntity.class);
final DocumentUpdateEntity<U> doc = util().deserialize(body, DocumentUpdateEntity.class);
final VPackSlice newDoc = body.get(NEW);
if (newDoc.isObject()) {
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, value.getClass()));
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, returnType));
}
final VPackSlice oldDoc = body.get(OLD);
if (oldDoc.isObject()) {
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, value.getClass()));
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, returnType));
}
if (options == null || Boolean.TRUE != options.getSilent()) {
final Map<DocumentField.Type, String> values = new HashMap<>();
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/arangodb/ArangoCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,23 @@ public void updateDocument() {
assertThat(readResult.getProperties().keySet(), hasItem("c"));
}

@Test
public void updateDocumentWithDifferentReturnType() {
final String key = "key-" + UUID.randomUUID().toString();
final BaseDocument doc = new BaseDocument(key);
doc.addAttribute("a", "test");
collection.insertDocument(doc);

final DocumentUpdateEntity<BaseDocument> updateResult = collection
.updateDocument(key, Collections.singletonMap("b", "test"), new DocumentUpdateOptions().returnNew(true), BaseDocument.class);
assertThat(updateResult, is(notNullValue()));
assertThat(updateResult.getKey(), is(key));
BaseDocument updated = updateResult.getNew();
assertThat(updated, is(notNullValue()));
assertThat(updated.getAttribute("a"), is("test"));
assertThat(updated.getAttribute("b"), is("test"));
}

@Test
public void updateDocumentUpdateRev() {
final BaseDocument doc = new BaseDocument();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/arangodb/async/ArangoCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,24 @@ public void updateDocument() throws InterruptedException, ExecutionException {
assertThat(readResult.getProperties().keySet(), hasItem("c"));
}

@Test
public void updateDocumentWithDifferentReturnType() throws ExecutionException, InterruptedException {
ArangoCollectionAsync collection = db.collection(COLLECTION_NAME);
final String key = "key-" + UUID.randomUUID().toString();
final BaseDocument doc = new BaseDocument(key);
doc.addAttribute("a", "test");
collection.insertDocument(doc);

final DocumentUpdateEntity<BaseDocument> updateResult = collection
.updateDocument(key, Collections.singletonMap("b", "test"), new DocumentUpdateOptions().returnNew(true), BaseDocument.class).get();
assertThat(updateResult, is(notNullValue()));
assertThat(updateResult.getKey(), is(key));
BaseDocument updated = updateResult.getNew();
assertThat(updated, is(notNullValue()));
assertThat(updated.getAttribute("a"), is("test"));
assertThat(updated.getAttribute("b"), is("test"));
}

@Test
public void updateDocumentIfMatch() throws InterruptedException, ExecutionException {
final BaseDocument doc = new BaseDocument();
Expand Down