use of io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest in project deephaven-core by deephaven.
the class JsInputTable method deleteTables.
public Promise<JsInputTable> deleteTables(JsTable[] tablesToDelete) {
if (tablesToDelete.length == 0) {
return Promise.resolve(this);
}
// for each table, make a view on that table of only key columns, then union the tables and drop together
final List<JsRunnable> cleanups = new ArrayList<>();
final Ticket ticketToDelete;
final Promise<?> failureToReport;
if (tablesToDelete.length == 1) {
JsTable onlyTable = tablesToDelete[0];
// don't try too hard to find matching columns, if it looks like we have a match go for it
if (onlyTable.getColumns().length == keys.length && onlyTable.findColumns(keys).length == keys.length) {
ticketToDelete = onlyTable.getHandle().makeTicket();
failureToReport = Promise.resolve((Object) null);
} else {
// view the only table
ticketToDelete = table.getConnection().getConfig().newTicket();
cleanups.add(() -> table.getConnection().releaseTicket(ticketToDelete));
SelectOrUpdateRequest view = new SelectOrUpdateRequest();
view.setSourceId(onlyTable.state().getHandle().makeTableReference());
view.setResultId(ticketToDelete);
view.setColumnSpecsList(keys);
failureToReport = Callbacks.grpcUnaryPromise(c -> table.getConnection().tableServiceClient().view(view, table.getConnection().metadata(), c::apply));
}
} else {
// there is more than one table here, construct a merge after making a view of each table
ticketToDelete = table.getConnection().getConfig().newTicket();
cleanups.add(() -> table.getConnection().releaseTicket(ticketToDelete));
BatchTableRequest batch = new BatchTableRequest();
for (int i = 0; i < tablesToDelete.length; i++) {
JsTable toDelete = tablesToDelete[i];
SelectOrUpdateRequest view = new SelectOrUpdateRequest();
view.setSourceId(toDelete.state().getHandle().makeTableReference());
view.setColumnSpecsList(keys);
batch.addOps(new Operation()).setView(view);
}
MergeTablesRequest mergeRequest = new MergeTablesRequest();
mergeRequest.setSourceIdsList(IntStream.range(0, tablesToDelete.length).mapToObj(i -> {
TableReference ref = new TableReference();
ref.setBatchOffset(i);
return ref;
}).toArray(TableReference[]::new));
mergeRequest.setResultId(ticketToDelete);
batch.addOps(new Operation()).setMerge(mergeRequest);
failureToReport = new Promise<>((resolve, reject) -> {
ResponseStreamWrapper<ExportedTableCreationResponse> wrapper = ResponseStreamWrapper.of(table.getConnection().tableServiceClient().batch(batch, table.getConnection().metadata()));
wrapper.onData(response -> {
// kill the promise on the first failure we see
if (!response.getSuccess()) {
reject.onInvoke(response.getErrorInfo());
}
});
wrapper.onEnd(status -> resolve.onInvoke((Object) null));
});
}
// perform the delete on the current input table
DeleteTableRequest deleteRequest = new DeleteTableRequest();
deleteRequest.setInputTable(table.getHeadHandle().makeTicket());
deleteRequest.setTableToRemove(ticketToDelete);
return Callbacks.grpcUnaryPromise(c -> {
table.getConnection().inputTableServiceClient().deleteTableFromInputTable(deleteRequest, table.getConnection().metadata(), c::apply);
}).then(success -> {
cleanups.forEach(JsRunnable::run);
return Promise.resolve(this);
}, err -> {
cleanups.forEach(JsRunnable::run);
// call
return (Promise) failureToReport.then(ignore -> Promise.reject(err));
});
}
Aggregations