mschuler / uniqueidgenerator Goto Github PK
View Code? Open in Web Editor NEWC# implementation of Twitter's Snowflake
License: MIT License
C# implementation of Twitter's Snowflake
License: MIT License
This lib has really cool hidden features.
After you have generated an ID, you can convert the ID into its parts:
I think it should be in the description.
I love this lib.
NextLong can return just long type since sql server don't support unsigned bigint type.
Hi,
Is this project dead? Can you provide a net core version?
I tried the generator (1 generator) for 1000 times for 3 threads, and it gives me duplicated values
// Create a new instance of the generator
Generator gen1 = new Generator(128, new DateTime(2019, 05, 1));
int max = 1000;
List<string> ids = new List<string>();
for (int i = 0; i < max; i++) {
ids.Add(gen1.Next());
}
Task t1 = Task.Factory.StartNew(() => {
for (int i = 0; i < max; i++) {
ids.Add(gen1.Next());
}
});
Task t2 = Task.Factory.StartNew(() => {
for (int i = 0; i < max; i++) {
ids.Add(gen1.Next());
}
});
Task.WaitAll(t1, t2);
var grouped = (from i in ids group i by i into g select new { Id = g.Key, Count = g.Count() });
var duplicated = grouped.Count(g => g.Count > 1);
Console.WriteLine("duplicated ids " + duplicated);
//Failed
Debug.Assert(0 == duplicated);
I like this implementation. I've seen some that use a Stopwatch instead of DateTime.Now because it's more accurate. That is the only possible improvement I can think of.
var blockingCollection = new BlockingCollection();
List tasks = new List();
for (int i = 0; i < 100; i++)
{
var task = Task.Run(() =>
{
for (int j = 0; j < 10000; j++)
{
blockingCollection.Add(new Generator(1, DateTime.MinValue).Next());
}
});
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine(blockingCollection.Distinct().Count());
Console.ReadKey();
What is the best way to reduce the number of digits for the unique ID. Currently the result ID is often bigger than 2^53-1 or 9007199254740991, which make it impossible to work with JavaScript's number integer. Thanks.
Hi,
When I set today date, it is giving generate negative number.
Any idea why?
Might not be the best solution, but since I am using smaller sequence sizes and this could add significant os calls and context switches, it might be prudent to do something like this. 2 new 'spin' lines to SpinToNextSequence().
private void SpinToNextSequence()
{
var spin = new SpinWait(); // create spin object
var time = GetTime();
while (time == _previousTime && _sequence >= _maxSequence)
{
spin.SpinOnce(); // spin locally in user space instead of forcing excess os calls to get next time.
// Or maybe Thread.Sleep(0)?
time = GetTime();
}
_sequence = time == _previousTime ? (short)(_sequence + 1) : (short)0;
_previousTime = time;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.