Skip to content

Commit

Permalink
Merge pull request #209 from zinal/rename_tables
Browse files Browse the repository at this point in the history
Support RenameTables() operation in YDB Java SDK
  • Loading branch information
alex268 authored Dec 31, 2023
2 parents 8b51bea + 9d2680a commit 0e20b2a
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 17 deletions.
11 changes: 11 additions & 0 deletions table/src/main/java/tech/ydb/table/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import tech.ydb.table.settings.PrepareDataQuerySettings;
import tech.ydb.table.settings.ReadRowsSettings;
import tech.ydb.table.settings.ReadTableSettings;
import tech.ydb.table.settings.RenameTablesSettings;
import tech.ydb.table.settings.RollbackTxSettings;
import tech.ydb.table.transaction.Transaction;
import tech.ydb.table.transaction.TxControl;
Expand Down Expand Up @@ -64,6 +65,8 @@ CompletableFuture<Status> createTable(String path, TableDescription tableDescrip

CompletableFuture<Status> copyTables(CopyTablesSettings settings);

CompletableFuture<Status> renameTables(RenameTablesSettings settings);

CompletableFuture<Result<TableDescription>> describeTable(String path, DescribeTableSettings settings);

CompletableFuture<Result<DataQuery>> prepareDataQuery(String query, PrepareDataQuerySettings settings);
Expand Down Expand Up @@ -129,6 +132,14 @@ default CompletableFuture<Status> copyTable(String src, String dst) {
return copyTable(src, dst, new CopyTableSettings());
}

default CompletableFuture<Status> renameTable(String src, String dst) {
return renameTables(new RenameTablesSettings().addTable(src, dst));
}

default CompletableFuture<Status> renameTable(String src, String dst, boolean overwrite) {
return renameTables(new RenameTablesSettings().addTable(src, dst, overwrite));
}

default CompletableFuture<Result<TableDescription>> describeTable(String path) {
return describeTable(path, new DescribeTableSettings());
}
Expand Down
31 changes: 31 additions & 0 deletions table/src/main/java/tech/ydb/table/impl/BaseSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import tech.ydb.table.settings.PrepareDataQuerySettings;
import tech.ydb.table.settings.ReadRowsSettings;
import tech.ydb.table.settings.ReadTableSettings;
import tech.ydb.table.settings.RenameTablesSettings;
import tech.ydb.table.settings.ReplicationPolicy;
import tech.ydb.table.settings.RollbackTxSettings;
import tech.ydb.table.settings.StoragePolicy;
Expand Down Expand Up @@ -445,6 +446,36 @@ private List<YdbTable.CopyTableItem> convertCopyTableItems(CopyTablesSettings ct
}).collect(Collectors.toList());
}

@Override
public CompletableFuture<Status> renameTables(RenameTablesSettings settings) {
YdbTable.RenameTablesRequest request = YdbTable.RenameTablesRequest.newBuilder()
.setSessionId(id)
.addAllTables(convertRenameTableItems(settings))
.build();

final GrpcRequestSettings grpcRequestSettings = makeGrpcRequestSettings(settings.getTimeoutDuration());
return tableRpc.renameTables(request, grpcRequestSettings);
}

private List<YdbTable.RenameTableItem> convertRenameTableItems(RenameTablesSettings cts) {
final String dbpath = tableRpc.getDatabase();
return cts.getItems().stream().map(t -> {
String sp = t.getSourcePath();
if (!sp.startsWith("/")) {
sp = dbpath + "/" + sp;
}
String dp = t.getDestinationPath();
if (!dp.startsWith("/")) {
dp = dbpath + "/" + dp;
}
return YdbTable.RenameTableItem.newBuilder()
.setSourcePath(sp)
.setDestinationPath(dp)
.setReplaceDestination(t.isReplaceDestination())
.build();
}).collect(Collectors.toList());
}

@Override
public CompletableFuture<Result<TableDescription>> describeTable(String path, DescribeTableSettings settings) {
YdbTable.DescribeTableRequest request = YdbTable.DescribeTableRequest.newBuilder()
Expand Down
9 changes: 9 additions & 0 deletions table/src/main/java/tech/ydb/table/rpc/TableRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import tech.ydb.proto.table.YdbTable.ReadRowsResponse;
import tech.ydb.proto.table.YdbTable.ReadTableRequest;
import tech.ydb.proto.table.YdbTable.ReadTableResponse;
import tech.ydb.proto.table.YdbTable.RenameTablesRequest;
import tech.ydb.proto.table.YdbTable.RollbackTransactionRequest;


Expand Down Expand Up @@ -121,6 +122,14 @@ CompletableFuture<Result<CreateSessionResult>> createSession(CreateSessionReques
*/
CompletableFuture<Status> copyTables(CopyTablesRequest request, GrpcRequestSettings settings);

/**
* Renames the given tables, possibly replacing the existing destination tables.
* @param request request proto
* @param settings rpc call settings
* @return completable future with status of operation
*/
CompletableFuture<Status> renameTables(RenameTablesRequest request, GrpcRequestSettings settings);

/**
* Returns information about given table (metadata).
* @param request request proto
Expand Down
10 changes: 10 additions & 0 deletions table/src/main/java/tech/ydb/table/rpc/grpc/GrpcTableRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import tech.ydb.proto.table.YdbTable.PrepareQueryResult;
import tech.ydb.proto.table.YdbTable.ReadRowsRequest;
import tech.ydb.proto.table.YdbTable.ReadRowsResponse;
import tech.ydb.proto.table.YdbTable.RenameTablesRequest;
import tech.ydb.proto.table.YdbTable.RenameTablesResponse;
import tech.ydb.proto.table.YdbTable.RollbackTransactionRequest;
import tech.ydb.proto.table.YdbTable.RollbackTransactionResponse;
import tech.ydb.proto.table.v1.TableServiceGrpc;
Expand Down Expand Up @@ -147,6 +149,14 @@ public CompletableFuture<Status> copyTables(CopyTablesRequest request,
.thenApply(OperationManager.syncStatusUnwrapper(CopyTablesResponse::getOperation));
}

@Override
public CompletableFuture<Status> renameTables(RenameTablesRequest request,
GrpcRequestSettings settings) {
return transport
.unaryCall(TableServiceGrpc.getRenameTablesMethod(), settings, request)
.thenApply(OperationManager.syncStatusUnwrapper(RenameTablesResponse::getOperation));
}

@Override
public CompletableFuture<Result<DescribeTableResult>> describeTable(DescribeTableRequest request,
GrpcRequestSettings settings) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tech.ydb.table.settings;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author Maksim Zinal
*/
public class RenameTablesSettings extends RequestSettings<RenameTablesSettings> {

private final List<Item> items = new ArrayList<>();

public List<Item> getItems() {
return items;
}

public RenameTablesSettings addTable(String sourcePath, String destinationPath,
boolean replaceDestination) {
items.add(new Item(sourcePath, destinationPath, replaceDestination));
return this;
}

public RenameTablesSettings addTable(String sourcePath, String destinationPath) {
items.add(new Item(sourcePath, destinationPath));
return this;
}

public static class Item {
private final String sourcePath;
private final String destinationPath;
private final boolean replaceDestination;

public Item(String sourcePath, String destinationPath, boolean replaceDestination) {
this.sourcePath = sourcePath;
this.destinationPath = destinationPath;
this.replaceDestination = replaceDestination;
}

public Item(String sourcePath, String destinationPath) {
this.sourcePath = sourcePath;
this.destinationPath = destinationPath;
this.replaceDestination = false;
}

public String getSourcePath() {
return sourcePath;
}

public String getDestinationPath() {
return destinationPath;
}

public boolean isReplaceDestination() {
return replaceDestination;
}
}

}
6 changes: 6 additions & 0 deletions table/src/test/java/tech/ydb/table/SessionStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tech.ydb.table.settings.CommitTxSettings;
import tech.ydb.table.settings.CopyTableSettings;
import tech.ydb.table.settings.CopyTablesSettings;
import tech.ydb.table.settings.RenameTablesSettings;
import tech.ydb.table.settings.CreateTableSettings;
import tech.ydb.table.settings.DescribeTableSettings;
import tech.ydb.table.settings.DropTableSettings;
Expand Down Expand Up @@ -89,6 +90,11 @@ public CompletableFuture<Status> copyTables(CopyTablesSettings settings) {
return notImplemented("copyTables()");
}

@Override
public CompletableFuture<Status> renameTables(RenameTablesSettings settings) {
return notImplemented("renameTables()");
}

@Override
public CompletableFuture<Result<TableDescription>> describeTable(String path, DescribeTableSettings settings) {
return notImplemented("describeTable()");
Expand Down
40 changes: 23 additions & 17 deletions table/src/test/java/tech/ydb/table/TableRpcStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import tech.ydb.core.grpc.GrpcRequestSettings;
import tech.ydb.core.impl.call.EmptyStream;
import tech.ydb.core.utils.Async;
import tech.ydb.proto.ValueProtos;
import tech.ydb.proto.table.YdbTable;
import tech.ydb.proto.table.YdbTable.AlterTableRequest;
import tech.ydb.proto.table.YdbTable.BeginTransactionRequest;
import tech.ydb.proto.table.YdbTable.BeginTransactionResult;
import tech.ydb.proto.table.YdbTable.CommitTransactionRequest;
import tech.ydb.proto.table.YdbTable.CopyTableRequest;
import tech.ydb.proto.table.YdbTable.CopyTablesRequest;
import tech.ydb.proto.table.YdbTable.RenameTablesRequest;
import tech.ydb.proto.table.YdbTable.CreateSessionRequest;
import tech.ydb.proto.table.YdbTable.CreateSessionResult;
import tech.ydb.proto.table.YdbTable.CreateTableRequest;
Expand All @@ -39,11 +39,11 @@
import tech.ydb.proto.table.YdbTable.RollbackTransactionRequest;
import tech.ydb.table.rpc.TableRpc;


/**
* @author Sergey Polovko
*/
public class TableRpcStub implements TableRpc {

private final ScheduledExecutorService scheduler;

public TableRpcStub(ScheduledExecutorService scheduler) {
Expand All @@ -52,73 +52,79 @@ public TableRpcStub(ScheduledExecutorService scheduler) {

@Override
public CompletableFuture<Result<CreateSessionResult>> createSession(CreateSessionRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("createSession()");
}

@Override
public CompletableFuture<Status> deleteSession(DeleteSessionRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("deleteSession()");
}

@Override
public CompletableFuture<Result<KeepAliveResult>> keepAlive(KeepAliveRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("keepAlive()");
}

@Override
public CompletableFuture<Status> createTable(CreateTableRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("createTable()");
}

@Override
public CompletableFuture<Status> dropTable(DropTableRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("dropTable()");
}

@Override
public CompletableFuture<Status> alterTable(AlterTableRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("alterTable()");
}

@Override
public CompletableFuture<Status> copyTable(CopyTableRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("copyTable()");
}

@Override
public CompletableFuture<Status> copyTables(CopyTablesRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("copyTables()");
}

@Override
public CompletableFuture<Status> renameTables(RenameTablesRequest request,
GrpcRequestSettings settings) {
return notImplemented("renameTables()");
}

@Override
public CompletableFuture<Result<DescribeTableResult>> describeTable(DescribeTableRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("describeTable()");
}

@Override
public CompletableFuture<Result<ExplainQueryResult>> explainDataQuery(ExplainDataQueryRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("explainDataQuery()");
}

@Override
public CompletableFuture<Result<PrepareQueryResult>> prepareDataQuery(PrepareDataQueryRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("prepareDataQuery()");
}

@Override
public CompletableFuture<Result<ExecuteQueryResult>> executeDataQuery(ExecuteDataQueryRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("executeDataQuery()");
}

Expand All @@ -129,19 +135,19 @@ public CompletableFuture<Result<ReadRowsResponse>> readRows(YdbTable.ReadRowsReq

@Override
public CompletableFuture<Status> executeSchemeQuery(ExecuteSchemeQueryRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("executeSchemeQuery()");
}

@Override
public CompletableFuture<Result<BeginTransactionResult>> beginTransaction(BeginTransactionRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("beginTransaction()");
}

@Override
public CompletableFuture<Status> commitTransaction(CommitTransactionRequest request,
GrpcRequestSettings settings) {
GrpcRequestSettings settings) {
return notImplemented("commitTransaction()");
}

Expand Down
Loading

0 comments on commit 0e20b2a

Please sign in to comment.