Skip to content

Commit 57b9359

Browse files
committed
CSHARP-5348: Introduce Bson*Context.PushContext
1 parent c20e2bb commit 57b9359

13 files changed

+68
-20
lines changed

benchmarks/MongoDB.Driver.Benchmarks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This suite implements the benchmarks described in this [spec](https://github.com
1313
(e.g `dotnet run -c Release -- --driverBenchmarks --envVars MONGODB_URI:"ConnectionString"`)
1414

1515
You can also select the benchmarks to run directly on the command for running the benchmarks as such
16-
`dotnet run -c Release -- --driverBenchmarks --fitler "*BenchmarkClassName*"`. The benchmarks are also grouped into categories namely: BSONBench, WriteBench
16+
`dotnet run -c Release -- --driverBenchmarks --filter "*BenchmarkClassName*"`. The benchmarks are also grouped into categories namely: BSONBench, WriteBench
1717
ReadBench, ParallelBench, SingleBench, MultiBench and DriverBench. So if you wanted to only run the WriteBench benchmarks, you can do so
1818
as follows: `dotnet run -c Release -- --driverBenchmarks --anyCategories "WriteBench"`.
1919

src/MongoDB.Bson/IO/BsonBinaryReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public override string ReadJavaScriptWithScope()
432432

433433
var startPosition = _bsonStream.Position; // position of size field
434434
var size = ReadSize();
435-
_context = new BsonBinaryReaderContext(_context, ContextType.JavaScriptWithScope, startPosition, size);
435+
_context = _context.PushContext(ContextType.JavaScriptWithScope, startPosition, size);
436436
var code = _bsonStream.ReadString(Settings.Encoding);
437437

438438
State = BsonReaderState.ScopeDocument;
@@ -590,7 +590,7 @@ public override void ReadStartArray()
590590

591591
var startPosition = _bsonStream.Position; // position of size field
592592
var size = ReadSize();
593-
_context = new BsonBinaryReaderContext(_context, ContextType.Array, startPosition, size);
593+
_context = _context.PushContext(ContextType.Array, startPosition, size);
594594
State = BsonReaderState.Type;
595595
}
596596

@@ -605,7 +605,7 @@ public override void ReadStartDocument()
605605
var contextType = (State == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
606606
var startPosition = _bsonStream.Position; // position of size field
607607
var size = ReadSize();
608-
_context = new BsonBinaryReaderContext(_context, contextType, startPosition, size);
608+
_context = _context.PushContext(contextType, startPosition, size);
609609
State = BsonReaderState.Type;
610610
}
611611

src/MongoDB.Bson/IO/BsonBinaryReaderContext.cs

+5
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,10 @@ public BsonBinaryReaderContext PopContext(long position)
8383
}
8484
return _parentContext;
8585
}
86+
87+
internal BsonBinaryReaderContext PushContext(ContextType contextType, long startPosition, long size)
88+
{
89+
return new BsonBinaryReaderContext(this, contextType, startPosition, size);
90+
}
8691
}
8792
}

src/MongoDB.Bson/IO/BsonBinaryWriter.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public override void WriteJavaScriptWithScope(string code)
400400

401401
_bsonStream.WriteBsonType(BsonType.JavaScriptWithScope);
402402
WriteNameHelper();
403-
_context = new BsonBinaryWriterContext(_context, ContextType.JavaScriptWithScope, _bsonStream.Position);
403+
_context = _context.PushContext(ContextType.JavaScriptWithScope, _bsonStream.Position);
404404
_bsonStream.WriteInt32(0); // reserve space for size of JavaScript with scope value
405405
_bsonStream.WriteString(code, Settings.Encoding);
406406

@@ -564,7 +564,7 @@ public override void WriteStartArray()
564564
base.WriteStartArray();
565565
_bsonStream.WriteBsonType(BsonType.Array);
566566
WriteNameHelper();
567-
_context = new BsonBinaryWriterContext(_context, ContextType.Array, _bsonStream.Position);
567+
_context = _context.PushContext(ContextType.Array, _bsonStream.Position);
568568
_bsonStream.WriteInt32(0); // reserve space for size
569569

570570
State = BsonWriterState.Value;
@@ -588,7 +588,10 @@ public override void WriteStartDocument()
588588
WriteNameHelper();
589589
}
590590
var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
591-
_context = new BsonBinaryWriterContext(_context, contextType, _bsonStream.Position);
591+
if (_context == null)
592+
_context = new BsonBinaryWriterContext(null, contextType, _bsonStream.Position);
593+
else
594+
_context = _context.PushContext(contextType, _bsonStream.Position);
592595
_bsonStream.WriteInt32(0); // reserve space for size
593596

594597
State = BsonWriterState.Name;

src/MongoDB.Bson/IO/BsonBinaryWriterContext.cs

+5
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ internal BsonBinaryWriterContext PopContext()
6060
{
6161
return _parentContext;
6262
}
63+
64+
internal BsonBinaryWriterContext PushContext(ContextType contextType, long startPosition)
65+
{
66+
return new BsonBinaryWriterContext(this, contextType, startPosition);
67+
}
6368
}
6469
}

src/MongoDB.Bson/IO/BsonDocumentReader.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public override void ReadStartArray()
413413
VerifyBsonType("ReadStartArray", BsonType.Array);
414414

415415
var array = _currentValue.AsBsonArray;
416-
_context = new BsonDocumentReaderContext(_context, ContextType.Array, array);
416+
_context = _context.PushContext(ContextType.Array, array);
417417
State = BsonReaderState.Type;
418418
}
419419

@@ -435,7 +435,7 @@ public override void ReadStartDocument()
435435
{
436436
document = _currentValue.AsBsonDocument;
437437
}
438-
_context = new BsonDocumentReaderContext(_context, ContextType.Document, document);
438+
_context = _context.PushContext(ContextType.Document, document);
439439
State = BsonReaderState.Type;
440440
}
441441

src/MongoDB.Bson/IO/BsonDocumentReaderContext.cs

+10
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,15 @@ public BsonDocumentReaderContext PopContext()
124124
{
125125
return _parentContext;
126126
}
127+
128+
internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonArray array)
129+
{
130+
return new BsonDocumentReaderContext(this, contextType, array);
131+
}
132+
133+
internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document)
134+
{
135+
return new BsonDocumentReaderContext(this, contextType, document);
136+
}
127137
}
128138
}

src/MongoDB.Bson/IO/BsonDocumentWriter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public override void WriteJavaScriptWithScope(string code)
304304
ThrowInvalidState("WriteJavaScriptWithScope", BsonWriterState.Value);
305305
}
306306

307-
_context = new BsonDocumentWriterContext(_context, ContextType.JavaScriptWithScope, code);
307+
_context = _context.PushContext(ContextType.JavaScriptWithScope, code);
308308
State = BsonWriterState.ScopeDocument;
309309
}
310310

@@ -407,7 +407,7 @@ public override void WriteStartArray()
407407
}
408408

409409
base.WriteStartArray();
410-
_context = new BsonDocumentWriterContext(_context, ContextType.Array, new BsonArray());
410+
_context = _context.PushContext(ContextType.Array, new BsonArray());
411411
State = BsonWriterState.Value;
412412
}
413413

@@ -430,10 +430,10 @@ public override void WriteStartDocument()
430430
_context = new BsonDocumentWriterContext(null, ContextType.Document, _document);
431431
break;
432432
case BsonWriterState.Value:
433-
_context = new BsonDocumentWriterContext(_context, ContextType.Document, new BsonDocument());
433+
_context = _context.PushContext(ContextType.Document, new BsonDocument());
434434
break;
435435
case BsonWriterState.ScopeDocument:
436-
_context = new BsonDocumentWriterContext(_context, ContextType.ScopeDocument, new BsonDocument());
436+
_context = _context.PushContext(ContextType.ScopeDocument, new BsonDocument());
437437
break;
438438
default:
439439
throw new BsonInternalException("Unexpected state.");

src/MongoDB.Bson/IO/BsonDocumentWriterContext.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal BsonDocumentWriterContext(
3636
_document = document;
3737
}
3838

39-
internal BsonDocumentWriterContext(
39+
private BsonDocumentWriterContext(
4040
BsonDocumentWriterContext parentContext,
4141
ContextType contextType,
4242
BsonArray array)
@@ -46,7 +46,7 @@ internal BsonDocumentWriterContext(
4646
_array = array;
4747
}
4848

49-
internal BsonDocumentWriterContext(
49+
private BsonDocumentWriterContext(
5050
BsonDocumentWriterContext parentContext,
5151
ContextType contextType,
5252
string code)
@@ -92,5 +92,20 @@ internal BsonDocumentWriterContext PopContext()
9292
{
9393
return _parentContext;
9494
}
95+
96+
internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonDocument document)
97+
{
98+
return new BsonDocumentWriterContext(this, contextType, document);
99+
}
100+
101+
internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonArray array)
102+
{
103+
return new BsonDocumentWriterContext(this, contextType, array);
104+
}
105+
106+
internal BsonDocumentWriterContext PushContext(ContextType contextType, string code)
107+
{
108+
return new BsonDocumentWriterContext(this, contextType, code);
109+
}
95110
}
96111
}

src/MongoDB.Bson/IO/JsonReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public override string ReadJavaScriptWithScope()
627627
{
628628
if (Disposed) { ThrowObjectDisposedException(); }
629629
VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
630-
_context = new JsonReaderContext(_context, ContextType.JavaScriptWithScope);
630+
_context = _context.PushContext(ContextType.JavaScriptWithScope);
631631
State = BsonReaderState.ScopeDocument;
632632
return _currentValue.AsString;
633633
}
@@ -723,7 +723,7 @@ public override void ReadStartArray()
723723
if (Disposed) { ThrowObjectDisposedException(); }
724724
VerifyBsonType("ReadStartArray", BsonType.Array);
725725

726-
_context = new JsonReaderContext(_context, ContextType.Array);
726+
_context = _context.PushContext(ContextType.Array);
727727
State = BsonReaderState.Type;
728728
}
729729

@@ -735,7 +735,7 @@ public override void ReadStartDocument()
735735
if (Disposed) { ThrowObjectDisposedException(); }
736736
VerifyBsonType("ReadStartDocument", BsonType.Document);
737737

738-
_context = new JsonReaderContext(_context, ContextType.Document);
738+
_context = _context.PushContext(ContextType.Document);
739739
State = BsonReaderState.Type;
740740
}
741741

src/MongoDB.Bson/IO/JsonReaderContext.cs

+5
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@ public JsonReaderContext PopContext()
5353
{
5454
return _parentContext;
5555
}
56+
57+
internal JsonReaderContext PushContext(ContextType contextType)
58+
{
59+
return new JsonReaderContext(this, contextType);
60+
}
5661
}
5762
}

src/MongoDB.Bson/IO/JsonWriter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public override void WriteStartArray()
641641
WriteNameHelper(Name);
642642
_textWriter.Write("[");
643643

644-
_context = new JsonWriterContext(_context, ContextType.Array, Settings.IndentChars);
644+
_context = _context.PushContext(ContextType.Array, Settings.IndentChars);
645645
State = BsonWriterState.Value;
646646
}
647647

@@ -664,7 +664,7 @@ public override void WriteStartDocument()
664664
_textWriter.Write("{");
665665

666666
var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
667-
_context = new JsonWriterContext(_context, contextType, Settings.IndentChars);
667+
_context = _context.PushContext(contextType, Settings.IndentChars);
668668
State = BsonWriterState.Name;
669669
}
670670

src/MongoDB.Bson/IO/JsonWriterContext.cs

+5
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,10 @@ internal JsonWriterContext PopContext()
5757
{
5858
return _parentContext;
5959
}
60+
61+
internal JsonWriterContext PushContext(ContextType contextType, string indentChars)
62+
{
63+
return new JsonWriterContext(this, contextType, indentChars);
64+
}
6065
}
6166
}

0 commit comments

Comments
 (0)