GithubHelp home page GithubHelp logo

uniqueidgenerator's People

Contributors

mschuler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

uniqueidgenerator's Issues

Really cool hidden features

This lib has really cool hidden features.

After you have generated an ID, you can convert the ID into its parts:

  • string Bits
  • TimeSpan Time
  • short GeneratorId
  • long Sequence

I think it should be in the description.

I love this lib.

Duplicate ids in multi-threaded

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);

Stopwatch vs DateTime.Now

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();

Reduce number of ID Digits

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.

Negative id

Hi,

When I set today date, it is giving generate negative number.

Any idea why?

SpinToNextSequence might cause excessive OS calls for small sequences.

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;
 }

@mschuler

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.