SourceMax.SimpleFlake/FlakeGenerator.cs
/// <summary>
/// Creates a Flake object based on a given date and time that can be used as part of a distributed ID system.
/// </summary>
/// <returns>A Flake that wraps an underlying BigInteger value made up of 41 bits of timestamp data plus 23 bits of random bits to avoid collisions in a distributed system.</returns>
Creates a Flake object based on a given date and time that can be used as part of a distributed ID system.
Creates a Flake object based on a given date and time that can be used as part of a distributed ID system .
public virtual T CreateFlake(DateTime timestamp) {
            if (timestamp < this.Epoch) {
                throw new Exception("The timestamp cannot be earlier than the Epoch time being used.");
            }
            var bigTicks = this.GetMillisecondsSinceEpoch(timestamp);
            var saltBytes = this.GetRandomBytes(3);
            var bigSalt = new BigInteger(saltBytes);
            bigSalt = BigInteger.Abs(bigSalt) >> 1;     
            bigTicks = bigTicks << 23;                  
            var result = bigTicks | bigSalt;
            var flake = this.InstantiateNewFlake(result);
            return flake;
        }
public virtual T CreateFlake ( DateTime timestamp ) { if ( timestamp < this . Epoch ) { throw new Exception ( " " ) ; } var bigTicks = this . GetMillisecondsSinceEpoch ( timestamp ) ; var saltBytes = this . GetRandomBytes ( 3 ) ; var bigSalt = new BigInteger ( saltBytes ) ; bigSalt = BigInteger . Abs ( bigSalt ) >> 1 ; bigTicks = bigTicks << 23 ; var result = bigTicks | bigSalt ; var flake = this . InstantiateNewFlake ( result ) ; return flake ; }
Creates a Flake object based on a given date and time that can be used as part of a distributed ID system.
Creates a Flake object based on a given date and time that can be used as part of a distributed ID system .
// 64-bit Id will be 41 bits of milliseconds + 23 bits random // Get some random bits // Convert the random bytes to a BigInteger so we can bit shift // We are going to bit-shift the two values so we end up with the  // correct number of bits in each value // Shift right to go from 24 bits to 23 // Shift left to make room for the Salt // Bitwise OR the bits together to get one big 64-bit string where the first 41 bits // are the timestamp and the last 23 are the salt // Create a new Flake of type T that will be returned
A Flake that wraps an underlying BigInteger value made up of 41 bits of timestamp data plus 23 bits of random bits to avoid collisions in a distributed system.
A Flake that wraps an underlying BigInteger value made up of 41 bits of timestamp data plus 23 bits of random bits to avoid collisions in a distributed system .
timestamp
DateTime