Search in sources :

Example 21 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project deephaven-core by deephaven.

the class BarrageStreamGenerator method getSubscriptionMetadata.

private ByteBuffer getSubscriptionMetadata(final SubView view) throws IOException {
    final FlatBufferBuilder metadata = new FlatBufferBuilder();
    int effectiveViewportOffset = 0;
    if (isSnapshot && view.isViewport()) {
        try (final RowSetGenerator viewportGen = new RowSetGenerator(view.viewport)) {
            effectiveViewportOffset = viewportGen.addToFlatBuffer(metadata);
        }
    }
    int effectiveColumnSetOffset = 0;
    if (isSnapshot && view.subscribedColumns != null) {
        effectiveColumnSetOffset = new BitSetGenerator(view.subscribedColumns).addToFlatBuffer(metadata);
    }
    final int rowsAddedOffset;
    if (isSnapshot && !view.isInitialSnapshot) {
        // client's don't need/want to receive the full RowSet on every snapshot
        rowsAddedOffset = EmptyRowSetGenerator.INSTANCE.addToFlatBuffer(metadata);
    } else {
        rowsAddedOffset = rowsAdded.addToFlatBuffer(metadata);
    }
    final int rowsRemovedOffset = rowsRemoved.addToFlatBuffer(metadata);
    final int shiftDataOffset = shifted.addToFlatBuffer(metadata);
    // Added Chunk Data:
    int addedRowsIncludedOffset = 0;
    // don't send `rowsIncluded` when identical to `rowsAdded`, client will infer they are the same
    if (isSnapshot || !view.addRowKeys.equals(rowsAdded.original)) {
        addedRowsIncludedOffset = rowsIncluded.addToFlatBuffer(view.addRowKeys, metadata);
    }
    // now add mod-column streams, and write the mod column indexes
    TIntArrayList modOffsets = new TIntArrayList(modColumnData.length);
    for (final ModColumnData mcd : modColumnData) {
        final int myModRowOffset;
        if (view.keyspaceViewport != null) {
            myModRowOffset = mcd.rowsModified.addToFlatBuffer(view.keyspaceViewport, metadata);
        } else {
            myModRowOffset = mcd.rowsModified.addToFlatBuffer(metadata);
        }
        modOffsets.add(BarrageModColumnMetadata.createBarrageModColumnMetadata(metadata, myModRowOffset));
    }
    BarrageUpdateMetadata.startModColumnNodesVector(metadata, modOffsets.size());
    modOffsets.forEachDescending(offset -> {
        metadata.addOffset(offset);
        return true;
    });
    final int nodesOffset = metadata.endVector();
    BarrageUpdateMetadata.startBarrageUpdateMetadata(metadata);
    BarrageUpdateMetadata.addNumAddBatches(metadata, LongSizedDataStructure.intSize("BarrageStreamGenerator", (view.numAddRows + view.batchSize() - 1) / view.batchSize()));
    BarrageUpdateMetadata.addNumModBatches(metadata, LongSizedDataStructure.intSize("BarrageStreamGenerator", (view.numModRows + view.batchSize() - 1) / view.batchSize()));
    BarrageUpdateMetadata.addIsSnapshot(metadata, isSnapshot);
    BarrageUpdateMetadata.addFirstSeq(metadata, firstSeq);
    BarrageUpdateMetadata.addLastSeq(metadata, lastSeq);
    BarrageUpdateMetadata.addEffectiveViewport(metadata, effectiveViewportOffset);
    BarrageUpdateMetadata.addEffectiveColumnSet(metadata, effectiveColumnSetOffset);
    BarrageUpdateMetadata.addAddedRows(metadata, rowsAddedOffset);
    BarrageUpdateMetadata.addRemovedRows(metadata, rowsRemovedOffset);
    BarrageUpdateMetadata.addShiftData(metadata, shiftDataOffset);
    BarrageUpdateMetadata.addAddedRowsIncluded(metadata, addedRowsIncludedOffset);
    BarrageUpdateMetadata.addModColumnNodes(metadata, nodesOffset);
    BarrageUpdateMetadata.addEffectiveReverseViewport(metadata, view.reverseViewport);
    metadata.finish(BarrageUpdateMetadata.endBarrageUpdateMetadata(metadata));
    final FlatBufferBuilder header = new FlatBufferBuilder();
    final int payloadOffset = BarrageMessageWrapper.createMsgPayloadVector(header, metadata.dataBuffer());
    BarrageMessageWrapper.startBarrageMessageWrapper(header);
    BarrageMessageWrapper.addMagic(header, BarrageUtil.FLATBUFFER_MAGIC);
    BarrageMessageWrapper.addMsgType(header, BarrageMessageType.BarrageUpdateMetadata);
    BarrageMessageWrapper.addMsgPayload(header, payloadOffset);
    header.finish(BarrageMessageWrapper.endBarrageMessageWrapper(header));
    return header.dataBuffer().slice();
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 22 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project deephaven-core by deephaven.

the class FlightServiceGrpcImpl method doGetCustom.

public void doGetCustom(final Flight.Ticket request, final StreamObserver<InputStream> responseObserver) {
    GrpcUtil.rpcWrapper(log, responseObserver, () -> {
        final SessionState session = sessionService.getCurrentSession();
        final SessionState.ExportObject<BaseTable> export = ticketRouter.resolve(session, request, "request");
        final long queueStartTm = System.nanoTime();
        final BarragePerformanceLog.SnapshotMetricsHelper metrics = new BarragePerformanceLog.SnapshotMetricsHelper();
        session.nonExport().require(export).onError(responseObserver).submit(() -> {
            metrics.queueNanos = System.nanoTime() - queueStartTm;
            final BaseTable table = export.get();
            metrics.tableId = Integer.toHexString(System.identityHashCode(table));
            metrics.tableKey = BarragePerformanceLog.getKeyFor(table);
            // create an adapter for the response observer
            final StreamObserver<BarrageStreamGenerator.View> listener = ArrowModule.provideListenerAdapter().adapt(responseObserver);
            // Send Schema wrapped in Message
            final FlatBufferBuilder builder = new FlatBufferBuilder();
            final int schemaOffset = BarrageUtil.makeSchemaPayload(builder, table.getDefinition(), table.getAttributes());
            builder.finish(MessageHelper.wrapInMessage(builder, schemaOffset, org.apache.arrow.flatbuf.MessageHeader.Schema));
            final ByteBuffer serializedMessage = builder.dataBuffer();
            // leverage the stream generator SchemaView constructor
            final BarrageStreamGenerator.SchemaView schemaView = new BarrageStreamGenerator.SchemaView(serializedMessage);
            // push the schema to the listener
            listener.onNext(schemaView);
            // get ourselves some data!
            final long snapshotStartTm = System.nanoTime();
            final BarrageMessage msg = ConstructSnapshot.constructBackplaneSnapshot(this, table);
            // actually no mod column data for DoGet
            msg.modColumnData = ZERO_MOD_COLUMNS;
            metrics.snapshotNanos = System.nanoTime() - snapshotStartTm;
            // translate the viewport to keyspace and make the call
            try (final BarrageStreamGenerator bsg = new BarrageStreamGenerator(msg, metrics)) {
                listener.onNext(bsg.getSnapshotView(DEFAULT_SNAPSHOT_DESER_OPTIONS));
            }
            listener.onCompleted();
        });
    });
}
Also used : SessionState(io.deephaven.server.session.SessionState) BarragePerformanceLog(io.deephaven.extensions.barrage.BarragePerformanceLog) BarrageStreamGenerator(io.deephaven.server.barrage.BarrageStreamGenerator) ByteBuffer(java.nio.ByteBuffer) BaseTable(io.deephaven.engine.table.impl.BaseTable) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BarrageMessage(io.deephaven.engine.table.impl.util.BarrageMessage)

Example 23 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nem2-sdk-java by nemtech.

the class AggregateTransaction method generateBytes.

byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    byte[] transactionsBytes = new byte[0];
    for (Transaction innerTransaction : innerTransactions) {
        byte[] transactionBytes = innerTransaction.toAggregateTransactionBytes();
        transactionsBytes = ArrayUtils.addAll(transactionsBytes, transactionBytes);
    }
    // Create Vectors
    int signatureVector = AggregateTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = AggregateTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = AggregateTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = AggregateTransactionBuffer.createFeeVector(builder, fee);
    int transactionsVector = AggregateTransactionBuffer.createTransactionsVector(builder, transactionsBytes);
    AggregateTransactionBuffer.startAggregateTransactionBuffer(builder);
    AggregateTransactionBuffer.addSize(builder, 120 + 4 + transactionsBytes.length);
    AggregateTransactionBuffer.addSignature(builder, signatureVector);
    AggregateTransactionBuffer.addSigner(builder, signerVector);
    AggregateTransactionBuffer.addVersion(builder, version);
    AggregateTransactionBuffer.addType(builder, getType().getValue());
    AggregateTransactionBuffer.addFee(builder, feeVector);
    AggregateTransactionBuffer.addDeadline(builder, deadlineVector);
    AggregateTransactionBuffer.addTransactionsSize(builder, transactionsBytes.length);
    AggregateTransactionBuffer.addTransactions(builder, transactionsVector);
    int codedTransaction = AggregateTransactionBuffer.endAggregateTransactionBuffer(builder);
    builder.finish(codedTransaction);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 24 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nem2-sdk-java by nemtech.

the class ModifyMultisigAccountTransaction method generateBytes.

byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    // Create Modifications
    int[] modificationsBuffers = new int[modifications.size()];
    for (int i = 0; i < modifications.size(); ++i) {
        MultisigCosignatoryModification multisigCosignatoryModification = modifications.get(i);
        byte[] byteCosignatoryPublicKey = HexEncoder.getBytes(multisigCosignatoryModification.getCosignatoryPublicAccount().getPublicKey());
        int cosignatoryPublicKey = CosignatoryModificationBuffer.createCosignatoryPublicKeyVector(builder, byteCosignatoryPublicKey);
        CosignatoryModificationBuffer.startCosignatoryModificationBuffer(builder);
        CosignatoryModificationBuffer.addType(builder, multisigCosignatoryModification.getType().getValue());
        CosignatoryModificationBuffer.addCosignatoryPublicKey(builder, cosignatoryPublicKey);
        modificationsBuffers[i] = CosignatoryModificationBuffer.endCosignatoryModificationBuffer(builder);
    }
    // Create Vectors
    int signatureVector = MultisigAggregateModificationTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = MultisigAggregateModificationTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = MultisigAggregateModificationTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = MultisigAggregateModificationTransactionBuffer.createFeeVector(builder, fee);
    int modificationsVector = TransferTransactionBuffer.createMosaicsVector(builder, modificationsBuffers);
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 123;
    MultisigAggregateModificationTransactionBuffer.startMultisigAggregateModificationTransactionBuffer(builder);
    MultisigAggregateModificationTransactionBuffer.addSize(builder, fixSize + (33 * modifications.size()));
    MultisigAggregateModificationTransactionBuffer.addSignature(builder, signatureVector);
    MultisigAggregateModificationTransactionBuffer.addSigner(builder, signerVector);
    MultisigAggregateModificationTransactionBuffer.addVersion(builder, version);
    MultisigAggregateModificationTransactionBuffer.addType(builder, getType().getValue());
    MultisigAggregateModificationTransactionBuffer.addFee(builder, feeVector);
    MultisigAggregateModificationTransactionBuffer.addDeadline(builder, deadlineVector);
    MultisigAggregateModificationTransactionBuffer.addMinApprovalDelta(builder, minApprovalDelta);
    MultisigAggregateModificationTransactionBuffer.addMinRemovalDelta(builder, minRemovalDelta);
    MultisigAggregateModificationTransactionBuffer.addNumModifications(builder, modifications.size());
    MultisigAggregateModificationTransactionBuffer.addModifications(builder, modificationsVector);
    int codedTransaction = MultisigAggregateModificationTransactionBuffer.endMultisigAggregateModificationTransactionBuffer(builder);
    builder.finish(codedTransaction);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Example 25 with FlatBufferBuilder

use of com.google.flatbuffers.FlatBufferBuilder in project nem2-sdk-java by nemtech.

the class MosaicDefinitionTransaction method generateBytes.

byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    int flags = 0;
    if (mosaicProperties.isSupplyMutable()) {
        flags += 1;
    }
    if (mosaicProperties.isTransferable()) {
        flags += 2;
    }
    if (mosaicProperties.isLevyMutable()) {
        flags += 4;
    }
    // Create Vectors
    int signatureVector = MosaicDefinitionCreationTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = MosaicDefinitionCreationTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = MosaicDefinitionCreationTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = MosaicDefinitionCreationTransactionBuffer.createFeeVector(builder, fee);
    int mosaicIdVector = MosaicDefinitionCreationTransactionBuffer.createParentIdVector(builder, UInt64.fromBigInteger(mosaicId.getId()));
    int namespaceIdVector = MosaicDefinitionCreationTransactionBuffer.createParentIdVector(builder, UInt64.fromBigInteger(namespaceId.getId()));
    int durationVector = MosaicDefinitionCreationTransactionBuffer.createDurationVector(builder, UInt64.fromBigInteger(mosaicProperties.getDuration()));
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 149;
    int name = builder.createString(mosaicName);
    MosaicDefinitionCreationTransactionBuffer.startMosaicDefinitionCreationTransactionBuffer(builder);
    MosaicDefinitionCreationTransactionBuffer.addSize(builder, fixSize + mosaicName.length());
    MosaicDefinitionCreationTransactionBuffer.addSignature(builder, signatureVector);
    MosaicDefinitionCreationTransactionBuffer.addSigner(builder, signerVector);
    MosaicDefinitionCreationTransactionBuffer.addVersion(builder, version);
    MosaicDefinitionCreationTransactionBuffer.addType(builder, getType().getValue());
    MosaicDefinitionCreationTransactionBuffer.addFee(builder, feeVector);
    MosaicDefinitionCreationTransactionBuffer.addDeadline(builder, deadlineVector);
    MosaicDefinitionCreationTransactionBuffer.addMosaicId(builder, mosaicIdVector);
    MosaicDefinitionCreationTransactionBuffer.addParentId(builder, namespaceIdVector);
    MosaicDefinitionCreationTransactionBuffer.addMosaicNameLength(builder, mosaicName.length());
    MosaicDefinitionCreationTransactionBuffer.addNumOptionalProperties(builder, 1);
    MosaicDefinitionCreationTransactionBuffer.addFlags(builder, flags);
    MosaicDefinitionCreationTransactionBuffer.addDivisibility(builder, mosaicProperties.getDivisibility());
    MosaicDefinitionCreationTransactionBuffer.addMosaicName(builder, name);
    MosaicDefinitionCreationTransactionBuffer.addIndicateDuration(builder, 2);
    MosaicDefinitionCreationTransactionBuffer.addDuration(builder, durationVector);
    int codedTransaction = MosaicDefinitionCreationTransactionBuffer.endMosaicDefinitionCreationTransactionBuffer(builder);
    builder.finish(codedTransaction);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger)

Aggregations

FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)51 DisplayName (org.junit.jupiter.api.DisplayName)14 Test (org.junit.jupiter.api.Test)14 BigInteger (java.math.BigInteger)10 A6ConfigRequest (org.apache.apisix.plugin.runner.A6ConfigRequest)9 HttpRequest (org.apache.apisix.plugin.runner.HttpRequest)9 ByteBuffer (java.nio.ByteBuffer)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 A6ConfigResponse (org.apache.apisix.plugin.runner.A6ConfigResponse)7 Test (org.junit.Test)7 Req (io.github.api7.A6.PrepareConf.Req)5 A6Conf (org.apache.apisix.plugin.runner.A6Conf)5 ByteStringAccess (com.google.protobuf.ByteStringAccess)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 UncheckedDeephavenException (io.deephaven.UncheckedDeephavenException)3 ChunkType (io.deephaven.chunk.ChunkType)3 Map (java.util.Map)3 lombok.val (lombok.val)3 ExtraInfoRequest (org.apache.apisix.plugin.runner.ExtraInfoRequest)3 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)3