Search in sources :

Example 6 with SequentialWriter

use of org.apache.cassandra.io.util.SequentialWriter in project eiger by wlloyd.

the class SSTableWriter method writeMetadata.

private static void writeMetadata(Descriptor desc, SSTableMetadata sstableMetadata) throws IOException {
    SequentialWriter out = SequentialWriter.open(new File(desc.filenameFor(SSTable.COMPONENT_STATS)), true);
    SSTableMetadata.serializer.serialize(sstableMetadata, out.stream);
    out.close();
}
Also used : SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) CompressedSequentialWriter(org.apache.cassandra.io.compress.CompressedSequentialWriter) SegmentedFile(org.apache.cassandra.io.util.SegmentedFile)

Example 7 with SequentialWriter

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

the class ChecksummedDataInputTest method testReadMethods.

@Test
public void testReadMethods() throws IOException {
    // Make sure this array is bigger than the reader buffer size
    // so we test updating the crc across buffer boundaries
    byte[] b = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE * 2];
    for (int i = 0; i < b.length; i++) b[i] = (byte) i;
    ByteBuffer buffer;
    // fill a bytebuffer with some input
    try (DataOutputBuffer out = new DataOutputBuffer()) {
        out.write(127);
        out.write(b);
        out.writeBoolean(false);
        out.writeByte(10);
        out.writeChar('t');
        out.writeDouble(3.3);
        out.writeFloat(2.2f);
        out.writeInt(42);
        out.writeLong(Long.MAX_VALUE);
        out.writeShort(Short.MIN_VALUE);
        out.writeUTF("utf");
        out.writeVInt(67L);
        out.writeUnsignedVInt(88L);
        out.writeBytes("abcdefghi");
        buffer = out.buffer();
    }
    // calculate expected CRC
    CRC32 crc = new CRC32();
    FBUtilities.updateChecksum(crc, buffer);
    // save the buffer to file to create a RAR
    File file = FileUtils.createTempFile("testReadMethods", "1");
    file.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(file)) {
        writer.write(buffer);
        writer.writeInt((int) crc.getValue());
        writer.finish();
    }
    assertTrue(file.exists());
    assertEquals(buffer.remaining() + 4, file.length());
    try (ChecksummedDataInput reader = ChecksummedDataInput.open(file)) {
        reader.limit(buffer.remaining() + 4);
        // assert that we read all the right values back
        assertEquals(127, reader.read());
        byte[] bytes = new byte[b.length];
        reader.readFully(bytes);
        assertTrue(Arrays.equals(bytes, b));
        assertEquals(false, reader.readBoolean());
        assertEquals(10, reader.readByte());
        assertEquals('t', reader.readChar());
        assertEquals(3.3, reader.readDouble());
        assertEquals(2.2f, reader.readFloat());
        assertEquals(42, reader.readInt());
        assertEquals(Long.MAX_VALUE, reader.readLong());
        assertEquals(Short.MIN_VALUE, reader.readShort());
        assertEquals("utf", reader.readUTF());
        assertEquals(67L, reader.readVInt());
        assertEquals(88L, reader.readUnsignedVInt());
        assertEquals("abcdefghi", new String(ByteBufferUtil.read(reader, 9).array(), StandardCharsets.UTF_8));
        // assert that the crc matches, and that we've read exactly as many bytes as expected
        assertTrue(reader.checkCrc());
        assertTrue(reader.isEOF());
        reader.checkLimit(0);
    }
}
Also used : CRC32(java.util.zip.CRC32) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) ByteBuffer(java.nio.ByteBuffer) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 8 with SequentialWriter

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

the class ChecksummedDataInputTest method testFailedCrc.

@Test
public void testFailedCrc() throws IOException {
    CRC32 crc = new CRC32();
    ByteBuffer buffer;
    // fill a bytebuffer with some input
    try (DataOutputBuffer out = new DataOutputBuffer()) {
        out.write(127);
        out.writeBoolean(false);
        out.writeByte(10);
        out.writeChar('t');
        buffer = out.buffer();
        FBUtilities.updateChecksum(crc, buffer);
        // update twice so it won't match
        FBUtilities.updateChecksum(crc, buffer);
        out.writeInt((int) crc.getValue());
        buffer = out.buffer();
    }
    // save the buffer to file to create a RAR
    File file = FileUtils.createTempFile("testFailedCrc", "1");
    file.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(file)) {
        writer.write(buffer);
        writer.finish();
    }
    assertTrue(file.exists());
    assertEquals(buffer.remaining(), file.length());
    try (ChecksummedDataInput reader = ChecksummedDataInput.open(file)) {
        reader.limit(buffer.remaining());
        // assert that we read all the right values back
        assertEquals(127, reader.read());
        assertEquals(false, reader.readBoolean());
        assertEquals(10, reader.readByte());
        assertEquals('t', reader.readChar());
        assertFalse(reader.checkCrc());
        assertTrue(reader.isEOF());
    }
}
Also used : CRC32(java.util.zip.CRC32) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) ByteBuffer(java.nio.ByteBuffer) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 9 with SequentialWriter

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

the class TokenTreeTest method generateTree.

private static TokenTree generateTree(final long minToken, final long maxToken, boolean isStatic) throws IOException {
    final SortedMap<Long, LongSet> toks = new TreeMap<Long, LongSet>() {

        {
            for (long i = minToken; i <= maxToken; i++) {
                LongSet offsetSet = new LongHashSet();
                offsetSet.add(i);
                put(i, offsetSet);
            }
        }
    };
    final TokenTreeBuilder builder = isStatic ? new StaticTokenTreeBuilder(new FakeCombinedTerm(toks)) : new DynamicTokenTreeBuilder(toks);
    builder.finish();
    final File treeFile = FileUtils.createTempFile("token-tree-get-test", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    RandomAccessReader reader = null;
    try {
        reader = RandomAccessReader.open(treeFile);
        return new TokenTree(new MappedBuffer(reader));
    } finally {
        FileUtils.closeQuietly(reader);
    }
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) LongHashSet(com.carrotsearch.hppc.LongHashSet) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) File(org.apache.cassandra.io.util.File) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

Example 10 with SequentialWriter

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

the class TokenTreeTest method testSerializedSize.

public void testSerializedSize(final TokenTreeBuilder builder) throws Exception {
    builder.finish();
    final File treeFile = FileUtils.createTempFile("token-tree-size-test", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    final RandomAccessReader reader = RandomAccessReader.open(treeFile);
    Assert.assertEquals((int) reader.bytesRemaining(), builder.serializedSize());
    reader.close();
}
Also used : RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) File(org.apache.cassandra.io.util.File)

Aggregations

SequentialWriter (org.apache.cassandra.io.util.SequentialWriter)12 File (org.apache.cassandra.io.util.File)10 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)6 MappedBuffer (org.apache.cassandra.index.sasi.utils.MappedBuffer)5 ByteBuffer (java.nio.ByteBuffer)4 Test (org.junit.Test)4 CRC32 (java.util.zip.CRC32)3 LongSet (com.carrotsearch.hppc.LongSet)2 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)2 CompressedSequentialWriter (org.apache.cassandra.io.compress.CompressedSequentialWriter)2 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)2 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 Sets (com.google.common.collect.Sets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1