From d829c97e56deff6dd449205601e517cfcd717ec7 Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Thu, 13 Mar 2025 10:15:17 +0300 Subject: [PATCH] Enhancing the two helper methods when fetching the generation time of the generated snowflakeId --- src/core/SnowflakOptions.cs | 7 ++++++- src/core/SnowflakeId.cs | 12 ++++++++++++ src/core/SnowflakeIdService.cs | 31 +++++++++++++++++-------------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/core/SnowflakOptions.cs b/src/core/SnowflakOptions.cs index 1931ad3..06ebf0c 100644 --- a/src/core/SnowflakOptions.cs +++ b/src/core/SnowflakOptions.cs @@ -7,8 +7,13 @@ public class SnowflakOptions /// /// 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. /// public int DataCenterId { get; set; } + + /// + /// Get or set the value that determined whether using console log or not, the default value is false. + /// + public bool UseConsoleLog { get; set; } } } diff --git a/src/core/SnowflakeId.cs b/src/core/SnowflakeId.cs index d101df8..82dff6e 100644 --- a/src/core/SnowflakeId.cs +++ b/src/core/SnowflakeId.cs @@ -6,7 +6,19 @@ namespace SnowflakeId.Core { public class SnowflakeId { + /// + /// Get or set snowflakeId. + /// public long Id { get; set; } + + /// + /// Get or set the generation datetime of the snowflakeId. + /// public DateTime GeneratedDateTime { get; set; } + + /// + /// Get or set the data centerid. + /// + public int DataCenterId { get; set; } } } diff --git a/src/core/SnowflakeIdService.cs b/src/core/SnowflakeIdService.cs index 9ea4b66..9ef900e 100644 --- a/src/core/SnowflakeIdService.cs +++ b/src/core/SnowflakeIdService.cs @@ -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) @@ -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; } } @@ -94,14 +95,14 @@ public virtual Task 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) @@ -117,7 +118,8 @@ public virtual Task 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 @@ -147,6 +149,7 @@ public virtual SnowflakeId GetSnowflakeById(long snowflakeId) result.GeneratedDateTime = snowflakeIdGeneratedTime; result.Id = snowflakeId; + result.DataCenterId = GetDataCenterIdBySnowflakeId(snowflakeId); return result; } @@ -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; } /// @@ -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; }