Skip to content

Commit dcc1d83

Browse files
author
Mark
committed
ArangoDatabase.getDocument(id) implemented
1 parent 2bc7718 commit dcc1d83

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed

docs/api_overview.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ E.g. in the previous example the object was stored as follows:
144144

145145
```
146146

147-
## read document (as JavaBean)
147+
## read document by key (as JavaBean)
148148
``` Java
149149
MyObject document = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey, MyObject.class).get();
150150
document.getName();
@@ -166,6 +166,12 @@ E.g. in the previous example the object was stored as follows:
166166

167167
```
168168

169+
## read document by id
170+
``` Java
171+
arangoDB.db("myDatabase").getDocument("myCollection/myKey", MyObject.class).get();
172+
173+
```
174+
169175
# Multi document operations
170176

171177
## insert documents

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ private <T> ResponseDeserializer<Collection<DocumentCreateEntity<T>>> insertDocu
308308
* @throws ArangoDBException
309309
*/
310310
public <T> Optional<T> getDocument(final String key, final Class<T> type) {
311+
validateDocumentKey(key);
311312
try {
312313
return Optional.ofNullable(executeSync(getDocumentRequest(key, new DocumentReadOptions()), type));
313314
} catch (final ArangoDBException e) {
@@ -334,6 +335,7 @@ public <T> Optional<T> getDocument(final String key, final Class<T> type) {
334335
*/
335336
public <T> Optional<T> getDocument(final String key, final Class<T> type, final DocumentReadOptions options)
336337
throws ArangoDBException {
338+
validateDocumentKey(key);
337339
try {
338340
return Optional.ofNullable(executeSync(getDocumentRequest(key, options), type));
339341
} catch (final ArangoDBException e) {
@@ -355,7 +357,9 @@ public <T> Optional<T> getDocument(final String key, final Class<T> type, final
355357
* The type of the document (POJO class, VPackSlice or String for Json)
356358
* @return the document identified by the key
357359
*/
358-
public <T> CompletableFuture<Optional<T>> getDocumentAsync(final String key, final Class<T> type) {
360+
public <T> CompletableFuture<Optional<T>> getDocumentAsync(final String key, final Class<T> type)
361+
throws ArangoDBException {
362+
validateDocumentKey(key);
359363
final CompletableFuture<Optional<T>> result = new CompletableFuture<>();
360364
final CompletableFuture<T> execute = executeAsync(getDocumentRequest(key, new DocumentReadOptions()), type);
361365
execute.whenComplete(
@@ -379,7 +383,8 @@ public <T> CompletableFuture<Optional<T>> getDocumentAsync(final String key, fin
379383
public <T> CompletableFuture<Optional<T>> getDocumentAsync(
380384
final String key,
381385
final Class<T> type,
382-
final DocumentReadOptions options) {
386+
final DocumentReadOptions options) throws ArangoDBException {
387+
validateDocumentKey(key);
383388
final CompletableFuture<Optional<T>> result = new CompletableFuture<>();
384389
final CompletableFuture<T> execute = executeAsync(getDocumentRequest(key, options), type);
385390
execute.whenComplete(

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.arangodb.model.AqlQueryParseOptions;
5353
import com.arangodb.model.CollectionCreateOptions;
5454
import com.arangodb.model.CollectionsReadOptions;
55+
import com.arangodb.model.DocumentReadOptions;
5556
import com.arangodb.model.GraphCreateOptions;
5657
import com.arangodb.model.OptionsBuilder;
5758
import com.arangodb.model.TransactionOptions;
@@ -976,4 +977,83 @@ private <E> Collection<E> deserializeEdges(final Class<E> edgeClass, final VPack
976977
}
977978
return edges;
978979
}
980+
981+
/**
982+
* Reads a single document
983+
*
984+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#read-document">API
985+
* Documentation</a>
986+
* @param id
987+
* The id of the document
988+
* @param type
989+
* The type of the document (POJO class, VPackSlice or String for Json)
990+
* @return the document identified by the id
991+
* @throws ArangoDBException
992+
*/
993+
public <T> Optional<T> getDocument(final String id, final Class<T> type) throws ArangoDBException {
994+
validateDocumentId(id);
995+
final String[] split = id.split("/");
996+
return collection(split[0]).getDocument(split[1], type);
997+
}
998+
999+
/**
1000+
* Reads a single document
1001+
*
1002+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#read-document">API
1003+
* Documentation</a>
1004+
* @param id
1005+
* The id of the document
1006+
* @param type
1007+
* The type of the document (POJO class, VPackSlice or String for Json)
1008+
* @param options
1009+
* Additional options, can be null
1010+
* @return the document identified by the id
1011+
* @throws ArangoDBException
1012+
*/
1013+
public <T> Optional<T> getDocument(final String id, final Class<T> type, final DocumentReadOptions options)
1014+
throws ArangoDBException {
1015+
validateDocumentId(id);
1016+
final String[] split = id.split("/");
1017+
return collection(split[0]).getDocument(split[1], type, options);
1018+
}
1019+
1020+
/**
1021+
* Reads a single document
1022+
*
1023+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#read-document">API
1024+
* Documentation</a>
1025+
* @param id
1026+
* The id of the document
1027+
* @param type
1028+
* The type of the document (POJO class, VPackSlice or String for Json)
1029+
* @return the document identified by the id
1030+
*/
1031+
public <T> CompletableFuture<Optional<T>> getDocumentAsync(final String id, final Class<T> type)
1032+
throws ArangoDBException {
1033+
validateDocumentId(id);
1034+
final String[] split = id.split("/");
1035+
return collection(split[0]).getDocumentAsync(split[1], type);
1036+
}
1037+
1038+
/**
1039+
* Reads a single document
1040+
*
1041+
* @see <a href="https://docs.arangodb.com/current/HTTP/Document/WorkingWithDocuments.html#read-document">API
1042+
* Documentation</a>
1043+
* @param id
1044+
* The id of the document
1045+
* @param type
1046+
* The type of the document (POJO class, VPackSlice or String for Json)
1047+
* @param options
1048+
* Additional options, can be null
1049+
* @return the document identified by the id
1050+
*/
1051+
public <T> CompletableFuture<Optional<T>> getDocumentAsync(
1052+
final String id,
1053+
final Class<T> type,
1054+
final DocumentReadOptions options) throws ArangoDBException {
1055+
validateDocumentId(id);
1056+
final String[] split = id.split("/");
1057+
return collection(split[0]).getDocumentAsync(split[1], type, options);
1058+
}
9791059
}

src/main/java/com/arangodb/ArangoExecuteable.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static interface ResponseDeserializer<T> {
5050
}
5151

5252
private static final String REGEX_DOCUMENT_KEY = "[^/]+";
53+
private static final String REGEX_DOCUMENT_ID = "[^/]+/[^/]+";
5354

5455
protected final Communication communication;
5556
protected final VPack vpacker;
@@ -105,12 +106,17 @@ protected String createPath(final String... params) {
105106
}
106107

107108
protected void validateDocumentKey(final String key) throws ArangoDBException {
108-
validateName(REGEX_DOCUMENT_KEY, key);
109+
validateName("document key", REGEX_DOCUMENT_KEY, key);
109110
}
110111

111-
protected void validateName(final String regex, final CharSequence name) throws ArangoDBException {
112+
protected void validateDocumentId(final String id) throws ArangoDBException {
113+
validateName("document id", REGEX_DOCUMENT_ID, id);
114+
}
115+
116+
protected void validateName(final String type, final String regex, final CharSequence name)
117+
throws ArangoDBException {
112118
if (!Pattern.matches(regex, name)) {
113-
throw new ArangoDBException(String.format("Name %s is not valid.", name));
119+
throw new ArangoDBException(String.format("%s %s is not valid.", type, name));
114120
}
115121
}
116122

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public void getDocumentNotFound() {
210210
assertThat(document.isPresent(), is(false));
211211
}
212212

213+
@Test(expected = ArangoDBException.class)
214+
public void getDocumentWrongKey() {
215+
db.collection(COLLECTION_NAME).getDocument("no/no", BaseDocument.class);
216+
}
217+
213218
@Test
214219
public void getDocumentAsyncNotFound() {
215220
try {

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.HashMap;
3434
import java.util.Iterator;
3535
import java.util.Map;
36+
import java.util.Optional;
3637
import java.util.concurrent.atomic.AtomicInteger;
3738
import java.util.stream.Stream;
3839

@@ -685,4 +686,21 @@ public void executeTraversal() {
685686
db.collection("knows").drop();
686687
}
687688
}
689+
690+
@Test
691+
public void getDocument() {
692+
db.createCollection(COLLECTION_NAME);
693+
final BaseDocument value = new BaseDocument();
694+
value.setKey("123");
695+
db.collection(COLLECTION_NAME).insertDocument(value);
696+
697+
final Optional<BaseDocument> document = db.getDocument(COLLECTION_NAME + "/123", BaseDocument.class);
698+
assertThat(document.isPresent(), is(true));
699+
assertThat(document.get().getKey(), is("123"));
700+
}
701+
702+
@Test(expected = ArangoDBException.class)
703+
public void getDocumentWrongId() {
704+
db.getDocument("123", BaseDocument.class);
705+
}
688706
}

0 commit comments

Comments
 (0)