Skip to content

Commit 1fdaf57

Browse files
authored
dev: create slo linq2db (#526)
1 parent 241649e commit 1fdaf57

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

.github/workflows/slo.yml

Lines changed: 5 additions & 1 deletion
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,7 +31,10 @@ jobs:
3031
- workload: EF
3132
read_rps: 1000
3233
write_rps: 100
33-
34+
- workload: Linq2db.Slo
35+
read_rps: 1000
36+
write_rps: 100
37+
3438
concurrency:
3539
group: slo-${{ github.ref }}-${{ matrix.workload }}
3640
cancel-in-progress: true
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 Internal;
2+
using Linq2db;
3+
4+
await Cli.Run(new SloTableContext(), args);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Internal;
2+
using Linq2db.Ydb;
3+
using Linq2db.Ydb.Internal;
4+
using LinqToDB;
5+
using LinqToDB.Async;
6+
using LinqToDB.Data;
7+
using LinqToDB.Mapping;
8+
9+
namespace Linq2db;
10+
11+
public sealed class SloTableContext : SloTableContext<SloTableContext.Linq2dbClient>
12+
{
13+
protected override string Job => "Linq2db";
14+
15+
static SloTableContext()
16+
{
17+
YdbSdkRetryPolicyRegistration.UseGloballyWithIdempotence();
18+
DataConnection.AddProviderDetector(YdbTools.ProviderDetector);
19+
}
20+
21+
public sealed class Linq2dbClient(string connectionString)
22+
{
23+
public DataConnection Open()
24+
=> new(new DataOptions().UseConnectionString("YDB", connectionString));
25+
}
26+
27+
protected override Linq2dbClient CreateClient(Config config) => new(config.ConnectionString);
28+
29+
protected override async Task Create(Linq2dbClient client, int operationTimeout)
30+
{
31+
await using var db = client.Open();
32+
db.CommandTimeout = operationTimeout;
33+
34+
await db.ExecuteAsync($@"
35+
CREATE TABLE `{SloTable.Name}` (
36+
Guid Uuid,
37+
Id Int32,
38+
PayloadStr Text,
39+
PayloadDouble Double,
40+
PayloadTimestamp Timestamp,
41+
PRIMARY KEY (Guid, Id)
42+
)");
43+
44+
await db.ExecuteAsync(SloTable.Options);
45+
}
46+
47+
protected override async Task<int> Save(Linq2dbClient client, SloTable sloTable, int writeTimeout)
48+
{
49+
await using var db = client.Open();
50+
db.CommandTimeout = writeTimeout;
51+
52+
var sql = $@"
53+
UPSERT INTO `{SloTable.Name}` (Guid, Id, PayloadStr, PayloadDouble, PayloadTimestamp)
54+
VALUES (@Guid, @Id, @PayloadStr, @PayloadDouble, @PayloadTimestamp);";
55+
56+
var affected = await db.ExecuteAsync(
57+
sql,
58+
new DataParameter("Guid", sloTable.Guid, DataType.Guid),
59+
new DataParameter("Id", sloTable.Id, DataType.Int32),
60+
new DataParameter("PayloadStr", sloTable.PayloadStr, DataType.NVarChar),
61+
new DataParameter("PayloadDouble", sloTable.PayloadDouble, DataType.Double),
62+
new DataParameter("PayloadTimestamp", sloTable.PayloadTimestamp, DataType.DateTime2)
63+
);
64+
65+
return affected > 0 ? affected : 1;
66+
}
67+
68+
protected override async Task<object?> Select(Linq2dbClient client, (Guid Guid, int Id) select, int readTimeout)
69+
{
70+
await using var db = client.Open();
71+
db.CommandTimeout = readTimeout;
72+
73+
var t = db.GetTable<SloRow>();
74+
75+
var row = await t
76+
.Where(r => r.Guid == select.Guid && r.Id == select.Id)
77+
.Select(r => new
78+
{
79+
r.Guid,
80+
r.Id,
81+
r.PayloadStr,
82+
r.PayloadDouble,
83+
r.PayloadTimestamp
84+
})
85+
.FirstOrDefaultAsync();
86+
87+
return row;
88+
}
89+
90+
protected override async Task<int> SelectCount(Linq2dbClient client)
91+
{
92+
await using var db = client.Open();
93+
return await db.GetTable<SloRow>().CountAsync();
94+
}
95+
96+
[Table(SloTable.Name)]
97+
private sealed class SloRow(Guid guid, int id, string? payloadStr, double payloadDouble, DateTime payloadTimestamp)
98+
{
99+
[Column] public Guid Guid { get; } = guid;
100+
[Column] public int Id { get; } = id;
101+
[Column] public string? PayloadStr { get; } = payloadStr;
102+
[Column] public double PayloadDouble { get; } = payloadDouble;
103+
[Column] public DateTime PayloadTimestamp { get; } = payloadTimestamp;
104+
}
105+
}

slo/src/src.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF", "EF\EF.csproj", "{291A
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdoNet.Dapper", "Dapper\AdoNet.Dapper.csproj", "{A6B9B4F1-4C7C-42C1-A212-B71A9B0D67F7}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Linq2db.Slo", "Linq2db.Slo\Linq2db.Slo.csproj", "{59758BC9-E53B-46C8-84AC-62670DD559D4}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{A6B9B4F1-4C7C-42C1-A212-B71A9B0D67F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{A6B9B4F1-4C7C-42C1-A212-B71A9B0D67F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{A6B9B4F1-4C7C-42C1-A212-B71A9B0D67F7}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{59758BC9-E53B-46C8-84AC-62670DD559D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{59758BC9-E53B-46C8-84AC-62670DD559D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{59758BC9-E53B-46C8-84AC-62670DD559D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{59758BC9-E53B-46C8-84AC-62670DD559D4}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)