Search in sources :

Example 6 with DataInputPlus

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

the class IntervalTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    List<Interval<Integer, String>> intervals = new ArrayList<Interval<Integer, String>>();
    intervals.add(Interval.<Integer, String>create(-300, -200, "a"));
    intervals.add(Interval.<Integer, String>create(-3, -2, "b"));
    intervals.add(Interval.<Integer, String>create(1, 2, "c"));
    intervals.add(Interval.<Integer, String>create(1, 3, "d"));
    intervals.add(Interval.<Integer, String>create(2, 4, "e"));
    intervals.add(Interval.<Integer, String>create(3, 6, "f"));
    intervals.add(Interval.<Integer, String>create(4, 6, "g"));
    intervals.add(Interval.<Integer, String>create(5, 7, "h"));
    intervals.add(Interval.<Integer, String>create(8, 9, "i"));
    intervals.add(Interval.<Integer, String>create(15, 20, "j"));
    intervals.add(Interval.<Integer, String>create(40, 50, "k"));
    intervals.add(Interval.<Integer, String>create(49, 60, "l"));
    IntervalTree<Integer, String, Interval<Integer, String>> it = IntervalTree.build(intervals);
    IVersionedSerializer<IntervalTree<Integer, String, Interval<Integer, String>>> serializer = IntervalTree.serializer(new ISerializer<Integer>() {

        public void serialize(Integer i, DataOutputPlus out) throws IOException {
            out.writeInt(i);
        }

        public Integer deserialize(DataInputPlus in) throws IOException {
            return in.readInt();
        }

        public long serializedSize(Integer i) {
            return 4;
        }
    }, new ISerializer<String>() {

        public void serialize(String v, DataOutputPlus out) throws IOException {
            out.writeUTF(v);
        }

        public String deserialize(DataInputPlus in) throws IOException {
            return in.readUTF();
        }

        public long serializedSize(String v) {
            return v.length();
        }
    }, (Constructor<Interval<Integer, String>>) (Object) Interval.class.getConstructor(Object.class, Object.class, Object.class));
    DataOutputBuffer out = new DataOutputBuffer();
    serializer.serialize(it, out, 0);
    DataInputPlus in = new DataInputBuffer(out.toByteArray());
    IntervalTree<Integer, String, Interval<Integer, String>> it2 = serializer.deserialize(in, 0);
    List<Interval<Integer, String>> intervals2 = new ArrayList<Interval<Integer, String>>();
    for (Interval<Integer, String> i : it2) intervals2.add(i);
    assertEquals(intervals, intervals2);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) Test(org.junit.Test)

Example 7 with DataInputPlus

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

the class BytesReadTrackerTest method internalTestSkipBytesAndReadFully.

public void internalTestSkipBytesAndReadFully(boolean inputStream) throws Exception {
    String testStr = "1234567890";
    byte[] testData = testStr.getBytes();
    DataInputPlus.DataInputStreamPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(testData));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        // read first 5 bytes
        byte[] out = new byte[5];
        reader.readFully(out, 0, 5);
        assertEquals("12345", new String(out));
        assertEquals(5, tracker.getBytesRead());
        // then skip 2 bytes
        reader.skipBytes(2);
        assertEquals(7, tracker.getBytesRead());
        // and read the rest
        out = new byte[3];
        reader.readFully(out);
        assertEquals("890", new String(out));
        assertEquals(10, tracker.getBytesRead());
        assertEquals(testData.length, tracker.getBytesRead());
    } finally {
        in.close();
    }
}
Also used : BytesReadTracker(org.apache.cassandra.io.util.BytesReadTracker) ByteArrayInputStream(java.io.ByteArrayInputStream) TrackedInputStream(org.apache.cassandra.io.util.TrackedInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus)

Example 8 with DataInputPlus

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

the class BytesReadTrackerTest method internalTestUnsignedRead.

public void internalTestUnsignedRead(boolean inputStream) throws Exception {
    byte[] testData;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    try {
        // byte
        out.writeByte(0x1);
        // short
        out.writeShort(1);
        testData = baos.toByteArray();
    } finally {
        out.close();
    }
    DataInputPlus.DataInputStreamPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(testData));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        // byte = 1byte
        int b = reader.readUnsignedByte();
        assertEquals(b, 1);
        assertEquals(1, tracker.getBytesRead());
        // short = 2bytes
        int s = reader.readUnsignedShort();
        assertEquals(1, s);
        assertEquals(3, tracker.getBytesRead());
        assertEquals(testData.length, tracker.getBytesRead());
    } finally {
        in.close();
    }
}
Also used : BytesReadTracker(org.apache.cassandra.io.util.BytesReadTracker) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) TrackedInputStream(org.apache.cassandra.io.util.TrackedInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus)

Example 9 with DataInputPlus

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

the class ReadMessageTest method serializeAndDeserializeReadMessage.

private ReadCommand serializeAndDeserializeReadMessage(ReadCommand rm) throws IOException {
    IVersionedSerializer<ReadCommand> rms = ReadCommand.serializer;
    DataOutputBuffer out = new DataOutputBuffer();
    rms.serialize(rm, out, MessagingService.current_version);
    DataInputPlus dis = new DataInputBuffer(out.getData());
    return rms.deserialize(dis, MessagingService.current_version);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus)

Example 10 with DataInputPlus

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

the class HintTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    long now = FBUtilities.timestampMicros();
    Mutation mutation = createMutation("testSerializer", now);
    Hint hint = Hint.create(mutation, now / 1000);
    // serialize
    int serializedSize = (int) Hint.serializer.serializedSize(hint, MessagingService.current_version);
    DataOutputBuffer dob = new DataOutputBuffer();
    Hint.serializer.serialize(hint, dob, MessagingService.current_version);
    assertEquals(serializedSize, dob.getLength());
    // deserialize
    DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
    Hint deserializedHint = Hint.serializer.deserialize(di, MessagingService.current_version);
    // compare before/after
    assertHintsEqual(hint, deserializedHint);
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Test(org.junit.Test)

Aggregations

DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)16 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)10 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)8 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BytesReadTracker (org.apache.cassandra.io.util.BytesReadTracker)4 TrackedDataInputPlus (org.apache.cassandra.io.util.TrackedDataInputPlus)4 TrackedInputStream (org.apache.cassandra.io.util.TrackedInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 Mutation (org.apache.cassandra.db.Mutation)2 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)2 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)2 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)2 TableMetadata (org.apache.cassandra.schema.TableMetadata)2 DataInputStream (java.io.DataInputStream)1