2727 - [ YANTask] ( #yantask )
2828 - [ YANProcess] ( #yanprocess )
2929 - [ YANExpression] ( #yanexpression )
30+ - [ YANLib.Snowflake] ( #yanlibsnowflake )
3031- [ Performance Benchmarks] ( #-performance-benchmarks )
3132- [ Code Examples] ( #-code-examples )
3233- [ Project Architecture] ( #-project-architecture )
@@ -361,6 +362,56 @@ var person = new Person { Name = "John", Age = 30 };
361362string name = (string )func (person ); // Returns "John"
362363```
363364
365+ ### YANLib.Snowflake
366+
367+ A robust implementation of the Snowflake ID algorithm for generating distributed, time-ordered unique identifiers in C# applications.
368+
369+ #### Key Features
370+
371+ - 64-bit unique ID generation
372+ - Alphabetic and alphanumeric string representations
373+ - Time-ordered IDs for easy sorting
374+ - Distributed system support with worker and datacenter IDs
375+ - Component extraction from existing IDs
376+ - Predefined bit allocation strategies for different use cases
377+ - Customizable bit allocation for specialized scaling needs
378+
379+ ``` csharp
380+ // Create an ID generator with worker ID 1 and datacenter ID 1
381+ var generator = new IdGenerator (1 , 1 );
382+
383+ // Generate a numeric ID
384+ long id = generator .NextId ();
385+ // Example: 6982190104624234496
386+
387+ // Generate an alphabetic ID (base-26, A-Z)
388+ string alphabeticId = generator .NextIdAlphabetic ();
389+ // Example: "BPNHEBKDMGLCQ"
390+
391+ // Generate an alphanumeric ID (base-36, 0-9, A-Z)
392+ string alphanumericId = generator .NextIdAlphanumeric ();
393+ // Example: "1Z9DHPKW0S"
394+
395+ // Create a generator with a predefined bit allocation strategy
396+ // MoreDistributed: 10 bits for worker ID (0-1023), 10 bits for datacenter ID (0-1023), 3 bits for sequence (0-7)
397+ var distributedGenerator = new IdGenerator (100 , 200 , BitAllocationStrategy .MoreDistributed );
398+
399+ // HighVolume: 2 bits for worker ID (0-3), 2 bits for datacenter ID (0-3), 19 bits for sequence (0-524,287)
400+ var highVolumeGenerator = new IdGenerator (1 , 1 , BitAllocationStrategy .HighVolume );
401+
402+ // Extract components from an ID
403+ var components = IdGenerator .ExtractIdComponents (id );
404+ DateTime timestamp = components .Timestamp ;
405+ long workerId = components .WorkerId ;
406+ long datacenterId = components .DatacenterId ;
407+
408+ // Extract components using the same strategy used to generate the ID
409+ var strategicComponents = IdGenerator .ExtractIdComponents (
410+ distributedGenerator .NextId (),
411+ BitAllocationStrategy .MoreDistributed
412+ );
413+ ```
414+
364415
365416## 📊 Performance Benchmarks
366417
@@ -468,6 +519,61 @@ public Customer PrepareCustomerData(Customer customer)
468519}
469520```
470521
522+ ### Unique ID Generation with Snowflake
523+
524+ ``` csharp
525+ public class IdService
526+ {
527+ private readonly IdGenerator _userIdGenerator ;
528+ private readonly IdGenerator _orderIdGenerator ;
529+ private readonly IdGenerator _highVolumeGenerator ;
530+
531+ public IdService ()
532+ {
533+ // Create separate generators for different entity types
534+ _userIdGenerator = new IdGenerator (1 , 1 );
535+ _orderIdGenerator = new IdGenerator (2 , 1 );
536+
537+ // Create a high-volume generator with custom bit allocation
538+ // 2 bits for worker ID, 2 bits for datacenter ID, 19 bits for sequence
539+ _highVolumeGenerator = new IdGenerator (
540+ workerId : 1 ,
541+ datacenterId : 1 ,
542+ sequence : 0 ,
543+ workerIdBits : 2 ,
544+ datacenterIdBits : 2 ,
545+ sequenceBits : 19
546+ );
547+ }
548+
549+ public string GenerateUserId ()
550+ {
551+ // Generate a compact alphanumeric ID for users
552+ return _userIdGenerator .NextIdAlphanumeric ();
553+ }
554+
555+ public long GenerateOrderId ()
556+ {
557+ // Generate a numeric ID for orders
558+ return _orderIdGenerator .NextId ();
559+ }
560+
561+ public long GenerateHighVolumeId ()
562+ {
563+ // Generate an ID optimized for high volume (up to 524,287 IDs per millisecond)
564+ return _highVolumeGenerator .NextId ();
565+ }
566+
567+ public DateTime GetIdCreationTime (long id )
568+ {
569+ // Extract the timestamp from an ID
570+ var components = IdGenerator .ExtractIdComponents (id );
571+
572+ return components .Timestamp ;
573+ }
574+ }
575+ ```
576+
471577### Random Test Data Generation
472578
473579``` csharp
@@ -532,6 +638,7 @@ YANLib is based on .NET 8.0 (LTS) and consists of the following main components:
532638- Task
533639- Process
534640- Expression
641+ - Snowflake
535642
536643### Project Structure
537644
@@ -581,9 +688,13 @@ YANLib/
581688│ ├── YANProcess.Collection.cs
582689│ └── ...
583690└── Advanced/
584- └── Expression/
585- ├── YANExpression.cs
586- ├── YANExpression.Implement.cs
691+ ├── Expression/
692+ │ ├── YANExpression.cs
693+ │ ├── YANExpression.Implement.cs
694+ │ └── ...
695+ └── Snowflake/
696+ ├── IdGenerator.Constant.cs
697+ ├── IdGenerator.Constructor.cs
587698 └── ...
588699```
589700
@@ -602,6 +713,7 @@ Each component in YANLib is implemented with careful attention to performance, m
602713- ** YANTask** : Uses HashSet<Task` <T> ` > for efficient task tracking and implements async/await patterns
603714- ** YANProcess** : Uses Process.GetProcessesByName() with proper resource cleanup
604715- ** YANExpression** : Uses expression trees with thread-safe caching in ConcurrentDictionary
716+ - ** YANLib.Snowflake** : Implements the Snowflake algorithm with bit manipulation for efficient ID generation and customizable bit allocation
605717
606718
607719### Important Notes
0 commit comments