Skip to content

Commit 22f2cad

Browse files
author
a-brandt
committed
added AQL query cache options
1 parent 3d7b9d7 commit 22f2cad

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

src/main/java/com/arangodb/BaseCursorProxy.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,17 @@ public Long getCursorId() {
127127
return baseCursor.getEntity().getCursorId();
128128
}
129129

130+
/**
131+
* a boolean flag indicating whether the query result was served from the
132+
* query cache or not. If the query result is served from the query cache,
133+
* the extra return attribute will not contain any stats sub-attribute and
134+
* no profile sub-attribute. (since ArangoDB 2.7)
135+
*
136+
* @return true, if the result is cached
137+
*
138+
*/
139+
public boolean isCached() {
140+
return baseCursor.getEntity().isCached();
141+
}
142+
130143
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public class CursorEntity<T> extends BaseEntity implements Iterable<T> {
5252
*/
5353
long cursorId = -1;
5454

55+
/**
56+
* a boolean flag indicating whether the query result was served from the
57+
* query cache or not. If the query result is served from the query cache,
58+
* the extra return attribute will not contain any stats sub-attribute and
59+
* no profile sub-attribute. (since ArangoDB 2.7)
60+
*/
61+
boolean cached = false;
62+
5563
/**
5664
* A list of bind variables returned by the query
5765
*/
@@ -188,4 +196,12 @@ public void setExtra(Map<String, Object> extra) {
188196
this.extra = extra;
189197
}
190198

199+
public boolean isCached() {
200+
return cached;
201+
}
202+
203+
public void setCached(boolean cached) {
204+
this.cached = cached;
205+
}
206+
191207
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ public CursorEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseriali
560560
entity.cursorId = obj.getAsJsonPrimitive("id").getAsLong();
561561
}
562562

563+
if (obj.has("cached")) {
564+
entity.cached = obj.getAsJsonPrimitive("cached").getAsBoolean();
565+
}
566+
563567
if (obj.has("bindVars")) {
564568
entity.bindVars = context.deserialize(obj.get("bindVars"), bindVarsType);
565569
}

src/main/java/com/arangodb/util/AqlQueryOptions.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class AqlQueryOptions implements OptionsInterface {
77
private Boolean count;
88
private Integer batchSize;
99
private Boolean fullCount;
10+
private Boolean cache;
1011
private Integer ttl;
1112

1213
/**
@@ -124,6 +125,33 @@ public AqlQueryOptions setTtl(Integer ttl) {
124125
return this;
125126
}
126127

128+
/**
129+
* flag to determine whether the AQL query cache shall be used. If set to
130+
* false, then any query cache lookup will be skipped for the query. If set
131+
* to true, it will lead to the query cache being checked for the query if
132+
* the query cache mode is either on or demand. (since ArangoDB 2.7)
133+
*
134+
* @return boolean flag
135+
*/
136+
public Boolean getCache() {
137+
return fullCount;
138+
}
139+
140+
/**
141+
* flag to determine whether the AQL query cache shall be used. If set to
142+
* false, then any query cache lookup will be skipped for the query. If set
143+
* to true, it will lead to the query cache being checked for the query if
144+
* the query cache mode is either on or demand. (since ArangoDB 2.7)
145+
*
146+
* @param cache
147+
* boolean flag
148+
* @return this
149+
*/
150+
public AqlQueryOptions setCache(Boolean cache) {
151+
this.cache = cache;
152+
return this;
153+
}
154+
127155
@Override
128156
public Map<String, Object> toMap() {
129157
MapBuilder mp = new MapBuilder();
@@ -136,6 +164,9 @@ public Map<String, Object> toMap() {
136164
if (ttl != null) {
137165
mp.put("ttl", ttl);
138166
}
167+
if (cache != null) {
168+
mp.put("cache", cache);
169+
}
139170

140171
MapBuilder optionsMp = new MapBuilder();
141172

src/test/java/com/arangodb/ArangoDriverDocumentCursorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.slf4j.LoggerFactory;
3030

3131
import com.arangodb.entity.DocumentEntity;
32+
import com.arangodb.entity.QueryCachePropertiesEntity;
3233
import com.arangodb.util.AqlQueryOptions;
3334
import com.arangodb.util.MapBuilder;
3435

@@ -151,6 +152,28 @@ public void test3_BatchSize5() throws ArangoException {
151152

152153
}
153154

155+
@Test
156+
public void test_withCache() throws ArangoException {
157+
if (isMinimumVersion(VERSION_2_7)) {
158+
QueryCachePropertiesEntity properties = new QueryCachePropertiesEntity();
159+
properties.setMode("on");
160+
driver.setQueryCacheProperties(properties);
161+
162+
String query = "FOR t IN unit_test_query_test FILTER t.age >= @age SORT t.age RETURN t";
163+
Map<String, Object> bindVars = new MapBuilder().put("age", 90).get();
164+
165+
AqlQueryOptions aqlQueryOptions = getAqlQueryOptions(true, 5, null);
166+
aqlQueryOptions.setCache(true);
167+
168+
DocumentCursor<TestComplexEntity01> rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions,
169+
TestComplexEntity01.class);
170+
171+
rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions, TestComplexEntity01.class);
172+
173+
assertThat(rs.isCached(), is(true));
174+
}
175+
}
176+
154177
@Test
155178
public void test4_withIterator() throws ArangoException {
156179

0 commit comments

Comments
 (0)