Skip to content

Commit d6b60b0

Browse files
committed
add linq2db package
1 parent a25beb6 commit d6b60b0

File tree

8 files changed

+139
-343
lines changed

8 files changed

+139
-343
lines changed

.github/workflows/slo.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
- AdoNet
2121
- Dapper
2222
- EF
23+
- Linq2db.Slo
2324
include:
2425
- workload: AdoNet
2526
read_rps: 1000
@@ -30,6 +31,9 @@ jobs:
3031
- workload: EF
3132
read_rps: 1000
3233
write_rps: 100
34+
- workload: Linq2db.Slo
35+
read_rps: 1000
36+
write_rps: 100
3337

3438
concurrency:
3539
group: slo-${{ github.ref }}-${{ matrix.workload }}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>Linq2db</RootNamespace>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Community.Ydb.Linq2db" Version="0.0.1" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\Internal\Internal.csproj" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<PackageReference Include="linq2db" Version="6.0.0-rc.3" />
18+
</ItemGroup>
19+
</Project>

slo/src/Linq2db.Slo/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using Linq2db;
2+
using Internal;
3+
4+
await Cli.Run(new SloTableContext(), args);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using LinqToDB;
4+
using LinqToDB.Data;
5+
using LinqToDB.Mapping;
6+
using Internal;
7+
using Linq2db.Ydb.Internal;
8+
using LinqToDB.Async;
9+
using LinqToDB.Internal.DataProvider.Ydb.Internal;
10+
11+
namespace Linq2db;
12+
13+
public sealed class SloTableContext : SloTableContext<SloTableContext.Linq2dbClient>
14+
{
15+
protected override string Job => "Linq2DB";
16+
17+
static SloTableContext()
18+
{
19+
// Включаем ретраи SDK глобально (как и раньше)
20+
YdbSdkRetryPolicyRegistration.UseGloballyWithIdempotence(
21+
maxAttempts: 10,
22+
onRetry: (attempt, ex, delay) => { /* лог/метрики при желании */ }
23+
);
24+
}
25+
26+
public sealed class Linq2dbClient
27+
{
28+
private readonly string _connectionString;
29+
public Linq2dbClient(string connectionString) => _connectionString = connectionString;
30+
public DataConnection Open() => new DataConnection("YDB", _connectionString);
31+
}
32+
33+
protected override Linq2dbClient CreateClient(Config config) => new(config.ConnectionString);
34+
35+
protected override async Task Create(Linq2dbClient client, int operationTimeout)
36+
{
37+
await using var db = client.Open();
38+
db.CommandTimeout = operationTimeout;
39+
40+
try
41+
{
42+
await db.ExecuteAsync($@"
43+
CREATE TABLE `{SloTable.Name}` (
44+
Guid Uuid,
45+
Id Int32,
46+
PayloadStr Text,
47+
PayloadDouble Double,
48+
PayloadTimestamp Timestamp,
49+
PRIMARY KEY (Guid, Id)
50+
)");
51+
}
52+
catch
53+
{
54+
// Таблица уже есть — ок
55+
}
56+
57+
if (!string.IsNullOrWhiteSpace(SloTable.Options))
58+
await db.ExecuteAsync(SloTable.Options);
59+
}
60+
61+
// ВАЖНО: вернуть >0 при успехе, иначе write-графики будут пустые.
62+
protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable, int writeTimeout)
63+
{
64+
await using var db = client.Open();
65+
db.CommandTimeout = writeTimeout;
66+
67+
var sql = $@"
68+
UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp)
69+
VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);";
70+
71+
var affected = await db.ExecuteAsync(
72+
sql,
73+
new DataParameter("Guid", sloTable.Guid, DataType.Guid),
74+
new DataParameter("Id", sloTable.Id, DataType.Int32),
75+
new DataParameter("PayloadStr", sloTable.PayloadStr, DataType.NVarChar),
76+
new DataParameter("PayloadDouble", sloTable.PayloadDouble, DataType.Double),
77+
new DataParameter("PayloadTimestamp",sloTable.PayloadTimestamp,DataType.DateTime2)
78+
);
79+
80+
return affected > 0 ? affected : 1;
81+
}
82+
83+
protected override async Task<object?> Select(Linq2dbClient client, (Guid Guid, int Id) select, int readTimeout)
84+
{
85+
await using var db = client.Open();
86+
db.CommandTimeout = readTimeout;
87+
88+
var t = db.GetTable<SloRow>();
89+
return await t.FirstOrDefaultAsync(r => r.Guid == select.Guid && r.Id == select.Id);
90+
}
91+
92+
protected override async Task<int> SelectCount(Linq2dbClient client)
93+
{
94+
await using var db = client.Open();
95+
return await db.GetTable<SloRow>().CountAsync();
96+
}
97+
98+
[Table(SloTable.Name)]
99+
private sealed class SloRow
100+
{
101+
[Column] public Guid Guid { get; set; }
102+
[Column] public int Id { get; set; }
103+
[Column] public string? PayloadStr { get; set; }
104+
[Column] public double PayloadDouble { get; set; }
105+
[Column] public DateTime PayloadTimestamp { get; set; }
106+
}
107+
}

slo/src/Linq2db/AdoNet.Linq2db.csproj

Lines changed: 0 additions & 16 deletions
This file was deleted.

slo/src/Linq2db/Program.cs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)