Skip to content

Commit c6283a6

Browse files
committed
CSHARP-1224: Modified the readers and writers as necessary so that they can be used to read or write multiple documents.
1 parent b8fa239 commit c6283a6

File tree

9 files changed

+201
-59
lines changed

9 files changed

+201
-59
lines changed

src/MongoDB.Bson.Tests/IO/BsonBinaryReaderTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,44 @@
2020
using MongoDB.Bson;
2121
using MongoDB.Bson.IO;
2222
using MongoDB.Bson.Serialization;
23+
using FluentAssertions;
2324
using NUnit.Framework;
2425

2526
namespace MongoDB.Bson.Tests.IO
2627
{
2728
[TestFixture]
2829
public class BsonBinaryReaderTests
2930
{
31+
[Test]
32+
public void BsonBinaryReader_should_support_reading_multiple_documents(
33+
[Range(0, 3)]
34+
int numberOfDocuments)
35+
{
36+
var document = new BsonDocument("x", 1);
37+
var bson = document.ToBson();
38+
var input = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
39+
var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
40+
41+
using (var stream = new MemoryStream(input))
42+
using (var binaryReader = new BsonBinaryReader(stream))
43+
{
44+
var result = new List<BsonDocument>();
45+
46+
while (!binaryReader.IsAtEndOfFile())
47+
{
48+
binaryReader.ReadStartDocument();
49+
var name = binaryReader.ReadName();
50+
var value = binaryReader.ReadInt32();
51+
binaryReader.ReadEndDocument();
52+
53+
var resultDocument = new BsonDocument(name, value);
54+
result.Add(resultDocument);
55+
}
56+
57+
result.Should().Equal(expectedResult);
58+
}
59+
}
60+
3061
[Test]
3162
public void TestHelloWorld()
3263
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.IO;
17+
using System.Linq;
18+
using FluentAssertions;
19+
using MongoDB.Bson.IO;
20+
using NUnit.Framework;
21+
22+
namespace MongoDB.Bson.Tests.IO
23+
{
24+
[TestFixture]
25+
public class BsonBinaryWriterTests
26+
{
27+
[Test]
28+
public void BsonBinaryWriter_should_support_writing_multiple_documents(
29+
[Range(0, 3)]
30+
int numberOfDocuments)
31+
{
32+
var document = new BsonDocument("x", 1);
33+
var bson = document.ToBson();
34+
var expectedResult = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
35+
36+
using (var stream = new MemoryStream())
37+
using (var binaryWriter = new BsonBinaryWriter(stream))
38+
{
39+
for (var n = 0; n < numberOfDocuments; n++)
40+
{
41+
binaryWriter.WriteStartDocument();
42+
binaryWriter.WriteName("x");
43+
binaryWriter.WriteInt32(1);
44+
binaryWriter.WriteEndDocument();
45+
}
46+
47+
var result = stream.ToArray();
48+
result.Should().Equal(expectedResult);
49+
}
50+
}
51+
}
52+
}

src/MongoDB.Bson.Tests/IO/JsonReaderTests.cs

Lines changed: 78 additions & 46 deletions
Large diffs are not rendered by default.

src/MongoDB.Bson.Tests/IO/JsonWriterTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using MongoDB.Bson;
2121
using MongoDB.Bson.IO;
2222
using MongoDB.Bson.Serialization;
23+
using FluentAssertions;
2324
using NUnit.Framework;
2425

2526
namespace MongoDB.Bson.Tests.IO
@@ -38,6 +39,31 @@ public TestData(T value, string expected)
3839
}
3940
}
4041

42+
[Test]
43+
public void JsonWriter_should_support_writing_multiple_documents(
44+
[Range(0, 3)]
45+
int numberOfDocuments)
46+
{
47+
var document = new BsonDocument("x", 1);
48+
var json = document.ToJson();
49+
var expectedResult = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j);
50+
51+
using (var stringWriter = new StringWriter())
52+
using (var jsonWriter = new JsonWriter(stringWriter))
53+
{
54+
for (var n = 0; n < numberOfDocuments; n++)
55+
{
56+
jsonWriter.WriteStartDocument();
57+
jsonWriter.WriteName("x");
58+
jsonWriter.WriteInt32(1);
59+
jsonWriter.WriteEndDocument();
60+
}
61+
62+
var result = stringWriter.ToString();
63+
result.Should().Be(expectedResult);
64+
}
65+
}
66+
4167
[Test]
4268
public void TestEmptyDocument()
4369
{

src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="BsonExtensionMethodsTests.cs" />
8484
<Compile Include="BsonUtilsTests.cs" />
8585
<Compile Include="IO\ArrayElementNameAcceleratorTests.cs" />
86+
<Compile Include="IO\BsonBinaryWriterTests.cs" />
8687
<Compile Include="IO\BsonChunkPoolTests.cs" />
8788
<Compile Include="IO\BsonStreamAdapterTests.cs" />
8889
<Compile Include="IO\BsonStreamExtensionsTests.cs" />

src/MongoDB.Bson/IO/BsonBinaryReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public override bool ReadBoolean()
188188
public override BsonType ReadBsonType()
189189
{
190190
if (Disposed) { ThrowObjectDisposedException(); }
191-
if (State == BsonReaderState.Initial || State == BsonReaderState.Done || State == BsonReaderState.ScopeDocument)
191+
if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
192192
{
193193
// there is an implied type of Document for the top level and for scope documents
194194
CurrentBsonType = BsonType.Document;
@@ -318,7 +318,7 @@ public override void ReadEndArray()
318318
{
319319
case ContextType.Array: State = BsonReaderState.Type; break;
320320
case ContextType.Document: State = BsonReaderState.Type; break;
321-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
321+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
322322
default: throw new BsonInternalException("Unexpected ContextType.");
323323
}
324324
}
@@ -351,7 +351,7 @@ public override void ReadEndDocument()
351351
{
352352
case ContextType.Array: State = BsonReaderState.Type; break;
353353
case ContextType.Document: State = BsonReaderState.Type; break;
354-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
354+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
355355
default: throw new BsonInternalException("Unexpected ContextType.");
356356
}
357357
}
@@ -497,7 +497,7 @@ public override IByteBuffer ReadRawBsonArray()
497497
{
498498
case ContextType.Array: State = BsonReaderState.Type; break;
499499
case ContextType.Document: State = BsonReaderState.Type; break;
500-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
500+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
501501
default: throw new BsonInternalException("Unexpected ContextType.");
502502
}
503503

@@ -525,7 +525,7 @@ public override IByteBuffer ReadRawBsonDocument()
525525
{
526526
case ContextType.Array: State = BsonReaderState.Type; break;
527527
case ContextType.Document: State = BsonReaderState.Type; break;
528-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
528+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
529529
default: throw new BsonInternalException("Unexpected ContextType.");
530530
}
531531

@@ -719,7 +719,7 @@ private BsonReaderState GetNextState()
719719
case ContextType.ScopeDocument:
720720
return BsonReaderState.Type;
721721
case ContextType.TopLevel:
722-
return BsonReaderState.Done;
722+
return BsonReaderState.Initial;
723723
default:
724724
throw new BsonInternalException("Unexpected ContextType.");
725725
}

src/MongoDB.Bson/IO/BsonReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void Dispose()
124124
/// <returns>The current BsonType.</returns>
125125
public BsonType GetCurrentBsonType()
126126
{
127-
if (_state == BsonReaderState.Initial || _state == BsonReaderState.Done || _state == BsonReaderState.ScopeDocument || _state == BsonReaderState.Type)
127+
if (_state == BsonReaderState.Initial || _state == BsonReaderState.ScopeDocument || _state == BsonReaderState.Type)
128128
{
129129
ReadBsonType();
130130
}

src/MongoDB.Bson/IO/JsonReader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public override bool ReadBoolean()
151151
public override BsonType ReadBsonType()
152152
{
153153
if (Disposed) { ThrowObjectDisposedException(); }
154-
if (State == BsonReaderState.Initial || State == BsonReaderState.Done || State == BsonReaderState.ScopeDocument)
154+
if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
155155
{
156-
if (State == BsonReaderState.Initial || State == BsonReaderState.Done)
156+
if (State == BsonReaderState.Initial)
157157
{
158158
_buffer.ResetBuffer();
159159
}
@@ -431,7 +431,7 @@ public override void ReadEndArray()
431431
{
432432
case ContextType.Array: State = BsonReaderState.Type; break;
433433
case ContextType.Document: State = BsonReaderState.Type; break;
434-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
434+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
435435
default: throw new BsonInternalException("Unexpected ContextType.");
436436
}
437437

@@ -474,7 +474,7 @@ public override void ReadEndDocument()
474474
{
475475
case ContextType.Array: State = BsonReaderState.Type; break;
476476
case ContextType.Document: State = BsonReaderState.Type; break;
477-
case ContextType.TopLevel: State = BsonReaderState.Done; break;
477+
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
478478
default: throw new BsonInternalException("Unexpected ContextType");
479479
}
480480

@@ -867,7 +867,7 @@ private BsonReaderState GetNextState()
867867
case ContextType.Document:
868868
return BsonReaderState.Type;
869869
case ContextType.TopLevel:
870-
return BsonReaderState.Done;
870+
return BsonReaderState.Initial;
871871
default:
872872
throw new BsonInternalException("Unexpected ContextType.");
873873
}

src/MongoDB.Bson/IO/JsonWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ private string EscapedString(string value)
717717

718718
private BsonWriterState GetNextState()
719719
{
720-
if (_context.ContextType == ContextType.Array)
720+
if (_context.ContextType == ContextType.Array || _context.ContextType == ContextType.TopLevel)
721721
{
722722
return BsonWriterState.Value;
723723
}

0 commit comments

Comments
 (0)