go-driver
go-driver copied to clipboard
Query with Cursor ID
I am missing a method to set an existing cursor ID and get the next batch of results. I know cursor.ReadDocument() deals with the cursor automatically, but I'd like to be able to simply use it in a more manual way, closer to the REST API behavior. An example (and my current problem) would be to expose the cursor endpoint through my application to let a user to make queries but let him to iterate the cursor using just the ID (the cursor instance will be lost after returning the first batch of results).
I could use a connection to make my own request, but then I would lose all the driver features to apply context settings, parsing result, handling errors... as I cannot reuse many of the methods and structs used internally because they are unexported. And in fact there is no way to even get the cursor ID without using reflection.
Maybe I'm missing something and setting a cursor ID manually is possible, but I cannot see how.
In case this is a reasonable request, I could submit a PR with the changes. I have some ideas:
- The
Cursorinterface would need a newID() stringmethod to return the cursor ID. - Since using the context is not an option, I could add a function to make a query from a cursor. Something like
QueryCursor(ctx context.Context, cursorID string) (Cursor, error) - Another way could be to simply add the ability to create a new
Cursorfrom a cursor ID. But since getting the next batch from a cursor is done internally when callingReadDocument(), it may not be clear how to use it.
I don't know if any of these solutions make any sense to you, or even if you think it is a good idea to add this feature, but it would be great to know your opinion.
Hello @flusflas!
Fell free to open PR with proposed changes (with some tests).
You should also create one additional method for deleting the unprocessed cursors - right now we're handling that in our library for existing cursor objects: https://github.com/arangodb/go-driver/blob/028a51b103bb074011646b14084216a35f13d594/cursor_impl.go#L176-L207
As described in the doc (https://www.arangodb.com/docs/3.8/http/aql-query-cursor-accessing-cursors.html#delete-cursor):
The cursor will automatically be destroyed on the server when the client has retrieved all documents from it. The client can also explicitly destroy the cursor at any earlier time using an HTTP DELETE request. The cursor id must be included as part of the URL.
So according to above we should allow client to destroy the cursor in the case when he decides to not process the rest of the results
Also before you create a PR, please sign CLA - more info here: https://www.arangodb.com/community/
We have added a clear implementation of the Cursor batch read in V2: https://github.com/arangodb/go-driver/commit/97a72731
There are two methods: ReadNextBatch and RetryReadBatch to repeat the last call in case of failure.