Git Product home page Git Product logo

linearkeyallocator's Introduction

LinearKeyAllocator

LinearKeyAllocator

LinearKeyAllocator allows to generate incremental keys (int, long) based on last seed value stored in database.

Works likes the HiLo algorithm (from NHibernate) but better becouse do not create big holes when application restarts.

It aims to be a tool for generating database keys without having to rely on RDBMS identity column.

LinearKeyAllocator works like database 'Sequence'. It means does not attach to any ambient transaction.

Once generated chunk of keys are available until application restarts. After restart it will start from last saved max value.

Why is it better than HiLo?

Set LinearKeyAllocator seedSize to 20.

LinearKeyAllocator          HiLo
...
100                         65536
101                         65537
102                         65538
... application restart
120                         131072
121                         131073
122                         131073
... application restart
140                         196608

Example

class Program
{
    private static string cnn =
        "Server=(local); Database=Test; Trusted_Connection=True; MultipleActiveResultSets=true";

    static async Task Main(string[] args)
    {
        var generator = await new KeyAllocator(cnn).Initialize();

        const string type1 = "Type1";
        const string type2 = "Type2";

        using var scope =
            new TransactionScope(
                TransactionScopeOption.Required,
                TransactionScopeAsyncFlowOption.Enabled);

        for (var i = 0; i < 5000; i++)
        {
            var key = (i % 2 == 0) ? type1 : type2;

            var value = await generator.GenerateInt32(key);
            Console.WriteLine("{0}: {1}", key, value);
        }

        scope.Complete();

        Console.WriteLine("Press any key...");
        Console.ReadKey();
    }
}

Sql Script

CREATE TABLE [dbo].[LinearKeyAllocator](
    [Key] [varchar](255) NOT NULL,
    [NextMax] [bigint] NOT NULL,
    CONSTRAINT [PK_LinearKeyAllocator] PRIMARY KEY CLUSTERED ([Key] ASC)
) ON [PRIMARY]

linearkeyallocator's People

Contributors

dario-l avatar dependabot[bot] avatar

Watchers

 avatar  avatar

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.