Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions bson/src/main/org/bson/BsonDocumentReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ protected Context getContext() {
protected class Mark extends AbstractBsonReader.Mark {
private final BsonValue currentValue;
private final Context context;
private final List<Integer> positions;

/**
* Construct an instance.
Expand All @@ -257,14 +258,15 @@ protected Mark() {
currentValue = BsonDocumentReader.this.currentValue;
context = BsonDocumentReader.this.getContext();
context.mark();
positions = context.getPositions();
}

@Override
public void reset() {
super.reset();
BsonDocumentReader.this.currentValue = currentValue;
BsonDocumentReader.this.setContext(context);
context.reset();
context.reset(positions);
}
}

Expand All @@ -288,12 +290,18 @@ protected void mark() {
marking = true;
}

/**
* Gets the position to reset to. Once marked, the iterator keeps every value it returns, so that the position stays valid.
*/
protected int getPosition() {
return curIndex;
}

/**
*
*/
protected void reset() {
curIndex = 0;
marking = false;
protected void reset(final int position) {
curIndex = position;
}


Expand All @@ -308,11 +316,7 @@ public T next() {
//TODO: check closed
if (curIndex < markIterator.size()) {
value = markIterator.get(curIndex);
if (marking) {
curIndex++;
} else {
markIterator.remove(0);
}
curIndex++;
} else {
value = baseIterator.next();
if (marking) {
Expand Down Expand Up @@ -395,17 +399,33 @@ protected void mark() {
* Reset the context.
*/
protected void reset() {
if (documentIterator != null) {
documentIterator.reset();
} else {
arrayIterator.reset();
}
getIterator().reset(0);

if (getParentContext() != null) {
((Context) getParentContext()).reset();
}
}

private List<Integer> getPositions() {
List<Integer> positions = new ArrayList<>();
for (Context context = this; context != null; context = (Context) context.getParentContext()) {
positions.add(context.getIterator().getPosition());
}
return positions;
}

private void reset(final List<Integer> positions) {
Context context = this;
for (int position : positions) {
context.getIterator().reset(position);
context = (Context) context.getParentContext();
}
}

private BsonDocumentMarkableIterator<?> getIterator() {
return documentIterator != null ? documentIterator : arrayIterator;
}

/**
* Gets the next value.
*
Expand Down
144 changes: 144 additions & 0 deletions bson/src/test/unit/org/bson/BsonReaderMarkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bson;

import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.io.BasicOutputBuffer;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.ByteBuffer;
import java.util.function.Function;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

// Each test runs against BsonBinaryReader too, so that BsonDocumentReader is held to the same behavior
class BsonReaderMarkTest {

private static Stream<Named<Function<BsonDocument, BsonReader>>> readers() {
return Stream.of(
Named.of("BsonDocumentReader", BsonDocumentReader::new),
Named.of("BsonBinaryReader", document -> new BsonBinaryReader(ByteBuffer.wrap(toBytes(document)))));
}

@ParameterizedTest
@MethodSource("readers")
@DisplayName("should reset to the mark that is reset when several marks are outstanding")
void shouldResetToTheMarkThatIsReset(final Function<BsonDocument, BsonReader> readerFactory) {
BsonReader reader = readerFactory.apply(BsonDocument.parse("{a: 1, b: 2, c: 3}"));
reader.readStartDocument();
BsonReaderMark firstMark = reader.getMark();
assertEquals("a", readInt32FieldName(reader));
BsonReaderMark secondMark = reader.getMark();
assertEquals("b", readInt32FieldName(reader));

secondMark.reset();
assertEquals("b", readInt32FieldName(reader));
assertEquals("c", readInt32FieldName(reader));

firstMark.reset();
assertEquals("a", readInt32FieldName(reader));
assertEquals("b", readInt32FieldName(reader));
}

@ParameterizedTest
@MethodSource("readers")
@DisplayName("should reset to the same mark more than once")
void shouldResetToTheSameMarkMoreThanOnce(final Function<BsonDocument, BsonReader> readerFactory) {
BsonReader reader = readerFactory.apply(BsonDocument.parse("{a: 1, b: 2, c: 3}"));
reader.readStartDocument();
BsonReaderMark mark = reader.getMark();
assertEquals("a", readInt32FieldName(reader));

mark.reset();
assertEquals("a", readInt32FieldName(reader));
assertEquals("b", readInt32FieldName(reader));

mark.reset();
assertEquals("a", readInt32FieldName(reader));
assertEquals("b", readInt32FieldName(reader));
assertEquals("c", readInt32FieldName(reader));
reader.readEndDocument();
}

@ParameterizedTest
@MethodSource("readers")
@DisplayName("should reset the enclosing documents and arrays to their state at the time of the mark")
void shouldResetNestedContexts(final Function<BsonDocument, BsonReader> readerFactory) {
BsonReader reader = readerFactory.apply(BsonDocument.parse("{a: {x: 1, y: 2, z: 3}, b: [4, 5, 6], c: 7}"));
reader.readStartDocument();
BsonReaderMark outerMark = reader.getMark();
reader.readName("a");
reader.readStartDocument();
assertEquals("x", readInt32FieldName(reader));
BsonReaderMark firstInnerMark = reader.getMark();
assertEquals("y", readInt32FieldName(reader));
BsonReaderMark secondInnerMark = reader.getMark();
assertEquals("z", readInt32FieldName(reader));
reader.readEndDocument();
reader.readName("b");
reader.readStartArray();
assertEquals(4, reader.readInt32());
BsonReaderMark arrayMark = reader.getMark();
assertEquals(5, reader.readInt32());
assertEquals(6, reader.readInt32());
reader.readEndArray();
assertEquals("c", readInt32FieldName(reader));

arrayMark.reset();
assertEquals(5, reader.readInt32());
assertEquals(6, reader.readInt32());
reader.readEndArray();
assertEquals("c", readInt32FieldName(reader));

secondInnerMark.reset();
assertEquals("z", readInt32FieldName(reader));
reader.readEndDocument();

firstInnerMark.reset();
assertEquals("y", readInt32FieldName(reader));
assertEquals("z", readInt32FieldName(reader));
reader.readEndDocument();
reader.readName("b");
reader.readStartArray();
assertEquals(4, reader.readInt32());

outerMark.reset();
reader.readName("a");
reader.readStartDocument();
assertEquals("x", readInt32FieldName(reader));
}

private static String readInt32FieldName(final BsonReader reader) {
assertEquals(BsonType.INT32, reader.readBsonType());
String name = reader.readName();
reader.readInt32();
return name;
}

private static byte[] toBytes(final BsonDocument document) {
BasicOutputBuffer buffer = new BasicOutputBuffer();
try (BsonBinaryWriter writer = new BsonBinaryWriter(buffer)) {
new BsonDocumentCodec().encode(writer, document, EncoderContext.builder().build());
}
return buffer.toByteArray();
}
}