Search in sources :

Example 26 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer in project cassandra by apache.

the class StreamingHistogramTest method testNumericTypes.

@Test
public void testNumericTypes() throws Exception {
    StreamingHistogram hist = new StreamingHistogram(5, 0, 1);
    hist.update(2);
    hist.update(2.0);
    hist.update(2L);
    Map<Number, long[]> asMap = hist.getAsMap();
    assertEquals(1, asMap.size());
    assertEquals(3L, asMap.get(2)[0]);
    //Make sure it's working with Serde
    DataOutputBuffer out = new DataOutputBuffer();
    StreamingHistogram.serializer.serialize(hist, out);
    byte[] bytes = out.toByteArray();
    StreamingHistogram deserialized = StreamingHistogram.serializer.deserialize(new DataInputBuffer(bytes));
    deserialized.update(2L);
    asMap = deserialized.getAsMap();
    assertEquals(1, asMap.size());
    assertEquals(4L, asMap.get(2)[0]);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) Test(org.junit.Test)

Example 27 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer in project cassandra by apache.

the class RepairMessageSerializerTest method serdes.

static RepairMessage serdes(RepairMessage message) {
    int expectedSize = (int) RepairMessage.serializer.serializedSize(message, MS_VERSION);
    try (DataOutputBuffer out = new DataOutputBuffer(expectedSize)) {
        RepairMessage.serializer.serialize(message, out, MS_VERSION);
        Assert.assertEquals(expectedSize, out.buffer().limit());
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), false)) {
            return RepairMessage.serializer.deserialize(in, MS_VERSION);
        }
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException)

Example 28 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer in project cassandra by apache.

the class DurationSerializer method validate.

public void validate(ByteBuffer bytes) throws MarshalException {
    if (bytes.remaining() < 3)
        throw new MarshalException(String.format("Expected at least 3 bytes for a duration (%d)", bytes.remaining()));
    try (DataInputBuffer in = new DataInputBuffer(bytes, true)) {
        int months = (int) in.readVInt();
        int days = (int) in.readVInt();
        long nanoseconds = in.readVInt();
        if (!((months >= 0 && days >= 0 && nanoseconds >= 0) || (months <= 0 && days <= 0 && nanoseconds <= 0)))
            throw new MarshalException(String.format("The duration months, days and nanoseconds must be all of the same sign (%d, %d, %d)", months, days, nanoseconds));
    } catch (IOException e) {
        // this should never happen with a DataInputBuffer
        throw new AssertionError("Unexpected error", e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) IOException(java.io.IOException)

Example 29 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer in project cassandra by apache.

the class DurationSerializer method deserialize.

public Duration deserialize(ByteBuffer bytes) {
    if (bytes.remaining() == 0)
        return null;
    try (DataInputBuffer in = new DataInputBuffer(bytes, true)) {
        int months = (int) in.readVInt();
        int days = (int) in.readVInt();
        long nanoseconds = in.readVInt();
        return Duration.newInstance(months, days, nanoseconds);
    } catch (IOException e) {
        // this should never happen with a DataInputBuffer
        throw new AssertionError("Unexpected error", e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) IOException(java.io.IOException)

Example 30 with DataInputBuffer

use of org.apache.cassandra.io.util.DataInputBuffer in project cassandra by apache.

the class BatchTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    TableMetadata cfm = Keyspace.open(KEYSPACE).getColumnFamilyStore(CF_STANDARD).metadata();
    long now = FBUtilities.timestampMicros();
    int version = MessagingService.current_version;
    UUID uuid = UUIDGen.getTimeUUID();
    List<Mutation> mutations = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), bytes(i)).clustering("name" + i).add("val", "val" + i).build());
    }
    Batch batch1 = Batch.createLocal(uuid, now, mutations);
    assertEquals(uuid, batch1.id);
    assertEquals(now, batch1.creationTime);
    assertEquals(mutations, batch1.decodedMutations);
    DataOutputBuffer out = new DataOutputBuffer();
    Batch.serializer.serialize(batch1, out, version);
    assertEquals(out.getLength(), Batch.serializer.serializedSize(batch1, version));
    DataInputPlus dis = new DataInputBuffer(out.getData());
    Batch batch2 = Batch.serializer.deserialize(dis, version);
    assertEquals(batch1.id, batch2.id);
    assertEquals(batch1.creationTime, batch2.creationTime);
    assertEquals(batch1.decodedMutations.size(), batch2.encodedMutations.size());
    Iterator<Mutation> it1 = batch1.decodedMutations.iterator();
    Iterator<ByteBuffer> it2 = batch2.encodedMutations.iterator();
    while (it1.hasNext()) {
        try (DataInputBuffer in = new DataInputBuffer(it2.next().array())) {
            assertEquals(it1.next().toString(), Mutation.serializer.deserialize(in, version).toString());
        }
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)54 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)33 Test (org.junit.Test)23 IOException (java.io.IOException)14 ByteBuffer (java.nio.ByteBuffer)13 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)13 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 Mutation (org.apache.cassandra.db.Mutation)5 UUID (java.util.UUID)4 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)4 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)4 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)3 ByteBuf (io.netty.buffer.ByteBuf)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ClusteringIndexSliceFilter (org.apache.cassandra.db.filter.ClusteringIndexSliceFilter)2