Browse Source

Added Freeze method to BsonBinaryReaderSettings, BsonBinaryWriterSettings and JsonWriterSettings. Made small changes throughout so that Freeze method is used consistently.

pull/47/head
rstam 15 years ago
parent
commit
1d6e9a9c0c
  1. 6
      Bson/DefaultSerializer/BsonClassMap.cs
  2. 2
      Bson/IO/BsonBinaryReader.cs
  3. 28
      Bson/IO/BsonBinaryReaderSettings.cs
  4. 2
      Bson/IO/BsonBinaryWriter.cs
  5. 28
      Bson/IO/BsonBinaryWriterSettings.cs
  6. 2
      Bson/IO/JsonWriter.cs
  7. 45
      Bson/IO/JsonWriterSettings.cs
  8. 2
      Driver/Core/MongoCollection.cs
  9. 3
      Driver/Core/MongoCollectionSettings.cs
  10. 3
      Driver/Core/MongoDatabase.cs
  11. 3
      Driver/Core/MongoDatabaseSettings.cs
  12. 4
      Driver/Core/MongoServer.cs
  13. 3
      Driver/Core/MongoServerSettings.cs
  14. 3
      Driver/Core/MongoUrl.cs
  15. 5
      Driver/GridFS/MongoGridFS.cs

6
Bson/DefaultSerializer/BsonClassMap.cs

@ -187,8 +187,7 @@ namespace MongoDB.Bson.DefaultSerializer {
classMap.AutoMap();
RegisterClassMap(classMap);
}
classMap.Freeze();
return classMap;
return classMap.Freeze();
}
}
@ -262,7 +261,7 @@ namespace MongoDB.Bson.DefaultSerializer {
return creator.Invoke();
}
public void Freeze() {
public BsonClassMap Freeze() {
lock (staticLock) {
if (!frozen) {
freezeNestingLevel++;
@ -342,6 +341,7 @@ namespace MongoDB.Bson.DefaultSerializer {
freezeNestingLevel--;
}
}
return this;
}
}

2
Bson/IO/BsonBinaryReader.cs

@ -40,7 +40,7 @@ namespace MongoDB.Bson.IO {
this.buffer = buffer;
this.disposeBuffer = false;
}
this.settings = settings;
this.settings = settings.Freeze();
context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
}
#endregion

28
Bson/IO/BsonBinaryReaderSettings.cs

@ -28,6 +28,7 @@ namespace MongoDB.Bson.IO {
private bool closeInput = false;
private bool fixOldBinarySubTypeOnInput = true;
private int maxDocumentSize = BsonDefaults.MaxDocumentSize;
private bool isFrozen;
#endregion
#region constructors
@ -38,23 +39,44 @@ namespace MongoDB.Bson.IO {
#region public static properties
public static BsonBinaryReaderSettings Defaults {
get { return defaults; }
set { defaults = value; }
}
#endregion
#region public properties
public bool CloseInput {
get { return closeInput; }
set { closeInput = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen"); }
closeInput = value;
}
}
public bool FixOldBinarySubTypeOnInput {
get { return fixOldBinarySubTypeOnInput; }
set { fixOldBinarySubTypeOnInput = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen"); }
fixOldBinarySubTypeOnInput = value;
}
}
public bool IsFrozen {
get { return isFrozen; }
}
public int MaxDocumentSize {
get { return maxDocumentSize; }
set { maxDocumentSize = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen"); }
maxDocumentSize = value;
}
}
#endregion
#region public methods
public BsonBinaryReaderSettings Freeze() {
isFrozen = true;
return this;
}
#endregion
}

2
Bson/IO/BsonBinaryWriter.cs

@ -43,7 +43,7 @@ namespace MongoDB.Bson.IO {
this.buffer = buffer;
this.disposeBuffer = false;
}
this.settings = settings;
this.settings = settings.Freeze();
context = null;
state = BsonWriterState.Initial;

28
Bson/IO/BsonBinaryWriterSettings.cs

@ -29,6 +29,7 @@ namespace MongoDB.Bson.IO {
private bool closeOutput = false;
private bool fixOldBinarySubTypeOnOutput = true;
private int maxDocumentSize = BsonDefaults.MaxDocumentSize;
private bool isFrozen;
#endregion
#region constructors
@ -39,23 +40,44 @@ namespace MongoDB.Bson.IO {
#region public static properties
public static BsonBinaryWriterSettings Defaults {
get { return defaults; }
set { defaults = value; }
}
#endregion
#region public properties
public bool CloseOutput {
get { return closeOutput; }
set { closeOutput = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen"); }
closeOutput = value;
}
}
public bool FixOldBinarySubTypeOnOutput {
get { return fixOldBinarySubTypeOnOutput; }
set { fixOldBinarySubTypeOnOutput = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen"); }
fixOldBinarySubTypeOnOutput = value;
}
}
public bool IsFrozen {
get { return isFrozen; }
}
public int MaxDocumentSize {
get { return maxDocumentSize; }
set { maxDocumentSize = value; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen"); }
maxDocumentSize = value;
}
}
#endregion
#region public methods
public BsonBinaryWriterSettings Freeze() {
isFrozen = true;
return this;
}
#endregion
}

2
Bson/IO/JsonWriter.cs

@ -35,7 +35,7 @@ namespace MongoDB.Bson.IO {
JsonWriterSettings settings
) {
this.textWriter = writer;
this.settings = settings;
this.settings = settings.Freeze();
context = new JsonWriterContext(null, ContextType.TopLevel, "");
state = BsonWriterState.Initial;
}

45
Bson/IO/JsonWriterSettings.cs

@ -32,6 +32,7 @@ namespace MongoDB.Bson.IO {
private string indentChars = " ";
private string newLineChars = "\r\n";
private JsonOutputMode outputMode = JsonOutputMode.Strict;
private bool isFrozen;
#endregion
#region constructors
@ -41,39 +42,69 @@ namespace MongoDB.Bson.IO {
#region public static properties
public static JsonWriterSettings Defaults {
get { return defaults; } // TODO: clone?
get { return defaults; }
set { defaults = value; }
}
#endregion
#region public properties
public bool CloseOutput {
get { return closeOutput; }
set { closeOutput = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
closeOutput = value;
}
}
public Encoding Encoding {
get { return encoding; }
set { encoding = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
encoding = value;
}
}
public bool Indent {
get { return indent; }
set { indent = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
indent = value;
}
}
public string IndentChars {
get { return indentChars; }
set { indentChars = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
indentChars = value;
}
}
public bool IsFrozen {
get { return isFrozen; }
}
public string NewLineChars {
get { return newLineChars; }
set { newLineChars = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
newLineChars = value;
}
}
public JsonOutputMode OutputMode {
get { return outputMode; }
set { outputMode = value; }
set {
if (isFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen"); }
outputMode = value;
}
}
#endregion
#region public methods
public JsonWriterSettings Freeze() {
isFrozen = true;
return this;
}
#endregion
}

2
Driver/Core/MongoCollection.cs

@ -50,7 +50,7 @@ namespace MongoDB.Driver {
ValidateCollectionName(settings.CollectionName);
this.server = database.Server;
this.database = database;
this.settings = settings;
this.settings = settings.Freeze();
this.name = settings.CollectionName;
}
#endregion

3
Driver/Core/MongoCollectionSettings.cs

@ -89,12 +89,13 @@ namespace MongoDB.Driver {
#endregion
#region public methods
public void Freeze() {
public MongoCollectionSettings Freeze() {
if (!isFrozen) {
frozenHashCode = GetHashCodeHelper();
frozenStringRepresentation = ToStringHelper();
isFrozen = true;
}
return this;
}
public override bool Equals(object obj) {

3
Driver/Core/MongoDatabase.cs

@ -52,7 +52,7 @@ namespace MongoDB.Driver {
) {
ValidateDatabaseName(settings.DatabaseName);
this.server = server;
this.settings = settings;
this.settings = settings.Freeze();
this.name = settings.DatabaseName;
// make sure commands get routed to the primary server by using slaveOk false
@ -390,7 +390,6 @@ namespace MongoDB.Driver {
) {
lock (databaseLock) {
MongoCollection collection;
collectionSettings.Freeze();
if (!collections.TryGetValue(collectionSettings, out collection)) {
collection = new MongoCollection<TDefaultDocument>(this, collectionSettings);
collections.Add(collectionSettings, collection);

3
Driver/Core/MongoDatabaseSettings.cs

@ -82,12 +82,13 @@ namespace MongoDB.Driver {
#endregion
#region public methods
public void Freeze() {
public MongoDatabaseSettings Freeze() {
if (!isFrozen) {
frozenHashCode = GetHashCodeHelper();
frozenStringRepresentation = ToStringHelper();
isFrozen = true;
}
return this;
}
public override bool Equals(object obj) {

4
Driver/Core/MongoServer.cs

@ -60,7 +60,7 @@ namespace MongoDB.Driver {
public MongoServer(
MongoServerSettings settings
) {
this.settings = settings;
this.settings = settings.Freeze();
foreach (var address in settings.Servers) {
endPoints.Add(address.ToIPEndPoint());
@ -107,7 +107,6 @@ namespace MongoDB.Driver {
) {
lock (staticLock) {
MongoServer server;
settings.Freeze();
if (!servers.TryGetValue(settings, out server)) {
server = new MongoServer(settings);
servers.Add(settings, server);
@ -533,7 +532,6 @@ namespace MongoDB.Driver {
) {
lock (serverLock) {
MongoDatabase database;
databaseSettings.Freeze();
if (!databases.TryGetValue(databaseSettings, out database)) {
database = new MongoDatabase(this, databaseSettings);
databases.Add(databaseSettings, database);

3
Driver/Core/MongoServerSettings.cs

@ -222,12 +222,13 @@ namespace MongoDB.Driver {
#endregion
#region public methods
public void Freeze() {
public MongoServerSettings Freeze() {
if (!isFrozen) {
frozenHashCode = GetHashCodeHelper();
frozenStringRepresentation = ToStringHelper();
isFrozen = true;
}
return this;
}
public override bool Equals(object obj) {

3
Driver/Core/MongoUrl.cs

@ -48,8 +48,7 @@ namespace MongoDB.Driver {
string url
) {
var builder = new MongoUrlBuilder(url); // parses url
serverSettings = builder.ToServerSettings();
serverSettings.Freeze();
serverSettings = builder.ToServerSettings().Freeze();
this.waitQueueMultiple = builder.WaitQueueMultiple;
this.waitQueueSize = builder.WaitQueueSize;
this.databaseName = builder.DatabaseName;

5
Driver/GridFS/MongoGridFS.cs

@ -42,11 +42,8 @@ namespace MongoDB.Driver.GridFS {
MongoDatabase database,
MongoGridFSSettings settings
) {
if (!settings.IsFrozen) {
settings = settings.Clone().Freeze();
}
this.database = database;
this.settings = settings;
this.settings = settings.Freeze();
this.chunks = database[settings.ChunksCollectionName, settings.SafeMode];
this.files = database[settings.FilesCollectionName, settings.SafeMode];
}

Loading…
Cancel
Save