Skip to content

Commit

Permalink
Transaction methods begin,commit,rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Oct 9, 2023
1 parent 327b45f commit c81eee0
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Ydb.Sdk/src/Services/Table/DescribeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public ResultData(Entry self, IReadOnlyList<ColumnMeta> columns, IReadOnlyList<s
IReadOnlyList<YdbValue> shardKeyBounds, IReadOnlyList<TableIndexDescription> indexes,
TableStats tableStats, TtlSettings? ttlSettings, StorageSettings storageSettings,
IReadOnlyList<ColumnFamily> columnFamilies, IReadOnlyDictionary<string, string> attributes,
PartitioningSettings partitioningSettings, FeatureFlag.Types.Status keyBloomFilter,
PartitioningSettings partitioningSettings, FeatureFlagStatus keyBloomFilter,
ReadReplicasSettings readReplicasSettings)
{
Self = self;
Expand All @@ -149,7 +149,7 @@ public ResultData(Entry self, IReadOnlyList<ColumnMeta> columns, IReadOnlyList<s
ColumnFamilies = columnFamilies;
Attributes = attributes;
PartitioningSettings = partitioningSettings;
KeyBloomFilter = keyBloomFilter.FromProto();
KeyBloomFilter = keyBloomFilter;
ReadReplicasSettings = readReplicasSettings;
}

Expand All @@ -167,7 +167,7 @@ internal static ResultData FromProto(DescribeTableResult resultProto)
columnFamilies: resultProto.ColumnFamilies.Select(proto => new ColumnFamily(proto)).ToList(),
attributes: new Dictionary<string, string>(resultProto.Attributes),
partitioningSettings: new PartitioningSettings(resultProto.PartitioningSettings),
keyBloomFilter: resultProto.KeyBloomFilter,
keyBloomFilter: resultProto.KeyBloomFilter.FromProto(),
readReplicasSettings: new ReadReplicasSettings(resultProto.ReadReplicasSettings)
);
}
Expand Down
192 changes: 191 additions & 1 deletion src/Ydb.Sdk/src/Services/Table/Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Ydb.Table;
using System.Diagnostics.CodeAnalysis;

Check warning on line 1 in src/Ydb.Sdk/src/Services/Table/Transaction.cs

View workflow job for this annotation

GitHub Actions / Inspection

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/Ydb.Sdk/src/Services/Table/Transaction.cs
using Ydb.Sdk.Client;
using Ydb.Table;
using Ydb.Table.V1;

namespace Ydb.Sdk.Services.Table;

Expand Down Expand Up @@ -93,4 +96,191 @@ internal TransactionControl ToProto()
{
return _proto.Clone();
}
}

public class BeginTransactionSettings : OperationRequestSettings
{
public TxControl? TxSettings { get; }

public BeginTransactionSettings(TxControl? txSettings = null)
{
TxSettings = txSettings;
}
}

public class CommitTransactionSettings : OperationRequestSettings
{
public TxControl? TxSettings { get; }

public CommitTransactionSettings(TxControl? txSettings = null)
{
TxSettings = txSettings;
}
}

public class RollbackTransactionSettings : OperationRequestSettings
{
public TxControl? TxSettings { get; }

public RollbackTransactionSettings(TxControl? txSettings = null)
{
TxSettings = txSettings;
}
}

public class BeginTransactionResponse : ResponseWithResultBase<BeginTransactionResponse.ResultData>
{
internal BeginTransactionResponse(Status status, ResultData? result = null) : base(status, result)
{
}

public Transaction? GetTx()
{
return Result.Transaction;
}

public class ResultData
{
public Transaction? Transaction { get; }

public ResultData(Transaction? transaction)
{
Transaction = transaction;
}

internal static ResultData FromProto(BeginTransactionResult resultProto)
{
return new ResultData(
transaction: Transaction.FromProto(resultProto.TxMeta)
);
}
}
}

public class CommitTransactionResponse : ResponseWithResultBase<CommitTransactionResponse.ResultData>
{
internal CommitTransactionResponse(Status status, ResultData? result = null) : base(status, result)
{
}

public class ResultData
{
public ResultData()

Check warning on line 168 in src/Ydb.Sdk/src/Services/Table/Transaction.cs

View workflow job for this annotation

GitHub Actions / Inspection

"[EmptyConstructor] Empty constructor is redundant. The compiler generates the same by default." on /home/runner/work/ydb-dotnet-sdk/ydb-dotnet-sdk/src/Ydb.Sdk/src/Services/Table/Transaction.cs(168,3689)
{
}

internal static ResultData FromProto(CommitTransactionResult resultProto)
{
return new ResultData();
}
}
}

public class RollbackTransactionResponse : ResponseBase
{
internal RollbackTransactionResponse(Status status) : base(status)
{
}
}

public partial class Session
{
public async Task<BeginTransactionResponse> BeginTransaction(BeginTransactionSettings? settings = null)
{
CheckSession();
settings ??= new BeginTransactionSettings();
var request = new BeginTransactionRequest
{
SessionId = Id,
OperationParams = MakeOperationParams(settings),
TxSettings = settings.TxSettings?.ToProto().BeginTx
};
try
{
var response = await Driver.UnaryCall(
method: TableService.BeginTransactionMethod,
request: request,
settings: settings
);
var status = UnpackOperation(response.Data.Operation, out BeginTransactionResult? resultProto);
OnResponseStatus(status);
BeginTransactionResponse.ResultData? result = null;

if (status.IsSuccess && resultProto is not null)
{
result = BeginTransactionResponse.ResultData.FromProto(resultProto);
}

return new BeginTransactionResponse(status, result);
}
catch (Driver.TransportException e)
{
return new BeginTransactionResponse(e.Status);
}
}

public async Task<CommitTransactionResponse> CommitTransaction(Transaction tx, CommitTransactionSettings? settings = null)
{
CheckSession();
settings ??= new CommitTransactionSettings();
var request = new CommitTransactionRequest
{
SessionId = Id,
OperationParams = MakeOperationParams(settings),
TxId = tx.TxId
};
try
{
var response = await Driver.UnaryCall(
method: TableService.CommitTransactionMethod,
request: request,
settings: settings
);
var status = UnpackOperation(response.Data.Operation, out CommitTransactionResult? resultProto);
OnResponseStatus(status);
CommitTransactionResponse.ResultData? result = null;

if (status.IsSuccess && resultProto is not null)
{
result = CommitTransactionResponse.ResultData.FromProto(resultProto);
}

return new CommitTransactionResponse(status, result);
}
catch (Driver.TransportException e)
{
return new CommitTransactionResponse(e.Status);
}
}

public async Task<RollbackTransactionResponse> RollbackTransaction(Transaction tx, RollbackTransactionSettings? settings = null)
{
CheckSession();
settings ??= new RollbackTransactionSettings();
var request = new RollbackTransactionRequest
{
SessionId = Id,
OperationParams = MakeOperationParams(settings),
TxId = tx.TxId
};

try
{
var response = await UnaryCall(
method: TableService.RollbackTransactionMethod,
request: request,
settings: settings
);

var status = UnpackOperation(response.Data.Operation);
OnResponseStatus(status);

return new RollbackTransactionResponse(status);

}
catch (Driver.TransportException e)
{
return new RollbackTransactionResponse(e.Status);
}
}
}
6 changes: 3 additions & 3 deletions src/Ydb.Sdk/tests/TableService/Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Ydb.Sdk.Tests.TableService;

public class Shared
{
public static async Task CreateSimpleTable(TableClient tableClient, string tablePath)
public static async Task CreateSimpleTable(TableClient tableClient, string tablePath, string columnName = "test")
{
var settings = new CreateTableSettings()
.WithColumn(new ColumnMeta("a", YdbValue.Uint64Type))
.WithPrimaryKeys("a");
.WithColumn(new ColumnMeta(columnName, YdbValue.Uint64Type))
.WithPrimaryKeys(columnName);

var response = await tableClient.CreateTable(tablePath, settings);
response.Status.EnsureSuccess();
Expand Down
5 changes: 3 additions & 2 deletions src/Ydb.Sdk/tests/TableService/TestDescribeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public async Task DescribeNotExisting()
public async Task CreateAndDescribe()
{
var tablePath = Guid.NewGuid().ToString("n");
const string columnName = "columnName";

await Shared.CreateSimpleTable(_tableClient, tablePath);
await Shared.CreateSimpleTable(_tableClient, tablePath, columnName);

var describeResponse = await _tableClient.DescribeTable(tablePath);
describeResponse.Status.EnsureSuccess();
Assert.True(describeResponse.Result.PrimaryKey.SequenceEqual(new[] { "a" }));
Assert.True(describeResponse.Result.PrimaryKey.SequenceEqual(new[] { "columnName" }));

await Shared.DropTable(_tableClient, tablePath);
}
Expand Down
Loading

0 comments on commit c81eee0

Please sign in to comment.