Example usage for org.apache.cassandra.io.util SequentialWriter resetAndTruncate

List of usage examples for org.apache.cassandra.io.util SequentialWriter resetAndTruncate

Introduction

In this page you can find the example usage for org.apache.cassandra.io.util SequentialWriter resetAndTruncate.

Prototype

public void resetAndTruncate(DataPosition mark) 

Source Link

Document

Drops all buffered data that's past the limits of our new file mark + buffer capacity, or syncs and truncates the underlying file to the marked position

Usage

From source file:com.fullcontact.cassandra.io.compress.CompressedRandomAccessReaderTest.java

License:Apache License

private void testResetAndTruncate(File f, boolean compressed, int junkSize) throws IOException {
    final String filename = f.getAbsolutePath();

    try {/* www .j a v  a2 s  . c om*/
        SSTableMetadata.Collector sstableMetadataCollector = SSTableMetadata.createCollector()
                .replayPosition(null);
        SequentialWriter writer = compressed
                ? new CompressedSequentialWriter(f, filename + ".metadata", false,
                        new CompressionParameters(SnappyCompressor.instance), sstableMetadataCollector)
                : new SequentialWriter(f, CompressionParameters.DEFAULT_CHUNK_LENGTH, false);

        writer.write("The quick ".getBytes());
        FileMark mark = writer.mark();
        writer.write("blue fox jumps over the lazy dog".getBytes());

        // write enough to be sure to change chunk
        for (int i = 0; i < junkSize; ++i) {
            writer.write((byte) 1);
        }

        writer.resetAndTruncate(mark);
        writer.write("brown fox jumps over the lazy dog".getBytes());
        writer.close();

        assert f.exists();
        RandomAccessReader reader = compressed
                ? CompressedRandomAccessReader.open(new Path(filename),
                        new CompressionMetadata(filename + ".metadata", f.length(), fs), false, fs)
                : RandomAccessReader.open(new Path(f.getPath()), fs);
        String expected = "The quick brown fox jumps over the lazy dog";
        assertEquals(expected.length(), reader.length());
        byte[] b = new byte[expected.length()];
        reader.readFully(b);
        assert new String(b).equals(expected) : "Expecting '" + expected + "', got '" + new String(b) + "'";
    } finally {
        // cleanup
        if (f.exists())
            f.delete();
        File metadata = new File(filename + ".metadata");
        if (compressed && metadata.exists())
            metadata.delete();
    }
}