Skip to content

Commit 51ae949

Browse files
committed
- PR comments
1 parent 78e106e commit 51ae949

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

src/MongoDB.Bson/IO/BsonBinaryReader.cs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class BsonBinaryReader : BsonReader
3232
#pragma warning restore CA2213 // Disposable never disposed
3333
private readonly BsonStream _bsonStream;
3434
private BsonBinaryReaderContext _context;
35-
private readonly Lazy<Stack<BsonBinaryReaderContext>> _contexts = new(() => new());
35+
private readonly Stack<BsonBinaryReaderContext> _contexts = new(4);
3636

3737
// constructors
3838
/// <summary>
@@ -113,7 +113,7 @@ public override void Close()
113113
/// </summary>
114114
/// <returns>A bookmark.</returns>
115115
public override BsonReaderBookmark GetBookmark() =>
116-
new BsonBinaryReaderBookmark(State, CurrentBsonType, CurrentName, _context, _contexts.Value.ToArray(), _bsonStream.Position);
116+
new BsonBinaryReaderBookmark(State, CurrentBsonType, CurrentName, _context, _contexts, _bsonStream.Position);
117117

118118
/// <summary>
119119
/// Determines whether this reader is at end of file.
@@ -342,7 +342,7 @@ public override void ReadEndArray()
342342
ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
343343
}
344344

345-
_context = PopContext();
345+
PopContext();
346346

347347
switch (_context.ContextType)
348348
{
@@ -372,10 +372,10 @@ public override void ReadEndDocument()
372372
ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
373373
}
374374

375-
_context = PopContext();
375+
PopContext();
376376
if (_context.ContextType == ContextType.JavaScriptWithScope)
377377
{
378-
_context = PopContext(); // JavaScriptWithScope
378+
PopContext(); // JavaScriptWithScope
379379
}
380380
switch (_context.ContextType)
381381
{
@@ -434,8 +434,7 @@ public override string ReadJavaScriptWithScope()
434434
var startPosition = _bsonStream.Position; // position of size field
435435
var size = ReadSize();
436436

437-
PushContext(_context);
438-
_context = new BsonBinaryReaderContext(ContextType.JavaScriptWithScope, startPosition, size);
437+
PushContext(new(ContextType.JavaScriptWithScope, startPosition, size));
439438

440439
var code = _bsonStream.ReadString(Settings.Encoding);
441440

@@ -557,7 +556,7 @@ public override IByteBuffer ReadRawBsonDocument()
557556

558557
if (_context.ContextType == ContextType.JavaScriptWithScope)
559558
{
560-
_context = PopContext(); // JavaScriptWithScope
559+
PopContext(); // JavaScriptWithScope
561560
}
562561
switch (_context.ContextType)
563562
{
@@ -595,8 +594,7 @@ public override void ReadStartArray()
595594
var startPosition = _bsonStream.Position; // position of size field
596595
var size = ReadSize();
597596

598-
PushContext(_context);
599-
_context = new(ContextType.Array, startPosition, size);
597+
PushContext(new(ContextType.Array, startPosition, size));
600598

601599
State = BsonReaderState.Type;
602600
}
@@ -612,8 +610,9 @@ public override void ReadStartDocument()
612610
var contextType = (State == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
613611
var startPosition = _bsonStream.Position; // position of size field
614612
var size = ReadSize();
615-
PushContext(_context);
616-
_context = new(contextType, startPosition, size);
613+
614+
PushContext(new(contextType, startPosition, size));
615+
617616
State = BsonReaderState.Type;
618617
}
619618

@@ -670,18 +669,11 @@ public override void ReadUndefined()
670669
public override void ReturnToBookmark(BsonReaderBookmark bookmark)
671670
{
672671
var binaryReaderBookmark = (BsonBinaryReaderBookmark)bookmark;
672+
673673
State = binaryReaderBookmark.State;
674674
CurrentBsonType = binaryReaderBookmark.CurrentBsonType;
675675
CurrentName = binaryReaderBookmark.CurrentName;
676-
677-
_context = binaryReaderBookmark.CurrentContext;
678-
679-
_contexts.Value.Clear();
680-
foreach (var context in binaryReaderBookmark.ContextsStack.Reverse())
681-
{
682-
_contexts.Value.Push(context);
683-
}
684-
676+
_context = binaryReaderBookmark.RestoreContext(_contexts);
685677
_bsonStream.Position = binaryReaderBookmark.Position;
686678
}
687679

@@ -761,7 +753,7 @@ protected override void Dispose(bool disposing)
761753
{
762754
Close();
763755
}
764-
catch { } // ignore exceptions
756+
catch { /* ignore exceptions */ }
765757
}
766758
base.Dispose(disposing);
767759
}
@@ -790,7 +782,7 @@ private string GenerateDottedElementName()
790782
elementName = "?";
791783
}
792784

793-
return GenerateDottedElementName(_contexts.Value.ToArray(), 0, elementName);
785+
return GenerateDottedElementName(_contexts.ToArray(), 0, elementName);
794786
}
795787

796788
private string GenerateDottedElementName(BsonBinaryReaderContext[] contexts, int currentContextIndex, string elementName)
@@ -851,20 +843,21 @@ private int ReadSize()
851843
return size;
852844
}
853845

854-
private BsonBinaryReaderContext PopContext()
846+
private void PopContext()
855847
{
856848
var actualSize = _bsonStream.Position - _context.StartPosition;
857849
if (actualSize != _context.Size)
858850
{
859851
throw new FormatException($"Expected size to be {_context.Size}, not {actualSize}.");
860852
}
861853

862-
return _contexts.Value.Pop();
854+
_context =_contexts.Pop();
863855
}
864856

865-
private void PushContext(BsonBinaryReaderContext context)
857+
private void PushContext(BsonBinaryReaderContext newContext)
866858
{
867-
_contexts.Value.Push(context);
859+
_contexts.Push(_context);
860+
_context = newContext;
868861
}
869862
}
870863
}

src/MongoDB.Bson/IO/BsonBinaryReaderBookmark.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,43 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
namespace MongoDB.Bson.IO;
1718

1819
/// <summary>
1920
/// Represents a bookmark that can be used to return a reader to the current position and state.
2021
/// </summary>
2122
public class BsonBinaryReaderBookmark : BsonReaderBookmark
2223
{
24+
private readonly BsonBinaryReaderContext _context;
25+
private readonly BsonBinaryReaderContext[] _contextArray;
26+
private readonly long _position;
27+
2328
internal BsonBinaryReaderBookmark(
2429
BsonReaderState state,
2530
BsonType currentBsonType,
2631
string currentName,
2732
BsonBinaryReaderContext currentContext,
28-
BsonBinaryReaderContext[] contextsStack,
33+
Stack<BsonBinaryReaderContext> contextsStack,
2934
long position)
3035
: base(state, currentBsonType, currentName)
3136
{
32-
CurrentContext = currentContext;
33-
ContextsStack = contextsStack;
34-
Position = position;
37+
_context = currentContext;
38+
_contextArray = contextsStack.ToArray();
39+
_position = position;
3540
}
3641

37-
internal BsonBinaryReaderContext CurrentContext { get; }
38-
internal BsonBinaryReaderContext[] ContextsStack { get; }
39-
internal long Position { get; }
42+
internal long Position => _position;
43+
44+
internal BsonBinaryReaderContext RestoreContext(Stack<BsonBinaryReaderContext> contextStack)
45+
{
46+
contextStack.Clear();
47+
48+
for (var i = _contextArray.Length - 1; i >= 0; i--)
49+
{
50+
contextStack.Push(_contextArray[i]);
51+
}
52+
53+
return _context;
54+
}
4055
}

src/MongoDB.Bson/IO/BsonBinaryWriter.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class BsonBinaryWriter : BsonWriter
3030
#pragma warning restore CA2213 // Disposable never disposed
3131
private readonly BsonStream _bsonStream;
3232
private BsonBinaryWriterContext _context;
33-
private readonly Lazy<Stack<BsonBinaryWriterContext>> _contexts = new(() => new());
33+
private readonly Stack<BsonBinaryWriterContext> _contexts = new(4);
3434

3535
// constructors
3636
/// <summary>
@@ -291,7 +291,7 @@ public override void WriteEndArray()
291291
_bsonStream.WriteByte(0);
292292
BackpatchSize(); // size of document
293293

294-
_context = _contexts.Value.Pop();
294+
PopContext();
295295
State = GetNextState();
296296
}
297297

@@ -314,7 +314,7 @@ public override void WriteEndDocument()
314314
_bsonStream.WriteByte(0);
315315
BackpatchSize(); // size of document
316316

317-
_context = _contexts.Value.Pop();
317+
PopContext();
318318
if (_context.ContextType == ContextType.TopLevel)
319319
{
320320
State = BsonWriterState.Done;
@@ -324,7 +324,7 @@ public override void WriteEndDocument()
324324
if (_context.ContextType == ContextType.JavaScriptWithScope)
325325
{
326326
BackpatchSize(); // size of the JavaScript with scope value
327-
_context = _contexts.Value.Pop();
327+
PopContext();
328328
}
329329
State = GetNextState();
330330
}
@@ -402,8 +402,7 @@ public override void WriteJavaScriptWithScope(string code)
402402
_bsonStream.WriteBsonType(BsonType.JavaScriptWithScope);
403403
WriteNameHelper();
404404

405-
_contexts.Value.Push(_context);
406-
_context = new(ContextType.JavaScriptWithScope, _bsonStream.Position);
405+
PushContext(new(ContextType.JavaScriptWithScope, _bsonStream.Position));
407406
_bsonStream.WriteInt32(0); // reserve space for size of JavaScript with scope value
408407
_bsonStream.WriteString(code, Settings.Encoding);
409408

@@ -527,7 +526,7 @@ public override void WriteRawBsonDocument(IByteBuffer slice)
527526
if (_context.ContextType == ContextType.JavaScriptWithScope)
528527
{
529528
BackpatchSize(); // size of the JavaScript with scope value
530-
_context = _contexts.Value.Pop();
529+
PopContext();
531530
}
532531
State = GetNextState();
533532
}
@@ -568,8 +567,7 @@ public override void WriteStartArray()
568567
_bsonStream.WriteBsonType(BsonType.Array);
569568
WriteNameHelper();
570569

571-
_contexts.Value.Push(_context);
572-
_context = new(ContextType.Array, _bsonStream.Position);
570+
PushContext(new(ContextType.Array, _bsonStream.Position));
573571
_bsonStream.WriteInt32(0); // reserve space for size
574572

575573
State = BsonWriterState.Value;
@@ -593,8 +591,7 @@ public override void WriteStartDocument()
593591
WriteNameHelper();
594592
}
595593
var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
596-
_contexts.Value.Push(_context);
597-
_context = new(contextType, _bsonStream.Position);
594+
PushContext(new(contextType, _bsonStream.Position));
598595
_bsonStream.WriteInt32(0); // reserve space for size
599596

600597
State = BsonWriterState.Name;
@@ -687,7 +684,7 @@ protected override void Dispose(bool disposing)
687684
{
688685
Close();
689686
}
690-
catch { } // ignore exceptions
687+
catch { /* ignore exceptions */ }
691688
}
692689
base.Dispose(disposing);
693690
}
@@ -733,5 +730,16 @@ private void WriteNameHelper()
733730
_bsonStream.WriteCString(Name);
734731
}
735732
}
733+
734+
private void PopContext()
735+
{
736+
_context = _contexts.Pop();
737+
}
738+
739+
private void PushContext(BsonBinaryWriterContext newContext)
740+
{
741+
_contexts.Push(_context);
742+
_context = newContext;
743+
}
736744
}
737745
}

0 commit comments

Comments
 (0)