Skip to content

Commit 332ffc7

Browse files
committed
Connection command updates
Legacy connection command methods removed Write operations are now responsible for batch splitting. OP_MSG payload support added JAVA-2584
1 parent 52d0c81 commit 332ffc7

File tree

87 files changed

+2795
-4186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2795
-4186
lines changed

bson/src/main/org/bson/AbstractBsonWriter.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.Closeable;
2323
import java.util.Arrays;
24-
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Stack;
2726

@@ -753,52 +752,36 @@ public void close() {
753752
@Override
754753
public void pipe(final BsonReader reader) {
755754
notNull("reader", reader);
756-
pipeDocument(reader, null);
757-
}
758-
759-
@Override
760-
public void pipe(final BsonReader reader, final List<BsonElement> extraElements) {
761-
notNull("reader", reader);
762-
notNull("extraElements", extraElements);
763-
pipeDocument(reader, extraElements);
764-
}
765-
766-
/**
767-
* Pipe a list of extra element to this writer
768-
*
769-
* @param extraElements the extra elements
770-
*/
771-
protected void pipeExtraElements(final List<BsonElement> extraElements) {
772-
notNull("extraElements", extraElements);
773-
for (BsonElement cur : extraElements) {
774-
writeName(cur.getName());
775-
pipeValue(cur.getValue());
755+
reader.readStartDocument();
756+
writeStartDocument();
757+
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
758+
writeName(reader.readName());
759+
pipeValue(reader);
776760
}
761+
reader.readEndDocument();
762+
writeEndDocument();
777763
}
778764

779-
private void pipeDocument(final BsonReader reader, final List<BsonElement> extraElements) {
765+
private void pipeDocument(final BsonReader reader) {
780766
reader.readStartDocument();
781767
writeStartDocument();
782768
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
783769
writeName(reader.readName());
784770
pipeValue(reader);
785771
}
786772
reader.readEndDocument();
787-
if (extraElements != null) {
788-
pipeExtraElements(extraElements);
789-
}
790773
writeEndDocument();
791774
}
792775

793776
private void pipeJavascriptWithScope(final BsonReader reader) {
794777
writeJavaScriptWithScope(reader.readJavaScriptWithScope());
795-
pipeDocument(reader, null);
778+
pipeDocument(reader);
796779
}
797780

798781
private void pipeValue(final BsonReader reader) {
799782
switch (reader.getCurrentBsonType()) {
800783
case DOCUMENT:
801-
pipeDocument(reader, null);
784+
pipeDocument(reader);
802785
break;
803786
case ARRAY:
804787
pipeArray(reader);

bson/src/main/org/bson/BsonBinaryWriter.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.bson.types.Decimal128;
2222
import org.bson.types.ObjectId;
2323

24-
import java.util.List;
2524
import java.util.Stack;
2625

2726
import static java.lang.String.format;
@@ -100,6 +99,14 @@ public BsonOutput getBsonOutput() {
10099
return bsonOutput;
101100
}
102101

102+
/**
103+
* @return the BsonBinaryWriterSettings
104+
* @since 3.6
105+
*/
106+
public BsonBinaryWriterSettings getBinaryWriterSettings() {
107+
return binaryWriterSettings;
108+
}
109+
103110
@Override
104111
public void flush() {
105112
}
@@ -296,17 +303,6 @@ public void doWriteUndefined() {
296303
@Override
297304
public void pipe(final BsonReader reader) {
298305
notNull("reader", reader);
299-
pipeDocument(reader, null);
300-
}
301-
302-
@Override
303-
public void pipe(final BsonReader reader, final List<BsonElement> extraElements) {
304-
notNull("reader", reader);
305-
notNull("extraElements", extraElements);
306-
pipeDocument(reader, extraElements);
307-
}
308-
309-
private void pipeDocument(final BsonReader reader, final List<BsonElement> extraElements) {
310306
if (reader instanceof BsonBinaryReader) {
311307
BsonBinaryReader binaryReader = (BsonBinaryReader) reader;
312308
if (getState() == State.VALUE) {
@@ -326,16 +322,6 @@ private void pipeDocument(final BsonReader reader, final List<BsonElement> extra
326322

327323
binaryReader.setState(AbstractBsonReader.State.TYPE);
328324

329-
if (extraElements != null) {
330-
bsonOutput.truncateToPosition(bsonOutput.getPosition() - 1);
331-
setContext(new Context(getContext(), BsonContextType.DOCUMENT, pipedDocumentStartPosition));
332-
setState(State.NAME);
333-
pipeExtraElements(extraElements);
334-
bsonOutput.writeByte(0);
335-
bsonOutput.writeInt32(pipedDocumentStartPosition, bsonOutput.getPosition() - pipedDocumentStartPosition);
336-
setContext(getContext().getParentContext());
337-
}
338-
339325
if (getContext() == null) {
340326
setState(State.DONE);
341327
} else {
@@ -345,8 +331,8 @@ private void pipeDocument(final BsonReader reader, final List<BsonElement> extra
345331
}
346332
setState(getNextState());
347333
}
348-
} else if (extraElements != null) {
349-
super.pipe(reader, extraElements);
334+
335+
validateSize(bsonOutput.getPosition() - pipedDocumentStartPosition);
350336
} else {
351337
super.pipe(reader);
352338
}
@@ -399,11 +385,15 @@ private void writeCurrentName() {
399385

400386
private void backpatchSize() {
401387
int size = bsonOutput.getPosition() - getContext().startPosition;
388+
validateSize(size);
389+
bsonOutput.writeInt32(bsonOutput.getPosition() - size, size);
390+
}
391+
392+
private void validateSize(final int size) {
402393
if (size > maxDocumentSizeStack.peek()) {
403394
throw new BsonSerializationException(format("Document size of %d is larger than maximum of %d.", size,
404395
maxDocumentSizeStack.peek()));
405396
}
406-
bsonOutput.writeInt32(bsonOutput.getPosition() - size, size);
407397
}
408398

409399
protected class Context extends AbstractBsonWriter.Context {

bson/src/main/org/bson/BsonWriter.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.bson.types.Decimal128;
2020
import org.bson.types.ObjectId;
2121

22-
import java.util.List;
23-
2422
/**
2523
* An interface for writing a logical BSON document using a push-oriented API.
2624
*
@@ -359,13 +357,4 @@ public interface BsonWriter {
359357
*/
360358
void pipe(BsonReader reader);
361359

362-
/**
363-
* Reads a single document from a BsonReader and writes it to this, appending the given extra elements to the end of
364-
* the document.
365-
*
366-
* @param reader The source.
367-
* @param extraElements The extra BSON elements to append
368-
* @since 3.6
369-
*/
370-
void pipe(BsonReader reader, List<BsonElement> extraElements);
371360
}

bson/src/test/unit/org/bson/BsonBinaryWriterTest.java

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import java.io.ByteArrayOutputStream;
2727
import java.io.IOException;
2828
import java.nio.ByteBuffer;
29-
import java.util.List;
3029

31-
import static java.util.Arrays.asList;
3230
import static org.hamcrest.CoreMatchers.is;
3331
import static org.junit.Assert.assertArrayEquals;
3432
import static org.junit.Assert.assertEquals;
@@ -635,98 +633,6 @@ public void testPipeDocumentIntoScopeDocument() {
635633
reader2.readEndDocument();
636634
}
637635

638-
@Test
639-
public void testPipeWithExtraElements() {
640-
writer.writeStartDocument();
641-
writer.writeBoolean("a", true);
642-
writer.writeString("$db", "test");
643-
writer.writeStartDocument("$readPreference");
644-
writer.writeString("mode", "primary");
645-
writer.writeEndDocument();
646-
writer.writeEndDocument();
647-
648-
byte[] bytes = buffer.toByteArray();
649-
650-
BasicOutputBuffer pipedBuffer = new BasicOutputBuffer();
651-
BsonBinaryWriter pipedWriter = new BsonBinaryWriter(new BsonWriterSettings(100),
652-
new BsonBinaryWriterSettings(1024), pipedBuffer);
653-
654-
pipedWriter.writeStartDocument();
655-
pipedWriter.writeBoolean("a", true);
656-
pipedWriter.writeEndDocument();
657-
658-
List<BsonElement> extraElements = asList(
659-
new BsonElement("$db", new BsonString("test")),
660-
new BsonElement("$readPreference", new BsonDocument("mode", new BsonString("primary")))
661-
);
662-
663-
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
664-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
665-
try {
666-
BsonBinaryReader reader =
667-
new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(pipedBuffer.toByteArray()))));
668-
try {
669-
newWriter.pipe(reader, extraElements);
670-
} finally {
671-
reader.close();
672-
}
673-
} finally {
674-
newWriter.close();
675-
}
676-
assertArrayEquals(bytes, newBuffer.toByteArray());
677-
}
678-
679-
@Test
680-
public void testPipeOfNestedDocumentWithExtraElements() {
681-
writer.writeStartDocument();
682-
writer.writeStartDocument("nested");
683-
684-
writer.writeBoolean("a", true);
685-
writer.writeString("$db", "test");
686-
writer.writeStartDocument("$readPreference");
687-
writer.writeString("mode", "primary");
688-
writer.writeEndDocument();
689-
writer.writeEndDocument();
690-
691-
writer.writeBoolean("b", true);
692-
writer.writeEndDocument();
693-
694-
byte[] bytes = buffer.toByteArray();
695-
696-
BasicOutputBuffer pipedBuffer = new BasicOutputBuffer();
697-
BsonBinaryWriter pipedWriter = new BsonBinaryWriter(new BsonWriterSettings(100),
698-
new BsonBinaryWriterSettings(1024), pipedBuffer);
699-
700-
pipedWriter.writeStartDocument();
701-
pipedWriter.writeBoolean("a", true);
702-
pipedWriter.writeEndDocument();
703-
704-
List<BsonElement> extraElements = asList(
705-
new BsonElement("$db", new BsonString("test")),
706-
new BsonElement("$readPreference", new BsonDocument("mode", new BsonString("primary")))
707-
);
708-
709-
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
710-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
711-
try {
712-
BsonBinaryReader reader =
713-
new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(pipedBuffer.toByteArray()))));
714-
try {
715-
newWriter.writeStartDocument();
716-
newWriter.writeName("nested");
717-
newWriter.pipe(reader, extraElements);
718-
newWriter.writeBoolean("b", true);
719-
newWriter.writeEndDocument();
720-
} finally {
721-
reader.close();
722-
}
723-
} finally {
724-
newWriter.close();
725-
}
726-
byte[] actualBytes = newBuffer.toByteArray();
727-
assertArrayEquals(bytes, actualBytes);
728-
}
729-
730636
@Test
731637
public void testPipeOfDocumentWithInvalidSize() {
732638
byte[] bytes = {4, 0, 0, 0}; // minimum document size is 5;

bson/src/test/unit/org/bson/BsonDocumentWriterSpecification.groovy

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,4 @@ class BsonDocumentWriterSpecification extends Specification {
4646
then:
4747
document == documentWithValuesOfEveryType()
4848
}
49-
50-
def 'should pipe all types with extra elements'() {
51-
given:
52-
def document = new BsonDocument()
53-
def reader = new BsonDocumentReader(new BsonDocument())
54-
def writer = new BsonDocumentWriter(document)
55-
56-
def extraElements = []
57-
for (def entry : documentWithValuesOfEveryType()) {
58-
extraElements.add(new BsonElement(entry.getKey(), entry.getValue()))
59-
}
60-
61-
when:
62-
writer.pipe(reader, extraElements)
63-
64-
then:
65-
document == documentWithValuesOfEveryType()
66-
}
67-
}
49+
}

config/findbugs-exclude.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
</Match>
101101

102102
<Match>
103-
<Class name="~com.mongodb.connection.ByteBufBsonDocument.*"/>
103+
<Class name="~com.mongodb.connection.AbstractByteBufBsonDocument.*"/>
104104
<Bug pattern="NP_BOOLEAN_RETURN_NULL"/>
105105
</Match>
106106

0 commit comments

Comments
 (0)