Search in sources :

Example 1 with SelectOrUpdateRequest

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));
    });
}
Also used : SelectOrUpdateRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest) IntStream(java.util.stream.IntStream) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) BatchTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.BatchTableRequest) Arrays(java.util.Arrays) JsOptional(jsinterop.annotations.JsOptional) Promise(elemental2.promise.Promise) JsPropertyMap(jsinterop.base.JsPropertyMap) ArrayList(java.util.ArrayList) Callbacks(io.deephaven.web.client.api.Callbacks) JsProperty(jsinterop.annotations.JsProperty) TableReference(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference) Operation(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.batchtablerequest.Operation) JsTable(io.deephaven.web.client.api.JsTable) DeleteTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.DeleteTableRequest) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) JsType(jsinterop.annotations.JsType) JsObject(elemental2.core.JsObject) SelectOrUpdateRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) ExportedTableCreationResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableCreationResponse) AddTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.AddTableRequest) MergeTablesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest) Column(io.deephaven.web.client.api.Column) JsIgnore(jsinterop.annotations.JsIgnore) List(java.util.List) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) ArrayList(java.util.ArrayList) Operation(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.batchtablerequest.Operation) DeleteTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.DeleteTableRequest) Promise(elemental2.promise.Promise) JsTable(io.deephaven.web.client.api.JsTable) TableReference(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference) BatchTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.BatchTableRequest) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) MergeTablesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest) JsObject(elemental2.core.JsObject)

Aggregations

JsObject (elemental2.core.JsObject)1 Promise (elemental2.promise.Promise)1 AddTableRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.AddTableRequest)1 DeleteTableRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb.DeleteTableRequest)1 BatchTableRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.BatchTableRequest)1 ExportedTableCreationResponse (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableCreationResponse)1 MergeTablesRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest)1 SelectOrUpdateRequest (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.SelectOrUpdateRequest)1 TableReference (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference)1 Operation (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.batchtablerequest.Operation)1 Ticket (io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket)1 Callbacks (io.deephaven.web.client.api.Callbacks)1 Column (io.deephaven.web.client.api.Column)1 JsTable (io.deephaven.web.client.api.JsTable)1 ResponseStreamWrapper (io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper)1 JsRunnable (io.deephaven.web.shared.fu.JsRunnable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 IntStream (java.util.stream.IntStream)1