/// <summary> /// Initializes a new instance of the <see cref="IdGenerator"/> class. The <see cref="DefaultTimeSource"/> is /// used to retrieve timestamp information. /// </summary> /// <param name="generatorId">The Id of the generator.</param> /// <param name="epoch">The Epoch of the generator.</param> /// <param name="maskConfig">The <see cref="MaskConfig"/> of the generator.</param> /// <exception cref="ArgumentNullException">Thrown when maskConfig is null.</exception> /// <exception cref="InvalidOperationException">Thrown when maskConfig defines a non-63 bit bitmask.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when GeneratorId or Sequence masks are >31 bit, GeneratorId exceeds maximum value or epoch in future. /// </exception> public IdGenerator(int generatorId, DateTimeOffset epoch, MaskConfig maskConfig) : this(generatorId, maskConfig, new DefaultTimeSource(epoch)) { }
/// <summary> /// Initializes a new instance of the <see cref="IdGenerator"/> class. The <see cref="DefaultTimeSource"/> is /// used to retrieve timestamp information. /// </summary> /// <param name="generatorId">The Id of the generator.</param> /// <param name="maskConfig">The <see cref="MaskConfig"/> of the generator.</param> /// <exception cref="ArgumentNullException">Thrown when maskConfig is null.</exception> /// <exception cref="InvalidOperationException">Thrown when maskConfig defines a non-63 bit bitmask.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when GeneratorId or Sequence masks are >31 bit, GeneratorId exceeds maximum value or epoch in future. /// </exception> public IdGenerator(int generatorId, MaskConfig maskConfig) : this(generatorId, maskConfig, new DefaultTimeSource(DefaultEpoch)) { }
public void MaskConfigProperty_Returns_CorrectValue() { var md = MaskConfig.Default; var mc = new MaskConfig(21, 21, 21); Assert.ReferenceEquals(md, new IdGenerator(0, TESTEPOCH, md).MaskConfig); Assert.ReferenceEquals(mc, new IdGenerator(0, TESTEPOCH, mc).MaskConfig); }
public void GeneratorId_ShouldBePresent_InID2() { // We setup our generator so that the time (current - epoch) results in 0, generator id 4095 so that all 12 bits // are set for the generator. var ts = new MockTimeSource(TESTEPOCH); var m = new MaskConfig(40, 12, 11); // We use a custom mask-config with 12 bits for the generator this time var g = new IdGenerator(4095, TESTEPOCH, m, ts); // Make sure all expected bits are set Assert.AreEqual(-1 & ((1 << 12) - 1), g.Id); Assert.AreEqual((1 << 12) - 1 << 11, g.CreateId()); }
/// <summary> /// Initializes a new instance of the <see cref="IdGenerator"/> class. The <see cref="DefaultTimeSource"/> is /// used to retrieve timestamp information. /// </summary> /// <param name="generatorId">The Id of the generator.</param> /// <param name="epoch">The Epoch of the generator.</param> /// <param name="maskConfig">The <see cref="MaskConfig"/> of the generator.</param> /// <exception cref="ArgumentNullException">Thrown when maskConfig is null.</exception> /// <exception cref="InvalidOperationException">Thrown when maskConfig defines a non-63 bit bitmask.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when GeneratorId or Sequence masks are >31 bit, GeneratorId exceeds maximum value or epoch in future. /// </exception> public IdGenerator(int generatorId, DateTimeOffset epoch, MaskConfig maskConfig) : this(generatorId, epoch, maskConfig, defaulttimesource) { }
/// <summary> /// Returns a new instance of an <see cref="IdGenerator"/> based on the (managed) thread this method is invoked on. /// </summary> /// <param name="epoch">The Epoch of the generator.</param> /// <param name="maskConfig">The <see cref="MaskConfig"/> of the generator.</param> /// <param name="timeSource">The time-source to use when acquiring time data.</param> /// <returns>A new instance of an <see cref="IdGenerator"/> based on the (managed) thread this method is invoked on.</returns> /// <remarks> /// Note: This method can be used when using several threads on a single machine to get thread-specific generators; /// if this method is used across machines there's a high probability of collisions in generator-id's. In that /// case prefer to explicitly set the generator id's via configuration file or other means instead. /// </remarks> public static IdGenerator GetThreadSpecificGenerator(DateTime epoch, MaskConfig maskConfig, ITimeSource timeSource) { return(new IdGenerator(GetThreadId() & (int)GetMask(maskConfig.GeneratorIdBits), epoch, maskConfig, timeSource)); }
/// <summary> /// Returns a new instance of an <see cref="IdGenerator"/> based on the (managed) thread this method is invoked on. /// </summary> /// <param name="epoch">The Epoch of the generator.</param> /// <param name="maskConfig">The <see cref="MaskConfig"/> of the generator.</param> /// <returns>A new instance of an <see cref="IdGenerator"/> based on the (managed) thread this method is invoked on.</returns> /// <remarks> /// Note: This method can be used when using several threads on a single machine to get thread-specific generators; /// if this method is used across machines there's a high probability of collisions in generator-id's. In that /// case prefer to explicitly set the generator id's via configuration file or other means instead. /// </remarks> public static IdGenerator GetThreadSpecificGenerator(DateTime epoch, MaskConfig maskConfig) { return(GetThreadSpecificGenerator(epoch, maskConfig, defaulttimesource)); }