public final class CursorStreamingCallback extends AbstractValidatingReplyCallback implements MongoCursorControl, AddressAware
CursorableMessage
Reply
into a series
of callback for each document received.ERROR_CODE_FIELD, ERROR_MESSAGE_FIELDS
BATCH_SIZE_FIELD, CURSOR_ID_FIELD, LIMIT_FIELD, NAME_SPACE_FIELD, SERVER_FIELD
Constructor and Description |
---|
CursorStreamingCallback(Client client,
CursorableMessage originalMessage,
boolean command,
StreamCallback<Document> results)
Create a new CursorCallback.
|
CursorStreamingCallback(Client client,
Document cursorDocument,
StreamCallback<Document> results)
Create a new CursorCallback from a cursor document.
|
Modifier and Type | Method and Description |
---|---|
Document |
asDocument()
Returns a
Document that can be used to restart the
cursor/iterator. |
protected MongoDbException |
asError(Reply reply,
int okValue,
int errorNumber,
String errorMessage)
Creates an exception from the parsed reply fields.
|
void |
close()
Overridden to close the iterator and send a
KillCursors for the
open cursor, if any. |
void |
exception(Throwable thrown)
Called when the operation fails due to an exception.
|
String |
getAddress()
Returns the server the original request was sent to.
|
int |
getBatchSize()
Returns the size for batches of documents that are requested.
|
Client |
getClient()
Returns the client value.
|
String |
getCollectionName()
Returns the collection name.
|
long |
getCursorId()
Returns the cursor Id value.
|
String |
getDatabaseName()
Returns the database name value.
|
int |
getLimit()
Returns the limit value.
|
protected void |
handle(Reply reply)
Called once the
Reply has been validated. |
protected boolean |
isCommand()
Returns true if the callback should expect a command formated cursor
reply.
|
boolean |
isLightWeight()
Returns true if the callback is lightweight and can be safely performed
in the receive thread.
|
protected List<Document> |
loadDocuments(Reply reply)
Loads more documents.
|
protected int |
nextBatchSize()
Computes the size for the next batch of documents to get.
|
void |
restart()
Restarts the stream by sending a request for the next batch of documents.
|
protected void |
sendKill()
Sends a
KillCursors message if there is an active cursor. |
protected void |
sendRequest()
Sends a request to start the next match of documents.
|
void |
setAddress(String address)
Sets the value of the server the original request was sent to.
|
void |
setBatchSize(int batchSize)
Sets the size for future batch sizes.
|
void |
stop()
Stops the iterator after consuming any received and/or requested batches.
|
asError, asError, asError, asString, callback, checkForError, toInt, verify
public CursorStreamingCallback(Client client, CursorableMessage originalMessage, boolean command, StreamCallback<Document> results)
client
- The client interface to the server.originalMessage
- The original message.command
- If true then the callback should expect a command formated
cursor reply.results
- The callback to update with each document.public CursorStreamingCallback(Client client, Document cursorDocument, StreamCallback<Document> results)
client
- The client interface to the server.cursorDocument
- The original query.results
- The callback to update with each document.MongoIteratorImpl.asDocument()
public Document asDocument()
Document
that can be used to restart the
cursor/iterator.
If this iterator is exhausted or closed then the cursor is also closed on the server and this method will return null.
If the cursor/Find
was not created with out a timeout then
eventually the server will automatically remove the cursor and the
restart will fail.
Returns the active cursor in the form:
{ "ns" : '<database_name>.$lt;collection_name>', "cursor_id" : <cursor_id>, "server" : '<server>', "limit" : <remaining_limit> "batch_size" : <batch_size> }
Overridden to return the current state of the stream as a document.
asDocument
in interface MongoCursorControl
null
if the server's cursor has been exhausted or
closed.public void close()
KillCursors
for the
open cursor, if any.close
in interface MongoCursorControl
close
in interface Closeable
close
in interface AutoCloseable
public void exception(Throwable thrown)
Overridden to forward the error the the user.
exception
in interface Callback<Reply>
exception
in class AbstractValidatingReplyCallback
thrown
- The thrown exception.public String getAddress()
public int getBatchSize()
Overridden to set the batch size.
getBatchSize
in interface MongoCursorControl
public Client getClient()
public String getCollectionName()
public long getCursorId()
public String getDatabaseName()
public int getLimit()
public boolean isLightWeight()
Overridden to return false.
isLightWeight
in interface ReplyCallback
public void restart()
MongoDbException
- On a failure to send the request for more document.public void setAddress(String address)
setAddress
in interface AddressAware
address
- The new value for the server the original request was sent to.public void setBatchSize(int batchSize)
Overridden to get the batch size.
setBatchSize
in interface MongoCursorControl
batchSize
- The size to request for future batch sizes.public void stop()
WARNING: This will leave the cursor open on the server. Even a
MongoCursorControl.close()
on this object will not close the cursor on the server.
Users should persist the state of the cursor as returned from
MongoCursorControl.asDocument()
and restart the cursor using one of the
MongoClient.restart(com.allanbank.mongodb.bson.DocumentAssignable)
or
MongoClient.restart(StreamCallback, com.allanbank.mongodb.bson.DocumentAssignable)
methods. Use MongoCursorControl.stop()
with extreme caution.
The iterator or stream will naturally stop (
Iterator.hasNext()
will return false or the stream's call
back StreamCallback.done()
method will be called) when the
current batch and any batches already requested are exhausted.
Overridden to stop requesting more batches of documents.
stop
in interface MongoCursorControl
protected MongoDbException asError(Reply reply, int okValue, int errorNumber, String errorMessage)
Overridden to add the Query
to the exception.
asError
in class AbstractValidatingReplyCallback
reply
- The raw reply.okValue
- The 'ok' field.errorNumber
- The 'errno' field.errorMessage
- The 'errmsg' field.AbstractValidatingReplyCallback.asError(Reply, int, int, String)
protected void handle(Reply reply) throws MongoDbException
Reply
has been validated.
Overridden to push the documents to the application's callback.
handle
in class AbstractValidatingReplyCallback
reply
- The Reply
to be handled.MongoDbException
AbstractReplyCallback.convert(Reply)
protected boolean isCommand()
protected List<Document> loadDocuments(Reply reply) throws RuntimeException
reply
- The last reply received.RuntimeException
- On a failure to load documents.protected int nextBatchSize()
protected void sendKill() throws MongoDbException
KillCursors
message if there is an active cursor.MongoDbException
- On a failure to send the KillCursors
message.protected void sendRequest() throws MongoDbException
MongoDbException
- On a failure to send the request.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.