Created
August 10, 2017 21:25
-
-
Save jyemin/348daf84ef4f87e8bde0251d52e62486 to your computer and use it in GitHub Desktop.
Two implementations of an Iterator of raw BSON documents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This implemenation iterates over a document sequence as returned by OP_QUERY or by OP_MSG in the future | |
| class RawBsonDocumentSequenceIterator implements Iterator<RawBsonDocument> { | |
| private final ByteBuffer documentSequence; | |
| public RawBsonDocumentSequenceIterator(final ByteBuffer documentSequence) { | |
| this.documentSequence = documentSequence; | |
| } | |
| @Override | |
| public boolean hasNext() { | |
| return documentSequence.hasRemaining(); | |
| } | |
| @Override | |
| public RawBsonDocument next() { | |
| ByteBuffer slice = documentSequence.slice(); | |
| slice.order(ByteOrder.LITTLE_ENDIAN); | |
| int size = slice.getInt(0); | |
| slice.limit(size); | |
| return new RawBsonDocument(slice); | |
| } | |
| } | |
| // This implementation iterates over a raw BSON array of documents. The RawBsonArray takes care of skipping over the document keys | |
| class RawBsonArrayIterator implements Iterator<RawBsonDocument> { | |
| final Iterator<BsonValue> wrapped; | |
| public RawBsonArrayIterator(final RawBsonArray rawBsonArray) { | |
| wrapped = rawBsonArray.iterator(); | |
| } | |
| @Override | |
| public boolean hasNext() { | |
| return wrapped.hasNext(); | |
| } | |
| @Override | |
| public RawBsonDocument next() { | |
| return (RawBsonDocument) wrapped.next(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment