Skip to content

Commit 1472375

Browse files
author
rstam
committed
CSHARP-737: Improve behavior of GridFS when using a ReadPreference other than Primary.
1 parent 65c0c81 commit 1472375

File tree

8 files changed

+941
-279
lines changed

8 files changed

+941
-279
lines changed

MongoDB.Driver/GridFS/MongoGridFS.cs

Lines changed: 294 additions & 72 deletions
Large diffs are not rendered by default.

MongoDB.Driver/GridFS/MongoGridFSFileInfo.cs

Lines changed: 272 additions & 43 deletions
Large diffs are not rendered by default.

MongoDB.Driver/GridFS/MongoGridFSSettings.cs

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
18+
using MongoDB.Bson;
1719

1820
namespace MongoDB.Driver.GridFS
1921
{
@@ -28,10 +30,14 @@ public class MongoGridFSSettings : IEquatable<MongoGridFSSettings>
2830

2931
// private fields
3032
private Setting<int> _chunkSize;
33+
private Setting<GuidRepresentation> _guidRepresentation;
34+
private Setting<UTF8Encoding> _readEncoding;
35+
private Setting<ReadPreference> _readPreference;
3136
private Setting<string> _root;
3237
private Setting<bool> _updateMD5;
3338
private Setting<bool> _verifyMD5;
3439
private Setting<WriteConcern> _writeConcern;
40+
private Setting<UTF8Encoding> _writeEncoding;
3541

3642
private bool _isFrozen;
3743
private int _frozenHashCode;
@@ -143,6 +149,19 @@ public string FilesCollectionName
143149
get { return (_root.Value == null) ? null : _root.Value + ".files"; }
144150
}
145151

152+
/// <summary>
153+
/// Gets or sets the GuidRepresentation.
154+
/// </summary>
155+
public GuidRepresentation GuidRepresentation
156+
{
157+
get { return _guidRepresentation.Value; }
158+
set
159+
{
160+
if (_isFrozen) { ThrowFrozen(); }
161+
_guidRepresentation.Value = value;
162+
}
163+
}
164+
146165
/// <summary>
147166
/// Gets whether the settings are frozen.
148167
/// </summary>
@@ -151,6 +170,36 @@ public bool IsFrozen
151170
get { return _isFrozen; }
152171
}
153172

173+
/// <summary>
174+
/// Gets or sets the read encoding.
175+
/// </summary>
176+
public UTF8Encoding ReadEncoding
177+
{
178+
get { return _readEncoding.Value; }
179+
set
180+
{
181+
if (_isFrozen) { ThrowFrozen(); }
182+
_readEncoding.Value = value;
183+
}
184+
}
185+
186+
/// <summary>
187+
/// Gets or sets the ReadPreference.
188+
/// </summary>
189+
public ReadPreference ReadPreference
190+
{
191+
get { return _readPreference.Value; }
192+
set
193+
{
194+
if (_isFrozen) { ThrowFrozen(); }
195+
if (value == null)
196+
{
197+
throw new ArgumentNullException("value");
198+
}
199+
_readPreference.Value = value;
200+
}
201+
}
202+
154203
/// <summary>
155204
/// Gets or sets the root collection name (the files and chunks collection names are derived from the root).
156205
/// </summary>
@@ -227,6 +276,19 @@ public WriteConcern WriteConcern
227276
}
228277
}
229278

279+
/// <summary>
280+
/// Gets or sets the write encoding.
281+
/// </summary>
282+
public UTF8Encoding WriteEncoding
283+
{
284+
get { return _writeEncoding.Value; }
285+
set
286+
{
287+
if (_isFrozen) { ThrowFrozen(); }
288+
_writeEncoding.Value = value;
289+
}
290+
}
291+
230292
// public operators
231293
/// <summary>
232294
/// Compares two MongoGridFSSettings.
@@ -259,10 +321,14 @@ public MongoGridFSSettings Clone()
259321
{
260322
var clone = new MongoGridFSSettings();
261323
clone._chunkSize = _chunkSize.Clone();
324+
clone._guidRepresentation = _guidRepresentation.Clone();
325+
clone._readEncoding = _readEncoding.Clone();
326+
clone._readPreference = _readPreference.Clone();
262327
clone._root = _root.Clone();
263328
clone._updateMD5 = _updateMD5.Clone();
264329
clone._verifyMD5 = _verifyMD5.Clone();
265330
clone._writeConcern = _writeConcern.Clone();
331+
clone._writeEncoding = _writeEncoding.Clone();
266332
return clone;
267333
}
268334

@@ -276,10 +342,14 @@ public bool Equals(MongoGridFSSettings rhs)
276342
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
277343
return
278344
_chunkSize.Value == rhs._chunkSize.Value &&
345+
_guidRepresentation.Value == rhs._guidRepresentation.Value &&
346+
object.Equals(_readEncoding.Value, rhs._readEncoding.Value) &&
347+
_readPreference.Value == rhs._readPreference.Value &&
279348
_root.Value == rhs._root.Value &&
280349
_updateMD5.Value == rhs._updateMD5.Value &&
281350
_verifyMD5.Value == rhs._verifyMD5.Value &&
282-
_writeConcern.Value == rhs._writeConcern.Value;
351+
_writeConcern.Value == rhs._writeConcern.Value &&
352+
object.Equals(_writeEncoding.Value, rhs._writeEncoding.Value);
283353
}
284354

285355
/// <summary>
@@ -337,10 +407,14 @@ public override int GetHashCode()
337407
// see Effective Java by Joshua Bloch
338408
int hash = 17;
339409
hash = 37 * hash + _chunkSize.Value.GetHashCode();
410+
hash = 37 * hash + _guidRepresentation.Value.GetHashCode();
411+
hash = 37 * hash + ((_readEncoding.Value == null) ? 0 : _readEncoding.Value.GetHashCode());
412+
hash = 37 * hash + ((_readPreference.Value == null) ? 0 : _readPreference.Value.GetHashCode());
340413
hash = 37 * hash + ((_root.Value == null) ? 0 : _root.Value.GetHashCode());
341414
hash = 37 * hash + _updateMD5.Value.GetHashCode();
342415
hash = 37 * hash + _verifyMD5.Value.GetHashCode();
343416
hash = 37 * hash + ((_writeConcern.Value == null) ? 0 : _writeConcern.Value.GetHashCode());
417+
hash = 37 * hash + ((_writeEncoding.Value == null) ? 0 : _writeEncoding.Value.GetHashCode());
344418
return hash;
345419
}
346420

@@ -351,6 +425,18 @@ internal void ApplyDefaultValues(MongoDatabaseSettings databaseSettings)
351425
{
352426
ChunkSize = __defaults.ChunkSize;
353427
}
428+
if (!_guidRepresentation.HasBeenSet)
429+
{
430+
GuidRepresentation = databaseSettings.GuidRepresentation;
431+
}
432+
if (!_readEncoding.HasBeenSet)
433+
{
434+
ReadEncoding = databaseSettings.ReadEncoding;
435+
}
436+
if (!_readPreference.HasBeenSet)
437+
{
438+
ReadPreference = databaseSettings.ReadPreference;
439+
}
354440
if (!_root.HasBeenSet)
355441
{
356442
Root = __defaults.Root;
@@ -367,6 +453,62 @@ internal void ApplyDefaultValues(MongoDatabaseSettings databaseSettings)
367453
{
368454
WriteConcern = databaseSettings.WriteConcern;
369455
}
456+
if (!_writeEncoding.HasBeenSet)
457+
{
458+
WriteEncoding = databaseSettings.ReadEncoding;
459+
}
460+
}
461+
462+
internal void ApplyDefaultValues(MongoServerSettings serverSettings)
463+
{
464+
if (!_chunkSize.HasBeenSet)
465+
{
466+
ChunkSize = __defaults.ChunkSize;
467+
}
468+
if (!_guidRepresentation.HasBeenSet)
469+
{
470+
GuidRepresentation = serverSettings.GuidRepresentation;
471+
}
472+
if (!_readEncoding.HasBeenSet)
473+
{
474+
ReadEncoding = serverSettings.ReadEncoding;
475+
}
476+
if (!_readPreference.HasBeenSet)
477+
{
478+
ReadPreference = serverSettings.ReadPreference;
479+
}
480+
if (!_root.HasBeenSet)
481+
{
482+
Root = __defaults.Root;
483+
}
484+
if (!_updateMD5.HasBeenSet)
485+
{
486+
UpdateMD5 = __defaults.UpdateMD5;
487+
}
488+
if (!_verifyMD5.HasBeenSet)
489+
{
490+
VerifyMD5 = __defaults.VerifyMD5;
491+
}
492+
if (!_writeConcern.HasBeenSet)
493+
{
494+
WriteConcern = serverSettings.WriteConcern;
495+
}
496+
if (!_writeEncoding.HasBeenSet)
497+
{
498+
WriteEncoding = serverSettings.ReadEncoding;
499+
}
500+
}
501+
502+
internal MongoDatabaseSettings GetDatabaseSettings()
503+
{
504+
return new MongoDatabaseSettings
505+
{
506+
GuidRepresentation = _guidRepresentation.Value,
507+
ReadEncoding = _readEncoding.Value,
508+
ReadPreference = _readPreference.Value,
509+
WriteConcern = _writeConcern.Value,
510+
WriteEncoding = _writeEncoding.Value
511+
};
370512
}
371513

372514
// private methods

0 commit comments

Comments
 (0)