Skip to content

Commit ee97df7

Browse files
committed
CSHARP-1594: Add support for Decimal128 BSON type.
1 parent 9aa35d8 commit ee97df7

File tree

94 files changed

+14209
-178
lines changed

Some content is hidden

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

94 files changed

+14209
-178
lines changed

src/MongoDB.Bson/IO/BsonBinaryReader.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ public override long ReadDateTime()
304304
return value;
305305
}
306306

307+
/// <inheritdoc />
308+
public override Decimal128 ReadDecimal128()
309+
{
310+
if (Disposed) { ThrowObjectDisposedException(); }
311+
VerifyBsonType(nameof(ReadDecimal128), BsonType.Decimal128);
312+
State = GetNextState();
313+
return _bsonStream.ReadDecimal128();
314+
}
315+
307316
/// <summary>
308317
/// Reads a BSON Double from the reader.
309318
/// </summary>
@@ -702,6 +711,7 @@ public override void SkipValue()
702711
case BsonType.Boolean: skip = 1; break;
703712
case BsonType.DateTime: skip = 8; break;
704713
case BsonType.Document: skip = ReadSize() - 4; break;
714+
case BsonType.Decimal128: skip = 16; break;
705715
case BsonType.Double: skip = 8; break;
706716
case BsonType.Int32: skip = 4; break;
707717
case BsonType.Int64: skip = 8; break;

src/MongoDB.Bson/IO/BsonBinaryWriter.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -87,7 +87,7 @@ public Stream BaseStream
8787
/// </value>
8888
public BsonStream BsonStream
8989
{
90-
get { return _bsonStream; }
90+
get { return _bsonStream; }
9191
}
9292

9393
// public methods
@@ -268,6 +268,22 @@ public override void WriteDateTime(long value)
268268
State = GetNextState();
269269
}
270270

271+
/// <inheritdoc />
272+
public override void WriteDecimal128(Decimal128 value)
273+
{
274+
if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
275+
if (State != BsonWriterState.Value)
276+
{
277+
ThrowInvalidState(nameof(WriteDecimal128), BsonWriterState.Value);
278+
}
279+
280+
_bsonStream.WriteBsonType(BsonType.Decimal128);
281+
WriteNameHelper();
282+
_bsonStream.WriteDecimal128(value);
283+
284+
State = GetNextState();
285+
}
286+
271287
/// <summary>
272288
/// Writes a BSON Double to the writer.
273289
/// </summary>

src/MongoDB.Bson/IO/BsonDocumentReader.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -193,6 +193,15 @@ public override long ReadDateTime()
193193
return _currentValue.AsBsonDateTime.MillisecondsSinceEpoch;
194194
}
195195

196+
/// <inheritdoc />
197+
public override Decimal128 ReadDecimal128()
198+
{
199+
if (Disposed) { ThrowObjectDisposedException(); }
200+
VerifyBsonType(nameof(ReadDecimal128), BsonType.Decimal128);
201+
State = GetNextState();
202+
return _currentValue.AsDecimal128;
203+
}
204+
196205
/// <summary>
197206
/// Reads a BSON Double from the reader.
198207
/// </summary>

src/MongoDB.Bson/IO/BsonDocumentWriter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -146,6 +146,19 @@ public override void WriteDateTime(long value)
146146
State = GetNextState();
147147
}
148148

149+
/// <inheritdoc />
150+
public override void WriteDecimal128(Decimal128 value)
151+
{
152+
if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
153+
if (State != BsonWriterState.Value)
154+
{
155+
ThrowInvalidState(nameof(WriteDecimal128), BsonWriterState.Value);
156+
}
157+
158+
WriteValue(new BsonDecimal128(value));
159+
State = GetNextState();
160+
}
161+
149162
/// <summary>
150163
/// Writes a BSON Double to the writer.
151164
/// </summary>

src/MongoDB.Bson/IO/BsonReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ public BsonType GetCurrentBsonType()
173173
/// <returns>The number of milliseconds since the Unix epoch.</returns>
174174
public abstract long ReadDateTime();
175175

176+
/// <inheritdoc />
177+
public abstract Decimal128 ReadDecimal128();
178+
176179
/// <summary>
177180
/// Reads a BSON Double from the reader.
178181
/// </summary>

src/MongoDB.Bson/IO/BsonStream.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -40,6 +40,12 @@ public abstract class BsonStream : Stream
4040
/// <returns>An ArraySegment containing the CString bytes (without the null byte).</returns>
4141
public abstract ArraySegment<byte> ReadCStringBytes();
4242

43+
/// <summary>
44+
/// Reads a BSON Decimal128 from the stream.
45+
/// </summary>
46+
/// <returns>A <see cref="Decimal128"/>.</returns>
47+
public abstract Decimal128 ReadDecimal128();
48+
4349
/// <summary>
4450
/// Reads a BSON double from the stream.
4551
/// </summary>
@@ -94,6 +100,12 @@ public abstract class BsonStream : Stream
94100
/// <param name="value">The value.</param>
95101
public abstract void WriteCStringBytes(byte[] value);
96102

103+
/// <summary>
104+
/// Writes a BSON Decimal128 to the stream.
105+
/// </summary>
106+
/// <param name="value">The value.</param>
107+
public abstract void WriteDecimal128(Decimal128 value);
108+
97109
/// <summary>
98110
/// Writes a BSON double to the stream.
99111
/// </summary>

src/MongoDB.Bson/IO/BsonStreamAdapter.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Text;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -308,6 +309,15 @@ public override ArraySegment<byte> ReadCStringBytes()
308309
}
309310
}
310311

312+
/// <inheritdoc/>
313+
public override Decimal128 ReadDecimal128()
314+
{
315+
ThrowIfDisposed();
316+
var lowBits = (ulong)ReadInt64();
317+
var highBits = (ulong)ReadInt64();
318+
return Decimal128.FromIEEEBits(highBits, lowBits);
319+
}
320+
311321
/// <inheritdoc/>
312322
public override double ReadDouble()
313323
{
@@ -479,6 +489,14 @@ public override void WriteCStringBytes(byte[] value)
479489
WriteByte(0);
480490
}
481491

492+
/// <inheritdoc/>
493+
public override void WriteDecimal128(Decimal128 value)
494+
{
495+
ThrowIfDisposed();
496+
WriteInt64((long)value.GetIEEELowBits());
497+
WriteInt64((long)value.GetIEEEHighBits());
498+
}
499+
482500
/// <inheritdoc/>
483501
public override void WriteDouble(double value)
484502
{

src/MongoDB.Bson/IO/BsonWriter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -170,6 +170,9 @@ public void PushElementNameValidator(IElementNameValidator validator)
170170
/// <param name="value">The number of milliseconds since the Unix epoch.</param>
171171
public abstract void WriteDateTime(long value);
172172

173+
/// <inheritdoc />
174+
public abstract void WriteDecimal128(Decimal128 value);
175+
173176
/// <summary>
174177
/// Writes a BSON Double to the writer.
175178
/// </summary>

src/MongoDB.Bson/IO/ByteBufferStream.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ public override ArraySegment<byte> ReadCStringBytes()
385385
}
386386
}
387387

388+
/// <inheritdoc/>
389+
public override Decimal128 ReadDecimal128()
390+
{
391+
ThrowIfDisposed();
392+
ThrowIfEndOfStream(16);
393+
394+
var lowBits = (ulong)ReadInt64();
395+
var highBits = (ulong)ReadInt64();
396+
return Decimal128.FromIEEEBits(highBits, lowBits);
397+
}
398+
388399
/// <inheritdoc/>
389400
public override double ReadDouble()
390401
{
@@ -531,7 +542,7 @@ public override void WriteCString(string value)
531542
throw new ArgumentNullException("value");
532543
}
533544
ThrowIfDisposed();
534-
545+
535546
var maxLength = CStringUtf8Encoding.GetMaxByteCount(value.Length) + 1;
536547
PrepareToWrite(maxLength);
537548

@@ -586,11 +597,19 @@ public override void WriteCStringBytes(byte[] value)
586597
SetPositionAfterWrite(_position + length + 1);
587598
}
588599

600+
/// <inheritdoc/>
601+
public override void WriteDecimal128(Decimal128 value)
602+
{
603+
ThrowIfDisposed();
604+
WriteInt64((long)value.GetIEEELowBits());
605+
WriteInt64((long)value.GetIEEEHighBits());
606+
}
607+
589608
/// <inheritdoc/>
590609
public override void WriteDouble(double value)
591610
{
592611
ThrowIfDisposed();
593-
612+
594613
PrepareToWrite(8);
595614

596615
var bytes = BitConverter.GetBytes(value);
@@ -603,7 +622,7 @@ public override void WriteDouble(double value)
603622
public override void WriteInt32(int value)
604623
{
605624
ThrowIfDisposed();
606-
625+
607626
PrepareToWrite(4);
608627

609628
var segment = _buffer.AccessBackingBytes(_position);
@@ -630,7 +649,7 @@ public override void WriteInt32(int value)
630649
public override void WriteInt64(long value)
631650
{
632651
ThrowIfDisposed();
633-
652+
634653
PrepareToWrite(8);
635654

636655
var bytes = BitConverter.GetBytes(value);
@@ -643,7 +662,7 @@ public override void WriteInt64(long value)
643662
public override void WriteObjectId(ObjectId value)
644663
{
645664
ThrowIfDisposed();
646-
665+
647666
PrepareToWrite(12);
648667

649668
var segment = _buffer.AccessBackingBytes(_position);
@@ -664,7 +683,7 @@ public override void WriteObjectId(ObjectId value)
664683
public override void WriteString(string value, UTF8Encoding encoding)
665684
{
666685
ThrowIfDisposed();
667-
686+
668687
var maxLength = encoding.GetMaxByteCount(value.Length) + 5;
669688
PrepareToWrite(maxLength);
670689

src/MongoDB.Bson/IO/IBsonReader.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public interface IBsonReader : IDisposable
6363
/// Reads BSON binary data from the reader.
6464
/// </summary>
6565
/// <returns>A BsonBinaryData.</returns>
66-
BsonBinaryData ReadBinaryData();
66+
BsonBinaryData ReadBinaryData();
6767

6868
/// <summary>
6969
/// Reads a BSON boolean from the reader.
@@ -89,6 +89,12 @@ public interface IBsonReader : IDisposable
8989
/// <returns>The number of milliseconds since the Unix epoch.</returns>
9090
long ReadDateTime();
9191

92+
/// <summary>
93+
/// Reads a BSON Decimal128 from the reader.
94+
/// </summary>
95+
/// <returns>A <see cref="Decimal128" />.</returns>
96+
Decimal128 ReadDecimal128();
97+
9298
/// <summary>
9399
/// Reads a BSON Double from the reader.
94100
/// </summary>

0 commit comments

Comments
 (0)