-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-122: Refactored Sequence Number Generator (#223)
feat: Refactored Unique Number Generator
- Loading branch information
Showing
16 changed files
with
848 additions
and
28 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/VirtoCommerce.CoreModule.Core/Common/CounterOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace VirtoCommerce.CoreModule.Core.Common | ||
{ | ||
/// <summary> | ||
/// Represents counter options for ITenantUniqueNumberGenerator. | ||
/// </summary> | ||
public class CounterOptions | ||
{ | ||
private static Regex NumberTemplateRegex = new Regex(@"(?<Template>[^@]+)@(?<ResetCounterType>None|Daily|Weekly|Monthly|Yearly)(:(?<StartCounterFrom>\d+))?(:(?<CounterIncrement>\d+))?", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase); | ||
|
||
/// <summary> | ||
/// Represents number temp[late | ||
/// </summary> | ||
public string NumberTemplate { get; set; } | ||
|
||
/// <summary> | ||
/// Represents type of counter reset. Can be one of this value: None, Daily, Weekly, Monthly, Yearly. Default value: Daily | ||
/// </summary> | ||
public ResetCounterType ResetCounterType { get; set; } = ResetCounterType.Daily; | ||
/// <summary> | ||
/// Represents start counter from value. Default value: 1 | ||
/// </summary> | ||
public int StartCounterFrom { get; set; } = 1; | ||
/// <summary> | ||
/// Represents counter increment value. Default value: 1 | ||
/// </summary> | ||
public int CounterIncrement { get; set; } = 1; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{NumberTemplate}@{ResetCounterType}:{StartCounterFrom}:{CounterIncrement}"; | ||
} | ||
|
||
/// <summary> | ||
/// Parse counter options from string template: "numberTemplate" or "number_template@reset_counter_type:start_counter_from:counter_increment". | ||
/// </summary> | ||
/// <param name="template"></param> | ||
/// <returns></returns> | ||
public static CounterOptions Parse(string template) | ||
{ | ||
ArgumentNullException.ThrowIfNull(template); | ||
|
||
var match = NumberTemplateRegex.Match(template); | ||
|
||
if (match.Success) | ||
{ | ||
return new CounterOptions | ||
{ | ||
NumberTemplate = match.Groups["Template"].Value, | ||
ResetCounterType = Enum.Parse<ResetCounterType>(match.Groups["ResetCounterType"].Value), | ||
StartCounterFrom = string.IsNullOrEmpty(match.Groups["StartCounterFrom"].Value) ? 1 : int.Parse(match.Groups["StartCounterFrom"].Value), | ||
CounterIncrement = string.IsNullOrEmpty(match.Groups["CounterIncrement"].Value) ? 1 : int.Parse(match.Groups["CounterIncrement"].Value), | ||
}; | ||
} | ||
else | ||
{ | ||
return new CounterOptions { NumberTemplate = template }; | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/VirtoCommerce.CoreModule.Core/Common/ITenantUniqueNumberGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace VirtoCommerce.CoreModule.Core.Common | ||
{ | ||
/// <summary> | ||
/// Represents interface for unique number generation for tenant. | ||
/// </summary> | ||
public interface ITenantUniqueNumberGenerator | ||
{ | ||
/// <summary> | ||
/// Generates unique number using given template with resetCounterType for tentantId. | ||
/// </summary> | ||
/// <param name="tenantId"></param> | ||
/// <param name="counterOptions"></param> | ||
/// <returns></returns> | ||
string GenerateNumber(string tenantId, CounterOptions counterOptions); | ||
|
||
/// <summary> | ||
/// Generates unique number using given template for tentantId. | ||
/// </summary> | ||
/// <param name="tenantId"></param> | ||
/// <param name="numberTemplate"></param> | ||
/// <returns></returns> | ||
string GenerateNumber(string tenantId, string numberTemplate); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/VirtoCommerce.CoreModule.Core/Common/ResetCounterType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace VirtoCommerce.CoreModule.Core.Common | ||
{ | ||
public enum ResetCounterType | ||
{ | ||
None = 0, | ||
Daily = 1, | ||
Weekly = 2, | ||
Monthly = 3, | ||
Yearly = 5, | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace VirtoCommerce.CoreModule.Core.Common | ||
{ | ||
public class SequenceNumberGeneratorOptions | ||
{ | ||
/// <summary> | ||
/// Defines the number of retries to generate unique number if either DbUpdateConcurrencyException or InvalidOperationException is occured. | ||
/// </summary> | ||
public int RetryCount { get; set; } = 15; | ||
/// <summary> | ||
/// Defines the delay between retries in seconds. | ||
/// </summary> | ||
public int RetryDelay { get; set; } = 5; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...ommerce.CoreModule.Data.PostgreSql/Migrations/20240126111303_UpdateRowVersion.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/VirtoCommerce.CoreModule.Data.PostgreSql/Migrations/20240126111303_UpdateRowVersion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace VirtoCommerce.CoreModule.Data.PostgreSql.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class UpdateRowVersion : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.RenameColumn( | ||
name: "RowVersion", | ||
table: "Sequence", | ||
newName: "xmin"); | ||
|
||
migrationBuilder.AlterColumn<uint>( | ||
name: "xmin", | ||
table: "Sequence", | ||
type: "xid", | ||
rowVersion: true, | ||
nullable: true, | ||
oldClrType: typeof(byte[]), | ||
oldType: "bytea", | ||
oldRowVersion: true, | ||
oldNullable: true); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.RenameColumn( | ||
name: "xmin", | ||
table: "Sequence", | ||
newName: "RowVersion"); | ||
|
||
migrationBuilder.AlterColumn<byte[]>( | ||
name: "RowVersion", | ||
table: "Sequence", | ||
type: "bytea", | ||
rowVersion: true, | ||
nullable: true, | ||
oldClrType: typeof(uint), | ||
oldType: "xid", | ||
oldRowVersion: true, | ||
oldNullable: true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
# Generate Migrations | ||
|
||
## Package manager | ||
Add-Migration Initial -Context VirtoCommerce.CoreModule.Data.Repositories.CoreDbContext -Verbose -OutputDir Migrations -Project VirtoCommerce.CoreModule.Data.PostgreSql -StartupProject VirtoCommerce.CoreModule.Data.PostgreSql -Debug | ||
|
||
|
||
|
||
### Entity Framework Core Commands | ||
``` | ||
dotnet tool install --global dotnet-ef --version 6.* | ||
## Install CLI tools for Entity Framework Core | ||
```cmd | ||
dotnet tool install --global dotnet-ef --version 8.0.0 | ||
``` | ||
|
||
**Generate Migrations** | ||
or update | ||
|
||
```cmd | ||
dotnet tool update --global dotnet-ef --version 8.0.0 | ||
``` | ||
dotnet ef migrations add Initial -- "{connection string}" | ||
dotnet ef migrations add Update1 -- "{connection string}" | ||
dotnet ef migrations add Update2 -- "{connection string}" | ||
``` | ||
|
||
etc.. | ||
|
||
**Apply Migrations** | ||
## Add Migration | ||
Select Data.<Provider> folder and run following command for each provider: | ||
|
||
`dotnet ef database update -- "{connection string}"` | ||
```cmd | ||
dotnet ef migrations add <migration-name> | ||
``` |
24 changes: 24 additions & 0 deletions
24
src/VirtoCommerce.CoreModule.Data.PostgreSql/SequenceEntityConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using VirtoCommerce.CoreModule.Data.Model; | ||
|
||
namespace VirtoCommerce.CoreModule.Data.PostgreSql | ||
{ | ||
public class SequenceEntityConfiguration : IEntityTypeConfiguration<SequenceEntity> | ||
{ | ||
public void Configure(EntityTypeBuilder<SequenceEntity> builder) | ||
{ | ||
var converter = new ValueConverter<byte[], long>( | ||
v => BitConverter.ToInt64(v, 0), | ||
v => BitConverter.GetBytes(v)); | ||
|
||
builder.Property(c => c.RowVersion) | ||
.HasColumnType("xid") | ||
.HasColumnName("xmin") | ||
.HasConversion(converter) | ||
.ValueGeneratedOnAddOrUpdate() | ||
.IsConcurrencyToken(); | ||
} | ||
} | ||
} |
Oops, something went wrong.