Skip to content

Commit 4def021

Browse files
committed
refactoring ArangoDatabase.query() overloads
1 parent 64ee518 commit 4def021

13 files changed

+93
-117
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,43 +246,43 @@ public interface ArangoDatabase extends ArangoSerdeAccessor {
246246
* {@code ArangoCursor} instance for the result list.
247247
*
248248
* @param query An AQL query string
249+
* @param type The type of the result (POJO or {@link com.arangodb.util.RawData})
249250
* @param bindVars key/value pairs defining the variables to bind the query to
250251
* @param options Additional options that will be passed to the query API, can be null
251-
* @param type The type of the result (POJO or {@link com.arangodb.util.RawData})
252252
* @return cursor of the results
253253
* @see
254254
* <a href="https://www.arangodb.com/docs/stable/http/aql-query-cursor-accessing-cursors.html#create-cursor">API
255255
* Documentation</a>
256256
*/
257-
<T> ArangoCursor<T> query(String query, Map<String, Object> bindVars, AqlQueryOptions options, Class<T> type);
257+
<T> ArangoCursor<T> query(String query, Class<T> type, Map<String, Object> bindVars, AqlQueryOptions options);
258258

259259
/**
260260
* Performs a database query using the given {@code query}, then returns a new {@code ArangoCursor} instance for the
261261
* result list.
262262
*
263263
* @param query An AQL query string
264-
* @param options Additional options that will be passed to the query API, can be null
265264
* @param type The type of the result (POJO or {@link com.arangodb.util.RawData})
265+
* @param options Additional options that will be passed to the query API, can be null
266266
* @return cursor of the results
267267
* @see
268268
* <a href="https://www.arangodb.com/docs/stable/http/aql-query-cursor-accessing-cursors.html#create-cursor">API
269269
* Documentation</a>
270270
*/
271-
<T> ArangoCursor<T> query(String query, AqlQueryOptions options, Class<T> type);
271+
<T> ArangoCursor<T> query(String query, Class<T> type, AqlQueryOptions options);
272272

273273
/**
274274
* Performs a database query using the given {@code query} and {@code bindVars}, then returns a new
275275
* {@code ArangoCursor} instance for the result list.
276276
*
277277
* @param query An AQL query string
278-
* @param bindVars key/value pairs defining the variables to bind the query to
279278
* @param type The type of the result (POJO or {@link com.arangodb.util.RawData})
279+
* @param bindVars key/value pairs defining the variables to bind the query to
280280
* @return cursor of the results
281281
* @see
282282
* <a href="https://www.arangodb.com/docs/stable/http/aql-query-cursor-accessing-cursors.html#create-cursor">API
283283
* Documentation</a>
284284
*/
285-
<T> ArangoCursor<T> query(String query, Map<String, Object> bindVars, Class<T> type);
285+
<T> ArangoCursor<T> query(String query, Class<T> type, Map<String, Object> bindVars);
286286

287287
/**
288288
* Performs a database query using the given {@code query}, then returns a new {@code ArangoCursor} instance for the

core/src/main/java/com/arangodb/internal/ArangoDatabaseImpl.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.arangodb.model.arangosearch.SearchAliasCreateOptions;
3434

3535
import java.util.Collection;
36+
import java.util.Collections;
3637
import java.util.Map;
3738

3839
import static com.arangodb.internal.serde.SerdeUtils.constructListType;
@@ -158,31 +159,26 @@ public Permissions getPermissions(final String user) {
158159

159160
@Override
160161
public <T> ArangoCursor<T> query(
161-
final String query, final Map<String, Object> bindVars, final AqlQueryOptions options,
162-
final Class<T> type) {
163-
162+
final String query, final Class<T> type, final Map<String, Object> bindVars, final AqlQueryOptions options) {
164163
final InternalRequest request = queryRequest(query, bindVars, options);
165164
final HostHandle hostHandle = new HostHandle();
166165
final InternalCursorEntity result = executor.execute(request, InternalCursorEntity.class, hostHandle);
167-
168166
return createCursor(result, type, options, hostHandle);
169-
170167
}
171168

172169
@Override
173-
public <T> ArangoCursor<T> query(
174-
final String query, final Map<String, Object> bindVars, final Class<T> type) {
175-
return query(query, bindVars, null, type);
170+
public <T> ArangoCursor<T> query(final String query, final Class<T> type, final Map<String, Object> bindVars) {
171+
return query(query, type, bindVars, new AqlQueryOptions());
176172
}
177173

178174
@Override
179-
public <T> ArangoCursor<T> query(final String query, final AqlQueryOptions options, final Class<T> type) {
180-
return query(query, null, options, type);
175+
public <T> ArangoCursor<T> query(final String query, final Class<T> type, final AqlQueryOptions options) {
176+
return query(query, type, null, options);
181177
}
182178

183179
@Override
184180
public <T> ArangoCursor<T> query(final String query, final Class<T> type) {
185-
return query(query, null, null, type);
181+
return query(query, type, null, new AqlQueryOptions());
186182
}
187183

188184
@Override

driver/src/test/java/com/arangodb/ArangoCursorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ void firstStream(ArangoDatabase db) {
5757
@ParameterizedTest(name = "{index}")
5858
@MethodSource("dbs")
5959
void next(ArangoDatabase db) {
60-
final ArangoCursor<JsonNode> cursor = db.query("FOR i IN 0..99 RETURN i", new AqlQueryOptions().batchSize(5),
61-
JsonNode.class);
60+
final ArangoCursor<JsonNode> cursor = db.query("FOR i IN 0..99 RETURN i", JsonNode.class, new AqlQueryOptions().batchSize(5));
6261
while (cursor.hasNext()) {
6362
cursor.next();
6463
}

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

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ void query(ArangoDatabase db) {
565565
for (int i = 0; i < 10; i++) {
566566
db.collection(CNAME1).insertDocument(new BaseDocument(), null);
567567
}
568-
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", null, null, String.class);
568+
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", String.class);
569569
assertThat((Object) cursor).isNotNull();
570570
for (int i = 0; i < 10; i++, cursor.next()) {
571571
assertThat((Iterator<?>) cursor).hasNext();
@@ -575,8 +575,7 @@ void query(ArangoDatabase db) {
575575
@ParameterizedTest(name = "{index}")
576576
@MethodSource("dbs")
577577
void queryWithNullBindVar(ArangoDatabase db) {
578-
final ArangoCursor<Object> cursor = db.query("return @foo", Collections.singletonMap("foo", null), null,
579-
Object.class);
578+
final ArangoCursor<Object> cursor = db.query("return @foo", Object.class, Collections.singletonMap("foo", null));
580579
assertThat(cursor.hasNext()).isTrue();
581580
assertThat(cursor.next()).isNull();
582581
}
@@ -587,7 +586,7 @@ void queryForEach(ArangoDatabase db) {
587586
for (int i = 0; i < 10; i++) {
588587
db.collection(CNAME1).insertDocument(new BaseDocument(), null);
589588
}
590-
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", null, null, String.class);
589+
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", String.class);
591590
assertThat((Object) cursor).isNotNull();
592591

593592
int i = 0;
@@ -606,8 +605,7 @@ void queryWithCount(ArangoDatabase db) {
606605
}
607606

608607
final ArangoCursor<String> cursor = db
609-
.query("for i in " + CNAME1 + " Limit 6 return i._id", null, new AqlQueryOptions().count(true),
610-
String.class);
608+
.query("for i in " + CNAME1 + " Limit 6 return i._id", String.class, new AqlQueryOptions().count(true));
611609
assertThat((Object) cursor).isNotNull();
612610
for (int i = 1; i <= 6; i++, cursor.next()) {
613611
assertThat(cursor.hasNext()).isTrue();
@@ -623,8 +621,7 @@ void queryWithLimitAndFullCount(ArangoDatabase db) {
623621
}
624622

625623
final ArangoCursor<String> cursor = db
626-
.query("for i in " + CNAME1 + " Limit 5 return i._id", null, new AqlQueryOptions().fullCount(true),
627-
String.class);
624+
.query("for i in " + CNAME1 + " Limit 5 return i._id", String.class, new AqlQueryOptions().fullCount(true));
628625
assertThat((Object) cursor).isNotNull();
629626
for (int i = 0; i < 5; i++, cursor.next()) {
630627
assertThat((Iterator<?>) cursor).hasNext();
@@ -642,8 +639,7 @@ void queryWithBatchSize(ArangoDatabase db) {
642639
}
643640

644641
final ArangoCursor<String> cursor = db
645-
.query("for i in " + CNAME1 + " return i._id", null, new AqlQueryOptions().batchSize(5).count(true),
646-
String.class);
642+
.query("for i in " + CNAME1 + " return i._id", String.class, new AqlQueryOptions().batchSize(5).count(true));
647643

648644
assertThat((Object) cursor).isNotNull();
649645
for (int i = 0; i < 10; i++, cursor.next()) {
@@ -659,8 +655,7 @@ void queryIterateWithBatchSize(ArangoDatabase db) {
659655
}
660656

661657
final ArangoCursor<String> cursor = db
662-
.query("for i in " + CNAME1 + " return i._id", null, new AqlQueryOptions().batchSize(5).count(true),
663-
String.class);
658+
.query("for i in " + CNAME1 + " return i._id", String.class, new AqlQueryOptions().batchSize(5).count(true));
664659

665660
assertThat((Object) cursor).isNotNull();
666661
final AtomicInteger i = new AtomicInteger(0);
@@ -681,8 +676,7 @@ void queryWithTTL(ArangoDatabase db) throws InterruptedException {
681676
}
682677

683678
final ArangoCursor<String> cursor = db
684-
.query("for i in " + CNAME1 + " return i._id", null, new AqlQueryOptions().batchSize(5).ttl(ttl),
685-
String.class);
679+
.query("for i in " + CNAME1 + " return i._id", String.class, new AqlQueryOptions().batchSize(5).ttl(ttl));
686680

687681
assertThat((Iterable<String>) cursor).isNotNull();
688682

@@ -733,15 +727,15 @@ void queryWithCache(ArangoDatabase db) {
733727
db.setQueryCacheProperties(properties);
734728

735729
final ArangoCursor<String> cursor = db
736-
.query("FOR t IN " + CNAME1 + " FILTER t.age >= 10 SORT t.age RETURN t._id", null,
737-
new AqlQueryOptions().cache(true), String.class);
730+
.query("FOR t IN " + CNAME1 + " FILTER t.age >= 10 SORT t.age RETURN t._id", String.class,
731+
new AqlQueryOptions().cache(true));
738732

739733
assertThat((Object) cursor).isNotNull();
740734
assertThat(cursor.isCached()).isFalse();
741735

742736
final ArangoCursor<String> cachedCursor = db
743-
.query("FOR t IN " + CNAME1 + " FILTER t.age >= 10 SORT t.age RETURN t._id", null,
744-
new AqlQueryOptions().cache(true), String.class);
737+
.query("FOR t IN " + CNAME1 + " FILTER t.age >= 10 SORT t.age RETURN t._id", String.class,
738+
new AqlQueryOptions().cache(true));
745739

746740
assertThat((Object) cachedCursor).isNotNull();
747741
assertThat(cachedCursor.isCached()).isTrue();
@@ -754,34 +748,34 @@ void queryWithCache(ArangoDatabase db) {
754748
@ParameterizedTest(name = "{index}")
755749
@MethodSource("dbs")
756750
void queryWithMemoryLimit(ArangoDatabase db) {
757-
Throwable thrown = catchThrowable(() -> db.query("RETURN 1..100000", null,
758-
new AqlQueryOptions().memoryLimit(32 * 1024L), String.class));
751+
Throwable thrown = catchThrowable(() -> db.query("RETURN 1..100000", String.class,
752+
new AqlQueryOptions().memoryLimit(32 * 1024L)));
759753
assertThat(thrown).isInstanceOf(ArangoDBException.class);
760754
assertThat(((ArangoDBException) thrown).getErrorNum()).isEqualTo(32);
761755
}
762756

763757
@ParameterizedTest(name = "{index}")
764758
@MethodSource("dbs")
765759
void queryWithFailOnWarningTrue(ArangoDatabase db) {
766-
Throwable thrown = catchThrowable(() -> db.query("RETURN 1 / 0", null,
767-
new AqlQueryOptions().failOnWarning(true), String.class));
760+
Throwable thrown = catchThrowable(() -> db.query("RETURN 1 / 0", String.class,
761+
new AqlQueryOptions().failOnWarning(true)));
768762
assertThat(thrown).isInstanceOf(ArangoDBException.class);
769763
}
770764

771765
@ParameterizedTest(name = "{index}")
772766
@MethodSource("dbs")
773767
void queryWithFailOnWarningFalse(ArangoDatabase db) {
774768
final ArangoCursor<String> cursor = db
775-
.query("RETURN 1 / 0", null, new AqlQueryOptions().failOnWarning(false), String.class);
769+
.query("RETURN 1 / 0", String.class, new AqlQueryOptions().failOnWarning(false));
776770
assertThat(cursor.next()).isNull();
777771
}
778772

779773
@ParameterizedTest(name = "{index}")
780774
@MethodSource("dbs")
781775
void queryWithTimeout(ArangoDatabase db) {
782776
assumeTrue(isAtLeastVersion(3, 6));
783-
Throwable thrown = catchThrowable(() -> db.query("RETURN SLEEP(1)", null,
784-
new AqlQueryOptions().maxRuntime(0.1), String.class).next());
777+
Throwable thrown = catchThrowable(() -> db.query("RETURN SLEEP(1)", String.class,
778+
new AqlQueryOptions().maxRuntime(0.1)).next());
785779
assertThat(thrown).isInstanceOf(ArangoDBException.class);
786780
assertThat(((ArangoDBException) thrown).getResponseCode()).isEqualTo(410);
787781
}
@@ -790,10 +784,10 @@ void queryWithTimeout(ArangoDatabase db) {
790784
@MethodSource("dbs")
791785
void queryWithMaxWarningCount(ArangoDatabase db) {
792786
final ArangoCursor<String> cursorWithWarnings = db
793-
.query("RETURN 1 / 0", null, new AqlQueryOptions(), String.class);
787+
.query("RETURN 1 / 0", String.class, new AqlQueryOptions());
794788
assertThat(cursorWithWarnings.getWarnings()).hasSize(1);
795789
final ArangoCursor<String> cursorWithLimitedWarnings = db
796-
.query("RETURN 1 / 0", null, new AqlQueryOptions().maxWarningCount(0L), String.class);
790+
.query("RETURN 1 / 0", String.class, new AqlQueryOptions().maxWarningCount(0L));
797791
final Collection<CursorWarning> warnings = cursorWithLimitedWarnings.getWarnings();
798792
assertThat(warnings).isNullOrEmpty();
799793
}
@@ -807,8 +801,8 @@ void queryCursor(ArangoDatabase db) {
807801
}
808802

809803
final int batchSize = 5;
810-
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", null,
811-
new AqlQueryOptions().batchSize(batchSize).count(true), String.class);
804+
final ArangoCursor<String> cursor = db.query("for i in " + CNAME1 + " return i._id", String.class,
805+
new AqlQueryOptions().batchSize(batchSize).count(true));
812806
assertThat((Object) cursor).isNotNull();
813807
assertThat(cursor.getCount()).isGreaterThanOrEqualTo(numbDocs);
814808

@@ -859,8 +853,7 @@ void queryWithBindVars(ArangoDatabase db) {
859853
bindVars.put("age", 25);
860854

861855
final ArangoCursor<String> cursor = db
862-
.query("FOR t IN @@coll FILTER t.age >= @age SORT t.age RETURN t._id", bindVars, null,
863-
String.class);
856+
.query("FOR t IN @@coll FILTER t.age >= @age SORT t.age RETURN t._id", String.class, bindVars);
864857

865858
assertThat((Object) cursor).isNotNull();
866859

@@ -876,7 +869,7 @@ void queryWithRawBindVars(ArangoDatabase db) {
876869
bindVars.put("foo", RawJson.of("\"fooValue\""));
877870
bindVars.put("bar", RawBytes.of(db.getSerde().serializeUserData(11)));
878871

879-
final JsonNode res = db.query("RETURN {foo: @foo, bar: @bar}", bindVars, null, JsonNode.class).next();
872+
final JsonNode res = db.query("RETURN {foo: @foo, bar: @bar}", JsonNode.class, bindVars).next();
880873

881874
assertThat(res.get("foo").textValue()).isEqualTo("fooValue");
882875
assertThat(res.get("bar").intValue()).isEqualTo(11);
@@ -885,7 +878,7 @@ void queryWithRawBindVars(ArangoDatabase db) {
885878
@ParameterizedTest(name = "{index}")
886879
@MethodSource("arangos")
887880
void queryWithWarning(ArangoDB arangoDB) {
888-
final ArangoCursor<String> cursor = arangoDB.db().query("return 1/0", null, null, String.class);
881+
final ArangoCursor<String> cursor = arangoDB.db().query("return 1/0", String.class);
889882

890883
assertThat((Object) cursor).isNotNull();
891884
assertThat(cursor.getWarnings()).isNotNull();
@@ -896,8 +889,7 @@ void queryWithWarning(ArangoDB arangoDB) {
896889
void queryStream(ArangoDatabase db) {
897890
if (isAtLeastVersion(3, 4)) {
898891
final ArangoCursor<Void> cursor = db
899-
.query("FOR i IN 1..2 RETURN i", null, new AqlQueryOptions().stream(true).count(true),
900-
Void.class);
892+
.query("FOR i IN 1..2 RETURN i", Void.class, new AqlQueryOptions().stream(true).count(true));
901893
assertThat((Object) cursor).isNotNull();
902894
assertThat(cursor.getCount()).isNull();
903895
}
@@ -907,7 +899,7 @@ void queryStream(ArangoDatabase db) {
907899
@MethodSource("arangos")
908900
void queryClose(ArangoDB arangoDB) throws IOException {
909901
final ArangoCursor<String> cursor = arangoDB.db()
910-
.query("for i in 1..2 return i", null, new AqlQueryOptions().batchSize(1), String.class);
902+
.query("for i in 1..2 return i", String.class, new AqlQueryOptions().batchSize(1));
911903
cursor.close();
912904
AtomicInteger count = new AtomicInteger();
913905
Throwable thrown = catchThrowable(() -> {
@@ -925,25 +917,24 @@ void queryClose(ArangoDB arangoDB) throws IOException {
925917
@MethodSource("dbs")
926918
void queryNoResults(ArangoDatabase db) throws IOException {
927919
final ArangoCursor<BaseDocument> cursor = db
928-
.query("FOR i IN @@col RETURN i", new MapBuilder().put("@col", CNAME1).get(), null,
929-
BaseDocument.class);
920+
.query("FOR i IN @@col RETURN i", BaseDocument.class, new MapBuilder().put("@col", CNAME1).get());
930921
cursor.close();
931922
}
932923

933924
@ParameterizedTest(name = "{index}")
934925
@MethodSource("dbs")
935926
void queryWithNullBindParam(ArangoDatabase db) throws IOException {
936927
final ArangoCursor<BaseDocument> cursor = db.query("FOR i IN @@col FILTER i.test == @test RETURN i",
937-
new MapBuilder().put("@col", CNAME1).put("test", null).get(), null, BaseDocument.class);
928+
BaseDocument.class, new MapBuilder().put("@col", CNAME1).put("test", null).get());
938929
cursor.close();
939930
}
940931

941932
@ParameterizedTest(name = "{index}")
942933
@MethodSource("dbs")
943934
void queryAllowDirtyRead(ArangoDatabase db) throws IOException {
944935
final ArangoCursor<BaseDocument> cursor = db.query("FOR i IN @@col FILTER i.test == @test RETURN i",
945-
new MapBuilder().put("@col", CNAME1).put("test", null).get(),
946-
new AqlQueryOptions().allowDirtyRead(true), BaseDocument.class);
936+
BaseDocument.class, new MapBuilder().put("@col", CNAME1).put("test", null).get(),
937+
new AqlQueryOptions().allowDirtyRead(true));
947938
if (isAtLeastVersion(3, 10)) {
948939
assertThat(cursor.isPotentialDirtyRead()).isTrue();
949940
}
@@ -1023,7 +1014,7 @@ void parseQuery(ArangoDatabase db) {
10231014
@MethodSource("dbs")
10241015
void getCurrentlyRunningQueries(ArangoDatabase db) throws InterruptedException {
10251016
String query = "return sleep(1)";
1026-
Thread t = new Thread(() -> db.query(query, null, null, Void.class));
1017+
Thread t = new Thread(() -> db.query(query, Void.class));
10271018
t.start();
10281019
Thread.sleep(300);
10291020
final Collection<QueryEntity> currentlyRunningQueries = db.getCurrentlyRunningQueries();
@@ -1040,7 +1031,7 @@ void killQuery(ArangoDatabase db) throws InterruptedException, ExecutionExceptio
10401031
ExecutorService es = Executors.newSingleThreadExecutor();
10411032
Future<?> future = es.submit(() -> {
10421033
try {
1043-
db.query("return sleep(5)", null, null, Void.class);
1034+
db.query("return sleep(5)", Void.class);
10441035
fail();
10451036
} catch (ArangoDBException e) {
10461037
assertThat(e.getResponseCode()).isEqualTo(410);
@@ -1074,7 +1065,7 @@ void getAndClearSlowQueries(ArangoDatabase db) {
10741065
properties.setSlowQueryThreshold(1L);
10751066
db.setQueryTrackingProperties(properties);
10761067

1077-
db.query("return sleep(1.1)", null, null, Void.class);
1068+
db.query("return sleep(1.1)", Void.class);
10781069
final Collection<QueryEntity> slowQueries = db.getSlowQueries();
10791070
assertThat(slowQueries).hasSize(1);
10801071
final QueryEntity queryEntity = slowQueries.iterator().next();

0 commit comments

Comments
 (0)