Search in sources :

Example 1 with ByteBuffer

use of io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer in project deephaven-core by deephaven.

the class ClientTableState method applyTableCreationResponse.

public void applyTableCreationResponse(ExportedTableCreationResponse def) {
    assert def.getResultId().getTicket().getTicket_asB64().equals(getHandle().makeTicket().getTicket_asB64()) : "Ticket is incompatible with the table details";
    // by definition, the ticket is now exported and connected
    handle.setState(TableTicket.State.EXPORTED);
    handle.setConnected(true);
    // we conform to flight's schema representation of:
    // - IPC_CONTINUATION_TOKEN (4-byte int of -1)
    // - message size (4-byte int)
    // - a Message wrapping the schema
    ByteBuffer bb = new ByteBuffer(def.getSchemaHeader_asU8());
    bb.setPosition(bb.position() + 8);
    Message headerMessage = Message.getRootAsMessage(bb);
    assert headerMessage.headerType() == MessageHeader.Schema;
    Schema schema = headerMessage.header(new Schema());
    ColumnDefinition[] cols = new ColumnDefinition[(int) schema.fieldsLength()];
    for (int i = 0; i < schema.fieldsLength(); i++) {
        cols[i] = new ColumnDefinition();
        Field f = schema.fields(i);
        Map<String, String> fieldMetadata = keyValuePairs("deephaven:", f.customMetadataLength(), f::customMetadata);
        cols[i].setName(f.name().asString());
        cols[i].setColumnIndex(i);
        cols[i].setType(fieldMetadata.get("type"));
        cols[i].setStyleColumn("true".equals(fieldMetadata.get("isStyle")));
        cols[i].setFormatColumn("true".equals(fieldMetadata.get("isDateFormat")) || "true".equals(fieldMetadata.get("isNumberFormat")));
        cols[i].setForRow("true".equals(fieldMetadata.get("isRowStyle")));
        String formatColumnName = fieldMetadata.get("dateFormatColumn");
        if (formatColumnName == null) {
            formatColumnName = fieldMetadata.get("numberFormatColumn");
        }
        cols[i].setFormatColumnName(formatColumnName);
        cols[i].setStyleColumnName(fieldMetadata.get("styleColumn"));
        if (fieldMetadata.containsKey("inputtable.isKey")) {
            cols[i].setInputTableKeyColumn(Boolean.parseBoolean(fieldMetadata.get("inputtable.isKey")));
        }
        cols[i].setDescription(fieldMetadata.get("description"));
    }
    TableAttributesDefinition attributes = new TableAttributesDefinition(keyValuePairs("deephaven:attribute.", schema.customMetadataLength(), schema::customMetadata), keyValuePairs("deephaven:unsent.attribute.", schema.customMetadataLength(), schema::customMetadata).keySet());
    setTableDef(new InitialTableDefinition().setAttributes(attributes).setColumns(cols));
    setResolution(ResolutionState.RUNNING);
    setSize(Long.parseLong(def.getSize()));
}
Also used : TableAttributesDefinition(io.deephaven.web.client.api.barrage.def.TableAttributesDefinition) Message(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message) Schema(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Schema) ByteBuffer(io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer) ColumnDefinition(io.deephaven.web.client.api.barrage.def.ColumnDefinition) Field(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Field) InitialTableDefinition(io.deephaven.web.client.api.barrage.def.InitialTableDefinition)

Example 2 with ByteBuffer

use of io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer in project deephaven-core by deephaven.

the class TableViewportSubscription method snapshot.

@JsMethod
public Promise<TableData> snapshot(JsRangeSet rows, Column[] columns) {
    // TODO #1039 slice rows and drop columns
    return copy.then(table -> {
        final ClientTableState state = table.state();
        String[] columnTypes = Arrays.stream(state.getTableDef().getColumns()).map(ColumnDefinition::getType).toArray(String[]::new);
        final BitSet columnBitset = table.lastVisibleState().makeBitset(columns);
        return Callbacks.<TableSnapshot, String>promise(this, callback -> {
            WorkerConnection connection = table.getConnection();
            BiDiStream<FlightData, FlightData> stream = connection.<FlightData, FlightData>streamFactory().create(headers -> connection.flightServiceClient().doExchange(headers), (first, headers) -> connection.browserFlightServiceClient().openDoExchange(first, headers), (next, headers, c) -> connection.browserFlightServiceClient().nextDoExchange(next, headers, c::apply), new FlightData());
            Builder doGetRequest = new Builder(1024);
            double columnsOffset = BarrageSubscriptionRequest.createColumnsVector(doGetRequest, makeUint8ArrayFromBitset(columnBitset));
            double viewportOffset = BarrageSubscriptionRequest.createViewportVector(doGetRequest, serializeRanges(Collections.singleton(rows.getRange())));
            double serializationOptionsOffset = BarrageSnapshotOptions.createBarrageSnapshotOptions(doGetRequest, ColumnConversionMode.Stringify, true, 0);
            double tableTicketOffset = BarrageSubscriptionRequest.createTicketVector(doGetRequest, state.getHandle().getTicket());
            BarrageSnapshotRequest.startBarrageSnapshotRequest(doGetRequest);
            BarrageSnapshotRequest.addTicket(doGetRequest, tableTicketOffset);
            BarrageSnapshotRequest.addColumns(doGetRequest, columnsOffset);
            BarrageSnapshotRequest.addSnapshotOptions(doGetRequest, serializationOptionsOffset);
            BarrageSnapshotRequest.addViewport(doGetRequest, viewportOffset);
            doGetRequest.finish(BarrageSnapshotRequest.endBarrageSnapshotRequest(doGetRequest));
            FlightData request = new FlightData();
            request.setAppMetadata(BarrageUtils.wrapMessage(doGetRequest, BarrageMessageType.BarrageSnapshotRequest));
            stream.send(request);
            stream.end();
            stream.onData(flightData -> {
                Message message = Message.getRootAsMessage(new ByteBuffer(flightData.getDataHeader_asU8()));
                if (message.headerType() == MessageHeader.Schema) {
                    // ignore for now, we'll handle this later
                    return;
                }
                assert message.headerType() == MessageHeader.RecordBatch;
                RecordBatch header = message.header(new RecordBatch());
                Uint8Array appMetadataBytes = flightData.getAppMetadata_asU8();
                BarrageUpdateMetadata update = null;
                if (appMetadataBytes.length != 0) {
                    BarrageMessageWrapper barrageMessageWrapper = BarrageMessageWrapper.getRootAsBarrageMessageWrapper(new io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer(appMetadataBytes));
                    update = BarrageUpdateMetadata.getRootAsBarrageUpdateMetadata(new ByteBuffer(new Uint8Array(barrageMessageWrapper.msgPayloadArray())));
                }
                TableSnapshot snapshot = BarrageUtils.createSnapshot(header, BarrageUtils.typedArrayToLittleEndianByteBuffer(flightData.getDataBody_asU8()), update, true, columnTypes);
                callback.onSuccess(snapshot);
            });
            stream.onStatus(status -> {
                if (!status.isOk()) {
                    callback.onFailure(status.getDetails());
                }
            });
        }).then(defer()).then(snapshot -> {
            SubscriptionTableData pretendSubscription = new SubscriptionTableData(Js.uncheckedCast(columns), state.getRowFormatColumn() == null ? NO_ROW_FORMAT_COLUMN : state.getRowFormatColumn().getIndex(), null);
            TableData data = pretendSubscription.handleSnapshot(snapshot);
            return Promise.resolve(data);
        }).then(defer());
    });
}
Also used : BarrageSubscriptionOptions(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSubscriptionOptions) ColumnConversionMode(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.ColumnConversionMode) IThenable(elemental2.promise.IThenable) Arrays(java.util.Arrays) BarrageSnapshotRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSnapshotRequest) JsOptional(jsinterop.annotations.JsOptional) BarrageUtils.serializeRanges(io.deephaven.web.client.api.barrage.BarrageUtils.serializeRanges) BarrageUpdateMetadata(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageUpdateMetadata) Event(elemental2.dom.Event) JsRangeSet(io.deephaven.web.client.api.JsRangeSet) BarrageMessageType(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageType) JsMethod(jsinterop.annotations.JsMethod) HasEventHandling(io.deephaven.web.client.api.HasEventHandling) Promise(elemental2.promise.Promise) BiDiStream(io.deephaven.web.client.api.barrage.stream.BiDiStream) ByteBuffer(io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer) Callbacks(io.deephaven.web.client.api.Callbacks) TableData(io.deephaven.web.client.api.TableData) Js(jsinterop.base.Js) WorkerConnection(io.deephaven.web.client.api.WorkerConnection) JsTable(io.deephaven.web.client.api.JsTable) BarrageUtils.makeUint8ArrayFromBitset(io.deephaven.web.client.api.barrage.BarrageUtils.makeUint8ArrayFromBitset) Builder(io.deephaven.javascript.proto.dhinternal.flatbuffers.Builder) FlightData(io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb.FlightData) BarrageSnapshotOptions(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSnapshotOptions) Int8Array(elemental2.core.Int8Array) CustomEvent(elemental2.dom.CustomEvent) JsLog(io.deephaven.web.client.fu.JsLog) DomGlobal(elemental2.dom.DomGlobal) ClientTableState(io.deephaven.web.client.state.ClientTableState) CustomEventInit(elemental2.dom.CustomEventInit) NO_ROW_FORMAT_COLUMN(io.deephaven.web.client.api.subscription.ViewportData.NO_ROW_FORMAT_COLUMN) ColumnDefinition(io.deephaven.web.client.api.barrage.def.ColumnDefinition) RecordBatch(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.RecordBatch) Column(io.deephaven.web.client.api.Column) BarrageUtils(io.deephaven.web.client.api.barrage.BarrageUtils) Uint8Array(elemental2.core.Uint8Array) TableSnapshot(io.deephaven.web.shared.data.TableSnapshot) Message(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message) BarrageSubscriptionRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSubscriptionRequest) BitSet(java.util.BitSet) MessageHeader(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.MessageHeader) Collections(java.util.Collections) BarrageMessageWrapper(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageWrapper) BarrageMessageWrapper(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageWrapper) Message(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message) RecordBatch(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.RecordBatch) Builder(io.deephaven.javascript.proto.dhinternal.flatbuffers.Builder) BitSet(java.util.BitSet) ByteBuffer(io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer) BarrageUpdateMetadata(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageUpdateMetadata) BiDiStream(io.deephaven.web.client.api.barrage.stream.BiDiStream) FlightData(io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb.FlightData) ClientTableState(io.deephaven.web.client.state.ClientTableState) TableSnapshot(io.deephaven.web.shared.data.TableSnapshot) Uint8Array(elemental2.core.Uint8Array) TableData(io.deephaven.web.client.api.TableData) WorkerConnection(io.deephaven.web.client.api.WorkerConnection) JsMethod(jsinterop.annotations.JsMethod)

Aggregations

Message (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message)2 ByteBuffer (io.deephaven.javascript.proto.dhinternal.flatbuffers.ByteBuffer)2 ColumnDefinition (io.deephaven.web.client.api.barrage.def.ColumnDefinition)2 Int8Array (elemental2.core.Int8Array)1 Uint8Array (elemental2.core.Uint8Array)1 CustomEvent (elemental2.dom.CustomEvent)1 CustomEventInit (elemental2.dom.CustomEventInit)1 DomGlobal (elemental2.dom.DomGlobal)1 Event (elemental2.dom.Event)1 IThenable (elemental2.promise.IThenable)1 Promise (elemental2.promise.Promise)1 MessageHeader (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.MessageHeader)1 RecordBatch (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.RecordBatch)1 Field (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Field)1 Schema (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Schema)1 FlightData (io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb.FlightData)1 Builder (io.deephaven.javascript.proto.dhinternal.flatbuffers.Builder)1 BarrageMessageType (io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageType)1 BarrageMessageWrapper (io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageWrapper)1 BarrageSnapshotOptions (io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSnapshotOptions)1