Example usage for org.apache.thrift TDeserializer TDeserializer

List of usage examples for org.apache.thrift TDeserializer TDeserializer

Introduction

In this page you can find the example usage for org.apache.thrift TDeserializer TDeserializer.

Prototype

public TDeserializer(TProtocolFactory protocolFactory) 

Source Link

Document

Create a new TDeserializer.

Usage

From source file:com.facebook.buck.log.ThriftRuleKeyLoggerTest.java

License:Apache License

@Test
public void testWritesToGivenFileAndCreatesDirectories() throws Exception {
    Path logPath = logDir.resolve("logs").resolve("out.log").toAbsolutePath();
    try (ThriftRuleKeyLogger logger = ThriftRuleKeyLogger.create(logPath)) {
        Assert.assertNotNull(logger);//w ww.j a  v  a 2s. c  o m
        logger.write(new FullRuleKey("rule_key", "name", "rule_type",
                ImmutableMap.of("key", Value.stringValue("value"))));
    }
    Assert.assertTrue(logPath.toFile().exists());

    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    try (FileInputStream logFileStream = new FileInputStream(logPath.toFile())) {
        Assert.assertTrue(logFileStream.available() > 4);

        logFileStream.read(lengthBuf.array());
        int length = lengthBuf.getInt();

        Assert.assertEquals(length, logFileStream.available()); // Only should have one object

        byte[] serialized = new byte[length];
        logFileStream.read(serialized);
        TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
        FullRuleKey ruleKey = new FullRuleKey();
        serializer.deserialize(ruleKey, serialized);

        Assert.assertEquals("rule_key", ruleKey.key);
        Assert.assertEquals("name", ruleKey.name);
        Assert.assertEquals("rule_type", ruleKey.type);
        Assert.assertEquals(1, ruleKey.values.size());
        Assert.assertEquals("value", ruleKey.values.get("key").getStringValue());
    }
}

From source file:com.facebook.buck.rules.keys.ThriftRuleKeyHasherTest.java

License:Apache License

private FullRuleKey getRuleKey() throws TException {
    byte[] outputArray = output.toByteArray();
    ByteBuffer lengthBuf = ByteBuffer.wrap(outputArray, 0, 4);
    int length = lengthBuf.getInt();

    Assert.assertEquals(outputArray.length - 4, length);

    TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
    FullRuleKey ruleKey = new FullRuleKey();
    serializer.deserialize(ruleKey, Arrays.copyOfRange(outputArray, 4, outputArray.length));
    return ruleKey;
}

From source file:com.facebook.buck.slb.ThriftUtil.java

License:Apache License

public static void deserialize(ThriftProtocol protocol, byte[] source, TBase<?, ?> dest)
        throws ThriftException {
    TDeserializer deserializer = new TDeserializer(getProtocolFactory(protocol));
    dest.clear();/*from   w w  w . j  a  v a2s .com*/
    try {
        deserializer.deserialize(dest, source);
    } catch (TException e) {
        throw new ThriftException(e);
    }
}

From source file:com.facebook.buck.tools.consistency.RuleKeyLogFileReader.java

License:Apache License

/**
 * Reads a file in and runs a predicate on each deserialized rule key
 *
 * @param filename The name of the file to read
 * @param visitor Called for each rule key that gets deserialized. Return {@code true} if
 *     deserialization should halt, or false if it should proceed
 * @throws ParseException There was an error reading data, or invalid data was found in the file
 *///  ww w  .  j av  a 2 s .co  m
public void readFile(Path filename, Predicate<FullRuleKey> visitor) throws ParseException {
    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    try (FileInputStream fileInputStream = new FileInputStream(filename.toFile())) {
        while (fileInputStream.available() >= 4) {
            fileInputStream.read(lengthBuf.array());
            int length = lengthBuf.getInt();
            lengthBuf.rewind();

            byte[] serialized = new byte[length];
            int bytesRead = fileInputStream.read(serialized);
            if (bytesRead != length) {
                throw new ParseException(filename, "Invalid length specified. Expected %s bytes, only got %s",
                        length, bytesRead);
            }
            TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());
            FullRuleKey ruleKey = new FullRuleKey();
            deserializer.deserialize(ruleKey, serialized);
            // The thrift deserializer doesn't blow up on invalid data, it just sets all fields to
            // null. 'key' is required, so if it's null, we failed to deserialize. Yes, deserialize()
            // /should/ throw a TException, but it doesn't.
            if (ruleKey.key == null) {
                throw new ParseException(filename, "Could not deserialize array of size %s", serialized.length);
            }
            if (visitor.test(ruleKey)) {
                return;
            }
        }
    } catch (TException | IOException e) {
        throw new ParseException(e, filename, "Error reading file: %s", e.getMessage());
    }
}

From source file:com.facebook.buck.util.ThriftRuleKeyDeserializer.java

License:Apache License

/**
 * Reads in a list of rule keys from a file
 *
 * @param logPath The path to the file/*from   w w w .j  ava  2  s . c  o m*/
 * @return A list of FullRuleKey objects from the file
 * @throws IOException Could not read the file
 * @throws TException Could not deserialize an entry from the file
 */
public static List<FullRuleKey> readRuleKeys(Path logPath) throws IOException, TException {
    ByteBuffer lengthBuf = ByteBuffer.allocate(4);
    List<FullRuleKey> ret = new ArrayList<>();
    try (FileInputStream logFileStream = new FileInputStream(logPath.toAbsolutePath().toString())) {
        while (logFileStream.available() > 0) {
            logFileStream.read(lengthBuf.array());
            int length = lengthBuf.getInt();
            lengthBuf.rewind();

            byte[] serialized = new byte[length];
            logFileStream.read(serialized);
            TDeserializer serializer = new TDeserializer(new TCompactProtocol.Factory());
            FullRuleKey ruleKey = new FullRuleKey();
            serializer.deserialize(ruleKey, serialized);
            ret.add(ruleKey);
        }
        return ret;
    }
}

From source file:com.helixleisure.candidates.test.ThriftSerializeDeserializeTest.java

License:Apache License

/**
 * Shows how to deserialize one object from a JSON Thrift format.
 * @throws Exception/*from ww w .  j a v a2s .co m*/
 */
@Test
public void testDeserializeAsJSON() throws Exception {
    des = new TDeserializer(new TJSONProtocol.Factory());
    String json = "{\"1\":{\"rec\":{\"1\":{\"str\":\"2015-06-01 12:34:12.0\"}}},\"2\":{\"rec\":{\"3\":{\"rec\":{\"1\":{\"rec\":{\"1\":{\"i64\":12345}}},\"2\":{\"rec\":{\"1\":{\"str\":\"Snapshot\"}}},\"3\":{\"i64\":1}}}}}}";
    Data data = new Data();
    des.deserialize(data, json.getBytes());
    assertEquals(
            "Data(pedigree:Pedigree(timestamp:2015-06-01 12:34:12.0), dataunit:<DataUnit game_play:GamePlayEdge(location:<LocationID location_id:12345>, game:<GameID name:Snapshot>, nonce:1)>)",
            data.toString());
}

From source file:com.jxt.web.mapper.AgentStatMapper.java

License:Apache License

@Deprecated
private List<AgentStat> readAgentStatThriftDto(String agentId, long timestamp, byte[] tAgentStatByteArray)
        throws TException {
    // CompactProtocol used
    TDeserializer deserializer = new TDeserializer(factory);
    TAgentStat tAgentStat = new TAgentStat();
    deserializer.deserialize(tAgentStat, tAgentStatByteArray);
    TJvmGc gc = tAgentStat.getGc();/* www  .  j a  v a 2 s. c o  m*/
    if (gc == null) {
        return Collections.emptyList();
    }
    AgentStatMemoryGcBo.Builder memoryGcBoBuilder = new AgentStatMemoryGcBo.Builder(tAgentStat.getAgentId(),
            tAgentStat.getStartTimestamp(), tAgentStat.getTimestamp());
    memoryGcBoBuilder.gcType(gc.getType().name());
    memoryGcBoBuilder.jvmMemoryHeapUsed(gc.getJvmMemoryHeapUsed());
    memoryGcBoBuilder.jvmMemoryHeapMax(gc.getJvmMemoryHeapMax());
    memoryGcBoBuilder.jvmMemoryNonHeapUsed(gc.getJvmMemoryNonHeapUsed());
    memoryGcBoBuilder.jvmMemoryNonHeapMax(gc.getJvmMemoryNonHeapMax());
    memoryGcBoBuilder.jvmGcOldCount(gc.getJvmGcOldCount());
    memoryGcBoBuilder.jvmGcOldTime(gc.getJvmGcOldTime());

    AgentStat agentStat = new AgentStat(agentId, timestamp);
    AgentStatMemoryGcBo agentStatMemoryGcBo = memoryGcBoBuilder.build();
    agentStat.setGcType(agentStatMemoryGcBo.getGcType());
    agentStat.setGcOldCount(agentStatMemoryGcBo.getJvmGcOldCount());
    agentStat.setGcOldTime(agentStatMemoryGcBo.getJvmGcOldTime());
    agentStat.setHeapUsed(agentStatMemoryGcBo.getJvmMemoryHeapUsed());
    agentStat.setHeapMax(agentStatMemoryGcBo.getJvmMemoryHeapMax());
    agentStat.setNonHeapUsed(agentStatMemoryGcBo.getJvmMemoryNonHeapUsed());
    agentStat.setNonHeapMax(agentStatMemoryGcBo.getJvmMemoryNonHeapMax());

    List<AgentStat> result = new ArrayList<>(1);
    result.add(agentStat);
    return result;
}

From source file:com.knewton.mapreduce.io.StudentEventWritable.java

License:Apache License

public StudentEventWritable(StudentEvent studentEvent, long timestamp) {
    TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
    this.serializer = new TSerializer(protocolFactory);
    this.deserializer = new TDeserializer(protocolFactory);
    this.studentEvent = studentEvent;
    this.timestamp = timestamp;
}

From source file:com.knewton.mapreduce.io.StudentEventWritableTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    TProtocolFactory factory = new TCompactProtocol.Factory();
    this.deserializer = new TDeserializer(factory);
}

From source file:com.knewton.mapreduce.StudentEventAbstractMapper.java

License:Apache License

public StudentEventAbstractMapper() {
    TProtocolFactory factory = new TCompactProtocol.Factory();
    decoder = new TDeserializer(factory);
    setSkipDeletedColumns(true);
}