Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created August 10, 2017 21:25
Show Gist options
  • Select an option

  • Save jyemin/348daf84ef4f87e8bde0251d52e62486 to your computer and use it in GitHub Desktop.

Select an option

Save jyemin/348daf84ef4f87e8bde0251d52e62486 to your computer and use it in GitHub Desktop.
Two implementations of an Iterator of raw BSON documents
// 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