Skip to content

Commit e074dde

Browse files
author
mpv1989
committed
Add ArangoCollection.getDocuments(Collection<String>, Class)
1 parent 8535184 commit e074dde

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ v4.2.2 (xxxx-xx-xx)
22
---------------------------
33
* added ArangoDatabase.grantAccess(String, Permissions)
44
* added ArangoCollection.grantAccess(String, Permissions)
5+
* added ArangoCollection.getDocuments(Collection<String>, Class)
56

67
v4.2.1 (2017-06-20)
78
---------------------------

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ public <T> T getDocument(final String key, final Class<T> type, final DocumentRe
251251
}
252252
}
253253

254+
/**
255+
* Reads multiple documents
256+
*
257+
* @param keys
258+
* The keys of the documents
259+
* @param type
260+
* The type of the documents (POJO class, VPackSlice or String for Json)
261+
* @return the documents and possible errors
262+
* @throws ArangoDBException
263+
*/
264+
public <T> MultiDocumentEntity<T> getDocuments(final Collection<String> keys, final Class<T> type)
265+
throws ArangoDBException {
266+
final DocumentReadOptions options = new DocumentReadOptions();
267+
return executor.execute(getDocumentsRequest(keys, options), getDocumentsResponseDeserializer(type, options));
268+
}
269+
254270
/**
255271
* Replaces the document with key with the one in the body, provided there is such a document and no precondition is
256272
* violated

src/main/java/com/arangodb/entity/MultiDocumentEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author Mark Vollmary
2727
*
2828
*/
29-
public class MultiDocumentEntity<E extends DocumentEntity> {
29+
public class MultiDocumentEntity<E> {
3030

3131
private Collection<E> documents;
3232
private Collection<ErrorEntity> errors;

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,39 @@ protected Request getDocumentRequest(final String key, final DocumentReadOptions
197197
return request;
198198
}
199199

200+
protected Request getDocumentsRequest(final Collection<String> keys, final DocumentReadOptions options) {
201+
return new Request(db.name(), RequestType.PUT, executor.createPath(ArangoDBConstants.PATH_API_DOCUMENT, name))
202+
.putQueryParam("onlyget", true)
203+
.putHeaderParam(ArangoDBConstants.IF_NONE_MATCH, options.getIfNoneMatch())
204+
.putHeaderParam(ArangoDBConstants.IF_MATCH, options.getIfMatch()).setBody(util().serialize(keys));
205+
}
206+
207+
protected <T> ResponseDeserializer<MultiDocumentEntity<T>> getDocumentsResponseDeserializer(
208+
final Class<T> type,
209+
final DocumentReadOptions options) {
210+
return new ResponseDeserializer<MultiDocumentEntity<T>>() {
211+
@SuppressWarnings("unchecked")
212+
@Override
213+
public MultiDocumentEntity<T> deserialize(final Response response) throws VPackException {
214+
final MultiDocumentEntity<T> multiDocument = new MultiDocumentEntity<T>();
215+
final Collection<T> docs = new ArrayList<T>();
216+
final Collection<ErrorEntity> errors = new ArrayList<ErrorEntity>();
217+
final VPackSlice body = response.getBody();
218+
for (final Iterator<VPackSlice> iterator = body.arrayIterator(); iterator.hasNext();) {
219+
final VPackSlice next = iterator.next();
220+
if (next.get(ArangoDBConstants.ERROR).isTrue()) {
221+
errors.add((ErrorEntity) util().deserialize(next, ErrorEntity.class));
222+
} else {
223+
docs.add((T) util().deserialize(next, type));
224+
}
225+
}
226+
multiDocument.setDocuments(docs);
227+
multiDocument.setErrors(errors);
228+
return multiDocument;
229+
}
230+
};
231+
}
232+
200233
protected <T> Request replaceDocumentRequest(
201234
final String key,
202235
final T value,

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.hamcrest.Matchers.hasItems;
2828
import static org.hamcrest.Matchers.instanceOf;
2929
import static org.hamcrest.Matchers.is;
30+
import static org.hamcrest.Matchers.isOneOf;
3031
import static org.hamcrest.Matchers.not;
3132
import static org.hamcrest.Matchers.notNullValue;
3233
import static org.hamcrest.Matchers.nullValue;
@@ -35,7 +36,9 @@
3536
import static org.junit.Assert.fail;
3637

3738
import java.util.ArrayList;
39+
import java.util.Arrays;
3840
import java.util.Collection;
41+
import java.util.Collections;
3942
import java.util.HashMap;
4043
import java.util.Map;
4144

@@ -224,6 +227,41 @@ public void getDocumentWrongKey() {
224227
db.collection(COLLECTION_NAME).getDocument("no/no", BaseDocument.class);
225228
}
226229

230+
@Test
231+
public void getDocuments() {
232+
final Collection<BaseDocument> values = new ArrayList<BaseDocument>();
233+
values.add(new BaseDocument("1"));
234+
values.add(new BaseDocument("2"));
235+
values.add(new BaseDocument("3"));
236+
db.collection(COLLECTION_NAME).insertDocuments(values);
237+
final MultiDocumentEntity<BaseDocument> documents = db.collection(COLLECTION_NAME)
238+
.getDocuments(Arrays.asList("1", "2", "3"), BaseDocument.class);
239+
assertThat(documents, is(notNullValue()));
240+
assertThat(documents.getDocuments().size(), is(3));
241+
for (final BaseDocument document : documents.getDocuments()) {
242+
assertThat(document.getId(),
243+
isOneOf(COLLECTION_NAME + "/" + "1", COLLECTION_NAME + "/" + "2", COLLECTION_NAME + "/" + "3"));
244+
}
245+
}
246+
247+
@Test
248+
public void getDocumentsNotFound() {
249+
final MultiDocumentEntity<BaseDocument> readResult = db.collection(COLLECTION_NAME)
250+
.getDocuments(Collections.singleton("no"), BaseDocument.class);
251+
assertThat(readResult, is(notNullValue()));
252+
assertThat(readResult.getDocuments().size(), is(0));
253+
assertThat(readResult.getErrors().size(), is(1));
254+
}
255+
256+
@Test
257+
public void getDocumentsWrongKey() {
258+
final MultiDocumentEntity<BaseDocument> readResult = db.collection(COLLECTION_NAME)
259+
.getDocuments(Collections.singleton("no/no"), BaseDocument.class);
260+
assertThat(readResult, is(notNullValue()));
261+
assertThat(readResult.getDocuments().size(), is(0));
262+
assertThat(readResult.getErrors().size(), is(1));
263+
}
264+
227265
@Test
228266
public void updateDocument() {
229267
final BaseDocument doc = new BaseDocument();

0 commit comments

Comments
 (0)