Skip to content

Commit 1285877

Browse files
committed
Merge branch 'master' into preview
2 parents c57080b + 4c12293 commit 1285877

16 files changed

+303
-42
lines changed

src/main/java/com/arangodb/ArangoCollection.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceDocuments(
271271
<T> DocumentUpdateEntity<T> updateDocument(String key, T value, DocumentUpdateOptions options)
272272
throws ArangoDBException;
273273

274+
/**
275+
* Partially updates the document identified by document-key. The value must contain a document with the attributes
276+
* to patch (the patch document). All attributes from the patch document will be added to the existing document if
277+
* they do not yet exist, and overwritten in the existing document if they do exist there.
278+
*
279+
* @param key The key of the document
280+
* @param value A representation of a single document (POJO, VPackSlice or String for JSON)
281+
* @param options Additional options, can be null
282+
* @param returnType Type of the returned newDocument and/or oldDocument
283+
* @return information about the document
284+
* @throws ArangoDBException
285+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-document">API
286+
* Documentation</a>
287+
*/
288+
<T, U> DocumentUpdateEntity<U> updateDocument(String key, T value, DocumentUpdateOptions options, Class<U> returnType)
289+
throws ArangoDBException;
290+
274291
/**
275292
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
276293
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
@@ -301,6 +318,23 @@ <T> DocumentUpdateEntity<T> updateDocument(String key, T value, DocumentUpdateOp
301318
<T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateDocuments(
302319
Collection<T> values, DocumentUpdateOptions options) throws ArangoDBException;
303320

321+
/**
322+
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
323+
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
324+
* attributes from the patch documents will be added to the existing documents if they do not yet exist, and
325+
* overwritten in the existing documents if they do exist there.
326+
*
327+
* @param values A list of documents (POJO, VPackSlice or String for JSON)
328+
* @param options Additional options, can be null
329+
* @param returnType Type of the returned newDocument and/or oldDocument
330+
* @return information about the documents
331+
* @throws ArangoDBException
332+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-documents">API
333+
* Documentation</a>
334+
*/
335+
<T, U> MultiDocumentEntity<DocumentUpdateEntity<U>> updateDocuments(
336+
Collection<T> values, DocumentUpdateOptions options, Class<U> returnType) throws ArangoDBException;
337+
304338
/**
305339
* Deletes the document with the given {@code key} from the collection.
306340
*

src/main/java/com/arangodb/async/ArangoCollectionAsync.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(
265265
final T value,
266266
final DocumentUpdateOptions options);
267267

268+
/**
269+
* Partially updates the document identified by document-key. The value must contain a document with the attributes
270+
* to patch (the patch document). All attributes from the patch document will be added to the existing document if
271+
* they do not yet exist, and overwritten in the existing document if they do exist there.
272+
*
273+
* @param key The key of the document
274+
* @param value A representation of a single document (POJO, VPackSlice or String for Json)
275+
* @param options Additional options, can be null
276+
* @param returnType Type of the returned newDocument and/or oldDocument
277+
* @return information about the document
278+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-document">API
279+
* Documentation</a>
280+
*/
281+
<T, U> CompletableFuture<DocumentUpdateEntity<U>> updateDocument(
282+
final String key,
283+
final T value,
284+
final DocumentUpdateOptions options,
285+
final Class<U> returnType);
286+
268287
/**
269288
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
270289
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
@@ -294,6 +313,24 @@ <T> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocume
294313
final Collection<T> values,
295314
final DocumentUpdateOptions options);
296315

316+
/**
317+
* Partially updates documents, the documents to update are specified by the _key attributes in the objects on
318+
* values. Vales must contain a list of document updates with the attributes to patch (the patch documents). All
319+
* attributes from the patch documents will be added to the existing documents if they do not yet exist, and
320+
* overwritten in the existing documents if they do exist there.
321+
*
322+
* @param values A list of documents (POJO, VPackSlice or String for Json)
323+
* @param options Additional options, can be null
324+
* @param returnType Type of the returned newDocument and/or oldDocument
325+
* @return information about the documents
326+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#update-documents">API
327+
* Documentation</a>
328+
*/
329+
<T, U> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<U>>> updateDocuments(
330+
final Collection<T> values,
331+
final DocumentUpdateOptions options,
332+
final Class<U> returnType);
333+
297334
/**
298335
* Removes a document
299336
*

src/main/java/com/arangodb/async/internal/ArangoCollectionAsyncImpl.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,35 +167,48 @@ public <T> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<T>>> repla
167167

168168
@Override
169169
public <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(final String key, final T value) {
170-
final DocumentUpdateOptions options = new DocumentUpdateOptions();
171-
return executor.execute(updateDocumentRequest(key, value, options),
172-
updateDocumentResponseDeserializer(value, options));
170+
return updateDocument(key, value, new DocumentUpdateOptions());
173171
}
174172

175173
@Override
176174
public <T> CompletableFuture<DocumentUpdateEntity<T>> updateDocument(
177175
final String key,
178176
final T value,
179177
final DocumentUpdateOptions options) {
178+
return updateDocument(key, value, options, (Class<T>) value.getClass());
179+
}
180+
181+
@Override
182+
public <T, U> CompletableFuture<DocumentUpdateEntity<U>> updateDocument(
183+
final String key,
184+
final T value,
185+
final DocumentUpdateOptions options,
186+
final Class<U> returnType) {
180187
return executor.execute(updateDocumentRequest(key, value, options),
181-
updateDocumentResponseDeserializer(value, options));
188+
updateDocumentResponseDeserializer(value, options, returnType));
182189
}
183190

184191
@Override
185192
public <T> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocuments(
186193
final Collection<T> values) {
187-
final DocumentUpdateOptions params = new DocumentUpdateOptions();
188-
return executor.execute(updateDocumentsRequest(values, params),
189-
updateDocumentsResponseDeserializer(values, params));
194+
return updateDocuments(values, new DocumentUpdateOptions());
190195
}
191196

192197
@Override
193198
public <T> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocuments(
194199
final Collection<T> values,
195200
final DocumentUpdateOptions options) {
201+
return updateDocuments(values, options, values.isEmpty() ? null : (Class<T>) values.iterator().next().getClass());
202+
}
203+
204+
@Override
205+
public <T, U> CompletableFuture<MultiDocumentEntity<DocumentUpdateEntity<U>>> updateDocuments(
206+
final Collection<T> values,
207+
final DocumentUpdateOptions options,
208+
final Class<U> returnType) {
196209
final DocumentUpdateOptions params = (options != null ? options : new DocumentUpdateOptions());
197210
return executor.execute(updateDocumentsRequest(values, params),
198-
updateDocumentsResponseDeserializer(values, params));
211+
updateDocumentsResponseDeserializer(returnType));
199212
}
200213

201214
@Override

src/main/java/com/arangodb/internal/ArangoCollectionImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,14 @@ public <T> DocumentUpdateEntity<T> updateDocument(final String key, final T valu
168168
@Override
169169
public <T> DocumentUpdateEntity<T> updateDocument(
170170
final String key, final T value, final DocumentUpdateOptions options) throws ArangoDBException {
171+
return updateDocument(key, value, options, (Class<T>) value.getClass());
172+
}
173+
174+
@Override
175+
public <T, U> DocumentUpdateEntity<U> updateDocument(
176+
final String key, final T value, final DocumentUpdateOptions options, final Class<U> returnType) throws ArangoDBException {
171177
return executor.execute(updateDocumentRequest(key, value, options),
172-
updateDocumentResponseDeserializer(value, options));
178+
updateDocumentResponseDeserializer(value, options, returnType));
173179
}
174180

175181
@Override
@@ -181,9 +187,15 @@ public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateDocuments(final Co
181187
@Override
182188
public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateDocuments(
183189
final Collection<T> values, final DocumentUpdateOptions options) throws ArangoDBException {
190+
return updateDocuments(values, options, values.isEmpty() ? null : (Class<T>) values.iterator().next().getClass());
191+
}
192+
193+
@Override
194+
public <T, U> MultiDocumentEntity<DocumentUpdateEntity<U>> updateDocuments(
195+
final Collection<T> values, final DocumentUpdateOptions options, final Class<U> returnType) throws ArangoDBException {
184196
final DocumentUpdateOptions params = (options != null ? options : new DocumentUpdateOptions());
185197
return executor
186-
.execute(updateDocumentsRequest(values, params), updateDocumentsResponseDeserializer(values, params));
198+
.execute(updateDocumentsRequest(values, params), updateDocumentsResponseDeserializer(returnType));
187199
}
188200

189201
@Override

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,18 @@ protected <T> Request updateDocumentRequest(final String key, final T value, fin
365365
return request;
366366
}
367367

368-
protected <T> ResponseDeserializer<DocumentUpdateEntity<T>> updateDocumentResponseDeserializer(
369-
final T value, final DocumentUpdateOptions options) {
368+
protected <T, U> ResponseDeserializer<DocumentUpdateEntity<U>> updateDocumentResponseDeserializer(
369+
final T value, final DocumentUpdateOptions options, final Class<U> returnType) {
370370
return response -> {
371371
final VPackSlice body = response.getBody();
372-
final DocumentUpdateEntity<T> doc = util().deserialize(body, DocumentUpdateEntity.class);
372+
final DocumentUpdateEntity<U> doc = util().deserialize(body, DocumentUpdateEntity.class);
373373
final VPackSlice newDoc = body.get(NEW);
374374
if (newDoc.isObject()) {
375-
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, value.getClass()));
375+
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, returnType));
376376
}
377377
final VPackSlice oldDoc = body.get(OLD);
378378
if (oldDoc.isObject()) {
379-
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, value.getClass()));
379+
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, returnType));
380380
}
381381
if (options == null || Boolean.TRUE != options.getSilent()) {
382382
final Map<DocumentField.Type, String> values = new HashMap<>();
@@ -407,14 +407,8 @@ protected <T> Request updateDocumentsRequest(final Collection<T> values, final D
407407

408408
@SuppressWarnings("unchecked")
409409
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocumentsResponseDeserializer(
410-
final Collection<T> values, final DocumentUpdateOptions params) {
410+
final Class<T> returnType) {
411411
return response -> {
412-
Class<T> type = null;
413-
if (Boolean.TRUE == params.getReturnNew() || Boolean.TRUE == params.getReturnOld()) {
414-
if (!values.isEmpty()) {
415-
type = (Class<T>) values.iterator().next().getClass();
416-
}
417-
}
418412
final MultiDocumentEntity<DocumentUpdateEntity<T>> multiDocument = new MultiDocumentEntity<>();
419413
final Collection<DocumentUpdateEntity<T>> docs = new ArrayList<>();
420414
final Collection<ErrorEntity> errors = new ArrayList<>();
@@ -431,11 +425,11 @@ protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>>
431425
final DocumentUpdateEntity<T> doc = util().deserialize(next, DocumentUpdateEntity.class);
432426
final VPackSlice newDoc = next.get(NEW);
433427
if (newDoc.isObject()) {
434-
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, type));
428+
doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, returnType));
435429
}
436430
final VPackSlice oldDoc = next.get(OLD);
437431
if (oldDoc.isObject()) {
438-
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, type));
432+
doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, returnType));
439433
}
440434
docs.add(doc);
441435
documentsAndErrors.add(doc);

src/main/java/com/arangodb/internal/velocystream/internal/VstConnection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
import java.nio.ByteOrder;
4343
import java.util.Collection;
4444
import java.util.Date;
45-
import java.util.HashMap;
45+
import java.util.Map;
4646
import java.util.concurrent.Callable;
47+
import java.util.concurrent.ConcurrentHashMap;
4748
import java.util.concurrent.ExecutorService;
4849
import java.util.concurrent.Executors;
4950

@@ -68,7 +69,7 @@ public abstract class VstConnection implements Connection {
6869

6970
private final HostDescription host;
7071

71-
private final HashMap<Long, Long> sendTimestamps = new HashMap<>();
72+
private final Map<Long, Long> sendTimestamps = new ConcurrentHashMap<>();
7273

7374
private final String connectionName;
7475

@@ -252,9 +253,8 @@ protected Chunk readChunk() throws IOException {
252253
final Chunk chunk = new Chunk(messageId, chunkX, messageLength, 0, contentLength);
253254

254255
if (LOGGER.isDebugEnabled()) {
255-
256256
LOGGER.debug(String.format("Received chunk %s:%s from message %s", chunk.getChunk(), chunk.isFirstChunk() ? 1 : 0, chunk.getMessageId()));
257-
LOGGER.debug("Responsetime for Message " + chunk.getMessageId() + " is " + (sendTimestamps.get(chunk.getMessageId()) - System.currentTimeMillis()));
257+
LOGGER.debug("Responsetime for Message " + chunk.getMessageId() + " is " + (System.currentTimeMillis() - sendTimestamps.get(chunk.getMessageId())));
258258
}
259259

260260
return chunk;

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,23 @@ public void updateDocument() {
532532
assertThat(readResult.getProperties().keySet(), hasItem("c"));
533533
}
534534

535+
@Test
536+
public void updateDocumentWithDifferentReturnType() {
537+
final String key = "key-" + UUID.randomUUID().toString();
538+
final BaseDocument doc = new BaseDocument(key);
539+
doc.addAttribute("a", "test");
540+
collection.insertDocument(doc);
541+
542+
final DocumentUpdateEntity<BaseDocument> updateResult = collection
543+
.updateDocument(key, Collections.singletonMap("b", "test"), new DocumentUpdateOptions().returnNew(true), BaseDocument.class);
544+
assertThat(updateResult, is(notNullValue()));
545+
assertThat(updateResult.getKey(), is(key));
546+
BaseDocument updated = updateResult.getNew();
547+
assertThat(updated, is(notNullValue()));
548+
assertThat(updated.getAttribute("a"), is("test"));
549+
assertThat(updated.getAttribute("b"), is("test"));
550+
}
551+
535552
@Test
536553
public void updateDocumentUpdateRev() {
537554
final BaseDocument doc = new BaseDocument();
@@ -2320,6 +2337,36 @@ public void updateDocuments() {
23202337
assertThat(updateResult.getErrors().size(), is(0));
23212338
}
23222339

2340+
@Test
2341+
public void updateDocumentsWithDifferentReturnType() {
2342+
List<String> keys = IntStream.range(0, 3).mapToObj(it -> "key-" + UUID.randomUUID().toString()).collect(Collectors.toList());
2343+
List<BaseDocument> docs = keys.stream()
2344+
.map(BaseDocument::new)
2345+
.peek(it -> it.addAttribute("a", "test"))
2346+
.collect(Collectors.toList());
2347+
2348+
collection.insertDocuments(docs);
2349+
2350+
List<Map<String, Object>> modifiedDocs = docs.stream()
2351+
.peek(it -> it.addAttribute("b", "test"))
2352+
.map(it -> {
2353+
Map<String, Object> map = new HashMap<>();
2354+
map.put("_key", it.getKey());
2355+
map.put("a", it.getAttribute("a"));
2356+
map.put("b", it.getAttribute("b"));
2357+
return map;
2358+
})
2359+
.collect(Collectors.toList());
2360+
2361+
final MultiDocumentEntity<DocumentUpdateEntity<BaseDocument>> updateResult = collection
2362+
.updateDocuments(modifiedDocs, new DocumentUpdateOptions().returnNew(true), BaseDocument.class);
2363+
assertThat(updateResult.getDocuments().size(), is(3));
2364+
assertThat(updateResult.getErrors().size(), is(0));
2365+
assertThat(updateResult.getDocuments().stream().map(DocumentUpdateEntity::getNew)
2366+
.allMatch(it -> it.getAttribute("a").equals("test") && it.getAttribute("b").equals("test")),
2367+
is(true));
2368+
}
2369+
23232370
@Test
23242371
public void updateDocumentsOne() {
23252372
final Collection<BaseDocument> values = new ArrayList<>();

src/test/java/com/arangodb/ArangoDBTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.arangodb.entity.*;
2424
import com.arangodb.model.*;
2525
import com.arangodb.model.LogOptions.SortOrder;
26+
import com.arangodb.util.TestUtils;
2627
import com.arangodb.velocypack.exception.VPackException;
2728
import com.arangodb.velocystream.Request;
2829
import com.arangodb.velocystream.RequestType;
@@ -101,8 +102,7 @@ private boolean isCluster() {
101102
}
102103

103104
private boolean isAtLeastVersion(final int major, final int minor) {
104-
final String[] split = arangoDB.getVersion().getVersion().split("\\.");
105-
return Integer.parseInt(split[0]) >= major && Integer.parseInt(split[1]) >= minor;
105+
return TestUtils.isAtLeastVersion(arangoDB.getVersion().getVersion(), major, minor, 0);
106106
}
107107

108108
@Test

src/test/java/com/arangodb/BaseTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.arangodb.entity.*;
2424
import com.arangodb.model.CollectionCreateOptions;
2525
import com.arangodb.model.GraphCreateOptions;
26+
import com.arangodb.util.TestUtils;
2627
import org.junit.AfterClass;
2728
import org.junit.BeforeClass;
2829
import org.junit.runners.Parameterized.Parameters;
@@ -111,8 +112,11 @@ static String rnd() {
111112
}
112113

113114
boolean isAtLeastVersion(final int major, final int minor) {
114-
final String[] split = arangoDB.getVersion().getVersion().split("\\.");
115-
return Integer.parseInt(split[0]) >= major && Integer.parseInt(split[1]) >= minor;
115+
return isAtLeastVersion(major, minor, 0);
116+
}
117+
118+
boolean isAtLeastVersion(final int major, final int minor, final int patch) {
119+
return TestUtils.isAtLeastVersion(arangoDB.getVersion().getVersion(), major, minor, patch);
116120
}
117121

118122
boolean isStorageEngine(ArangoDBEngine.StorageEngineName name) {

src/test/java/com/arangodb/ConcurrentStreamTransactionsTest.java renamed to src/test/java/com/arangodb/StreamTransactionConflictsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @author Michele Rastelli
4141
*/
4242
@RunWith(Parameterized.class)
43-
public class ConcurrentStreamTransactionsTest extends BaseTest {
43+
public class StreamTransactionConflictsTest extends BaseTest {
4444

4545
private static final String COLLECTION_NAME = "db_concurrent_stream_transactions_test";
4646

@@ -49,7 +49,7 @@ public static void init() {
4949
BaseTest.initCollections(COLLECTION_NAME);
5050
}
5151

52-
public ConcurrentStreamTransactionsTest(final ArangoDB arangoDB) {
52+
public StreamTransactionConflictsTest(final ArangoDB arangoDB) {
5353
super(arangoDB);
5454
}
5555

0 commit comments

Comments
 (0)