Java Code Examples for org.msgpack.core.MessagePack#newDefaultUnpacker()

The following examples show how to use org.msgpack.core.MessagePack#newDefaultUnpacker() . 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: MaestroDeserializer.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
public static MaestroNote deserialize(byte[] bytes) throws IOException, MalformedNoteException {
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)){
        final short tmpType = unpacker.unpackShort();
        final MaestroNoteType type = MaestroNoteType.from(tmpType);

        switch (Objects.requireNonNull(type)) {
            case MAESTRO_TYPE_REQUEST:
                return deserializeRequest(unpacker);
            case MAESTRO_TYPE_RESPONSE:
                return deserializeResponse(unpacker);
            case MAESTRO_TYPE_NOTIFICATION:
                return deserializeNotification(unpacker);
            case MAESTRO_TYPE_DATA:
                return deserializeData(unpacker);
            default:
                throw new MalformedNoteException("Invalid note type: " + tmpType);
        }
    }
}
 
Example 2
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the {@code message} with the byte array using the given {@code schema}.
 */
public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema, boolean numeric)
        throws IOException
{

    ArrayBufferInput bios = new ArrayBufferInput(data, offset, length);

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bios);

    try
    {
        mergeFrom(unpacker, message, schema, numeric);
    }
    finally
    {
        unpacker.close();
    }
}
 
Example 3
Source File: MessagePackTraverser.java    From influxdb-java with MIT License 6 votes vote down vote up
/**
 * Traverse over the whole message pack stream.
 * This method can be used for converting query results in chunk.
 *
 * @param is
 *          The MessagePack format input stream
 * @return an Iterable over the QueryResult objects
 *
 */
public Iterable<QueryResult> traverse(final InputStream is) {
  MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(is);

  return () -> {
    return new Iterator<QueryResult>() {
      @Override
      public boolean hasNext() {
        try {
          return unpacker.hasNext();
        } catch (IOException e) {
          throw new InfluxDBException(e);
        }
      }

      @Override
      public QueryResult next() {
        return parse(unpacker);
      }
    };
  };

}
 
Example 4
Source File: FluentdIngesterTest.java    From fluency with Apache License 2.0 6 votes vote down vote up
@Test
void ingestWithoutAck()
        throws IOException
{
    Ingester ingester = new FluentdIngester(new FluentdIngester.Config(), fluentdSender);
    ingester.ingest(TAG, ByteBuffer.wrap(DATA));

    verify(fluentdSender, times(1)).send(byteBuffersArgumentCaptor.capture());
    List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0);
    byte[] ingested = getIngestedData(byteBuffers);

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested);
    ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue();
    assertEquals(3, arrayValue.size());
    assertEquals(TAG, arrayValue.get(0).asStringValue().asString());
    assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray());
    Map<Value, Value> options = arrayValue.get(2).asMapValue().map();
    assertEquals(1, options.size());
    assertEquals(DATA.length,
            options.get(ValueFactory.newString("size")).asIntegerValue().asInt());
}
 
Example 5
Source File: Deserializer.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
public A deserialize(byte[] bytes) throws IOException {
  long start = System.currentTimeMillis();
  if (null == bytes)
    return null;

  try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) {
    final long id = unpacker.unpackLong();
    final String label = unpacker.unpackString();
    final Map<String, long[]> inEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker);
    final Map<String, long[]> outEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker);
    final Map<String, Object> properties = unpackProperties(unpacker);

    A a = createElement(id, label, properties, inEdgeIdsByLabel, outEdgeIdsByLabel);

    deserializedCount++;
    deserializationTimeSpentMillis += System.currentTimeMillis() - start;
    if (deserializedCount % 100000 == 0) {
      float avgDeserializationTime = deserializationTimeSpentMillis / (float) deserializedCount;
      logger.debug("stats: deserialized " + deserializedCount + " vertices in total (avg time: " + avgDeserializationTime + "ms)");
    }
    return a;
  }
}
 
Example 6
Source File: MaestroDeserializer.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
public static MaestroEvent deserializeEvent(byte[] bytes) throws IOException, MalformedNoteException {
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)){
        final short tmpType = unpacker.unpackShort();
        final MaestroNoteType type = MaestroNoteType.from(tmpType);

        switch (Objects.requireNonNull(type)) {
            case MAESTRO_TYPE_REQUEST:
                return deserializeRequest(unpacker);
            case MAESTRO_TYPE_NOTIFICATION:
                return deserializeNotification(unpacker);
            case MAESTRO_TYPE_DATA:
                return deserializeData(unpacker);
            default:
                throw new MalformedNoteException("Invalid event type: " + tmpType);
        }
    }
}
 
Example 7
Source File: Deserializer.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * only deserialize the part we're keeping in memory, used during startup when initializing from disk
 */
public ElementRef deserializeRef(byte[] bytes) throws IOException {
  try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) {
    long id = unpacker.unpackLong();
    String label = unpacker.unpackString();

    Map<String, long[]> inEdgeIdsByLabel = null;
    Map<String, long[]> outEdgeIdsByLabel = null;
    if (elementRefRequiresAdjacentElements()) {
      inEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker);
      outEdgeIdsByLabel = unpackEdgeIdsByLabel(unpacker);
    }
    return createElementRef(id, label, inEdgeIdsByLabel, outEdgeIdsByLabel);
  }
}
 
Example 8
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema, boolean numeric) throws IOException
{

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(in);

    try
    {
        return parseListFrom(unpacker, schema, numeric);
    }
    finally
    {
        unpacker.close();
    }
}
 
Example 9
Source File: MessagePackRecordAccessor.java    From fluency with Apache License 2.0 5 votes vote down vote up
MapValueBytes(ByteBuffer byteBuffer)
{
    byte[] mapValueBytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(mapValueBytes);
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(mapValueBytes)) {
        this.map = unpacker.unpackValue().asMapValue().map();
        this.byteArray = mapValueBytes;
    }
    catch (IOException e) {
        throw new IllegalArgumentException("Invalid MessagePack ByteBuffer", e);
    }
}
 
Example 10
Source File: MessagePackRecordFormatterTest.java    From fluency with Apache License 2.0 5 votes vote down vote up
private void assertRecord0(byte[] formatted, long expectedTime)
        throws IOException
{
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) {
        Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
        assertEquals(4, map.size());
        assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong());
        assertEquals("first", map.get(KEY_NAME).asStringValue().asString());
        assertEquals(42, map.get(KEY_AGE).asNumberValue().toInt());
        assertEquals("[email protected]", map.get(KEY_EMAIL).asStringValue().asString());
    }
}
 
Example 11
Source File: MessagePackRecordFormatterTest.java    From fluency with Apache License 2.0 5 votes vote down vote up
private void assertRecord1(byte[] formatted, long expectedTime)
        throws IOException
{
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) {
        Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
        assertEquals(4, map.size());
        assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong());
        assertEquals("second", map.get(KEY_NAME).asStringValue().asString());
        assertEquals(55, map.get(KEY_AGE).asNumberValue().toInt());
        assertEquals("zzzzzz", map.get(KEY_COMMENT).asStringValue().asString());
    }
}
 
Example 12
Source File: MessagePackRecordFormatterTest.java    From fluency with Apache License 2.0 5 votes vote down vote up
private void assertRecord2(byte[] formatted, long expectedTime)
        throws IOException
{
    try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) {
        Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
        assertEquals(4, map.size());
        assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong());
        assertEquals("third", map.get(KEY_NAME).asStringValue().asString());
        assertEquals(99, map.get(KEY_AGE).asNumberValue().toInt());
        assertEquals("knight", map.get(KEY_JOB).asStringValue().asString());
    }
}
 
Example 13
Source File: FluentdIngesterTest.java    From fluency with Apache License 2.0 5 votes vote down vote up
@Test
void ingestWithAck()
        throws IOException
{
    FluentdIngester.Config config = new FluentdIngester.Config();
    config.setAckResponseMode(true);
    Ingester ingester = new FluentdIngester(config, fluentdSender);
    ingester.ingest(TAG, ByteBuffer.wrap(DATA));

    ArgumentCaptor<byte[]> ackTokenArgumentCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(fluentdSender, times(1))
            .sendWithAck(byteBuffersArgumentCaptor.capture(), ackTokenArgumentCaptor.capture());
    List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0);
    byte[] ingested = getIngestedData(byteBuffers);

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested);
    ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue();
    assertEquals(3, arrayValue.size());
    assertEquals(TAG, arrayValue.get(0).asStringValue().asString());
    assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray());
    Map<Value, Value> options = arrayValue.get(2).asMapValue().map();
    assertEquals(2, options.size());
    assertEquals(DATA.length, options.get(ValueFactory.newString("size")).asIntegerValue().asInt());
    String ackToken = options.get(ValueFactory.newString("chunk")).asRawValue().asString();
    UUID uuidFromAckToken = UUID.fromString(ackToken);

    List<byte[]> ackTokenArgumentCaptorAllValues = ackTokenArgumentCaptor.getAllValues();
    assertEquals(1, ackTokenArgumentCaptorAllValues.size());
    assertEquals(uuidFromAckToken,
            UUID.fromString(new String(ackTokenArgumentCaptorAllValues.get(0), CHARSET)));
}
 
Example 14
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Merges the {@code message} from the {@link MessageBufferInput} using the given {@code schema}.
 */
public static <T> void mergeFrom(MessageBufferInput in, T message, Schema<T> schema, boolean numeric) throws IOException
{

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(in);

    try
    {
        mergeFrom(unpacker, message, schema, numeric);
    }
    finally
    {
        unpacker.close();
    }
}
 
Example 15
Source File: InfluxDBException.java    From influxdb-java with MIT License 5 votes vote down vote up
/**
 * Create corresponding InfluxDBException from the message pack error body.
 * @param messagePackErrorBody
 *          the error body if any
 * @return the Exception
 */
public static InfluxDBException buildExceptionForErrorState(final InputStream messagePackErrorBody) {
  try {
    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(messagePackErrorBody);
    ImmutableMapValue mapVal = (ImmutableMapValue) unpacker.unpackValue();
    return InfluxDBException.buildExceptionFromErrorMessage(
        mapVal.map().get(new ImmutableStringValueImpl("error")).toString());
  } catch (Exception e) {
    return new InfluxDBException(e);
  }
}
 
Example 16
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
 */
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, boolean numeric) throws IOException
{

    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(in);

    try
    {
        mergeFrom(unpacker, message, schema, numeric);
    }
    finally
    {
        unpacker.close();
    }
}
 
Example 17
Source File: MpackTypes.java    From funcj with MIT License 4 votes vote down vote up
public static InStream inputOf(InputStream is) {
    return new InputImpl(MessagePack.newDefaultUnpacker(is));
}
 
Example 18
Source File: Message.java    From locust4j with MIT License 4 votes vote down vote up
public Message(byte[] bytes) throws IOException {
    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes);

    int arrayHeader = unpacker.unpackArrayHeader();
    this.type = unpacker.unpackString();

    // unpack data
    if (unpacker.getNextFormat() != MessageFormat.NIL) {
        int mapSize = unpacker.unpackMapHeader();
        this.data = new HashMap<>(6);
        while (mapSize > 0) {
            String key = null;
            // unpack key
            if (unpacker.getNextFormat() == MessageFormat.NIL) {
                unpacker.unpackNil();
            } else {
                key = unpacker.unpackString();
            }
            // unpack value
            MessageFormat messageFormat = unpacker.getNextFormat();
            Object value;

            switch (messageFormat.getValueType()) {
                case BOOLEAN:
                    value = unpacker.unpackBoolean();
                    break;
                case FLOAT:
                    value = unpacker.unpackFloat();
                    break;
                case INTEGER:
                    value = unpacker.unpackInt();
                    break;
                case NIL:
                    value = null;
                    unpacker.unpackNil();
                    break;
                case STRING:
                    value = unpacker.unpackString();
                    break;
                default:
                    throw new IOException("Message received unsupported type: " + messageFormat.getValueType());
            }
            if (null != key) {
                this.data.put(key, value);
            }
            mapSize--;
        }

    } else {
        unpacker.unpackNil();
        this.data = null;
    }
    if (unpacker.getNextFormat() != MessageFormat.NIL) {
        this.nodeID = unpacker.unpackString();
    } else {
        unpacker.unpackNil();
        this.nodeID = null;
    }
    unpacker.close();
}
 
Example 19
Source File: MsgpackIOUtil.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a msgpack pipe from an {@link MessageBufferInput}.
 */
public static Pipe newPipe(MessageBufferInput in, boolean numeric) throws IOException
{
    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(in);
    return newPipe(unpacker, numeric);
}
 
Example 20
Source File: MessagePackTraverser.java    From influxdb-java with MIT License 2 votes vote down vote up
/**
 * Parse the message pack stream.
 * This method can be used for converting query
 * result from normal query response where exactly one QueryResult returned
 *
 * @param is
 *          The MessagePack format input stream
 * @return QueryResult
 *
 */
public QueryResult parse(final InputStream is) {
  MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(is);
  return parse(unpacker);
}