Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dario-l/linearkeyallocator
Linear key allocator for MS Sql Server
https://github.com/dario-l/linearkeyallocator
chunk database generator hilo identifier key-allocator linear rdbms
Last synced: 11 days ago
JSON representation
Linear key allocator for MS Sql Server
- Host: GitHub
- URL: https://github.com/dario-l/linearkeyallocator
- Owner: dario-l
- License: mit
- Created: 2019-12-04T15:16:22.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T08:41:09.000Z (12 months ago)
- Last Synced: 2024-11-05T19:12:39.267Z (about 2 months ago)
- Topics: chunk, database, generator, hilo, identifier, key-allocator, linear, rdbms
- Language: C#
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LinearKeyAllocator
![LinearKeyAllocator](https://img.shields.io/nuget/v/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**
``` c#
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**
``` SQL
CREATE TABLE [dbo].[LinearKeyAllocator](
[Key] [varchar](255) NOT NULL,
[NextMax] [bigint] NOT NULL,
CONSTRAINT [PK_LinearKeyAllocator] PRIMARY KEY CLUSTERED ([Key] ASC)
) ON [PRIMARY]
```