Skip to content

Commit

Permalink
TableDescribe add protobuf wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Oct 5, 2023
1 parent 66023c7 commit 8310f19
Show file tree
Hide file tree
Showing 23 changed files with 866 additions and 236 deletions.
2 changes: 2 additions & 0 deletions src/Ydb.Sdk/src/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Driver : IDisposable, IAsyncDisposable
private readonly string _sdkInfo;
private readonly ChannelsCache _channels;
private bool _disposed;

internal string Database => _config.Database;

public Driver(DriverConfig config, ILoggerFactory? loggerFactory = null)
{
Expand Down
27 changes: 11 additions & 16 deletions src/Ydb.Sdk/src/Services/Table/CopyTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public class CopyTableItem
public string DestinationPath { get; }
public bool OmitIndexes { get; }

public CopyTableItem(string sourcePath, string destinationPath, bool omitIndexes = false)
public CopyTableItem(string sourcePath, string destinationPath, bool omitIndexes)
{
SourcePath = sourcePath;
DestinationPath = destinationPath;
OmitIndexes = omitIndexes;
}

public Ydb.Table.CopyTableItem ToProto()
public Ydb.Table.CopyTableItem GetProto(TableClient tableClient)
{
return new Ydb.Table.CopyTableItem
{
SourcePath = SourcePath,
DestinationPath = DestinationPath,
SourcePath = tableClient.MakeTablePath(SourcePath),
DestinationPath = tableClient.MakeTablePath(DestinationPath),
OmitIndexes = OmitIndexes
};
}
Expand Down Expand Up @@ -52,20 +52,15 @@ internal CopyTablesResponse(Status status) : base(status)

public partial class TableClient
{
public async Task<CopyTableResponse> CopyTable(CopyTableItem item, CreateTableSettings? settings = null)
{
return await CopyTable(item.SourcePath, item.DestinationPath);
}

public async Task<CopyTableResponse> CopyTable(string sourcePath, string destinationPath,
CreateTableSettings? settings = null)
CopyTableSettings? settings = null)
{
settings ??= new CreateTableSettings();
settings ??= new CopyTableSettings();
var request = new CopyTableRequest
{
OperationParams = MakeOperationParams(settings),
SourcePath = sourcePath,
DestinationPath = destinationPath
SourcePath = MakeTablePath(sourcePath),
DestinationPath = MakeTablePath(destinationPath)
};

try
Expand All @@ -85,14 +80,14 @@ public async Task<CopyTableResponse> CopyTable(string sourcePath, string destina
}

public async Task<CopyTablesResponse> CopyTables(List<CopyTableItem> tableItems,
CreateTableSettings? settings = null)
CopyTablesSettings? settings = null)
{
settings ??= new CreateTableSettings();
settings ??= new CopyTablesSettings();
var request = new CopyTablesRequest
{
OperationParams = MakeOperationParams(settings),
};
request.Tables.AddRange(tableItems.Select(item => item.ToProto()));
request.Tables.AddRange(tableItems.Select(item => item.GetProto(this)));

try
{
Expand Down
127 changes: 104 additions & 23 deletions src/Ydb.Sdk/src/Services/Table/CreateTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,98 @@

namespace Ydb.Sdk.Services.Table;

public class Index
{
public string Name { get; }

public List<string> Columns { get; }

public Index(string name, params string[] columns)
{
Name = name;
Columns = columns.ToList();
}

public TableIndex GetTableIndex()
{
return new TableIndex
{
Name = Name,
IndexColumns = { Columns },
GlobalIndex = new GlobalIndex()
};
}
}

public class ColumnFamily
{
public string Name { get; }

public StoragePool Data { get; }

public Ydb.Table.ColumnFamily.Types.Compression Compression { get; }

public FeatureFlagStatus KeepInMemory { get; }

public ColumnFamily(string name, StoragePool data,
Ydb.Table.ColumnFamily.Types.Compression compression = Ydb.Table.ColumnFamily.Types.Compression.Unspecified,
FeatureFlagStatus keepInMemory = FeatureFlagStatus.Unspecified)
{
Compression = compression;
Name = name;
Data = data;
KeepInMemory = keepInMemory;
}

internal ColumnFamily(Ydb.Table.ColumnFamily proto)
{
Compression = proto.Compression;
Name = proto.Name;
Data = new StoragePool(proto.Data);
KeepInMemory = proto.KeepInMemory.FromProto();
}

internal Ydb.Table.ColumnFamily GetProto() =>
new()
{
Compression = Compression,
Name = Name,
Data = Data.GetProto(),
KeepInMemory = KeepInMemory.GetProto()
};
}

public class Column
{
public string Name { get; }

public Type Type { get; }

public string? Family { get; }

public Column(string name, Type type, string? family = null)
{
Name = name;
Type = type;
Family = family ?? "";
}

internal Column(ColumnMeta proto)
{
Name = proto.Name;
Type = proto.Type;
Family = proto.Family;
}

internal ColumnMeta GetProto() =>
new()
{
Name = Name,
Family = Family,
Type = Type
};
}

public class CreateTableSettings : OperationRequestSettings
{
public List<Column> Columns { get; } = new();
Expand All @@ -16,7 +108,7 @@ public class CreateTableSettings : OperationRequestSettings

public TtlSettings? TtlSettings { get; private set; }

public StorageSettings StorageSettings { get; private set; } = new();
public StorageSettings? StorageSettings { get; private set; }

public List<ColumnFamily> ColumnFamilies { get; } = new();

Expand All @@ -26,13 +118,13 @@ public class CreateTableSettings : OperationRequestSettings
public ulong? UniformPartitions { get; private set; }
public ExplicitPartitions? PartitionAtKeys { get; private set; }

public PartitioningSettings PartitioningSettings { get; private set; } = new();
public PartitioningSettings? PartitioningSettings { get; private set; }

public string CompactionPolicy { get; private set; } = "default";

public Ydb.FeatureFlag.Types.Status KeyBloomFilter { get; private set; } = FeatureFlag.Types.Status.Unspecified;
public FeatureFlagStatus KeyBloomFilter { get; private set; } = FeatureFlagStatus.Unspecified;

public ReadReplicasSettings ReadReplicasSettings { get; private set; } = new() { AnyAzReadReplicasCount = 1 };
public ReadReplicasSettings? ReadReplicasSettings { get; private set; }

public CreateTableSettings WithColumn(Column column)
{
Expand Down Expand Up @@ -76,7 +168,7 @@ public CreateTableSettings WithTtlSettings(TtlSettings ttlSettings)
return this;
}

public CreateTableSettings WithStorageSettings(StorageSettings storageSettings)
public CreateTableSettings WithStorageSettings(StorageSettings? storageSettings)
{
StorageSettings = storageSettings;
return this;
Expand Down Expand Up @@ -108,13 +200,7 @@ public CreateTableSettings WithAttributes(Dictionary<string, string> attributes)

public CreateTableSettings WithCompactionPolicy(CompactionPolicy compactionPolicy)
{
CompactionPolicy = compactionPolicy switch
{
Table.CompactionPolicy.Default => "default",
Table.CompactionPolicy.SmallTable => "small_table",
Table.CompactionPolicy.LogTable => "log_table",
_ => throw new ArgumentOutOfRangeException(nameof(compactionPolicy), compactionPolicy, null)
};
CompactionPolicy = compactionPolicy.GetString();
return this;
}

Expand Down Expand Up @@ -150,11 +236,6 @@ public CreateTableSettings WithKeyBloomFilter(FeatureFlag.Types.Status keyBloomF

public CreateTableSettings WithReadReplicasSettings(ReadReplicasSettings readReplicasSettings)
{
if (readReplicasSettings is { AnyAzReadReplicasCount: 0, PerAzReadReplicasCount: 0 })
{
throw new ArgumentException("readReplicasCount should not be equal to zero");
}

ReadReplicasSettings = readReplicasSettings;
return this;
}
Expand Down Expand Up @@ -185,21 +266,21 @@ public async Task<CreateTableResponse> CreateTable(string tablePath, CreateTable
var request = new CreateTableRequest
{
OperationParams = MakeOperationParams(settings),
Path = tablePath,
Path = MakeTablePath(tablePath),
Columns = { settings.GetColumnsMeta() },
PrimaryKey = { settings.PrimaryKey },
Profile = settings.Profile,
Indexes = { settings.GetTableIndexes() },
StorageSettings = settings.StorageSettings,
StorageSettings = settings.StorageSettings?.GetProto(),
ColumnFamilies = { settings.GetColumnFamilies() },
Attributes = { settings.Attributes },
CompactionPolicy = settings.CompactionPolicy,
PartitioningSettings = settings.PartitioningSettings,
KeyBloomFilter = settings.KeyBloomFilter,
ReadReplicasSettings = settings.ReadReplicasSettings,
PartitioningSettings = settings.PartitioningSettings?.GetProto(),
KeyBloomFilter = settings.KeyBloomFilter.GetProto(),
ReadReplicasSettings = settings.ReadReplicasSettings?.GetProto()
};
if (settings.TtlSettings is not null)
request.TtlSettings = settings.TtlSettings;
request.TtlSettings = settings.TtlSettings.GetProto();
if (settings.UniformPartitions is not null)
request.UniformPartitions = (ulong)settings.UniformPartitions;
if (settings.PartitionAtKeys is not null)
Expand Down
Loading

0 comments on commit 8310f19

Please sign in to comment.