arangodb / arangodb-java-driver

The official ArangoDB Java driver.
Apache License 2.0
201 stars 94 forks source link

why db.query return Cursor, if it's ok to return Collection? #74

Closed timzaak closed 8 years ago

timzaak commented 8 years ago

I find it will send a request to db if I call the cursor.next() method, if it possible to return all the result as Collection(List), or make it configurable to decide which one to return?

christian-lechner commented 8 years ago

The cursor or iterator can fetch new data from the server if needed and discard already consumed data. While a list holds all entries in memory, you can run out of memory when using a list, especially with large result sets.

mvollmary commented 8 years ago

What Christian said is exactly what the cursor is good for. But if you still need a List you can call cursor.asListRemaining() to get all remaining result from you query in a List. This method simple iterates over the cursor and fills a new List.

timzaak commented 8 years ago

yes, when the result sets is big, the memory will become a problem. but sometimes, people will use limit( just like paginate) to make the result size small enough. I think it may improve the efficiency when sending the small result to the client in one response.

mvollmary commented 8 years ago

This already happen. There is a batchSize which controls how many documents are send in one response. The default in the server is higher then 1 :-).

You can also set it in the driver call for a single query:

ArangoCursor<BaseDocument> cursor = arangoDB.db().query("your query", null, new AqlQueryOptions().batchSize(100000), BaseDocument.class);
List<BaseDocument> resultAsList = cursor.asListRemaining();
timzaak commented 8 years ago

@mpv1989 thanks very match.