Search in sources :

Example 11 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project cassandra by apache.

the class RowTest method assertRangeTombstoneMarkers.

private void assertRangeTombstoneMarkers(ClusteringBound<?> start, ClusteringBound<?> end, DeletionTime deletionTime, Object[] expected) {
    AbstractType clusteringType = (AbstractType) metadata.comparator.subtype(0);
    assertEquals(1, start.size());
    assertEquals(start.kind(), ClusteringPrefix.Kind.INCL_START_BOUND);
    assertEquals(expected[0], clusteringType.getString(start.bufferAt(0)));
    assertEquals(1, end.size());
    assertEquals(end.kind(), ClusteringPrefix.Kind.INCL_END_BOUND);
    assertEquals(expected[1], clusteringType.getString(end.bufferAt(0)));
    assertEquals(expected[2], deletionTime.markedForDeleteAt());
    assertEquals(expected[3], deletionTime.localDeletionTime());
}
Also used : AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Example 12 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType 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 13 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project cassandra by apache.

the class SSTableHeaderFixTest method assertFrozenUdt.

private void assertFrozenUdt(String name, AbstractType<?> type, boolean frozen, boolean checkInner) {
    if (type instanceof CompositeType) {
        if (checkInner)
            for (AbstractType<?> component : ((CompositeType) type).types) assertFrozenUdt(name, component, frozen, true);
    } else if (type instanceof CollectionType) {
        if (checkInner) {
            if (type instanceof MapType) {
                MapType map = (MapType) type;
                // only descend for non-frozen types (checking frozen in frozen is just stupid)
                if (map.isMultiCell()) {
                    assertFrozenUdt(name + "<map-key>", map.getKeysType(), frozen, true);
                    assertFrozenUdt(name + "<map-value>", map.getValuesType(), frozen, true);
                }
            } else if (type instanceof SetType) {
                SetType set = (SetType) type;
                // only descend for non-frozen types (checking frozen in frozen is just stupid)
                if (set.isMultiCell())
                    assertFrozenUdt(name + "<set>", set.getElementsType(), frozen, true);
            } else if (type instanceof ListType) {
                ListType list = (ListType) type;
                // only descend for non-frozen types (checking frozen in frozen is just stupid)
                if (list.isMultiCell())
                    assertFrozenUdt(name + "<list>", list.getElementsType(), frozen, true);
            }
        }
    } else if (type instanceof TupleType) {
        if (checkInner) {
            TupleType tuple = (TupleType) type;
            // only descend for non-frozen types (checking frozen in frozen is just stupid)
            if (tuple.isMultiCell())
                for (AbstractType<?> component : tuple.allTypes()) assertFrozenUdt(name + "<tuple>", component, frozen, true);
        }
    }
    if (type instanceof UserType) {
        String typeString = type.toString();
        assertEquals(name + ": " + typeString, frozen, !type.isMultiCell());
        if (typeString.startsWith(UserType.class.getName() + '('))
            if (frozen)
                fail(name + ": " + typeString);
        if (typeString.startsWith(FrozenType.class.getName() + '(' + UserType.class.getName() + '('))
            if (!frozen)
                fail(name + ": " + typeString);
    }
}
Also used : SetType(org.apache.cassandra.db.marshal.SetType) CollectionType(org.apache.cassandra.db.marshal.CollectionType) ListType(org.apache.cassandra.db.marshal.ListType) AbstractType(org.apache.cassandra.db.marshal.AbstractType) TupleType(org.apache.cassandra.db.marshal.TupleType) UserType(org.apache.cassandra.db.marshal.UserType) MapType(org.apache.cassandra.db.marshal.MapType) CompositeType(org.apache.cassandra.db.marshal.CompositeType) AbstractCompositeType(org.apache.cassandra.db.marshal.AbstractCompositeType)

Example 14 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project cassandra by apache.

the class CassandraGenerators method toStringRecursive.

/**
 * Uses reflection to generate a toString.  This method is aware of common Cassandra classes and can be used for
 * generators or tests to provide more details for debugging.
 */
public static String toStringRecursive(Object o) {
    return ReflectionToStringBuilder.toString(o, new MultilineRecursiveToStringStyle() {

        private String spacer = "";

        {
            // common lang uses start/end chars that are not the common ones used, so switch to the common ones
            setArrayStart("[");
            setArrayEnd("]");
            setContentStart("{");
            setContentEnd("}");
            setUseIdentityHashCode(false);
            setUseShortClassName(true);
        }

        protected boolean accept(Class<?> clazz) {
            return // toString enums
            !clazz.isEnum() && // if no fields, just toString
            Stream.of(clazz.getDeclaredFields()).anyMatch(f -> !Modifier.isStatic(f.getModifiers()));
        }

        public void appendDetail(StringBuffer buffer, String fieldName, Object value) {
            if (value instanceof ByteBuffer) {
                value = ByteBufferUtil.bytesToHex((ByteBuffer) value);
            } else if (value instanceof AbstractType) {
                value = SchemaCQLHelper.toCqlType((AbstractType) value);
            } else if (value instanceof Token || value instanceof InetAddressAndPort || value instanceof FieldIdentifier) {
                value = value.toString();
            } else if (value instanceof TableMetadata) {
                // to make sure the correct indents are taken, convert to CQL, then replace newlines with the indents
                // then prefix with the indents.
                String cql = SchemaCQLHelper.getTableMetadataAsCQL((TableMetadata) value);
                cql = NEWLINE_PATTERN.matcher(cql).replaceAll(Matcher.quoteReplacement("\n  " + spacer));
                cql = "\n  " + spacer + cql;
                value = cql;
            }
            super.appendDetail(buffer, fieldName, value);
        }

        // MultilineRecursiveToStringStyle doesn't look at what was set and instead hard codes the values when it "resets" the level
        protected void setArrayStart(String arrayStart) {
            super.setArrayStart(arrayStart.replace("{", "["));
        }

        protected void setArrayEnd(String arrayEnd) {
            super.setArrayEnd(arrayEnd.replace("}", "]"));
        }

        protected void setContentStart(String contentStart) {
            // use this to infer the spacer since it isn't exposed.
            String[] split = contentStart.split("\n", 2);
            spacer = split.length == 2 ? split[1] : "";
            super.setContentStart(contentStart.replace("[", "{"));
        }

        protected void setContentEnd(String contentEnd) {
            super.setContentEnd(contentEnd.replace("]", "}"));
        }
    }, true);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) MultilineRecursiveToStringStyle(org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle) AbstractType(org.apache.cassandra.db.marshal.AbstractType) FieldIdentifier(org.apache.cassandra.cql3.FieldIdentifier) Token(org.apache.cassandra.dht.Token) ByteBuffer(java.nio.ByteBuffer)

Example 15 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project cassandra by apache.

the class PartitionIterator method getKeyAsString.

public String getKeyAsString() {
    StringBuilder sb = new StringBuilder();
    int i = 0;
    for (Object key : partitionKey) {
        if (i > 0)
            sb.append("|");
        AbstractType type = generator.partitionKey.get(i++).type;
        sb.append(type.getString(type.decompose(key)));
    }
    return sb.toString();
}
Also used : AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Aggregations

AbstractType (org.apache.cassandra.db.marshal.AbstractType)51 ByteBuffer (java.nio.ByteBuffer)20 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)9 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 List (java.util.List)6 Map (java.util.Map)6 CollectionType (org.apache.cassandra.db.marshal.CollectionType)6 CompositeType (org.apache.cassandra.db.marshal.CompositeType)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 CQL3Type (org.apache.cassandra.cql3.CQL3Type)5 SerializationHeader (org.apache.cassandra.db.SerializationHeader)5 TupleType (org.apache.cassandra.db.marshal.TupleType)5 UserType (org.apache.cassandra.db.marshal.UserType)5 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)4 FieldIdentifier (org.apache.cassandra.cql3.FieldIdentifier)4 Test (org.junit.Test)4 AuditLogContext (org.apache.cassandra.audit.AuditLogContext)3