Skip to content

Commit cf08b23

Browse files
committed
Merge remote-tracking branch 'origin/main' into slo
# Conflicts: # .github/workflows/lint.yml
2 parents 8aa1595 + f09a8fe commit cf08b23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1726
-120
lines changed

.github/workflows/lint.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ jobs:
1010
autoformatter:
1111
strategy:
1212
matrix:
13-
source-dir: ["./src/", "./slo/src/"]
13+
source-dir: ["./src/", "./examples/src/", "./slo/src/"]
1414
include:
1515
- source-dir: "./src/"
1616
solutionFile: "YdbSdk.sln"
17+
- source-dir: "./examples/src/"
18+
solutionFile: "YdbExamples.sln"
1719
- source-dir: "./slo/src/"
18-
solutionFile: "src.sln"
20+
solutionFile: "src.sln"
1921
name: autoformat check
2022
runs-on: ubuntu-latest
2123
steps:
@@ -28,16 +30,16 @@ jobs:
2830
- name: Restore
2931
run: dotnet restore ${{ matrix.source-dir }}${{ matrix.solutionFile }}
3032
- name: Install ReSharper
31-
run: dotnet tool install -g JetBrains.ReSharper.GlobalTools
33+
run: dotnet tool install -g JetBrains.ReSharper.GlobalTools --version 2023.2.1
3234
- name: format all files with auto-formatter
33-
run: bash ./.github/scripts/format-all-dotnet-code.sh ${{ matrix.source-dir }} ${{ matrix.solutionFile }} "Custom Cleanup"}
35+
run: bash ./.github/scripts/format-all-dotnet-code.sh ${{ matrix.source-dir }} ${{ matrix.solutionFile }} "Custom Cleanup"
3436
- name: Check repository diff
3537
run: bash ./.github/scripts/check-work-copy-equals-to-committed.sh "auto-format broken"
3638

3739
inspection:
3840
strategy:
3941
matrix:
40-
solutionPath: ["./src/YdbSdk.sln", "./slo/src/src.sln"]
42+
solutionPath: ["./src/YdbSdk.sln", "./examples/src/YdbExamples.sln", "./slo/src/src.sln"]
4143
runs-on: ubuntu-latest
4244
name: Inspection
4345
steps:

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ jobs:
6767
- name: Integration test
6868
run: |
6969
cd src
70-
dotnet test --filter "Category=Integration" -f ${{ matrix.dotnet-target-framework }}
70+
dotnet test --filter "Category=Integration" -f ${{ matrix.dotnet-target-framework }} -l "console;verbosity=normal"

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## v0.1.5
2+
- Fix timeout error on create session
3+
- Fix transport error on delete session
4+
5+
## v0.1.4
6+
- Add exception throwing when results truncated
7+
- lint: add line feed at file end
8+
9+
## v0.1.3
10+
- Add static auth
111
## v0.1.1
212
- Add static code analysis
313
- Add CodeQL analysis

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ await driver.Initialize(); // Make sure to await driver initialization
3737
```
3838

3939
### Credentials
40-
YDB SDK provides two standard ways for authentication:
40+
YDB SDK provides several standard ways for authentication:
4141
1) `Ydb.Sdk.Auth.AnonymousProvider`. Anonymous YDB access, mainly for tests purposes.
4242
2) `Ydb.Sdk.Auth.TokenProvider`. Token authentication for OAuth-like tokens.
43+
3) `Ydb.Sdk.Auth.StaticCredentialsProvider`. Username and password based authentication.
4344

4445
For Yandex.Cloud specific authentication methods, consider using **[ydb-dotnet-yc](https://github.com/ydb-platform/ydb-dotnet-yc)**.
4546

@@ -91,4 +92,4 @@ foreach (var row in resultSet.Rows)
9192

9293
## Examples
9394

94-
See **[ydb-dotnet-examples](https://github.com/ydb-platform/ydb-dotnet-examples)**.
95+
See **[examples folder](https://github.com/ydb-platform/ydb-dotnet-sdk/tree/main/examples)**

examples/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# YDB .NET SDK Examples
2+
3+
## Prerequisites
4+
.NET 6
5+
6+
## Running examples
7+
8+
1. Clone repository
9+
```bash
10+
git clone https://github.com/ydb-platform/ydb-dotnet-sdk.git
11+
```
12+
13+
2. Build solution
14+
```bash
15+
cd ydb-dotnet-sdk/examples/src
16+
dotnet build
17+
```
18+
19+
3. Run example
20+
```bash
21+
cd <ExampleName>
22+
dotnet run -e <Endpoint> -d <Database>
23+
```
24+
25+
## Provided examples
26+
27+
### BasicExample
28+
Demonstrates basic operations with YDB, including:
29+
* Driver initialization
30+
* Table client initialization
31+
* Table creation via SchemeQuery (DDL)
32+
* Data queries (OLTP) & transactions (read, modify)
33+
* Interactive transactions
34+
* ReadTable for streaming read of table contents
35+
* ScanQuery for streaming wide queries (OLAP)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Logging;
5+
using Ydb.Sdk.Auth;
6+
using Ydb.Sdk.Services.Table;
7+
8+
namespace Ydb.Sdk.Examples;
9+
10+
internal partial class BasicExample : TableExampleBase
11+
{
12+
private BasicExample(TableClient client, string database, string path)
13+
: base(client, database, path)
14+
{
15+
}
16+
17+
public static async Task Run(
18+
string endpoint,
19+
string database,
20+
ICredentialsProvider credentialsProvider,
21+
X509Certificate? customServerCertificate,
22+
string path,
23+
ILoggerFactory loggerFactory)
24+
{
25+
var config = new DriverConfig(
26+
endpoint: endpoint,
27+
database: database,
28+
credentials: credentialsProvider,
29+
customServerCertificate: customServerCertificate
30+
);
31+
32+
await using var driver = await Driver.CreateInitialized(
33+
config: config,
34+
loggerFactory: loggerFactory
35+
);
36+
37+
using var tableClient = new TableClient(driver, new TableClientConfig());
38+
39+
var example = new BasicExample(tableClient, database, path);
40+
41+
await example.SchemeQuery();
42+
await example.FillData();
43+
await example.SimpleSelect(1);
44+
await example.SimpleUpsert(10, "Coming soon", DateTime.UtcNow);
45+
await example.SimpleSelect(10);
46+
await example.InteractiveTx();
47+
await example.ReadTable();
48+
await example.ScanQuery(DateTime.Parse("2007-01-01"));
49+
}
50+
51+
private static ExecuteDataQuerySettings DefaultDataQuerySettings =>
52+
new()
53+
{
54+
// Indicates that client is no longer interested in the result of operation after the
55+
// specified duration starting from the moment when operation arrives at the server.
56+
// Status code TIMEOUT will be returned from server in case when operation result in
57+
// not available in the specified time period. This status code doesn't indicate the result
58+
// of operation, it might be completed or cancelled.
59+
OperationTimeout = TimeSpan.FromSeconds(1),
60+
61+
// Transport timeout from the moment operation was sent to server. It is useful in case
62+
// of possible network issues, to that query doesn't hang forever.
63+
// It is recommended to set this value to a larger value than OperationTimeout to give
64+
// server some time to issue a response.
65+
TransportTimeout = TimeSpan.FromSeconds(5),
66+
67+
// Keep query compilation result in query cache or not. Should be false for ad-hoc queries,
68+
// and true (default) for high-RPS queries.
69+
KeepInQueryCache = false
70+
};
71+
72+
private ExecuteDataQuerySettings DefaultCachedDataQuerySettings
73+
{
74+
get
75+
{
76+
var settings = DefaultDataQuerySettings;
77+
settings.KeepInQueryCache = true;
78+
return settings;
79+
}
80+
}
81+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
4+
<PropertyGroup>
5+
<OutputType>Exe</OutputType>
6+
<TargetFramework>net6.0</TargetFramework>
7+
<Nullable>enable</Nullable>
8+
<AssemblyName>Ydb.Sdk.Examples.BasicExample</AssemblyName>
9+
<RootNamespace>Ydb.Sdk.Examples</RootNamespace>
10+
</PropertyGroup>
11+
12+
<PropertyGroup>
13+
<RepositoryType>git</RepositoryType>
14+
<RepositoryUrl>https://github.com/ydb-platform/ydb-dotnet-examples</RepositoryUrl>
15+
<PackageProjectUrl>https://github.com/ydb-platform/ydb-dotnet-examples</PackageProjectUrl>
16+
<Company>YANDEX LLC</Company>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
21+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\Common\Common.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Ydb.Sdk.Services.Table;
5+
using Ydb.Sdk.Value;
6+
7+
namespace Ydb.Sdk.Examples;
8+
9+
internal partial class BasicExample
10+
{
11+
private async Task SimpleSelect(ulong id)
12+
{
13+
var response = await Client.SessionExec(async session =>
14+
{
15+
var query = @$"
16+
PRAGMA TablePathPrefix('{BasePath}');
17+
18+
DECLARE $id AS Uint64;
19+
20+
SELECT
21+
series_id,
22+
title,
23+
release_date
24+
FROM series
25+
WHERE series_id = $id;
26+
";
27+
28+
return await session.ExecuteDataQuery(
29+
query: query,
30+
txControl: TxControl.BeginSerializableRW().Commit(),
31+
parameters: new Dictionary<string, YdbValue>
32+
{
33+
{ "$id", YdbValue.MakeUint64(id) }
34+
},
35+
settings: DefaultCachedDataQuerySettings
36+
);
37+
});
38+
39+
response.Status.EnsureSuccess();
40+
41+
var queryResponse = (ExecuteDataQueryResponse)response;
42+
var resultSet = queryResponse.Result.ResultSets[0];
43+
44+
Console.WriteLine($"> SimpleSelect, " +
45+
$"columns: {resultSet.Columns.Count}, " +
46+
$"rows: {resultSet.Rows.Count}, " +
47+
$"truncated: {resultSet.Truncated}");
48+
49+
foreach (var row in resultSet.Rows)
50+
{
51+
Console.WriteLine($"> Series, " +
52+
$"series_id: {(ulong?)row["series_id"]}, " +
53+
$"title: {(string?)row["title"]}, " +
54+
$"release_date: {(DateTime?)row["release_date"]}");
55+
}
56+
}
57+
58+
private async Task SimpleUpsert(ulong id, string title, DateTime date)
59+
{
60+
var response = await Client.SessionExec(async session =>
61+
{
62+
var query = @$"
63+
PRAGMA TablePathPrefix('{BasePath}');
64+
65+
DECLARE $id AS Uint64;
66+
DECLARE $title AS Utf8;
67+
DECLARE $release_date AS Date;
68+
69+
UPSERT INTO series (series_id, title, release_date) VALUES
70+
($id, $title, $release_date);
71+
";
72+
73+
return await session.ExecuteDataQuery(
74+
query: query,
75+
txControl: TxControl.BeginSerializableRW().Commit(),
76+
parameters: new Dictionary<string, YdbValue>
77+
{
78+
{ "$id", YdbValue.MakeUint64(id) },
79+
{ "$title", YdbValue.MakeUtf8(title) },
80+
{ "$release_date", YdbValue.MakeDate(date) }
81+
},
82+
settings: DefaultCachedDataQuerySettings
83+
);
84+
});
85+
86+
response.Status.EnsureSuccess();
87+
}
88+
}

0 commit comments

Comments
 (0)