Search in sources :

Example 1 with SequentialWriter

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

the class SSTableHeaderFixTest method buildFakeSSTable.

private File buildFakeSSTable(File dir, int generation, TableMetadata.Builder cols, Function<ColumnMetadata, ColumnMetadata> freezer) {
    TableMetadata headerMetadata = cols.build();
    TableMetadata.Builder schemaCols = TableMetadata.builder("ks", "cf");
    for (ColumnMetadata cm : cols.columns()) schemaCols.addColumn(freezer.apply(cm));
    tableMetadata = schemaCols.build();
    try {
        Descriptor desc = new Descriptor(version, dir, "ks", "cf", generation, SSTableFormat.Type.BIG);
        // Just create the component files - we don't really need those.
        for (Component component : requiredComponents) assertTrue(new File(desc.filenameFor(component)).createFileIfNotExists());
        AbstractType<?> partitionKey = headerMetadata.partitionKeyType;
        List<AbstractType<?>> clusteringKey = headerMetadata.clusteringColumns().stream().map(cd -> cd.type).collect(Collectors.toList());
        Map<ByteBuffer, AbstractType<?>> staticColumns = headerMetadata.columns().stream().filter(cd -> cd.kind == ColumnMetadata.Kind.STATIC).collect(Collectors.toMap(cd -> cd.name.bytes, cd -> cd.type, (a, b) -> a));
        Map<ByteBuffer, AbstractType<?>> regularColumns = headerMetadata.columns().stream().filter(cd -> cd.kind == ColumnMetadata.Kind.REGULAR).collect(Collectors.toMap(cd -> cd.name.bytes, cd -> cd.type, (a, b) -> a));
        File statsFile = new File(desc.filenameFor(Component.STATS));
        SerializationHeader.Component header = SerializationHeader.Component.buildComponentForTools(partitionKey, clusteringKey, staticColumns, regularColumns, EncodingStats.NO_STATS);
        try (SequentialWriter out = new SequentialWriter(statsFile)) {
            desc.getMetadataSerializer().serialize(Collections.singletonMap(MetadataType.HEADER, header), out, version);
            out.finish();
        }
        return new File(desc.filenameFor(Component.DATA));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Arrays(java.util.Arrays) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) File(org.apache.cassandra.io.util.File) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer) Matcher(java.util.regex.Matcher) Map(java.util.Map) After(org.junit.After) SSTableFormat(org.apache.cassandra.io.sstable.format.SSTableFormat) Assert.fail(org.junit.Assert.fail) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) CollectionType(org.apache.cassandra.db.marshal.CollectionType) CompositeType(org.apache.cassandra.db.marshal.CompositeType) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) Version(org.apache.cassandra.io.sstable.format.Version) FBUtilities(org.apache.cassandra.utils.FBUtilities) MetadataType(org.apache.cassandra.io.sstable.metadata.MetadataType) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) FloatType(org.apache.cassandra.db.marshal.FloatType) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) FrozenType(org.apache.cassandra.db.marshal.FrozenType) TableMetadata(org.apache.cassandra.schema.TableMetadata) Pattern(java.util.regex.Pattern) IntStream(java.util.stream.IntStream) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SetType(org.apache.cassandra.db.marshal.SetType) FieldIdentifier(org.apache.cassandra.cql3.FieldIdentifier) Function(java.util.function.Function) HashSet(java.util.HashSet) Int32Type(org.apache.cassandra.db.marshal.Int32Type) ListType(org.apache.cassandra.db.marshal.ListType) UTF8Type(org.apache.cassandra.db.marshal.UTF8Type) AbstractCompositeType(org.apache.cassandra.db.marshal.AbstractCompositeType) TupleType(org.apache.cassandra.db.marshal.TupleType) BigFormat(org.apache.cassandra.io.sstable.format.big.BigFormat) SerializationHeader(org.apache.cassandra.db.SerializationHeader) Before(org.junit.Before) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MapType(org.apache.cassandra.db.marshal.MapType) EncodingStats(org.apache.cassandra.db.rows.EncodingStats) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) UserType(org.apache.cassandra.db.marshal.UserType) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) ByteBuffer(java.nio.ByteBuffer) SerializationHeader(org.apache.cassandra.db.SerializationHeader) AbstractType(org.apache.cassandra.db.marshal.AbstractType) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) File(org.apache.cassandra.io.util.File)

Example 2 with SequentialWriter

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

the class ChecksummedDataInputTest method testResetCrc.

@Test
public void testResetCrc() 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);
        out.writeInt((int) crc.getValue());
        int bufferPos = out.getLength();
        out.writeDouble(3.3);
        out.writeFloat(2.2f);
        out.writeInt(42);
        buffer = out.buffer();
        buffer.position(bufferPos);
        crc.reset();
        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("testResetCrc", "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());
        assertTrue(reader.checkCrc());
        reader.resetCrc();
        assertEquals(3.3, reader.readDouble());
        assertEquals(2.2f, reader.readFloat());
        assertEquals(42, reader.readInt());
        assertTrue(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 3 with SequentialWriter

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

the class TokenTreeTest method buildSerializeAndIterate.

public void buildSerializeAndIterate(TokenTreeBuilder builder, SortedMap<Long, LongSet> tokenMap) throws Exception {
    builder.finish();
    final File treeFile = FileUtils.createTempFile("token-tree-iterate-test1", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    final RandomAccessReader reader = RandomAccessReader.open(treeFile);
    final TokenTree tokenTree = new TokenTree(new MappedBuffer(reader));
    final Iterator<Token> tokenIterator = tokenTree.iterator(KEY_CONVERTER);
    final Iterator<Map.Entry<Long, LongSet>> listIterator = tokenMap.entrySet().iterator();
    while (tokenIterator.hasNext() && listIterator.hasNext()) {
        Token treeNext = tokenIterator.next();
        Map.Entry<Long, LongSet> listNext = listIterator.next();
        Assert.assertEquals(listNext.getKey(), treeNext.get());
        Assert.assertEquals(convert(listNext.getValue()), convert(treeNext));
    }
    Assert.assertFalse("token iterator not finished", tokenIterator.hasNext());
    Assert.assertFalse("list iterator not finished", listIterator.hasNext());
    reader.close();
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) File(org.apache.cassandra.io.util.File) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

Example 4 with SequentialWriter

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

the class TokenTreeTest method skipPastEnd.

public void skipPastEnd(TokenTreeBuilder builder, SortedMap<Long, LongSet> tokens) throws Exception {
    builder.finish();
    final File treeFile = FileUtils.createTempFile("token-tree-skip-past-test", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    final RandomAccessReader reader = RandomAccessReader.open(treeFile);
    final RangeIterator<Long, Token> tokenTree = new TokenTree(new MappedBuffer(reader)).iterator(KEY_CONVERTER);
    tokenTree.skipTo(tokens.lastKey() + 10);
}
Also used : RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) File(org.apache.cassandra.io.util.File) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

Example 5 with SequentialWriter

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

the class TokenTreeTest method buildSerializeIterateAndSkip.

// works with maps other than bigTokensMap but skips to a rather large token
// so likely for maps other than bigTokensMap skipping is not tested by this.
public void buildSerializeIterateAndSkip(TokenTreeBuilder builder, SortedMap<Long, LongSet> tokens) throws Exception {
    builder.finish();
    final File treeFile = FileUtils.createTempFile("token-tree-iterate-test2", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    final RandomAccessReader reader = RandomAccessReader.open(treeFile);
    final TokenTree tokenTree = new TokenTree(new MappedBuffer(reader));
    final RangeIterator<Long, Token> treeIterator = tokenTree.iterator(KEY_CONVERTER);
    final RangeIterator<Long, TokenWithOffsets> listIterator = new EntrySetSkippableIterator(tokens);
    long lastToken = 0L;
    while (treeIterator.hasNext() && lastToken < 12) {
        Token treeNext = treeIterator.next();
        TokenWithOffsets listNext = listIterator.next();
        Assert.assertEquals(listNext.token, (lastToken = treeNext.get()));
        Assert.assertEquals(convert(listNext.offsets), convert(treeNext));
    }
    treeIterator.skipTo(100548L);
    listIterator.skipTo(100548L);
    while (treeIterator.hasNext() && listIterator.hasNext()) {
        Token treeNext = treeIterator.next();
        TokenWithOffsets listNext = listIterator.next();
        Assert.assertEquals(listNext.token, (long) treeNext.get());
        Assert.assertEquals(convert(listNext.offsets), convert(treeNext));
    }
    Assert.assertFalse("Tree iterator not completed", treeIterator.hasNext());
    Assert.assertFalse("List iterator not completed", listIterator.hasNext());
    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) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

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