mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add Snowflake
This commit is contained in:
parent
ab95f57859
commit
e2fd7bb04f
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace SqlSugar.DistributedSystem.Snowflake
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
readonly Action _action;
|
||||
|
||||
public DisposableAction(Action action)
|
||||
{
|
||||
if (action == null)
|
||||
throw new ArgumentNullException("action");
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_action();
|
||||
}
|
||||
}
|
||||
}
|
118
Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs
Normal file
118
Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs
Normal file
@ -0,0 +1,118 @@
|
||||
/** Copyright 2010-2012 Twitter, Inc.*/
|
||||
|
||||
/**
|
||||
* An object that generates IDs.
|
||||
* This is broken into a separate class in case
|
||||
* we ever want to support multiple worker threads
|
||||
* per process
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
namespace SqlSugar.DistributedSystem.Snowflake
|
||||
{
|
||||
public class IdWorker
|
||||
{
|
||||
public const long Twepoch = 1288834974657L;
|
||||
|
||||
const int WorkerIdBits = 5;
|
||||
const int DatacenterIdBits = 5;
|
||||
const int SequenceBits = 12;
|
||||
const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits);
|
||||
const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits);
|
||||
|
||||
private const int WorkerIdShift = SequenceBits;
|
||||
private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
|
||||
public const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
|
||||
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
|
||||
|
||||
private long _sequence = 0L;
|
||||
private long _lastTimestamp = -1L;
|
||||
|
||||
|
||||
public IdWorker(long workerId, long datacenterId, long sequence = 0L)
|
||||
{
|
||||
WorkerId = workerId;
|
||||
DatacenterId = datacenterId;
|
||||
_sequence = sequence;
|
||||
|
||||
// sanity check for workerId
|
||||
if (workerId > MaxWorkerId || workerId < 0)
|
||||
{
|
||||
throw new ArgumentException( String.Format("worker Id can't be greater than {0} or less than 0", MaxWorkerId) );
|
||||
}
|
||||
|
||||
if (datacenterId > MaxDatacenterId || datacenterId < 0)
|
||||
{
|
||||
throw new ArgumentException( String.Format("datacenter Id can't be greater than {0} or less than 0", MaxDatacenterId));
|
||||
}
|
||||
|
||||
//log.info(
|
||||
// String.Format("worker starting. timestamp left shift {0}, datacenter id bits {1}, worker id bits {2}, sequence bits {3}, workerid {4}",
|
||||
// TimestampLeftShift, DatacenterIdBits, WorkerIdBits, SequenceBits, workerId)
|
||||
// );
|
||||
}
|
||||
|
||||
public long WorkerId {get; protected set;}
|
||||
public long DatacenterId {get; protected set;}
|
||||
|
||||
public long Sequence
|
||||
{
|
||||
get { return _sequence; }
|
||||
internal set { _sequence = value; }
|
||||
}
|
||||
|
||||
// def get_timestamp() = System.currentTimeMillis
|
||||
|
||||
readonly object _lock = new Object();
|
||||
|
||||
public virtual long NextId()
|
||||
{
|
||||
lock(_lock)
|
||||
{
|
||||
var timestamp = TimeGen();
|
||||
|
||||
if (timestamp < _lastTimestamp)
|
||||
{
|
||||
//exceptionCounter.incr(1);
|
||||
//log.Error("clock is moving backwards. Rejecting requests until %d.", _lastTimestamp);
|
||||
throw new InvalidSystemClock(String.Format(
|
||||
"Clock moved backwards. Refusing to generate id for {0} milliseconds", _lastTimestamp - timestamp));
|
||||
}
|
||||
|
||||
if (_lastTimestamp == timestamp)
|
||||
{
|
||||
_sequence = (_sequence + 1) & SequenceMask;
|
||||
if (_sequence == 0)
|
||||
{
|
||||
timestamp = TilNextMillis(_lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
_sequence = 0;
|
||||
}
|
||||
|
||||
_lastTimestamp = timestamp;
|
||||
var id = ((timestamp - Twepoch) << TimestampLeftShift) |
|
||||
(DatacenterId << DatacenterIdShift) |
|
||||
(WorkerId << WorkerIdShift) | _sequence;
|
||||
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual long TilNextMillis(long lastTimestamp)
|
||||
{
|
||||
var timestamp = TimeGen();
|
||||
while (timestamp <= lastTimestamp)
|
||||
{
|
||||
timestamp = TimeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
protected virtual long TimeGen()
|
||||
{
|
||||
return System.CurrentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace SqlSugar.DistributedSystem.Snowflake
|
||||
{
|
||||
public class InvalidSystemClock : Exception
|
||||
{
|
||||
public InvalidSystemClock(string message) : base(message) { }
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.DistributedSystem.Snowflake
|
||||
{
|
||||
public sealed class SnowFlakeSingle
|
||||
{
|
||||
public static readonly SnowFlakeSingle instance = new SnowFlakeSingle();
|
||||
private SnowFlakeSingle()
|
||||
{
|
||||
worker = new Snowflake.IdWorker(1, 1);
|
||||
}
|
||||
static SnowFlakeSingle() { }
|
||||
public static SnowFlakeSingle Instance
|
||||
{
|
||||
get { return instance; }
|
||||
}
|
||||
private Snowflake.IdWorker worker;
|
||||
public long getID()
|
||||
{
|
||||
return worker.NextId();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace SqlSugar.DistributedSystem.Snowflake
|
||||
{
|
||||
public static class System
|
||||
{
|
||||
public static Func<long> currentTimeFunc = InternalCurrentTimeMillis;
|
||||
|
||||
public static long CurrentTimeMillis()
|
||||
{
|
||||
return currentTimeFunc();
|
||||
}
|
||||
|
||||
public static IDisposable StubCurrentTime(Func<long> func)
|
||||
{
|
||||
currentTimeFunc = func;
|
||||
return new DisposableAction(() =>
|
||||
{
|
||||
currentTimeFunc = InternalCurrentTimeMillis;
|
||||
});
|
||||
}
|
||||
|
||||
public static IDisposable StubCurrentTime(long millis)
|
||||
{
|
||||
currentTimeFunc = () => millis;
|
||||
return new DisposableAction(() =>
|
||||
{
|
||||
currentTimeFunc = InternalCurrentTimeMillis;
|
||||
});
|
||||
}
|
||||
|
||||
private static readonly DateTime Jan1st1970 = new DateTime
|
||||
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
private static long InternalCurrentTimeMillis()
|
||||
{
|
||||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
@ -90,6 +90,7 @@
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="Abstract\Reportable\ReportableProvider.cs" />
|
||||
<Compile Include="Abstract\SaveableProvider\Storageable.cs" />
|
||||
<Compile Include="DistributedSystem\Snowflake\SnowFlakeSingle.cs" />
|
||||
<Compile Include="Entities\ConfigQuery.cs" />
|
||||
<Compile Include="Entities\ManyToManyConfig.cs" />
|
||||
<Compile Include="Entities\SingleColumnsEntity.cs" />
|
||||
@ -194,6 +195,10 @@
|
||||
<Compile Include="Realization\PostgreSQL\SqlBuilder\PostgreSQLQueryBuilder.cs" />
|
||||
<Compile Include="Realization\PostgreSQL\SqlBuilder\PostgreSQLUpdateBuilder.cs" />
|
||||
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerBlueCopy.cs" />
|
||||
<Compile Include="DistributedSystem\Snowflake\DisposableAction.cs" />
|
||||
<Compile Include="DistributedSystem\Snowflake\IdWorker.cs" />
|
||||
<Compile Include="DistributedSystem\Snowflake\InvalidSystemClock.cs" />
|
||||
<Compile Include="DistributedSystem\Snowflake\TimeExtensions.cs" />
|
||||
<Compile Include="SqlSugarClient.cs" />
|
||||
<Compile Include="Utilities\CallContext.cs" />
|
||||
<Compile Include="Utilities\CallContextAsync.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user