Skip to content

Commit ba4cfeb

Browse files
author
Jan Steemann
committed
added fullCount option for executeQuery
1 parent a951b5d commit ba4cfeb

File tree

6 files changed

+204
-6
lines changed

6 files changed

+204
-6
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,8 @@ public CursorEntity<?> validateQuery(String query) throws ArangoException {
21012101
* - if set to true the result count is returned
21022102
* @param batchSize
21032103
* - the batch size of the result cursor
2104+
* @param fullCount
2105+
* - if set to true, then all results before the final LIMIT will be counted
21042106
* @param <T>
21052107
* @return <T> CursorEntity<T>
21062108
* @throws ArangoException
@@ -2110,10 +2112,38 @@ public <T> CursorEntity<T> executeQuery(
21102112
Map<String, Object> bindVars,
21112113
Class<T> clazz,
21122114
Boolean calcCount,
2113-
Integer batchSize) throws ArangoException {
2115+
Integer batchSize,
2116+
Boolean fullCount) throws ArangoException {
2117+
2118+
return cursorDriver.executeQuery(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize, fullCount);
2119+
}
21142120

2115-
return cursorDriver.executeQuery(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize);
2121+
/**
2122+
* This method executes an AQL query and returns a CursorEntity
2123+
*
2124+
* @param query
2125+
* - an AQL query as string
2126+
* @param bindVars
2127+
* - a map containing all bind variables,
2128+
* @param clazz
2129+
* - the expected class, the result from the server request is
2130+
* deserialized to an instance of this class.
2131+
* @param calcCount
2132+
* - if set to true the result count is returned
2133+
* @param batchSize
2134+
* - the batch size of the result cursor
2135+
* @param <T>
2136+
* @return <T> CursorEntity<T>
2137+
* @throws ArangoException
2138+
*/
2139+
public <T> CursorEntity<T> executeQuery(
2140+
String query,
2141+
Map<String, Object> bindVars,
2142+
Class<T> clazz,
2143+
Boolean calcCount,
2144+
Integer batchSize) throws ArangoException {
21162145

2146+
return cursorDriver.executeQuery(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize, false);
21172147
}
21182148

21192149
/**
@@ -2144,6 +2174,37 @@ public DefaultEntity finishQuery(long cursorId) throws ArangoException {
21442174
return cursorDriver.finishQuery(getDefaultDatabase(), cursorId);
21452175
}
21462176

2177+
/**
2178+
* This method executes an AQL query and returns a CursorResultSet
2179+
*
2180+
* @param query
2181+
* - an AQL query as string
2182+
* @param bindVars
2183+
* - a map containing all bind variables,
2184+
* @param clazz
2185+
* - the expected class, the result from the server request is
2186+
* deserialized to an instance of this class.
2187+
* @param calcCount
2188+
* - if set to true the result count is returned
2189+
* @param batchSize
2190+
* - the batch size of the result cursor
2191+
* @param fullCount
2192+
* - if set to true, then all results before the final LIMIT will be counted
2193+
* @param <T>
2194+
* @return <T> CursorResultSet<T>
2195+
* @throws ArangoException
2196+
*/
2197+
public <T> CursorResultSet<T> executeQueryWithResultSet(
2198+
String query,
2199+
Map<String, Object> bindVars,
2200+
Class<T> clazz,
2201+
Boolean calcCount,
2202+
Integer batchSize,
2203+
Boolean fullCount) throws ArangoException {
2204+
2205+
return cursorDriver.executeQueryWithResultSet(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize, fullCount);
2206+
}
2207+
21472208
/**
21482209
* This method executes an AQL query and returns a CursorResultSet
21492210
*
@@ -2168,7 +2229,8 @@ public <T> CursorResultSet<T> executeQueryWithResultSet(
21682229
Class<T> clazz,
21692230
Boolean calcCount,
21702231
Integer batchSize) throws ArangoException {
2171-
return cursorDriver.executeQueryWithResultSet(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize);
2232+
2233+
return cursorDriver.executeQueryWithResultSet(getDefaultDatabase(), query, bindVars, clazz, calcCount, batchSize, false);
21722234
}
21732235

21742236
/**

src/main/java/com/arangodb/InternalCursorDriver.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
public interface InternalCursorDriver extends BaseDriverInterface {
1313
CursorEntity<?> validateQuery(String database, String query) throws ArangoException;
1414

15+
<T> CursorEntity<T> executeQuery(
16+
String database,
17+
String query,
18+
Map<String, Object> bindVars,
19+
Class<T> clazz,
20+
Boolean calcCount,
21+
Integer batchSize,
22+
Boolean fullCount) throws ArangoException;
23+
1524
<T> CursorEntity<T> executeQuery(
1625
String database,
1726
String query,
@@ -24,6 +33,15 @@ <T> CursorEntity<T> executeQuery(
2433

2534
DefaultEntity finishQuery(String database, long cursorId) throws ArangoException;
2635

36+
<T> CursorResultSet<T> executeQueryWithResultSet(
37+
String database,
38+
String query,
39+
Map<String, Object> bindVars,
40+
Class<T> clazz,
41+
Boolean calcCount,
42+
Integer batchSize,
43+
Boolean fullCount) throws ArangoException;
44+
2745
<T> CursorResultSet<T> executeQueryWithResultSet(
2846
String database,
2947
String query,

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Iterator;
2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import com.arangodb.util.CollectionUtils;
2324

@@ -38,6 +39,11 @@ public class CursorEntity<T> extends BaseEntity implements Iterable<T> {
3839
* The amount of results in the cursor
3940
*/
4041
int count = -1;
42+
43+
/**
44+
* The number of results before the final LIMIT
45+
*/
46+
int fullCount = -1;
4147

4248
/**
4349
* The cursor id
@@ -49,6 +55,11 @@ public class CursorEntity<T> extends BaseEntity implements Iterable<T> {
4955
*/
5056
List<String> bindVars;
5157

58+
/**
59+
* A list of extra data returned by the query
60+
*/
61+
Map<String, Object> extra;
62+
5263
/**
5364
* A list of objects containing the results
5465
*/
@@ -95,10 +106,15 @@ public List<? extends T> getResults() {
95106
public boolean isHasMore() {
96107
return hasMore;
97108
}
109+
98110
public boolean hasMore() {
99111
return hasMore;
100112
}
101113

114+
public int getFullCount() {
115+
return fullCount;
116+
}
117+
102118
public int getCount() {
103119
return count;
104120
}
@@ -110,6 +126,10 @@ public long getCursorId() {
110126
public List<String> getBindVars() {
111127
return bindVars;
112128
}
129+
130+
public Map<String, Object> getExtra() {
131+
return extra;
132+
}
113133

114134
public void setResults(List<T> results) {
115135
this.results = results;
@@ -118,6 +138,10 @@ public void setResults(List<T> results) {
118138
public void setHasMore(boolean hasMore) {
119139
this.hasMore = hasMore;
120140
}
141+
142+
public void setFullCount(int count) {
143+
this.fullCount = count;
144+
}
121145

122146
public void setCount(int count) {
123147
this.count = count;
@@ -131,4 +155,8 @@ public void setBindVars(List<String> bindVars) {
131155
this.bindVars = bindVars;
132156
}
133157

158+
public void setExtra(Map<String, Object> extra) {
159+
this.extra = extra;
160+
}
161+
134162
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ public static class CursorEntityDeserializer implements JsonDeserializer<CursorE
499499
private Type bindVarsType = new TypeToken<List<String>>() {
500500
}.getType();
501501

502+
private Type extraType = new TypeToken<Map<String, Object>>() {
503+
}.getType();
504+
502505
@Override
503506
public CursorEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
504507
throws JsonParseException {
@@ -549,6 +552,25 @@ public CursorEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseriali
549552
if (obj.has("bindVars")) {
550553
entity.bindVars = context.deserialize(obj.get("bindVars"), bindVarsType);
551554
}
555+
556+
if (obj.has("extra")) {
557+
entity.extra = context.deserialize(obj.get("extra"), extraType);
558+
559+
if (entity.extra.containsKey("stats")) {
560+
if (entity.extra.get("stats") instanceof Map<?, ?>) {
561+
Map<String, Object> m = (Map<String, Object>) entity.extra.get("stats");
562+
if (m.containsKey("fullCount")) {
563+
try {
564+
if (m.get("fullCount") instanceof Double) {
565+
Double v = (Double) m.get("fullCount");
566+
entity.fullCount = v.intValue();
567+
}
568+
}
569+
catch (Exception e) { }
570+
}
571+
}
572+
}
573+
}
552574

553575
return entity;
554576
}

src/main/java/com/arangodb/impl/InternalCursorDriverImpl.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ public <T> CursorEntity<T> executeQuery(
6565
Map<String, Object> bindVars,
6666
Class<T> clazz,
6767
Boolean calcCount,
68-
Integer batchSize) throws ArangoException {
68+
Integer batchSize,
69+
Boolean fullCount) throws ArangoException {
6970

7071
HttpResponseEntity res = httpManager.doPost(
7172
createEndpointUrl(baseUrl, database, "/_api/cursor"),
7273
null,
7374
EntityFactory.toJsonString(new MapBuilder().put("query", query)
7475
.put("bindVars", bindVars == null ? Collections.emptyMap() : bindVars).put("count", calcCount)
75-
.put("batchSize", batchSize).get()));
76+
.put("batchSize", batchSize).put("options", new MapBuilder().put("fullCount", fullCount).get()).get()));
7677
try {
7778
CursorEntity<T> entity = createEntity(res, CursorEntity.class, clazz);
7879
// resultを処理する
@@ -84,6 +85,22 @@ public <T> CursorEntity<T> executeQuery(
8485

8586
}
8687

88+
// ※Iteratorで綺麗に何回もRoundtripもしてくれる処理はClientのレイヤーで行う。
89+
// ※ここでは単純にコールするだけ
90+
91+
@Override
92+
public <T> CursorEntity<T> executeQuery(
93+
String database,
94+
String query,
95+
Map<String, Object> bindVars,
96+
Class<T> clazz,
97+
Boolean calcCount,
98+
Integer batchSize) throws ArangoException {
99+
100+
return executeQuery(database, query, bindVars, clazz, calcCount, batchSize, false);
101+
102+
}
103+
87104
@Override
88105
public <T> CursorEntity<T> continueQuery(String database, long cursorId, Class<?>... clazz) throws ArangoException {
89106

@@ -121,6 +138,22 @@ public DefaultEntity finishQuery(String database, long cursorId) throws ArangoEx
121138
}
122139
}
123140

141+
@Override
142+
public <T> CursorResultSet<T> executeQueryWithResultSet(
143+
String database,
144+
String query,
145+
Map<String, Object> bindVars,
146+
Class<T> clazz,
147+
Boolean calcCount,
148+
Integer batchSize,
149+
Boolean fullCount) throws ArangoException {
150+
151+
CursorEntity<T> entity = executeQuery(database, query, bindVars, clazz, calcCount, batchSize, fullCount);
152+
CursorResultSet<T> rs = new CursorResultSet<T>(database, this, entity, clazz);
153+
return rs;
154+
155+
}
156+
124157
@Override
125158
public <T> CursorResultSet<T> executeQueryWithResultSet(
126159
String database,
@@ -130,7 +163,7 @@ public <T> CursorResultSet<T> executeQueryWithResultSet(
130163
Boolean calcCount,
131164
Integer batchSize) throws ArangoException {
132165

133-
CursorEntity<T> entity = executeQuery(database, query, bindVars, clazz, calcCount, batchSize);
166+
CursorEntity<T> entity = executeQuery(database, query, bindVars, clazz, calcCount, batchSize, false);
134167
CursorResultSet<T> rs = new CursorResultSet<T>(database, this, entity, clazz);
135168
return rs;
136169

src/test/java/com/arangodb/ArangoDriverCursorTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,40 @@ public void test_executeQuery_2() throws ArangoException {
182182

183183
}
184184

185+
@Test
186+
public void test_executeQueryFullCount() throws ArangoException {
187+
188+
// Collectionを作る
189+
String collectionName = "unit_test_query_test";
190+
try {
191+
driver.createCollection(collectionName);
192+
} catch (ArangoException e) {}
193+
driver.truncateCollection(collectionName);
194+
195+
// テストデータを作る
196+
for (int i = 0; i < 100; i++) {
197+
TestComplexEntity01 value = new TestComplexEntity01(
198+
"user_" + (i % 10),
199+
"desc" + (i % 10),
200+
i);
201+
driver.createDocument(collectionName, value, null, null);
202+
}
203+
204+
//String query = "SELECT t FROM unit_test_query_test t WHERE t.age >= @age@";
205+
String query = "FOR t IN unit_test_query_test FILTER t.age >= @age LIMIT 2 RETURN t";
206+
Map<String, Object> bindVars = new MapBuilder().put("age", 10).get();
207+
208+
// 全件とれる範囲
209+
{
210+
CursorEntity<TestComplexEntity01> result = driver.<TestComplexEntity01>executeQuery(
211+
query, bindVars, TestComplexEntity01.class, true, 1, true);
212+
assertThat(result.size(), is(1));
213+
assertThat(result.getCount(), is(2));
214+
assertThat(result.getFullCount(), is(90));
215+
assertThat(result.hasMore(), is(true));
216+
}
217+
218+
}
219+
185220

186221
}

0 commit comments

Comments
 (0)