Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/core/SnowflakOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ public class SnowflakOptions
/// <summary>
/// This could be data center id or server id,
/// or any other unique things in your webfarm
/// Be sure to set this property with number greater than zero
/// Be sure to set this property with number greater than zero.
/// </summary>
public int DataCenterId { get; set; }

/// <summary>
/// Get or set the value that determined whether using console log or not, the default value is <c>false</c>.
/// </summary>
public bool UseConsoleLog { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/core/SnowflakeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ namespace SnowflakeId.Core
{
public class SnowflakeId
{
/// <summary>
/// Get or set snowflakeId.
/// </summary>
public long Id { get; set; }

/// <summary>
/// Get or set the generation datetime of the snowflakeId.
/// </summary>
public DateTime GeneratedDateTime { get; set; }

/// <summary>
/// Get or set the data centerid.
/// </summary>
public int DataCenterId { get; set; }
}
}
31 changes: 17 additions & 14 deletions src/core/SnowflakeIdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public virtual long GenerateSnowflakeId()

if (currentTimestamp < _lastTimestamp)
{
_logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp);
if (_snowflakOptions.UseConsoleLog)
_logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp);
throw new InvalidOperationException("Error_In_The_Server_Clock");
}

if (currentTimestamp == _lastTimestamp)
{
// generate a new timestamp when the _sequence is reached the ( 4096 - 1 )

_sequence = (_sequence + 1) & SnowflakeIdConfig.MaxSequenceId;

if (_sequence == 0)
Expand All @@ -73,7 +73,8 @@ public virtual long GenerateSnowflakeId()
_lastTimestamp = currentTimestamp;

long result = (currentTimestamp << _timeStampShift) | ((long)_snowflakOptions.DataCenterId << _machaineIdShift) | (_sequence);
_logger.LogInformation("the gnerated unique id is {0}", result);
if (_snowflakOptions.UseConsoleLog)
_logger.LogInformation("the gnerated unique id is {0}", result);
return result;
}
}
Expand All @@ -94,14 +95,14 @@ public virtual Task<long> GenerateSnowflakeIdAsync(CancellationToken cancellatio

if (currentTimestamp < _lastTimestamp)
{
_logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp);
if (_snowflakOptions.UseConsoleLog)
_logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp);
throw new InvalidOperationException("Error_In_The_Server_Clock");
}

if (currentTimestamp == _lastTimestamp)
{
// generate a new timestamp when the _sequence is reached the ( 4096 - 1 )

_sequence = (_sequence + 1) & SnowflakeIdConfig.MaxSequenceId;

if (_sequence == 0)
Expand All @@ -117,7 +118,8 @@ public virtual Task<long> GenerateSnowflakeIdAsync(CancellationToken cancellatio
_lastTimestamp = currentTimestamp;

long result = (currentTimestamp << _timeStampShift) | ((long)_snowflakOptions.DataCenterId << _machaineIdShift) | (_sequence);
_logger.LogInformation("the gnerated unique id is {0}", result);
if (_snowflakOptions.UseConsoleLog)
_logger.LogInformation("the gnerated unique id is {0}", result);
return Task.FromResult(result);
}
finally
Expand Down Expand Up @@ -147,6 +149,7 @@ public virtual SnowflakeId GetSnowflakeById(long snowflakeId)

result.GeneratedDateTime = snowflakeIdGeneratedTime;
result.Id = snowflakeId;
result.DataCenterId = GetDataCenterIdBySnowflakeId(snowflakeId);
return result;

}
Expand Down Expand Up @@ -190,10 +193,10 @@ public virtual long GetSecondsSinceUnixEpochFromId(long snowflakeId)
return 0L;
}

string result = Convert.ToString(snowflakeId, 2).PadLeft(64, '0');
string getTimestampInbit = result.Substring(1, 41);
long timestamp = Convert.ToInt64(getTimestampInbit, 2);
return timestamp;
// 41 bits of 1s, will shifted left by 22 bits.
long timestampMask = 0x1FFFFFFFFFF;
long timeStamp = (snowflakeId >> _timeStampShift) & timestampMask;
return timeStamp;
}

/// <summary>
Expand All @@ -208,11 +211,11 @@ public virtual int GetDataCenterIdBySnowflakeId(long snowflakeId)
return 0;
}

string result = Convert.ToString(snowflakeId, 2).PadLeft(64, '0');
string dataCenterInBits = result.Substring(42, 10);
int dataCenterId = Convert.ToInt32(dataCenterInBits, 2);
return dataCenterId;
// 10 bits mask (0b1111111111) will shifted left by 12 bits.
long dataCenterIdMask = 0x3FF;

long dataCenterId = (snowflakeId >> _machaineIdShift) & dataCenterIdMask;
return (int)dataCenterId;
}


Expand Down