Search in sources :

Example 1 with Version

use of org.apache.cassandra.io.sstable.format.Version 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 Version

use of org.apache.cassandra.io.sstable.format.Version in project cassandra by apache.

the class MetadataSerializerTest method pendingRepairCompatibility.

@Test
public void pendingRepairCompatibility() {
    Version mc = BigFormat.instance.getVersion("mc");
    assertFalse(mc.hasPendingRepair());
    Version md = BigFormat.instance.getVersion("md");
    assertTrue(md.hasPendingRepair());
}
Also used : Version(org.apache.cassandra.io.sstable.format.Version) Test(org.junit.Test)

Example 3 with Version

use of org.apache.cassandra.io.sstable.format.Version in project cassandra by apache.

the class Descriptor method fromFilenameWithComponent.

/**
 * Parse a sstable filename, extracting both the {@code Descriptor} and {@code Component} part.
 *
 * @param file the {@code File} object for the filename to parse.
 * @return a pair of the descriptor and component corresponding to the provided {@code file}.
 *
 * @throws IllegalArgumentException if the provided {@code file} does point to a valid sstable filename. This could
 * mean either that the filename doesn't look like a sstable file, or that it is for an old and unsupported
 * versions.
 */
public static Pair<Descriptor, Component> fromFilenameWithComponent(File file) {
    // absolute path.
    if (!file.isAbsolute())
        file = file.toAbsolute();
    String name = file.name();
    List<String> tokens = filenameSplitter.splitToList(name);
    int size = tokens.size();
    if (size != 4) {
        // but we're just trying to be helpful, not perfect.
        if (size == 5 || size == 6)
            throw new IllegalArgumentException(String.format("%s is of version %s which is now unsupported and cannot be read.", name, tokens.get(size - 3)));
        throw new IllegalArgumentException(String.format("Invalid sstable file %s: the name doesn't look like a supported sstable file name", name));
    }
    String versionString = tokens.get(0);
    if (!Version.validate(versionString))
        throw invalidSSTable(name, "invalid version %s", versionString);
    int generation;
    try {
        generation = Integer.parseInt(tokens.get(1));
    } catch (NumberFormatException e) {
        throw invalidSSTable(name, "the 'generation' part of the name doesn't parse as a number");
    }
    String formatString = tokens.get(2);
    SSTableFormat.Type format;
    try {
        format = SSTableFormat.Type.validate(formatString);
    } catch (IllegalArgumentException e) {
        throw invalidSSTable(name, "unknown 'format' part (%s)", formatString);
    }
    Component component = Component.parse(tokens.get(3));
    Version version = format.info.getVersion(versionString);
    if (!version.isCompatible())
        throw invalidSSTable(name, "incompatible sstable version (%s); you should have run upgradesstables before upgrading", versionString);
    File directory = parentOf(name, file);
    File tableDir = directory;
    // Check if it's a 2ndary index directory (not that it doesn't exclude it to be also a backup or snapshot)
    String indexName = "";
    if (Directories.isSecondaryIndexFolder(tableDir)) {
        indexName = tableDir.name();
        tableDir = parentOf(name, tableDir);
    }
    // Then it can be a backup or a snapshot
    if (tableDir.name().equals(Directories.BACKUPS_SUBDIR))
        tableDir = tableDir.parent();
    else if (parentOf(name, tableDir).name().equals(Directories.SNAPSHOT_SUBDIR))
        tableDir = parentOf(name, parentOf(name, tableDir));
    String table = tableDir.name().split("-")[0] + indexName;
    String keyspace = parentOf(name, tableDir).name();
    return Pair.create(new Descriptor(version, directory, keyspace, table, generation, format), component);
}
Also used : Version(org.apache.cassandra.io.sstable.format.Version) SSTableFormat(org.apache.cassandra.io.sstable.format.SSTableFormat) File(org.apache.cassandra.io.util.File)

Aggregations

Version (org.apache.cassandra.io.sstable.format.Version)3 SSTableFormat (org.apache.cassandra.io.sstable.format.SSTableFormat)2 File (org.apache.cassandra.io.util.File)2 Sets (com.google.common.collect.Sets)1 ByteBuffer (java.nio.ByteBuffer)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 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)1 FieldIdentifier (org.apache.cassandra.cql3.FieldIdentifier)1 IndexTarget (org.apache.cassandra.cql3.statements.schema.IndexTarget)1