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();
}
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);
}
}
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());
}
}
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);
}
}
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();
}
Aggregations