|
|
@ -24,23 +24,36 @@ using System.Text.RegularExpressions; |
|
|
|
using MongoDB.Bson.IO; |
|
|
|
|
|
|
|
namespace MongoDB.Bson.Serialization { |
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator for Guids using the COMB algorithm.
|
|
|
|
/// </summary>
|
|
|
|
public class CombGuidGenerator : IIdGenerator { |
|
|
|
#region private static fields
|
|
|
|
private static CombGuidGenerator instance = new CombGuidGenerator(); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the CombGuidGenerator class.
|
|
|
|
/// </summary>
|
|
|
|
public CombGuidGenerator() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static properties
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an instance of CombGuidGenerator.
|
|
|
|
/// </summary>
|
|
|
|
public static CombGuidGenerator Instance { |
|
|
|
get { return instance; } |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Generates an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An Id.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
var guidArray = Guid.NewGuid().ToByteArray(); |
|
|
|
|
|
|
@ -62,6 +75,11 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
return new Guid(guidArray); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
@ -70,27 +88,45 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator for Guids.
|
|
|
|
/// </summary>
|
|
|
|
public class GuidGenerator : IIdGenerator { |
|
|
|
#region private static fields
|
|
|
|
private static GuidGenerator instance = new GuidGenerator(); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the GuidGenerator class.
|
|
|
|
/// </summary>
|
|
|
|
public GuidGenerator() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static properties
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an instance of GuidGenerator.
|
|
|
|
/// </summary>
|
|
|
|
public static GuidGenerator Instance { |
|
|
|
get { return instance; } |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Generates an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An Id.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
return Guid.NewGuid(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
@ -99,27 +135,45 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator that only checks that the Id is not null.
|
|
|
|
/// </summary>
|
|
|
|
public class NullIdChecker : IIdGenerator { |
|
|
|
#region private static fields
|
|
|
|
private static NullIdChecker instance = new NullIdChecker(); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the NullIdChecker class.
|
|
|
|
/// </summary>
|
|
|
|
public NullIdChecker() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static properties
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an instance of NullIdChecker.
|
|
|
|
/// </summary>
|
|
|
|
public static NullIdChecker Instance { |
|
|
|
get { return instance; } |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Throws an exception because we can't generate an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Nothing.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
throw new InvalidOperationException("Id cannot be null"); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
@ -128,27 +182,45 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator for ObjectIds.
|
|
|
|
/// </summary>
|
|
|
|
public class ObjectIdGenerator : IIdGenerator { |
|
|
|
#region private static fields
|
|
|
|
private static ObjectIdGenerator instance = new ObjectIdGenerator(); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the ObjectIdGenerator class.
|
|
|
|
/// </summary>
|
|
|
|
public ObjectIdGenerator() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static properties
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an instance of ObjectIdGenerator.
|
|
|
|
/// </summary>
|
|
|
|
public static ObjectIdGenerator Instance { |
|
|
|
get { return instance; } |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Generates an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An Id.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
return ObjectId.GenerateNewId(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
@ -157,27 +229,45 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator for ObjectIds represented internally as strings.
|
|
|
|
/// </summary>
|
|
|
|
public class StringObjectIdGenerator : IIdGenerator { |
|
|
|
#region private static fields
|
|
|
|
private static StringObjectIdGenerator instance = new StringObjectIdGenerator(); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the StringObjectIdGenerator class.
|
|
|
|
/// </summary>
|
|
|
|
public StringObjectIdGenerator() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static properties
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an instance of StringObjectIdGenerator.
|
|
|
|
/// </summary>
|
|
|
|
public static StringObjectIdGenerator Instance { |
|
|
|
get { return instance; } |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Generates an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An Id.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
return ObjectId.GenerateNewId().ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
@ -186,18 +276,33 @@ namespace MongoDB.Bson.Serialization { |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an Id generator that only checks that the Id is not all zeros.
|
|
|
|
/// </summary>
|
|
|
|
// TODO: is it worth trying to remove the dependency on IEquatable<T>?
|
|
|
|
public class ZeroIdChecker<T> : IIdGenerator where T : struct, IEquatable<T> { |
|
|
|
#region constructors
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the ZeroIdChecker class.
|
|
|
|
/// </summary>
|
|
|
|
public ZeroIdChecker() { |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
/// <summary>
|
|
|
|
/// Throws an exception because we can't generate an Id.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An Id.</returns>
|
|
|
|
public object GenerateId() { |
|
|
|
throw new InvalidOperationException("Id cannot be default value (all zeros)"); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests whether an Id is empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The Id.</param>
|
|
|
|
/// <returns>True if the Id is empty.</returns>
|
|
|
|
public bool IsEmpty( |
|
|
|
object id |
|
|
|
) { |
|
|
|