Skip to content

Commit 6423ab5

Browse files
vincentkamrstam
authored andcommitted
CSHARP-2162: Add disableMD5 option and deprecate MD5 field
1 parent eb425d9 commit 6423ab5

File tree

13 files changed

+333
-22
lines changed

13 files changed

+333
-22
lines changed

src/MongoDB.Driver.GridFS/GridFSBucket.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,8 @@ private GridFSUploadStream<TFileId> CreateUploadStream(IReadWriteBindingHandle b
732732
options.Aliases,
733733
options.ContentType,
734734
chunkSizeBytes,
735-
batchSize);
735+
batchSize,
736+
options.DisableMD5);
736737
#pragma warning restore
737738
}
738739

src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class GridFSBucketOptions
2626
// fields
2727
private string _bucketName;
2828
private int _chunkSizeBytes;
29+
private bool _disableMD5 = false;
2930
private ReadConcern _readConcern;
3031
private ReadPreference _readPreference;
3132
private WriteConcern _writeConcern;
@@ -48,6 +49,7 @@ public GridFSBucketOptions(GridFSBucketOptions other)
4849
Ensure.IsNotNull(other, nameof(other));
4950
_bucketName = other.BucketName;
5051
_chunkSizeBytes = other.ChunkSizeBytes;
52+
_disableMD5 = other.DisableMD5;
5153
_readConcern = other.ReadConcern;
5254
_readPreference = other.ReadPreference;
5355
_writeConcern = other.WriteConcern;
@@ -62,6 +64,7 @@ public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
6264
Ensure.IsNotNull(other, nameof(other));
6365
_bucketName = other.BucketName;
6466
_chunkSizeBytes = other.ChunkSizeBytes;
67+
_disableMD5 = other.DisableMD5;
6568
_readConcern = other.ReadConcern;
6669
_readPreference = other.ReadPreference;
6770
_writeConcern = other.WriteConcern;
@@ -99,6 +102,18 @@ public int ChunkSizeBytes
99102
_chunkSizeBytes = value;
100103
}
101104
}
105+
106+
/// <summary>
107+
/// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
108+
/// </summary>
109+
/// <value>
110+
/// Whether MD5 checksum computation is disabled when uploading a GridFS file.
111+
/// </value>
112+
public bool DisableMD5
113+
{
114+
get { return _disableMD5; }
115+
set { _disableMD5 = value; }
116+
}
102117

103118
/// <summary>
104119
/// Gets or sets the read concern.
@@ -162,6 +177,7 @@ public static ImmutableGridFSBucketOptions Defaults
162177
// fields
163178
private readonly string _bucketName;
164179
private readonly int _chunkSizeBytes;
180+
private readonly bool _disableMD5 = false;
165181
private readonly ReadConcern _readConcern;
166182
private readonly ReadPreference _readPreference;
167183
private readonly WriteConcern _writeConcern;
@@ -185,6 +201,7 @@ public ImmutableGridFSBucketOptions(GridFSBucketOptions other)
185201
Ensure.IsNotNull(other, nameof(other));
186202
_bucketName = other.BucketName;
187203
_chunkSizeBytes = other.ChunkSizeBytes;
204+
_disableMD5 = other.DisableMD5;
188205
_readConcern = other.ReadConcern;
189206
_readPreference = other.ReadPreference;
190207
_writeConcern = other.WriteConcern;
@@ -212,6 +229,18 @@ public int ChunkSizeBytes
212229
{
213230
get { return _chunkSizeBytes; }
214231
}
232+
233+
/// <summary>
234+
/// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
235+
/// </summary>
236+
/// <value>
237+
/// Whether MD5 checksum computation is disabled when uploading a GridFS file.
238+
/// </value>
239+
public bool DisableMD5
240+
{
241+
get { return _disableMD5; }
242+
}
243+
215244

216245
/// <summary>
217246
/// Gets the read concern.

src/MongoDB.Driver.GridFS/GridFSFileInfoCompat.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public long Length
138138
/// <value>
139139
/// The MD5 checksum.
140140
/// </value>
141+
[Obsolete("MD5 support will be removed soon.")]
141142
public string MD5
142143
{
143144
get { return GetValue<string>("MD5", null); }

src/MongoDB.Driver.GridFS/GridFSForwardOnlyUploadStream.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal class GridFSForwardOnlyUploadStream<TFileId> : GridFSUploadStream<TFile
4545
private readonly int _chunkSizeBytes;
4646
private bool _closed;
4747
private readonly string _contentType;
48+
private readonly bool _disableMD5;
4849
private bool _disposed;
4950
private readonly string _filename;
5051
private readonly TFileId _id;
@@ -63,7 +64,8 @@ public GridFSForwardOnlyUploadStream(
6364
IEnumerable<string> aliases,
6465
string contentType,
6566
int chunkSizeBytes,
66-
int batchSize)
67+
int batchSize,
68+
bool disableMD5)
6769
{
6870
_bucket = bucket;
6971
_binding = binding;
@@ -76,7 +78,8 @@ public GridFSForwardOnlyUploadStream(
7678
_batchSize = batchSize;
7779

7880
_batch = new List<byte[]>();
79-
_md5 = IncrementalMD5.Create();
81+
_md5 = disableMD5 ? null : IncrementalMD5.Create();
82+
_disableMD5 = disableMD5;
8083

8184
var idSerializer = bucket.Options.SerializerRegistry.GetSerializer<TFileId>();
8285
var idSerializationInfo = new BsonSerializationInfo("_id", idSerializer, typeof(TFileId));
@@ -272,7 +275,7 @@ private BsonDocument CreateFilesCollectionDocument()
272275
{ "length", _length },
273276
{ "chunkSize", _chunkSizeBytes },
274277
{ "uploadDate", uploadDateTime },
275-
{ "md5", BsonUtils.ToHexString(_md5.GetHashAndReset()) },
278+
{ "md5", () => BsonUtils.ToHexString(_md5.GetHashAndReset()), !_disableMD5 },
276279
{ "filename", _filename },
277280
{ "contentType", _contentType, _contentType != null },
278281
{ "aliases", () => new BsonArray(_aliases.Select(a => new BsonString(a))), _aliases != null },
@@ -297,7 +300,7 @@ private IEnumerable<BsonDocument> CreateWriteBatchChunkDocuments()
297300
chunkDocuments.Add(chunkDocument);
298301

299302
_batchPosition += chunk.Length;
300-
_md5.AppendData(chunk, 0, chunk.Length);
303+
_md5?.AppendData(chunk, 0, chunk.Length);
301304
}
302305

303306
return chunkDocuments;

src/MongoDB.Driver.GridFS/GridFSUploadOptions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class GridFSUploadOptions
3030
private int? _batchSize;
3131
private int? _chunkSizeBytes;
3232
private string _contentType;
33+
private bool _disableMD5 = false;
3334
private BsonDocument _metadata;
35+
3436

3537
// properties
3638
/// <summary>
@@ -85,6 +87,18 @@ public string ContentType
8587
get { return _contentType; }
8688
set { _contentType = Ensure.IsNullOrNotEmpty(value, nameof(value)); }
8789
}
90+
91+
/// <summary>
92+
/// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
93+
/// </summary>
94+
/// <value>
95+
/// Whether or not MD5 checksum computation is disabled when uploading a GridFS file.
96+
/// </value>
97+
public bool DisableMD5
98+
{
99+
get { return _disableMD5; }
100+
set { _disableMD5 = value; }
101+
}
88102

89103
/// <summary>
90104
/// Gets or sets the metadata.

tests/MongoDB.Driver.GridFS.Tests.Dotnet/Specifications/gridfs/tests/upload.json

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"data": {
3-
"files": [
4-
5-
],
6-
"chunks": [
7-
8-
]
3+
"files": [],
4+
"chunks": []
95
},
106
"tests": [
117
{
@@ -386,6 +382,85 @@
386382
}
387383
]
388384
}
385+
},
386+
{
387+
"description": "Upload when length is 0 sans MD5",
388+
"act": {
389+
"operation": "upload",
390+
"arguments": {
391+
"filename": "filename",
392+
"source": {
393+
"$hex": ""
394+
},
395+
"options": {
396+
"chunkSizeBytes": 4,
397+
"disableMD5": true
398+
}
399+
}
400+
},
401+
"assert": {
402+
"result": "&result",
403+
"data": [
404+
{
405+
"insert": "expected.files",
406+
"documents": [
407+
{
408+
"_id": "*result",
409+
"length": 0,
410+
"chunkSize": 4,
411+
"uploadDate": "*actual",
412+
"filename": "filename"
413+
}
414+
]
415+
}
416+
]
417+
}
418+
},
419+
{
420+
"description": "Upload when length is 1 sans MD5",
421+
"act": {
422+
"operation": "upload",
423+
"arguments": {
424+
"filename": "filename",
425+
"source": {
426+
"$hex": "11"
427+
},
428+
"options": {
429+
"chunkSizeBytes": 4,
430+
"disableMD5": true
431+
}
432+
}
433+
},
434+
"assert": {
435+
"result": "&result",
436+
"data": [
437+
{
438+
"insert": "expected.files",
439+
"documents": [
440+
{
441+
"_id": "*result",
442+
"length": 1,
443+
"chunkSize": 4,
444+
"uploadDate": "*actual",
445+
"filename": "filename"
446+
}
447+
]
448+
},
449+
{
450+
"insert": "expected.chunks",
451+
"documents": [
452+
{
453+
"_id": "*actual",
454+
"files_id": "*result",
455+
"n": 0,
456+
"data": {
457+
"$hex": "11"
458+
}
459+
}
460+
]
461+
}
462+
]
463+
}
389464
}
390465
]
391-
}
466+
}

tests/MongoDB.Driver.GridFS.Tests.Dotnet/Specifications/gridfs/tests/upload.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,37 @@ tests:
156156
{ insert : "expected.chunks", documents : [
157157
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11" } }
158158
] }
159+
-
160+
description: "Upload when length is 0 sans MD5"
161+
act:
162+
operation: upload
163+
arguments:
164+
filename: "filename"
165+
source: { $hex : "" }
166+
options: { chunkSizeBytes : 4, disableMD5: true }
167+
assert:
168+
result: "&result"
169+
data:
170+
-
171+
{ insert : "expected.files", documents : [
172+
{ _id : "*result", length : 0, chunkSize : 4, uploadDate : "*actual", filename : "filename" }
173+
] }
174+
-
175+
description: "Upload when length is 1 sans MD5"
176+
act:
177+
operation: upload
178+
arguments:
179+
filename: "filename"
180+
source: { $hex : "11" }
181+
options: { chunkSizeBytes : 4, disableMD5: true }
182+
assert:
183+
result: "&result"
184+
data:
185+
-
186+
{ insert : "expected.files", documents : [
187+
{ _id : "*result", length : 1, chunkSize : 4, uploadDate : "*actual", filename : "filename" }
188+
] }
189+
-
190+
{ insert : "expected.chunks", documents : [
191+
{ _id : "*actual", files_id : "*result", n : 0, data : { $hex : "11" } }
192+
] }

tests/MongoDB.Driver.GridFS.Tests/GridFSBucketOptionsTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul
139139
result.WriteConcern.Should().BeNull();
140140
}
141141

142+
[Theory]
143+
[ParameterAttributeData]
144+
public void DisableMD5_get_and_set_should_return_expected_result(
145+
[Values(false, true)] bool disabled)
146+
{
147+
var subject = new GridFSBucketOptions { DisableMD5 = disabled};
148+
149+
var result = subject.DisableMD5;
150+
151+
result.Should().Be(disabled);
152+
}
153+
142154
[Fact]
143155
public void ReadPreference_get_should_return_expected_result()
144156
{
@@ -225,12 +237,21 @@ public void ChunkSizeBytes_get_should_return_expected_result()
225237
[Fact]
226238
public void constructor_with_arguments_should_initialize_instance()
227239
{
228-
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
240+
var mutable = new GridFSBucketOptions
241+
{
242+
BucketName = "bucket",
243+
ChunkSizeBytes = 123,
244+
DisableMD5 = true,
245+
ReadConcern = ReadConcern.Majority,
246+
ReadPreference = ReadPreference.Secondary,
247+
WriteConcern = WriteConcern.WMajority
248+
};
229249

230250
var result = new ImmutableGridFSBucketOptions(mutable);
231251

232252
result.BucketName.Should().Be("bucket");
233253
result.ChunkSizeBytes.Should().Be(123);
254+
result.DisableMD5.Should().Be(true);
234255
result.ReadConcern.Should().Be(ReadConcern.Majority);
235256
result.ReadPreference.Should().Be(ReadPreference.Secondary);
236257
result.WriteConcern.Should().Be(WriteConcern.WMajority);
@@ -243,6 +264,7 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul
243264

244265
result.BucketName.Should().Be("fs");
245266
result.ChunkSizeBytes.Should().Be(255 * 1024);
267+
result.DisableMD5.Should().Be(false);
246268
result.ReadConcern.Should().BeNull();
247269
result.ReadPreference.Should().BeNull();
248270
result.WriteConcern.Should().BeNull();
@@ -264,11 +286,24 @@ public void Defaults_get_should_return_expected_result()
264286

265287
result.BucketName.Should().Be("fs");
266288
result.ChunkSizeBytes.Should().Be(255 * 1024);
289+
result.DisableMD5.Should().Be(false);
267290
result.ReadConcern.Should().BeNull();
268291
result.ReadPreference.Should().BeNull();
269292
result.WriteConcern.Should().BeNull();
270293
}
271294

295+
[Theory]
296+
[ParameterAttributeData]
297+
public void DisableMD5_get_and_set_should_return_expected_result(
298+
[Values(false,true)] bool disabled)
299+
{
300+
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { DisableMD5 = disabled });
301+
302+
var result = subject.DisableMD5;
303+
304+
result.Should().Be(disabled);
305+
}
306+
272307
[Fact]
273308
public void ReadConcern_get_should_return_expected_result()
274309
{

0 commit comments

Comments
 (0)