Epoch Improvements

This was me starting work on a new epoch system.
This commit is contained in:
JacobTech 2023-05-09 18:15:19 -04:00
parent 0406903aa7
commit e739b5eb7a
5 changed files with 21 additions and 19 deletions

View File

@ -8,6 +8,12 @@ public class AppConfig
[JsonPropertyName("address")] [JsonPropertyName("address")]
public string Address { get; set; } = "127.0.0.1"; public string Address { get; set; } = "127.0.0.1";
[JsonInclude] [JsonInclude]
[JsonPropertyName("data_id")]
public byte DataId { get; set; } = 0;
[JsonInclude]
[JsonPropertyName("server_epoch")]
public long ServerEpoch { get; set; } = Luski.Info.GetTimestampFromEpoch(0);
[JsonInclude]
[JsonPropertyName("database")] [JsonPropertyName("database")]
public string Database { get; set; } = "Some database name"; public string Database { get; set; } = "Some database name";
[JsonInclude] [JsonInclude]

View File

@ -44,7 +44,7 @@ public static class EXT
if (LogInDB) if (LogInDB)
{ {
Tables.Logs.Insert( Tables.Logs.Insert(
Logs.ID.CreateParameter(Luski.Snowflake.GenerateSnowflake(WorkerId.Log).ID), Logs.ID.CreateParameter(Luski.Snowflake.GenerateSnowflake(Luski.Config.ServerEpoch) .ID),
Logs.Type.CreateParameter(LogType.Error), Logs.Type.CreateParameter(LogType.Error),
Logs.Message.CreateParameter(Error.ToString())); Logs.Message.CreateParameter(Error.ToString()));
} }

View File

@ -78,40 +78,35 @@ public static class Luski
} }
} }
public static readonly DateTime Epoch = new(2023, 1, 1, 0, 0, 0, 0); // public static readonly DateTime ServerEpoch = new(2023, 1, 1, 0, 0, 0, 0);
public static long Timestamp public static long GetTimestampFromEpoch(long Epoch)
{ {
get double ts = Math.Round(DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalMilliseconds - Epoch, 0);
{
double ts = Math.Round(DateTime.Now.Subtract(Epoch).TotalMilliseconds, 0);
return long.Parse(ts.ToString().Replace(".", string.Empty)); return long.Parse(ts.ToString().Replace(".", string.Empty));
} }
} }
}
public class Snowflake public class Snowflake
{ {
public Snowflake(long ID) public Snowflake(long ID)
{ {
this.ID = ID; this.ID = ID;
Increment = (ushort)((ID << 52) >> 52); Increment = (ushort)((ID << 52) >>> 52);
Worker_ID = (ushort)((ID << 47) >> 59); Data_ID = (byte)((ID << 44) >>> 56);
Server_ID = (ushort)((ID << 42) >> 59); Timestamp = ID >> 20;
Timestamp = ID >> 22;
} }
public static Snowflake GenerateSnowflake(WorkerId workerID) public static Snowflake GenerateSnowflake(long Epoch)
{ {
i++; i++;
if (i > 4096) i = 0; if (i > 0b_1111_1111_1111) i = 0;
return new Snowflake((((((Info.Timestamp << 5) | (ushort)0) << 5) | (ushort)workerID) << 12) | i); return new Snowflake((((Info.GetTimestampFromEpoch(Epoch) << 8) | Config.DataId) << 12) | i);
} }
public long ID { get; } public long ID { get; }
public long Timestamp { get; } public long Timestamp { get; }
public ushort Worker_ID { get; } public byte Data_ID { get; }
public ushort Server_ID { get; }
public ushort Increment { get; } public ushort Increment { get; }
private static ushort i = 0; private static ushort i = 0;

View File

@ -71,7 +71,7 @@ public class CreateAccountController : ControllerBase
{ {
int num = new Random().Next(1000, 1000000000); int num = new Random().Next(1000, 1000000000);
int num2 = new Random().Next(1000, 1000000000); int num2 = new Random().Next(1000, 1000000000);
Luski.Snowflake id = Luski.Snowflake.GenerateSnowflake(WorkerId.CreateAccount); Luski.Snowflake id = Luski.Snowflake.GenerateSnowflake(Luski.Config.ServerEpoch);
byte[] ID = Encoding.UTF8.GetBytes(id.ID.ToString()); byte[] ID = Encoding.UTF8.GetBytes(id.ID.ToString());
byte[] Timestamp = Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString()); byte[] Timestamp = Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString());
byte[] Number = Encoding.UTF8.GetBytes(num.ToString()); byte[] Number = Encoding.UTF8.GetBytes(num.ToString());

View File

@ -12,6 +12,7 @@ using Swashbuckle.AspNetCore.SwaggerGen;
[assembly: Microsoft.AspNetCore.Mvc.ApiController] [assembly: Microsoft.AspNetCore.Mvc.ApiController]
Luski.Config = Luski.GetSettings("/etc/luskiserver/app.json", AppConfigContext.Default.AppConfig, true); Luski.Config = Luski.GetSettings("/etc/luskiserver/app.json", AppConfigContext.Default.AppConfig, true);
Console.WriteLine($"Server starting with starting epoch of {DateTime.UnixEpoch.AddMilliseconds(Luski.Config.ServerEpoch).ToLocalTime()}");
Luski.Database = new Database(Luski.Config.Address, Luski.Database = new Database(Luski.Config.Address,
Luski.Config.Database, Luski.Config.Database,
Luski.Config.Username, Luski.Config.Username,