public class MongoIteratorImpl extends Object implements MongoIterator<Document>
BATCH_SIZE_FIELD, CURSOR_ID_FIELD, LIMIT_FIELD, NAME_SPACE_FIELD, SERVER_FIELD
Constructor and Description |
---|
MongoIteratorImpl(CursorableMessage originalQuery,
Client client,
String server,
Reply reply)
Create a new MongoDBInterator.
|
MongoIteratorImpl(Document cursorDocument,
Client client)
Create a new MongoIteratorImpl from a cursor document.
|
Modifier and Type | Method and Description |
---|---|
Document |
asDocument()
Returns a
Document that can be used to restart the
cursor/iterator. |
void |
close()
Close the iterator and release any resources it is holding.
|
int |
getBatchSize()
Returns the size for batches of documents that are requested.
|
protected Client |
getClient()
Returns the client value.
|
protected String |
getCollectionName()
Returns the collection name.
|
protected long |
getCursorId()
Returns the cursor Id value.
|
protected String |
getDatabaseName()
Returns the database name value.
|
protected int |
getLimit()
Returns the limit value.
|
ReadPreference |
getReadPerference()
Returns the iterator's read preference which points to the original
server performing the query.
|
boolean |
hasNext() |
Iterator<Document> |
iterator() |
protected void |
loadDocuments()
Loads more documents into the iterator.
|
protected List<Document> |
loadDocuments(boolean blockForTailable)
Loads more documents into the iterator.
|
Document |
next() |
int |
nextBatchSize()
Computes the size for the next batch of documents to get.
|
void |
remove() |
void |
restart()
Restarts the iterator by sending a request for more documents.
|
protected long |
retreiveCursorIdFromPendingRequest(long cursorId,
Future<Reply> replyFuture)
If the current cursor id is zero then waits for the response from the
pending request to determine the real cursor id.
|
protected void |
sendRequest()
Sends a request for more documents.
|
void |
setBatchSize(int batchSize)
Sets the size for future batch sizes.
|
void |
stop()
Stops the iterator after consuming any received and/or requested batches.
|
Object[] |
toArray()
Consumes all of the elements in the iterator and returns them in a single
array.
|
<S> S[] |
toArray(S[] to)
Consumes all of the elements in the iterator and returns them in a single
array.
|
List<Document> |
toList()
Consumes all of the elements in the iterator and returns them in a single
mutable list.
|
public MongoIteratorImpl(CursorableMessage originalQuery, Client client, String server, Reply reply)
originalQuery
- The original query being iterated over.client
- The client for issuing more requests.server
- The server that received the original query request.reply
- The initial results of the query that are available.public MongoIteratorImpl(Document cursorDocument, Client client)
client
- The client interface to the server.cursorDocument
- The original query.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 active cursor in the defined format.
asDocument
in interface MongoCursorControl
null
if the server's cursor has been exhausted or
closed.ClientImpl.isCursorDocument(Document)
public void close()
Overridden to close the iterator and send a KillCursors
for the
open cursor, if any.
close
in interface ClosableIterator<Document>
close
in interface MongoCursorControl
close
in interface Closeable
close
in interface AutoCloseable
public int getBatchSize()
Overridden to get the batch size from the original query or set explicitly.
getBatchSize
in interface ClosableIterator<Document>
getBatchSize
in interface MongoCursorControl
public ReadPreference getReadPerference()
public boolean hasNext()
Overridden to return true if there are more documents.
public Document next()
Overridden to return the next document from the query.
next
in interface Iterator<Document>
Iterator.next()
public int nextBatchSize()
public void remove()
Overridden to throw and UnsupportedOperationException
.
remove
in interface Iterator<Document>
Iterator.remove()
public void restart() throws MongoDbException
MongoDbException
- On a failure to send the request for more document.public void setBatchSize(int batchSize)
Overridden to set the batch size.
setBatchSize
in interface ClosableIterator<Document>
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. Users
should persist the state of the cursor as returned from
asDocument()
and restart the cursor using one of the
MongoClient.restart(com.allanbank.mongodb.bson.DocumentAssignable)
or
MongoClient.restart(com.allanbank.mongodb.StreamCallback, com.allanbank.mongodb.bson.DocumentAssignable)
methods. Use with extreme caution.
The iterator will naturally stop (hasNext()
will return false)
when the current batch and any already requested batches are finished.
stop
in interface MongoCursorControl
public Object[] toArray()
WARNING: This method loads all of the iterator results into memory and
may cause an OutOfMemoryError
.
Overridden to return the remaining elements as a array.
toArray
in interface MongoIterator<Document>
public <S> S[] toArray(S[] to)
WARNING: This method loads all of the iterator results into memory and
may cause an OutOfMemoryError
.
Overridden to return the remaining elements as a array.
toArray
in interface MongoIterator<Document>
S
- The type of elements in the array.to
- The array to copy into. If not the right size a new array will
be allocated of the right size.public List<Document> toList()
WARNING: This method loads all of the iterator results into memory and
may cause an OutOfMemoryError
.
Overridden to return the remaining elements as a list.
toList
in interface MongoIterator<Document>
protected Client getClient()
protected String getCollectionName()
protected long getCursorId()
protected String getDatabaseName()
protected int getLimit()
protected void loadDocuments() throws RuntimeException
RuntimeException
- On a failure to load documents.protected List<Document> loadDocuments(boolean blockForTailable) throws RuntimeException
blockForTailable
- If true then the method will recursively call itself on a
tailable cursor with no results. This makes the call blocking.
It false then the call will not block. This is used by the
method to ensure that the outermost load blocks but the
recursion is not inifinite.RuntimeException
- On a failure to load documents.protected long retreiveCursorIdFromPendingRequest(long cursorId, Future<Reply> replyFuture)
cursorId
- The presumed cursor id.replyFuture
- The pending reply's future.protected void sendRequest() throws MongoDbException
MongoDbException
- On a failure to send the request for more document.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.