Skip to content

Commit 3448eac

Browse files
authored
resend header params (#267)
* resend header params * added a cleanup method to only store meta headers that are allowed to be stored
1 parent 7015dc3 commit 3448eac

10 files changed

+107
-17
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
package com.arangodb.entity;
2222

2323
import java.util.Collection;
24+
import java.util.Map;
25+
26+
import org.apache.http.protocol.HTTP;
2427

2528
import com.arangodb.velocypack.VPackSlice;
2629

@@ -30,14 +33,16 @@
3033
* @see <a href="https://docs.arangodb.com/current/HTTP/AqlQueryCursor/AccessingCursors.html#create-cursor">API
3134
* Documentation</a>
3235
*/
33-
public class CursorEntity implements Entity {
36+
public class CursorEntity implements Entity, MetaAware {
3437

3538
private String id;
3639
private Integer count;
3740
private Extras extra;
3841
private Boolean cached;
3942
private Boolean hasMore;
4043
private VPackSlice result;
44+
45+
private Map<String, String> meta;
4146

4247
public String getId() {
4348
return id;
@@ -83,6 +88,23 @@ public Boolean getHasMore() {
8388
public VPackSlice getResult() {
8489
return result;
8590
}
91+
92+
public Map<String, String> getMeta() {
93+
return meta;
94+
}
95+
96+
/**
97+
* @return remove not allowed (valid storable) meta information
98+
*/
99+
public Map<String, String> cleanupMeta(Map<String, String> meta) {
100+
meta.remove(HTTP.CONTENT_LEN);
101+
meta.remove(HTTP.TRANSFER_ENCODING);
102+
return meta;
103+
}
104+
105+
public void setMeta(Map<String, String> meta) {
106+
this.meta = cleanupMeta(meta);
107+
}
86108

87109
public static class Warning {
88110

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.arangodb.entity;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author Mark Vollmary
7+
*
8+
*/
9+
public interface MetaAware {
10+
11+
public Map<String, String> getMeta();
12+
13+
public void setMeta(final Map<String, String> meta);
14+
15+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
package com.arangodb.internal;
2222

23+
import java.util.Map;
24+
2325
import com.arangodb.ArangoDBException;
2426
import com.arangodb.entity.CursorEntity;
2527

@@ -29,8 +31,8 @@
2931
*/
3032
public interface ArangoCursorExecute {
3133

32-
CursorEntity next(String id) throws ArangoDBException;
34+
CursorEntity next(String id, Map<String, String> meta) throws ArangoDBException;
3335

34-
void close(String id) throws ArangoDBException;
36+
void close(String id, Map<String, String> meta) throws ArangoDBException;
3537

3638
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,13 @@ public <T> ArangoCursor<T> query(
192192
final Map<String, Object> bindVars,
193193
final AqlQueryOptions options,
194194
final Class<T> type) throws ArangoDBException {
195+
195196
final Request request = queryRequest(query, bindVars, options);
196197
final HostHandle hostHandle = new HostHandle();
197198
final CursorEntity result = executor.execute(request, CursorEntity.class, hostHandle);
199+
198200
return createCursor(result, type, options, hostHandle);
201+
199202
}
200203

201204
@Override
@@ -218,7 +221,7 @@ public <T> ArangoCursor<T> query(final String query, final Class<T> type) throws
218221
@Override
219222
public <T> ArangoCursor<T> cursor(final String cursorId, final Class<T> type) throws ArangoDBException {
220223
final HostHandle hostHandle = new HostHandle();
221-
final CursorEntity result = executor.execute(queryNextRequest(cursorId, null), CursorEntity.class, hostHandle);
224+
final CursorEntity result = executor.execute(queryNextRequest(cursorId, null, null), CursorEntity.class, hostHandle);
222225
return createCursor(result, type, null, hostHandle);
223226
}
224227

@@ -227,17 +230,19 @@ private <T> ArangoCursor<T> createCursor(
227230
final Class<T> type,
228231
final AqlQueryOptions options,
229232
final HostHandle hostHandle) {
233+
230234
final ArangoCursorExecute execute = new ArangoCursorExecute() {
231235
@Override
232-
public CursorEntity next(final String id) {
233-
return executor.execute(queryNextRequest(id, options), CursorEntity.class, hostHandle);
236+
public CursorEntity next(final String id, Map<String, String> meta) {
237+
return executor.execute(queryNextRequest(id, options, meta), CursorEntity.class, hostHandle);
234238
}
235239

236240
@Override
237-
public void close(final String id) {
238-
executor.execute(queryCloseRequest(id, options), Void.class, hostHandle);
241+
public void close(final String id, Map<String, String> meta) {
242+
executor.execute(queryCloseRequest(id, options, meta), Void.class, hostHandle);
239243
}
240244
};
245+
241246
return cursorInitializer != null ? cursorInitializer.createInstance(this, execute, type, result)
242247
: new ArangoCursorImpl<T>(this, execute, type, result);
243248
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
import java.io.IOException;
2424
import java.lang.reflect.Type;
2525

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2629
import com.arangodb.ArangoDBException;
30+
import com.arangodb.entity.MetaAware;
2731
import com.arangodb.internal.net.CommunicationProtocol;
2832
import com.arangodb.internal.net.HostHandle;
2933
import com.arangodb.internal.util.ArangoSerializationFactory;
@@ -36,6 +40,8 @@
3640
*
3741
*/
3842
public class ArangoExecutorSync extends ArangoExecutor {
43+
44+
private static final Logger LOG = LoggerFactory.getLogger(ArangoExecutorSync.class);
3945

4046
private final CommunicationProtocol protocol;
4147

@@ -73,6 +79,11 @@ public <T> T execute(
7379
final Response response = protocol.execute(request, hostHandle);
7480
T deserialize = responseDeserializer.deserialize(response);
7581

82+
if(deserialize instanceof MetaAware) {
83+
LOG.debug("Respone is MetaAware " + deserialize.getClass().getName());
84+
((MetaAware) deserialize).setMeta(response.getMeta());
85+
}
86+
7687
return deserialize;
7788

7889
} catch (final VPackException e) {

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected void loadProperties(final Properties properties) {
136136
user = loadUser(properties, user);
137137
password = loadPassword(properties, password);
138138
useSsl = loadUseSsl(properties, useSsl);
139-
httpCookieSpec = loadhttpCookieSpec(properties, useSsl);
139+
httpCookieSpec = loadhttpCookieSpec(properties, httpCookieSpec);
140140
chunksize = loadChunkSize(properties, chunksize);
141141
maxConnections = loadMaxConnections(properties, maxConnections);
142142
connectionTtl = loadConnectionTtl(properties, connectionTtl);
@@ -203,7 +203,7 @@ protected void setSerializer(final ArangoSerialization serializer) {
203203

204204
protected HostResolver createHostResolver(final Collection<Host> hosts, final int maxConnections,final ConnectionFactory connectionFactory) {
205205

206-
if(acquireHostList) {
206+
if(acquireHostList != null && acquireHostList) {
207207
LOG.debug("acquireHostList -> Use ExtendedHostResolver");
208208
return new ExtendedHostResolver(new ArrayList<Host>(hosts), maxConnections, connectionFactory, acquireHostListInterval);
209209
} else {
@@ -288,7 +288,7 @@ private static Boolean loadUseSsl(final Properties properties, final Boolean cur
288288
getProperty(properties, PROPERTY_KEY_USE_SSL, currentValue, ArangoDefaults.DEFAULT_USE_SSL));
289289
}
290290

291-
private static String loadhttpCookieSpec(final Properties properties, final Boolean currentValue) {
291+
private static String loadhttpCookieSpec(final Properties properties, final String currentValue) {
292292
return getProperty(properties, PROPERTY_KEY_COOKIE_SPEC, currentValue, "");
293293
}
294294

@@ -330,8 +330,16 @@ protected static <T> String getProperty(
330330
final String key,
331331
final T currentValue,
332332
final T defaultValue) {
333-
return properties.getProperty(key,
334-
currentValue != null ? currentValue.toString() : defaultValue != null ? defaultValue.toString() : null);
333+
334+
String overrideDefaultValue = null;
335+
336+
if(currentValue != null) {
337+
overrideDefaultValue = currentValue.toString();
338+
} else if(defaultValue != null) {
339+
overrideDefaultValue = defaultValue.toString();
340+
}
341+
342+
return properties.getProperty(key, overrideDefaultValue);
335343
}
336344

337345
protected <C extends Connection> Collection<Host> createHostList(

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,36 @@ protected Request queryRequest(
208208
return request;
209209
}
210210

211-
protected Request queryNextRequest(final String id, final AqlQueryOptions options) {
211+
protected Request queryNextRequest(final String id, final AqlQueryOptions options, Map<String, String> meta) {
212+
212213
final Request request = request(name, RequestType.PUT, PATH_API_CURSOR, id);
214+
215+
if(meta != null) {
216+
request.getHeaderParam().putAll(meta);
217+
}
218+
213219
final AqlQueryOptions opt = options != null ? options : new AqlQueryOptions();
220+
214221
if (opt.getAllowDirtyRead() == Boolean.TRUE) {
215222
RequestUtils.allowDirtyRead(request);
216223
}
217224
return request;
218225
}
219226

220-
protected Request queryCloseRequest(final String id, final AqlQueryOptions options) {
227+
protected Request queryCloseRequest(final String id, final AqlQueryOptions options, Map<String, String> meta) {
228+
221229
final Request request = request(name, RequestType.DELETE, PATH_API_CURSOR, id);
230+
231+
if(meta != null) {
232+
request.getHeaderParam().putAll(meta);
233+
}
234+
222235
final AqlQueryOptions opt = options != null ? options : new AqlQueryOptions();
236+
223237
if (opt.getAllowDirtyRead() == Boolean.TRUE) {
224238
RequestUtils.allowDirtyRead(request);
225239
}
240+
226241
return request;
227242
}
228243

src/main/java/com/arangodb/internal/cursor/ArangoCursorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public boolean isCached() {
9898
@Override
9999
public void close() {
100100
if (id != null && hasNext()) {
101-
execute.close(id);
101+
execute.close(id, iterator.getResult().getMeta());
102102
}
103103
}
104104

src/main/java/com/arangodb/internal/cursor/ArangoCursorIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean hasNext() {
6666
@Override
6767
public T next() {
6868
if (pos >= result.getResult().size() && result.getHasMore()) {
69-
result = execute.next(cursor.getId());
69+
result = execute.next(cursor.getId(), result.getMeta());
7070
pos = 0;
7171
}
7272
if (!hasNext()) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.runners.Parameterized;
3737

3838
import com.arangodb.ArangoDB.Builder;
39+
import com.arangodb.model.AqlQueryOptions;
3940
import com.arangodb.velocypack.VPackSlice;
4041

4142
/**
@@ -57,6 +58,17 @@ public void first() {
5758
assertThat(first.isInteger(), is(true));
5859
assertThat(first.getAsLong(), is(0L));
5960
}
61+
62+
@Test
63+
public void next() {
64+
65+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", new AqlQueryOptions().batchSize(5), VPackSlice.class);
66+
67+
while(cursor.hasNext()) {
68+
cursor.next();
69+
}
70+
71+
}
6072

6173
@Test
6274
public void mapFilterCount() {

0 commit comments

Comments
 (0)