Search in sources :

Example 6 with TrackedDataInputPlus

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

the class BytesReadTrackerTest method internalTestReadLine.

public void internalTestReadLine(boolean inputStream) throws Exception {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream("1".getBytes()));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        String line = reader.readLine();
        if (inputStream)
            assertEquals(line, "1");
        else
            fail("Should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        if (inputStream)
            fail("Should have not thrown UnsupportedOperationException");
    } 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) DataInputStream(java.io.DataInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus)

Example 7 with TrackedDataInputPlus

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

the class BytesReadTrackerTest method internalTestBytesRead.

public void internalTestBytesRead(boolean inputStream) throws Exception {
    byte[] testData;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    try {
        // boolean
        out.writeBoolean(true);
        // byte
        out.writeByte(0x1);
        // char
        out.writeChar('a');
        // short
        out.writeShort(1);
        // int
        out.writeInt(1);
        // long
        out.writeLong(1L);
        // float
        out.writeFloat(1.0f);
        // double
        out.writeDouble(1.0d);
        // String
        out.writeUTF("abc");
        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 {
        // boolean = 1byte
        boolean bool = reader.readBoolean();
        assertTrue(bool);
        assertEquals(1, tracker.getBytesRead());
        // byte = 1byte
        byte b = reader.readByte();
        assertEquals(b, 0x1);
        assertEquals(2, tracker.getBytesRead());
        // char = 2byte
        char c = reader.readChar();
        assertEquals('a', c);
        assertEquals(4, tracker.getBytesRead());
        // short = 2bytes
        short s = reader.readShort();
        assertEquals(1, s);
        assertEquals((short) 6, tracker.getBytesRead());
        // int = 4bytes
        int i = reader.readInt();
        assertEquals(1, i);
        assertEquals(10, tracker.getBytesRead());
        // long = 8bytes
        long l = reader.readLong();
        assertEquals(1L, l);
        assertEquals(18, tracker.getBytesRead());
        // float = 4bytes
        float f = reader.readFloat();
        assertEquals(1.0f, f, 0);
        assertEquals(22, tracker.getBytesRead());
        // double = 8bytes
        double d = reader.readDouble();
        assertEquals(1.0d, d, 0);
        assertEquals(30, tracker.getBytesRead());
        // String("abc") = 2(string size) + 3 = 5 bytes
        String str = reader.readUTF();
        assertEquals("abc", str);
        assertEquals(35, tracker.getBytesRead());
        assertEquals(testData.length, tracker.getBytesRead());
    } finally {
        in.close();
    }
    tracker.reset(0);
    assertEquals(0, tracker.getBytesRead());
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) 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)

Aggregations

TrackedDataInputPlus (org.apache.cassandra.io.util.TrackedDataInputPlus)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BytesReadTracker (org.apache.cassandra.io.util.BytesReadTracker)4 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)4 TrackedInputStream (org.apache.cassandra.io.util.TrackedInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 SSTableMultiWriter (org.apache.cassandra.io.sstable.SSTableMultiWriter)2 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 UnknownTableException (org.apache.cassandra.exceptions.UnknownTableException)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 StreamCompressionInputStream (org.apache.cassandra.streaming.compress.StreamCompressionInputStream)1