-
Notifications
You must be signed in to change notification settings - Fork 28
create slo linq2db #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create slo linq2db #526
Changes from 6 commits
9e7715f
9a77e44
6c2d3c0
a25beb6
d6b60b0
160c31e
7ff65e6
91e42f7
beb7124
54377b1
815542c
ffd90ba
b6a7b7a
40635b0
0169d50
d215359
0b93fa1
1d1002e
192e105
31fca62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>Linq2db</RootNamespace> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Community.Ydb.Linq2db" Version="0.0.1" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Internal\Internal.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="linq2db" Version="6.0.0-rc.3" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| using Linq2db; | ||
| using Internal; | ||
|
|
||
| await Cli.Run(new SloTableContext(), args); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System; | ||
|
Check warning on line 1 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| using System.Threading.Tasks; | ||
|
Check warning on line 2 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| using LinqToDB; | ||
| using LinqToDB.Data; | ||
| using LinqToDB.Mapping; | ||
| using Internal; | ||
| using Linq2db.Ydb.Internal; | ||
| using LinqToDB.Async; | ||
| using LinqToDB.Internal.DataProvider.Ydb.Internal; | ||
|
Check warning on line 9 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
|
|
||
| namespace Linq2db; | ||
|
|
||
| public sealed class SloTableContext : SloTableContext<SloTableContext.Linq2dbClient> | ||
| { | ||
| protected override string Job => "Linq2DB"; | ||
|
|
||
| static SloTableContext() | ||
| { | ||
| // Включаем ретраи SDK глобально (как и раньше) | ||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| YdbSdkRetryPolicyRegistration.UseGloballyWithIdempotence( | ||
| maxAttempts: 10, | ||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| onRetry: (attempt, ex, delay) => { /* лог/метрики при желании */ } | ||
|
Check warning on line 22 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| public sealed class Linq2dbClient | ||
| { | ||
| private readonly string _connectionString; | ||
| public Linq2dbClient(string connectionString) => _connectionString = connectionString; | ||
| public DataConnection Open() => new DataConnection("YDB", _connectionString); | ||
|
Check warning on line 30 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| } | ||
|
|
||
| protected override Linq2dbClient CreateClient(Config config) => new(config.ConnectionString); | ||
|
|
||
| protected override async Task Create(Linq2dbClient client, int operationTimeout) | ||
| { | ||
| await using var db = client.Open(); | ||
| db.CommandTimeout = operationTimeout; | ||
|
|
||
| try | ||
| { | ||
| await db.ExecuteAsync($@" | ||
| CREATE TABLE `{SloTable.Name}` ( | ||
| Guid Uuid, | ||
| Id Int32, | ||
| PayloadStr Text, | ||
| PayloadDouble Double, | ||
| PayloadTimestamp Timestamp, | ||
| PRIMARY KEY (Guid, Id) | ||
| )"); | ||
| } | ||
| catch | ||
| { | ||
| // Таблица уже есть — ок | ||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(SloTable.Options)) | ||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await db.ExecuteAsync(SloTable.Options); | ||
| } | ||
|
|
||
| // ВАЖНО: вернуть >0 при успехе, иначе write-графики будут пустые. | ||
KirillKurdyukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable, int writeTimeout) | ||
| { | ||
| await using var db = client.Open(); | ||
| db.CommandTimeout = writeTimeout; | ||
|
|
||
| var sql = $@" | ||
| UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp) | ||
| VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);"; | ||
|
|
||
| var affected = await db.ExecuteAsync( | ||
| sql, | ||
| new DataParameter("Guid", sloTable.Guid, DataType.Guid), | ||
| new DataParameter("Id", sloTable.Id, DataType.Int32), | ||
| new DataParameter("PayloadStr", sloTable.PayloadStr, DataType.NVarChar), | ||
| new DataParameter("PayloadDouble", sloTable.PayloadDouble, DataType.Double), | ||
| new DataParameter("PayloadTimestamp",sloTable.PayloadTimestamp,DataType.DateTime2) | ||
| ); | ||
|
|
||
| return affected > 0 ? affected : 1; | ||
| } | ||
|
|
||
| protected override async Task<object?> Select(Linq2dbClient client, (Guid Guid, int Id) select, int readTimeout) | ||
| { | ||
| await using var db = client.Open(); | ||
| db.CommandTimeout = readTimeout; | ||
|
|
||
| var t = db.GetTable<SloRow>(); | ||
| return await t.FirstOrDefaultAsync(r => r.Guid == select.Guid && r.Id == select.Id); | ||
| } | ||
|
|
||
| protected override async Task<int> SelectCount(Linq2dbClient client) | ||
| { | ||
| await using var db = client.Open(); | ||
| return await db.GetTable<SloRow>().CountAsync(); | ||
| } | ||
|
|
||
| [Table(SloTable.Name)] | ||
| private sealed class SloRow | ||
| { | ||
| [Column] public Guid Guid { get; set; } | ||
|
Check warning on line 101 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| [Column] public int Id { get; set; } | ||
|
Check warning on line 102 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| [Column] public string? PayloadStr { get; set; } | ||
|
Check warning on line 103 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| [Column] public double PayloadDouble { get; set; } | ||
|
Check warning on line 104 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| [Column] public DateTime PayloadTimestamp { get; set; } | ||
|
Check warning on line 105 in slo/src/Linq2db.Slo/SloLinq2DbContext.cs
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.