Skip to content

Commit

Permalink
Refactored how GuidByteOrder is configured and handled. Instead of al…
Browse files Browse the repository at this point in the history
…lowing it to be configured at the individual property level (which was a pain), it is now configured at the server, database and collection levels. This introduces the restriction that all Guids in the same collection must use the same GuidByteOrder, but this restriction should actually help keep things sane and manageable.
  • Loading branch information
rstam committed Jun 5, 2011
1 parent 5d9d37b commit ddc1db0
Show file tree
Hide file tree
Showing 53 changed files with 1,009 additions and 479 deletions.
6 changes: 4 additions & 2 deletions Bson/Bson.csproj
Expand Up @@ -81,15 +81,17 @@
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="BsonExtensionMethods.cs" />
<Compile Include="IO\BsonDocumentReaderSettings.cs" />
<Compile Include="IO\BsonDocumentWriterSettings.cs" />
<Compile Include="ObjectModel\GuidConverter.cs" />
<Compile Include="IO\JsonReaderSettings.cs" />
<Compile Include="Serialization\Attributes\BsonDateTimeOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonExtraElementsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonGuidOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonRepresentationAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonSerializationOptionsAttribute.cs" />
<Compile Include="Serialization\Conventions\ExtraElementsMemberConventions.cs" />
<Compile Include="Serialization\Options\DateTimeSerializationOptions.cs" />
<Compile Include="Serialization\Options\DocumentSerializationOptions.cs" />
<Compile Include="Serialization\Options\GuidSerializationOptions.cs" />
<Compile Include="Serialization\Options\RepresentationSerializationOptions.cs" />
<Compile Include="Exceptions\TruncationException.cs" />
<Compile Include="IO\BsonBaseReader.cs" />
Expand Down
9 changes: 9 additions & 0 deletions Bson/BsonDefaults.cs
Expand Up @@ -24,11 +24,20 @@ namespace MongoDB.Bson {
/// </summary>
public static class BsonDefaults {
#region private static fields
private static GuidByteOrder guidByteOrder = GuidByteOrder.LittleEndian;
private static int initialBsonBufferSize = 4 * 1024; // 4KiB
private static int maxDocumentSize = 4 * 1024 * 1024; // 4MiB
#endregion

#region public static properties
/// <summary>
/// Gets or sets the default Guid byte order.
/// </summary>
public static GuidByteOrder GuidByteOrder {
get { return guidByteOrder; }
set { guidByteOrder = value; }
}

/// <summary>
/// Gets or sets the default initial BSON buffer size.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions Bson/IO/BsonBinaryReader.cs
Expand Up @@ -60,6 +60,13 @@ BsonBinaryReaderSettings settings
public BsonBuffer Buffer {
get { return buffer; }
}

/// <summary>
/// Gets the byte order for Guids.
/// </summary>
public override GuidByteOrder GuidByteOrder {
get { return settings.GuidByteOrder; }
}
#endregion

#region public methods
Expand Down
16 changes: 16 additions & 0 deletions Bson/IO/BsonBinaryReaderSettings.cs
Expand Up @@ -30,6 +30,7 @@ public class BsonBinaryReaderSettings {
#region private fields
private bool closeInput = false;
private bool fixOldBinarySubTypeOnInput = true;
private GuidByteOrder guidByteOrder = BsonDefaults.GuidByteOrder;
private int maxDocumentSize = BsonDefaults.MaxDocumentSize;
private bool isFrozen;
#endregion
Expand All @@ -46,14 +47,17 @@ public class BsonBinaryReaderSettings {
/// </summary>
/// <param name="closeInput">Whether to close the input stream when the reader is closed.</param>
/// <param name="fixOldBinarySubTypeOnInput">Whether to fix occurrences of the old binary subtype on input.</param>
/// <param name="guidByteOrder">The byte order for Guids.</param>
/// <param name="maxDocumentSize">The max document size.</param>
public BsonBinaryReaderSettings(
bool closeInput,
bool fixOldBinarySubTypeOnInput,
GuidByteOrder guidByteOrder,
int maxDocumentSize
) {
this.closeInput = closeInput;
this.fixOldBinarySubTypeOnInput = fixOldBinarySubTypeOnInput;
this.guidByteOrder = guidByteOrder;
this.maxDocumentSize = maxDocumentSize;
}
#endregion
Expand Down Expand Up @@ -91,6 +95,17 @@ int maxDocumentSize
}
}

/// <summary>
/// Gets or sets the byte order for Guids.
/// </summary>
public GuidByteOrder GuidByteOrder {
get { return guidByteOrder; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
guidByteOrder = value;
}
}

/// <summary>
/// Gets whether the settings are frozen.
/// </summary>
Expand Down Expand Up @@ -119,6 +134,7 @@ int maxDocumentSize
return new BsonBinaryReaderSettings(
closeInput,
fixOldBinarySubTypeOnInput,
guidByteOrder,
maxDocumentSize
);
}
Expand Down
7 changes: 7 additions & 0 deletions Bson/IO/BsonBinaryWriter.cs
Expand Up @@ -66,6 +66,13 @@ BsonBinaryWriterSettings settings
public BsonBuffer Buffer {
get { return buffer; }
}

/// <summary>
/// Gets the byte order for Guids.
/// </summary>
public override GuidByteOrder GuidByteOrder {
get { return settings.GuidByteOrder; }
}
#endregion

#region public methods
Expand Down
16 changes: 16 additions & 0 deletions Bson/IO/BsonBinaryWriterSettings.cs
Expand Up @@ -31,6 +31,7 @@ public class BsonBinaryWriterSettings {
#region private fields
private bool closeOutput = false;
private bool fixOldBinarySubTypeOnOutput = true;
private GuidByteOrder guidByteOrder = BsonDefaults.GuidByteOrder;
private int maxDocumentSize = BsonDefaults.MaxDocumentSize;
private bool isFrozen;
#endregion
Expand All @@ -47,14 +48,17 @@ public class BsonBinaryWriterSettings {
/// </summary>
/// <param name="closeOutput">Whether to close the output stream when the writer is closed.</param>
/// <param name="fixOldBinarySubTypeOnOutput">Whether to fix old binary data subtype on output.</param>
/// <param name="guidByteOrder">The byte order for Guids.</param>
/// <param name="maxDocumentSize">The max document size.</param>
public BsonBinaryWriterSettings(
bool closeOutput,
bool fixOldBinarySubTypeOnOutput,
GuidByteOrder guidByteOrder,
int maxDocumentSize
) {
this.closeOutput = closeOutput;
this.fixOldBinarySubTypeOnOutput = fixOldBinarySubTypeOnOutput;
this.guidByteOrder = guidByteOrder;
this.maxDocumentSize = maxDocumentSize;
}
#endregion
Expand Down Expand Up @@ -92,6 +96,17 @@ int maxDocumentSize
}
}

/// <summary>
/// Gets or sets the byte order for Guids.
/// </summary>
public GuidByteOrder GuidByteOrder {
get { return guidByteOrder; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
guidByteOrder = value;
}
}

/// <summary>
/// Gets whether the settings are frozen.
/// </summary>
Expand Down Expand Up @@ -120,6 +135,7 @@ int maxDocumentSize
return new BsonBinaryWriterSettings(
closeOutput,
fixOldBinarySubTypeOnOutput,
guidByteOrder,
maxDocumentSize
);
}
Expand Down
12 changes: 11 additions & 1 deletion Bson/IO/BsonDocumentReader.cs
Expand Up @@ -25,6 +25,7 @@ namespace MongoDB.Bson.IO {
/// </summary>
public class BsonDocumentReader : BsonBaseReader {
#region private fields
private BsonDocumentReaderSettings settings;
private BsonDocumentReaderContext context;
private BsonValue currentValue;
#endregion
Expand All @@ -34,15 +35,24 @@ public class BsonDocumentReader : BsonBaseReader {
/// Initializes a new instance of the BsonDocumentReader class.
/// </summary>
/// <param name="document">A BsonDocument.</param>
/// <param name="settings">The reader settings.</param>
public BsonDocumentReader(
BsonDocument document
BsonDocument document,
BsonDocumentReaderSettings settings
) {
context = new BsonDocumentReaderContext(null, ContextType.TopLevel, document);
currentValue = document;
this.settings = settings.Freeze();
}
#endregion

#region public properties
/// <summary>
/// Gets the byte order for Guids.
/// </summary>
public override GuidByteOrder GuidByteOrder {
get { return settings.GuidByteOrder; }
}
#endregion

#region public methods
Expand Down
104 changes: 104 additions & 0 deletions Bson/IO/BsonDocumentReaderSettings.cs
@@ -0,0 +1,104 @@
/* Copyright 2010-2011 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MongoDB.Bson.IO {
/// <summary>
/// Represents settings for a BsonDocumentReader.
/// </summary>
public class BsonDocumentReaderSettings {
#region private static fields
private static BsonDocumentReaderSettings defaults = new BsonDocumentReaderSettings();
#endregion

#region private fields
private GuidByteOrder guidByteOrder = BsonDefaults.GuidByteOrder;
private bool isFrozen;
#endregion

#region constructors
/// <summary>
/// Initializes a new instance of the BsonDocumentReaderSettings class.
/// </summary>
public BsonDocumentReaderSettings() {
}

/// <summary>
/// Initializes a new instance of the BsonDocumentReaderSettings class.
/// </summary>
/// <param name="guidByteOrder">The byte order for Guids.</param>
public BsonDocumentReaderSettings(
GuidByteOrder guidByteOrder
) {
this.guidByteOrder = guidByteOrder;
}
#endregion

#region public static properties
/// <summary>
/// Gets or sets the default settings for a BsonDocumentReader.
/// </summary>
public static BsonDocumentReaderSettings Defaults {
get { return defaults; }
set { defaults = value; }
}
#endregion

#region public properties
/// <summary>
/// Gets or sets the byte order for Guids.
/// </summary>
public GuidByteOrder GuidByteOrder {
get { return guidByteOrder; }
set {
if (isFrozen) { throw new InvalidOperationException("BsonDocumentReaderSettings is frozen."); }
guidByteOrder = value;
}
}

/// <summary>
/// Gets whether the settings are frozen.
/// </summary>
public bool IsFrozen {
get { return isFrozen; }
}
#endregion

#region public methods
/// <summary>
/// Creates a clone of the settings.
/// </summary>
/// <returns>A clone of the settings.</returns>
public BsonDocumentReaderSettings Clone() {
return new BsonDocumentReaderSettings(
guidByteOrder
);
}

/// <summary>
/// Freezes the settings.
/// </summary>
/// <returns>The settings.</returns>
public BsonDocumentReaderSettings Freeze() {
isFrozen = true;
return this;
}
#endregion
}
}
20 changes: 12 additions & 8 deletions Bson/IO/BsonDocumentWriter.cs
Expand Up @@ -26,31 +26,35 @@ namespace MongoDB.Bson.IO {
public class BsonDocumentWriter : BsonBaseWriter {
#region private fields
private BsonDocument topLevelDocument;
private BsonDocumentWriterSettings settings;
private BsonDocumentWriterContext context;
#endregion

#region constructors
/// <summary>
/// Initializes a new instance of the BsonDocumentWriter class.
/// </summary>
public BsonDocumentWriter()
: this(new BsonDocument()) {
}

/// <summary>
/// Initializes a new instance of the BsonDocumentWriter class.
/// </summary>
/// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param>
/// <param name="settings">The settings.</param>
public BsonDocumentWriter(
BsonDocument topLevelDocument
BsonDocument topLevelDocument,
BsonDocumentWriterSettings settings
) {
this.topLevelDocument = topLevelDocument;
this.settings = settings.Freeze();
context = null;
state = BsonWriterState.Initial;
}
#endregion

#region public properties
/// <summary>
/// Gets the byte order for Guids.
/// </summary>
public override GuidByteOrder GuidByteOrder {
get { return settings.GuidByteOrder; }
}

/// <summary>
/// Gets the top level BsonDocument.
/// </summary>
Expand Down

0 comments on commit ddc1db0

Please sign in to comment.