Java Code Examples for com.google.flatbuffers.FlatBufferBuilder#createString()

The following examples show how to use com.google.flatbuffers.FlatBufferBuilder#createString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogFileWriter.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Register the event name - "accuracy", "loss", etc for later use in recording events.
 * @param name Name to register
 * @return Number of bytes written
 */
public long registerEventName(String name) throws IOException {
    Preconditions.checkState(endStaticInfoOffset >= 0, "Cannot write name - have not written end of static info marker");

    FlatBufferBuilder fbb = new FlatBufferBuilder(0);
    long time = System.currentTimeMillis();
    int offset = UIEvent.createUIEvent(fbb, UIEventType.ADD_NAME, UIEventSubtype.NONE, -1, time, 0, 0, (short)-1, 0, 0);
    fbb.finish(offset);

    FlatBufferBuilder fbb2 = new FlatBufferBuilder(0);
    int idx = nameIndexCounter.getAndIncrement();
    nameIndexMap.put(idx, name);
    indexNameMap.put(name, idx);
    int strOffset = fbb2.createString(name);
    int offset2 = UIAddName.createUIAddName(fbb2, idx, strOffset);
    fbb2.finish(offset2);

    long l = append(fbb, fbb2);
    return l;
}
 
Example 2
Source File: FunctionProperties.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method converts this FunctionProperties instance to FlatBuffers representation
 * @param bufferBuilder
 * @return
 */
public int asFlatProperties(FlatBufferBuilder bufferBuilder) {
    int iname = bufferBuilder.createString(name);
    int ii = FlatProperties.createIVector(bufferBuilder, Ints.toArray(i));
    int il = FlatProperties.createLVector(bufferBuilder, Longs.toArray(l));
    int id = FlatProperties.createDVector(bufferBuilder, Doubles.toArray(d));

    int arrays[] = new int[a.size()];
    int cnt = 0;
    for (val array: a) {
        int off = array.toFlatArray(bufferBuilder);
        arrays[cnt++] = off;
    }

    int ia = FlatProperties.createAVector(bufferBuilder, arrays);

    return FlatProperties.createFlatProperties(bufferBuilder, iname, ii, il, id, ia);
}
 
Example 3
Source File: DatasetConfigUpgrade.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Converting old Arrow Schema to new one based on Arrow version used
 * in Dremio as of 2.1.0
 * @param oldSchema
 * @return
 */
@VisibleForTesting
byte[] convertFromOldSchema(OldSchema oldSchema) {
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int[] fieldOffsets = new int[oldSchema.fieldsLength()];
  for (int i = 0; i < oldSchema.fieldsLength(); i++) {
    fieldOffsets[i] = convertFromOldField(oldSchema.fields(i), builder);
  }
  int fieldsOffset = org.apache.arrow.flatbuf.Schema.createFieldsVector(builder, fieldOffsets);
  int[] metadataOffsets = new int[oldSchema.customMetadataLength()];
  for (int i = 0; i < metadataOffsets.length; i++) {
    int keyOffset = builder.createString(oldSchema.customMetadata(i).key());
    int valueOffset = builder.createString(oldSchema.customMetadata(i).value());
    KeyValue.startKeyValue(builder);
    KeyValue.addKey(builder, keyOffset);
    KeyValue.addValue(builder, valueOffset);
    metadataOffsets[i] = KeyValue.endKeyValue(builder);
  }
  int metadataOffset = org.apache.arrow.flatbuf.Field.createCustomMetadataVector(builder, metadataOffsets);
  org.apache.arrow.flatbuf.Schema.startSchema(builder);
  org.apache.arrow.flatbuf.Schema.addFields(builder, fieldsOffset);
  org.apache.arrow.flatbuf.Schema.addCustomMetadata(builder, metadataOffset);
  builder.finish(org.apache.arrow.flatbuf.Schema.endSchema(builder));
  return builder.sizedByteArray();
}
 
Example 4
Source File: LobbyScreen.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void CreateGame(GameSession session, ResponseListener listener) {
  Gdx.app.debug(TAG, "Creating game " + session);
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int gameNameOffset = builder.createString(session.name);
  int descriptionOffset = builder.createString(session.desc);
  CreateGame.startCreateGame(builder);
  CreateGame.addGameName(builder, gameNameOffset);
  CreateGame.addDescription(builder, descriptionOffset);
  int createGameOffset = CreateGame.endCreateGame(builder);
  int id = MCP.createMCP(builder, MCPData.CreateGame, createGameOffset);
  builder.finish(id);
  ByteBuffer data = builder.dataBuffer();
  connection.sendRequest(data, listener);
}
 
Example 5
Source File: ArrowSerde.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create the dimensions for the flatbuffer builder
 * @param bufferBuilder the buffer builder to use
 * @param arr the input array
 * @return
 */
public static int createDims(FlatBufferBuilder bufferBuilder,INDArray arr) {
    int[] tensorDimOffsets = new int[arr.rank()];
    int[] nameOffset = new int[arr.rank()];
    for(int i = 0; i < tensorDimOffsets.length; i++) {
        nameOffset[i] = bufferBuilder.createString("");
        tensorDimOffsets[i] = TensorDim.createTensorDim(bufferBuilder,arr.size(i),nameOffset[i]);
    }

    return Tensor.createShapeVector(bufferBuilder,tensorDimOffsets);
}
 
Example 6
Source File: LogFileWriter.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private int[] encodeStrings(FlatBufferBuilder fbb, List<String> list){
    if(list == null || list.isEmpty())
        return null;
    int[] idx = new int[list.size()];
    for( int i=0; i<idx.length; i++ ){
        idx[i] = fbb.createString(list.get(i));
    }
    return idx;
}
 
Example 7
Source File: LogFileWriter.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public long writeHistogramEventDiscrete(@NonNull String name, EventSubtype subtype, long time, int iteration, int epoch, List<String> binLabels, @NonNull INDArray y) throws IOException {
    Preconditions.checkState(binLabels == null || binLabels.size() == y.length(), "Number of bin labels (if present) must " +
            "be same as Y array length - got %s bins, array shape %ndShape", (binLabels == null ? 0L : binLabels.size()), y.length());
    Preconditions.checkState(y.rank() == 1, "Y array must be rank 1, got Y array with shape %ndShape", y);

    //TODO add support for plugin, variable and frame/iter
    Preconditions.checkState(indexNameMap.containsKey(name), "Name \"%s\" not yet registered", name);
    int idx = indexNameMap.get(name);

    FlatBufferBuilder fbb = new FlatBufferBuilder(0);
    int offset = UIEvent.createUIEvent(fbb, UIEventType.HISTOGRAM, subtype.asUIEventSubtype(), idx, time, iteration, epoch, (short)-1, 0, 0);
    fbb.finish(offset);

    FlatBufferBuilder fbb2 = new FlatBufferBuilder(0);
    int yOffset = y.toFlatArray(fbb2);
    int binLabelsOffset = 0;
    if(binLabels != null){
        int[] str = new int[binLabels.size()];
        for( int i=0; i<binLabels.size(); i++ ){
            String s = binLabels.get(i);
            if(s == null)
                s = "";
            str[i] = fbb2.createString(s);
        }
        binLabelsOffset = UIHistogram.createBinlabelsVector(fbb2, str);
    }
    int offset2 = UIHistogram.createUIHistogram(fbb2, UIHistogramType.DISCRETE, y.length(), 0, yOffset, binLabelsOffset);
    fbb2.finish(offset2);

    return append(fbb, fbb2);
}
 
Example 8
Source File: FlatBuffersMapper.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static int[] mapOrNull(List<String> list, FlatBufferBuilder fbb){
    if(list == null)
        return null;
    int[] out = new int[list.size()];
    int i=0;
    for(String s : list){
        out[i++] = fbb.createString(s);
    }
    return out;
}
 
Example 9
Source File: ArrowSerde.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create the dimensions for the flatbuffer builder
 * @param bufferBuilder the buffer builder to use
 * @param arr the input array
 * @return
 */
public static int createDims(FlatBufferBuilder bufferBuilder,INDArray arr) {
    int[] tensorDimOffsets = new int[arr.rank()];
    int[] nameOffset = new int[arr.rank()];
    for(int i = 0; i < tensorDimOffsets.length; i++) {
        nameOffset[i] = bufferBuilder.createString("");
        tensorDimOffsets[i] = TensorDim.createTensorDim(bufferBuilder,arr.size(i),nameOffset[i]);
    }

    return Tensor.createShapeVector(bufferBuilder,tensorDimOffsets);
}
 
Example 10
Source File: LobbyScreen.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void JoinGame(GameSession session, ResponseListener listener) {
  Gdx.app.debug(TAG, "Joining game " + session);
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int gameNameOffset = builder.createString(session.name);
  JoinGame.startJoinGame(builder);
  JoinGame.addGameName(builder, gameNameOffset);
  int joinGameOffset = JoinGame.endJoinGame(builder);
  int id = MCP.createMCP(builder, MCPData.JoinGame, joinGameOffset);
  builder.finish(id);
  ByteBuffer data = builder.dataBuffer();
  connection.sendRequest(data, listener);
}
 
Example 11
Source File: BNLS.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionDenied(Socket socket, String reason) throws IOException {
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int reasonOffset = builder.createString(reason);
  int connectionDeniedId = ConnectionClosed.createConnectionClosed(builder, reasonOffset);
  int id = com.riiablo.net.packet.bnls.BNLS.createBNLS(builder, BNLSData.ConnectionClosed, connectionDeniedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return true;
}
 
Example 12
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionDenied(Socket socket, String reason) throws IOException {
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int reasonOffset = builder.createString(reason);
  int connectionDeniedId = ConnectionClosed.createConnectionClosed(builder, reasonOffset);
  int id = com.riiablo.net.packet.mcp.MCP.createMCP(builder, MCPData.ConnectionClosed, connectionDeniedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return true;
}
 
Example 13
Source File: TimelyTcpIT.java    From timely with Apache License 2.0 5 votes vote down vote up
private int createMetric(FlatBufferBuilder builder, String name, long timestamp, double value,
        Map<String, String> tags) {
    int n = builder.createString(name);
    int[] t = new int[tags.size()];
    int i = 0;
    for (Entry<String, String> e : tags.entrySet()) {
        t[i] = timely.api.flatbuffer.Tag.createTag(builder, builder.createString(e.getKey()),
                builder.createString(e.getValue()));
        i++;
    }
    return timely.api.flatbuffer.Metric.createMetric(builder, n, timestamp, value,
            timely.api.flatbuffer.Metric.createTagsVector(builder, t));
}
 
Example 14
Source File: TimelyUdpIT.java    From timely with Apache License 2.0 5 votes vote down vote up
private int createMetric(FlatBufferBuilder builder, String name, long timestamp, double value,
        Map<String, String> tags) {
    int n = builder.createString(name);
    int[] t = new int[tags.size()];
    int i = 0;
    for (Entry<String, String> e : tags.entrySet()) {
        t[i] = timely.api.flatbuffer.Tag.createTag(builder, builder.createString(e.getKey()),
                builder.createString(e.getValue()));
        i++;
    }
    return timely.api.flatbuffer.Metric.createMetric(builder, n, timestamp, value,
            timely.api.flatbuffer.Metric.createTagsVector(builder, t));
}
 
Example 15
Source File: MetricsBufferDecoderTest.java    From timely with Apache License 2.0 5 votes vote down vote up
private int createMetric(FlatBufferBuilder builder, String name, long timestamp, double value,
        Map<String, String> tags) {
    int n = builder.createString(name);
    int[] t = new int[tags.size()];
    int i = 0;
    for (Entry<String, String> e : tags.entrySet()) {
        t[i] = timely.api.flatbuffer.Tag.createTag(builder, builder.createString(e.getKey()),
                builder.createString(e.getValue()));
        i++;
    }
    return timely.api.flatbuffer.Metric.createMetric(builder, n, timestamp, value,
            timely.api.flatbuffer.Metric.createTagsVector(builder, t));
}
 
Example 16
Source File: PlayerSerializer.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
public int putData(FlatBufferBuilder builder, Player c) {
  CharData data = c.data;
  int charNameOffset = builder.createString(data.name);
  return PlayerP.createPlayerP(builder, data.charClass, charNameOffset);
}
 
Example 17
Source File: Utils.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private static int createEncryptionCtxOffset(final FlatBufferBuilder builder, Optional<EncryptionContext> encryptionCtx) {
    if (!encryptionCtx.isPresent()) {
        return -1;
    }
    // Message.addEncryptionCtx(builder, encryptionCtxOffset);
    EncryptionContext ctx = encryptionCtx.get();
    int[] keysOffsets = new int[ctx.getKeys().size()];
    int keyIndex = 0;
    for (Entry<String, org.apache.pulsar.common.api.EncryptionContext.EncryptionKey> entry : ctx.getKeys()
            .entrySet()) {
        int key = builder.createString(entry.getKey());
        int value = EncryptionKey.createValueVector(builder, entry.getValue().getKeyValue());
        Map<String, String> metadata = entry.getValue().getMetadata();
        int[] metadataOffsets = new int[metadata.size()];
        int i = 0;
        for (Entry<String, String> m : metadata.entrySet()) {
            metadataOffsets[i++] = KeyValue.createKeyValue(builder, builder.createString(m.getKey()),
                    builder.createString(m.getValue()));
        }
        int metadataOffset = -1;
        if (metadata.size() > 0) {
            metadataOffset = EncryptionKey.createMetadataVector(builder, metadataOffsets);
        }
        EncryptionKey.startEncryptionKey(builder);
        EncryptionKey.addKey(builder, key);
        EncryptionKey.addValue(builder, value);
        if(metadataOffset!=-1) {
            EncryptionKey.addMetadata(builder, metadataOffset);
        }
        keysOffsets[keyIndex++] = EncryptionKey.endEncryptionKey(builder);
    }

    int keysOffset = EncryptionCtx.createKeysVector(builder, keysOffsets);
    int param = EncryptionCtx.createParamVector(builder, ctx.getParam());
    int algo = builder.createString(ctx.getAlgorithm());
    int batchSize = ctx.getBatchSize().isPresent() ? ctx.getBatchSize().get() : 1;
    byte compressionType;
    switch (ctx.getCompressionType()) {
    case LZ4:
        compressionType = org.apache.pulsar.io.kinesis.fbs.CompressionType.LZ4;
        break;
    case ZLIB:
        compressionType = org.apache.pulsar.io.kinesis.fbs.CompressionType.ZLIB;
        break;
    default:
        compressionType = org.apache.pulsar.io.kinesis.fbs.CompressionType.NONE;

    }
    return EncryptionCtx.createEncryptionCtx(builder, keysOffset, param, algo, compressionType,
            ctx.getUncompressedMessageSize(), batchSize, ctx.getBatchSize().isPresent());

}
 
Example 18
Source File: DatasetConfigUpgrade.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Converting old Arrow Field to new one based on Arrow version used
 * in Dremio as of 2.1.0
 * @param oldField
 * @param builder
 * @return
 */
private int convertFromOldField(OldField oldField, FlatBufferBuilder builder) {
  int nameOffset = oldField.name() == null ? -1 : builder.createString(oldField.name());
  ArrowType arrowType = getTypeForField(oldField);
  int typeOffset = arrowType.getType(builder);
  int dictionaryOffset = -1;
  org.apache.arrow.flatbuf.DictionaryEncoding oldDictionaryEncoding = oldField.dictionary();
  if (oldDictionaryEncoding != null) {
    int intType = Int.createInt(builder,
      oldDictionaryEncoding.indexType().bitWidth(),
      oldDictionaryEncoding.indexType().isSigned());

    dictionaryOffset =
      org.apache.arrow.flatbuf.DictionaryEncoding.createDictionaryEncoding(builder,
        oldDictionaryEncoding.id(),
        intType,
        oldDictionaryEncoding.isOrdered(),
        oldDictionaryEncoding.dictionaryKind());
  }
  int childrenLength = oldField.childrenLength();
  int[] childrenData = new int[childrenLength];
  for (int i = 0; i < childrenLength; i++) {
    childrenData[i] = convertFromOldField(oldField.children(i), builder);
  }
  int childrenOffset = org.apache.arrow.flatbuf.Field.createChildrenVector(builder, childrenData);
  int metadataLength = oldField.customMetadataLength();
  int[] metadataOffsets = new int[metadataLength];
  for (int i = 0; i < metadataOffsets.length; i++) {
    KeyValue keyValue = oldField.customMetadata(i);
    int keyOffset = builder.createString(keyValue.key());
    int valueOffset = builder.createString(keyValue.value());
    KeyValue.startKeyValue(builder);
    KeyValue.addKey(builder, keyOffset);
    KeyValue.addValue(builder, valueOffset);
    metadataOffsets[i] = KeyValue.endKeyValue(builder);
  }
  int metadataOffset = org.apache.arrow.flatbuf.Field.createCustomMetadataVector(builder, metadataOffsets);
  org.apache.arrow.flatbuf.Field.startField(builder);
  if (oldField.name() != null) {
    org.apache.arrow.flatbuf.Field.addName(builder, nameOffset);
  }
  org.apache.arrow.flatbuf.Field.addNullable(builder, oldField.nullable());
  org.apache.arrow.flatbuf.Field.addTypeType(builder, oldField.typeType());
  org.apache.arrow.flatbuf.Field.addType(builder, typeOffset);
  org.apache.arrow.flatbuf.Field.addChildren(builder, childrenOffset);
  org.apache.arrow.flatbuf.Field.addCustomMetadata(builder, metadataOffset);
  if (oldDictionaryEncoding != null) {
    org.apache.arrow.flatbuf.Field.addDictionary(builder, dictionaryOffset);
  }
  return org.apache.arrow.flatbuf.Field.endField(builder);
}