Example usage for org.apache.cassandra.db.marshal AbstractType asCQL3Type

List of usage examples for org.apache.cassandra.db.marshal AbstractType asCQL3Type

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal AbstractType asCQL3Type.

Prototype

public CQL3Type asCQL3Type() 

Source Link

Usage

From source file:com.protectwise.cassandra.retrospect.deletion.RuleBasedDeletionConvictor.java

License:Apache License

/**
 * Keyed by rulename, then by column name, then contains a list of 2-element arrays of ranges for that column.
 * This is not typed, everything is byte buffers, type is collapsed at testing time.
 *
 * @param statement//from   www. j  a va  2s.  c o  m
 * @return
 * @throws ConfigurationException
 */
public static Map<ByteBuffer, Map<ByteBuffer, List<ByteBuffer[]>>> parseRules(String statement)
        throws ConfigurationException {
    UntypedResultSet rawRuleData = null;
    try {
        if (!QueryHelper.hasStartedCQL()) {
            // Yuck, exceptions for control flow.  This will be caught upstream during compaction as a signal that
            // we should move to spooked mode.  Outside of compaction the exception will bubble up and be presented
            // to the user (though it seems extremely unlikely)
            throw new ConfigurationException(
                    "Node is not fully joined, so we cannot read deletion rules.  Falling back to standard compaction");
        }
        rawRuleData = QueryProcessor.process(statement, ConsistencyLevel.LOCAL_QUORUM);
    } catch (RequestExecutionException e) {
        ConfigurationException ce = new ConfigurationException(
                "Unable to query for rule data.  The failed statement was " + statement, e);
        throw ce;
    }

    Map<String, ColumnSpecification> cols = new HashMap<>();
    for (ColumnSpecification cs : rawRuleData.metadata()) {
        cols.put(cs.name.toString(), cs);
    }

    if (!cols.containsKey("column") || !cols.containsKey("rulename") || !cols.containsKey("range")) {
        throw new ConfigurationException(
                "The select statement must return the columns 'column', 'rulename', and 'range'");
    }

    CQL3Type columnType = cols.get("column").type.asCQL3Type();
    if (!columnType.equals(CQL3Type.Native.TEXT)) {
        throw new ConfigurationException(
                "The 'column' column must be a text type.  Found " + columnType.toString());
    }

    //  Validate that "range" is of type tuple<text,text>, ugh.
    CQL3Type rangeType = cols.get("range").type.asCQL3Type();
    if (!(rangeType instanceof CQL3Type.Tuple)) {
        throw new ConfigurationException("The column 'range' must be of type tuple<text,text>  Found "
                + cols.get("column").type.getSerializer().getType());
    }
    List<AbstractType<?>> subtypes = ((TupleType) ((CQL3Type.Tuple) rangeType).getType()).allTypes();
    if (subtypes.size() != 2) {
        throw new ConfigurationException("The column 'range' must be of type tuple<text,text>  Found "
                + cols.get("column").type.getSerializer().getType());
    }
    for (AbstractType<?> t : subtypes) {
        if (!t.asCQL3Type().equals(CQL3Type.Native.TEXT)) {
            throw new ConfigurationException("The column 'range' must be of type tuple<text,text>  Found "
                    + cols.get("column").type.getSerializer().getType());
        }
    }

    Iterator<UntypedResultSet.Row> resultIterator = rawRuleData.iterator();

    Map<ByteBuffer, Map<ByteBuffer, List<ByteBuffer[]>>> rules = new HashMap<>();
    while (resultIterator.hasNext()) {
        UntypedResultSet.Row row = resultIterator.next();
        ByteBuffer rulename = row.getBlob("rulename");
        Map<ByteBuffer, List<ByteBuffer[]>> rule;
        if (!rules.containsKey(rulename)) {
            rule = new HashMap<>();
            rules.put(rulename, rule);
        } else {
            rule = rules.get(rulename);
        }

        ByteBuffer column = row.getBlob("column");
        List<ByteBuffer[]> ranges;
        if (rule.containsKey(column)) {
            ranges = rule.get(column);
        } else {
            ranges = new ArrayList<>();
            rule.put(column, ranges);
        }
        ByteBuffer[] rawRange = ((TupleType) rangeType.getType()).split(row.getBlob("range"));
        ranges.add(rawRange);
        if (logger.isDebugEnabled()) {
            logger.debug("Rule {} on column {} is range {} to {} (now {} ranges on this column)",
                    PrintHelper.bufToString(rulename), PrintHelper.bufToString(column),
                    PrintHelper.bufToString(rawRange[0]), PrintHelper.bufToString(rawRange[1]), ranges.size());
        }
    }

    return rules;
}

From source file:com.tuplejump.stargate.cassandra.CassandraUtils.java

License:Apache License

public static Options getOptions(Properties mapping, ColumnFamilyStore baseCfs, String colName) {
    Properties primary = mapping;
    String defaultField = colName;
    Map<String, NumericConfig> numericFieldOptions = new HashMap<>();
    Map<String, FieldType> fieldDocValueTypes = new TreeMap<>();
    Map<String, FieldType> collectionFieldDocValueTypes = new TreeMap<>();

    Map<String, FieldType> fieldTypes = new TreeMap<>();
    Map<String, FieldType[]> collectionFieldTypes = new TreeMap<>();
    Map<String, AbstractType> validators = new TreeMap<>();
    Map<Integer, Pair<String, ByteBuffer>> clusteringKeysIndexed = new LinkedHashMap<>();
    Map<Integer, Pair<String, ByteBuffer>> partitionKeysIndexed = new LinkedHashMap<>();
    Map<String, Analyzer> perFieldAnalyzers;
    Set<String> indexedColumnNames;

    //getForRow all the fields options.
    indexedColumnNames = new TreeSet<>();
    indexedColumnNames.addAll(mapping.getFields().keySet());

    Set<String> added = new HashSet<>(indexedColumnNames.size());
    List<ColumnDefinition> partitionKeys = baseCfs.metadata.partitionKeyColumns();
    List<ColumnDefinition> clusteringKeys = baseCfs.metadata.clusteringKeyColumns();

    for (ColumnDefinition colDef : partitionKeys) {
        String columnName = CFDefinition.definitionType.getString(colDef.name);
        if (Options.logger.isDebugEnabled()) {
            Options.logger.debug("Partition key name is {} and index is {}", colName, colDef.componentIndex);
        }/*from  w  ww .java 2  s . co  m*/
        validators.put(columnName, colDef.getValidator());
        if (indexedColumnNames.contains(columnName)) {
            int componentIndex = colDef.componentIndex == null ? 0 : colDef.componentIndex;
            partitionKeysIndexed.put(componentIndex, Pair.create(columnName, colDef.name));
            Properties properties = mapping.getFields().get(columnName.toLowerCase());
            addFieldType(columnName, colDef.getValidator(), properties, numericFieldOptions, fieldDocValueTypes,
                    collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes);
            added.add(columnName.toLowerCase());
        }
    }

    for (ColumnDefinition colDef : clusteringKeys) {
        String columnName = CFDefinition.definitionType.getString(colDef.name);
        if (Options.logger.isDebugEnabled()) {
            Options.logger.debug("Clustering key name is {} and index is {}", colName,
                    colDef.componentIndex + 1);
        }
        validators.put(columnName, colDef.getValidator());
        if (indexedColumnNames.contains(columnName)) {
            clusteringKeysIndexed.put(colDef.componentIndex + 1, Pair.create(columnName, colDef.name));
            Properties properties = mapping.getFields().get(columnName.toLowerCase());
            addFieldType(columnName, colDef.getValidator(), properties, numericFieldOptions, fieldDocValueTypes,
                    collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes);
            added.add(columnName.toLowerCase());
        }
    }

    for (String columnName : indexedColumnNames) {
        if (added.add(columnName.toLowerCase())) {
            Properties options = mapping.getFields().get(columnName);
            ColumnDefinition colDef = getColumnDefinition(baseCfs, columnName);
            if (colDef != null) {
                validators.put(columnName, colDef.getValidator());
                addFieldType(columnName, colDef.getValidator(), options, numericFieldOptions,
                        fieldDocValueTypes, collectionFieldDocValueTypes, fieldTypes, collectionFieldTypes);
            } else {
                throw new IllegalArgumentException(
                        String.format("Column Definition for %s not found", columnName));
            }
            if (options.getType() == Properties.Type.object) {
                mapping.getFields().putAll(options.getFields());
            }
        }

    }
    Set<ColumnDefinition> otherColumns = baseCfs.metadata.regularColumns();
    for (ColumnDefinition colDef : otherColumns) {
        String columnName = CFDefinition.definitionType.getString(colDef.name);
        validators.put(columnName, colDef.getValidator());
    }

    numericFieldOptions.putAll(primary.getDynamicNumericConfig());

    Analyzer defaultAnalyzer = mapping.getLuceneAnalyzer();
    perFieldAnalyzers = mapping.perFieldAnalyzers();
    Analyzer analyzer = new PerFieldAnalyzerWrapper(defaultAnalyzer, perFieldAnalyzers);
    Map<String, Properties.Type> types = new TreeMap<>();
    Set<String> nestedFields = new TreeSet<>();
    for (Map.Entry<String, AbstractType> entry : validators.entrySet()) {
        CQL3Type cql3Type = entry.getValue().asCQL3Type();
        AbstractType inner = getValueValidator(cql3Type.getType());
        if (cql3Type.isCollection()) {
            types.put(entry.getKey(), fromAbstractType(inner.asCQL3Type()));
            nestedFields.add(entry.getKey());
        } else {
            types.put(entry.getKey(), fromAbstractType(cql3Type));
        }

    }

    return new Options(primary, numericFieldOptions, fieldDocValueTypes, collectionFieldDocValueTypes,
            fieldTypes, collectionFieldTypes, types, nestedFields, clusteringKeysIndexed, partitionKeysIndexed,
            perFieldAnalyzers, indexedColumnNames, analyzer, defaultField);
}

From source file:com.tuplejump.stargate.cassandra.CassandraUtils.java

License:Apache License

public static void setFromAbstractType(Properties properties, AbstractType type) {
    if (properties.getType() != null)
        return;// ww  w.  j a  v a  2 s  . c o m
    CQL3Type cqlType = type.asCQL3Type();
    Properties.Type fromAbstractType = fromAbstractType(cqlType);
    properties.setType(fromAbstractType);

}

From source file:com.tuplejump.stargate.Fields.java

License:Apache License

public static Field field(String name, AbstractType type, ByteBuffer byteBufferValue, FieldType fieldType) {
    if (fieldType.docValueType() != null) {
        if (fieldType.numericType() != null)
            return numericDocValuesField(name, type, byteBufferValue);
        else//ww w .  j  av a 2 s  .  co m
            return stringDocValuesField(name, type, byteBufferValue);
    }
    CQL3Type cqlType = type.asCQL3Type();
    if (cqlType == CQL3Type.Native.INT) {
        return new IntField(name, (Integer) type.compose(byteBufferValue), fieldType);
    } else if (cqlType == CQL3Type.Native.VARINT || cqlType == CQL3Type.Native.BIGINT
            || cqlType == CQL3Type.Native.COUNTER) {
        return new LongField(name, ((Number) type.compose(byteBufferValue)).longValue(), fieldType);
    } else if (cqlType == CQL3Type.Native.DECIMAL || cqlType == CQL3Type.Native.DOUBLE) {
        return new DoubleField(name, ((Number) type.compose(byteBufferValue)).doubleValue(), fieldType);
    } else if (cqlType == CQL3Type.Native.FLOAT) {
        return new FloatField(name, ((Number) type.compose(byteBufferValue)).floatValue(), fieldType);
    } else if (cqlType == CQL3Type.Native.ASCII || cqlType == CQL3Type.Native.TEXT
            || cqlType == CQL3Type.Native.VARCHAR) {
        return new Field(name, type.getString(byteBufferValue), fieldType);
    } else if (cqlType == CQL3Type.Native.UUID) {
        return new Field(name, type.getString(byteBufferValue), fieldType);
    } else if (cqlType == CQL3Type.Native.TIMEUUID) {
        //TimeUUID toString is not comparable. So we reorder to get a comparable value while searching
        return new Field(name, reorderTimeUUId(type.getString(byteBufferValue)), fieldType);
    } else if (cqlType == CQL3Type.Native.TIMESTAMP) {
        return new LongField(name, ((Date) type.compose(byteBufferValue)).getTime(), fieldType);
    } else if (cqlType == CQL3Type.Native.BOOLEAN) {
        Boolean val = ((Boolean) type.compose(byteBufferValue));
        return new Field(name, val.toString(), fieldType);
    } else {
        return new Field(name, toString(byteBufferValue, type), fieldType);
    }
}

From source file:com.tuplejump.stargate.Fields.java

License:Apache License

public static ByteBuffer defaultValue(AbstractType type, boolean min) {
    CQL3Type cqlType = type.asCQL3Type();
    if (cqlType == CQL3Type.Native.INT || cqlType == CQL3Type.Native.VARINT) {
        return ByteBufferUtil.bytes(min ? Integer.MIN_VALUE : Integer.MAX_VALUE);
    } else if (cqlType == CQL3Type.Native.BIGINT) {
        return ByteBufferUtil.bytes(min ? Long.MIN_VALUE : Long.MAX_VALUE);
    } else if (cqlType == CQL3Type.Native.DECIMAL || cqlType == CQL3Type.Native.DOUBLE) {
        return ByteBufferUtil.bytes(min ? Double.MIN_VALUE : Double.MAX_VALUE);
    } else if (cqlType == CQL3Type.Native.FLOAT) {
        return ByteBufferUtil.bytes(min ? Float.MIN_VALUE : Float.MAX_VALUE);
    } else if (cqlType == CQL3Type.Native.TEXT || cqlType == CQL3Type.Native.VARCHAR) {
        return ByteBufferUtil.bytes("");
    } else if (cqlType == CQL3Type.Native.UUID) {
        return ByteBufferUtil.bytes(UUID.randomUUID());
    } else if (cqlType == CQL3Type.Native.TIMEUUID) {
        return ByteBufferUtil.bytes(UUIDGen.getTimeUUID(0));
    } else if (cqlType == CQL3Type.Native.TIMESTAMP) {
        return ByteBufferUtil.bytes(0l);
    } else if (cqlType == CQL3Type.Native.BOOLEAN) {
        return BooleanType.instance.decompose(min ? false : true);
    } else if (type.isCollection()) {
        CollectionType collectionType = (CollectionType) type;
        List<Pair<ByteBuffer, Column>> collection = new ArrayList<>();
        ByteBuffer dummyColumn = defaultValue(collectionType.nameComparator());
        collection.add(Pair.create(dummyColumn,
                new Column(dummyColumn, defaultValue(collectionType.valueComparator(), min))));
        return collectionType.serialize(collection);
    } else {//from  www  .  j  av  a 2 s .  c o m
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;
    }
}

From source file:com.tuplejump.stargate.Fields.java

License:Apache License

public static void setNumericType(AbstractType type, FieldType luceneFieldType) {
    CQL3Type cqlType = type.asCQL3Type();
    if (cqlType == CQL3Type.Native.INT) {
        luceneFieldType.setNumericType(FieldType.NumericType.INT);
    } else if (cqlType == CQL3Type.Native.VARINT || cqlType == CQL3Type.Native.BIGINT
            || cqlType == CQL3Type.Native.COUNTER) {
        luceneFieldType.setNumericType(FieldType.NumericType.LONG);
    } else if (cqlType == CQL3Type.Native.DECIMAL || cqlType == CQL3Type.Native.DOUBLE) {
        luceneFieldType.setNumericType(FieldType.NumericType.DOUBLE);
    } else if (cqlType == CQL3Type.Native.FLOAT) {
        luceneFieldType.setNumericType(FieldType.NumericType.FLOAT);
    } else if (cqlType == CQL3Type.Native.TIMESTAMP) {
        luceneFieldType.setNumericType(FieldType.NumericType.LONG);
    }/*from w ww.  ja  va2s . co m*/
}

From source file:com.tuplejump.stargate.Fields.java

License:Apache License

private static Field numericDocValuesField(String name, final AbstractType abstractType,
        final ByteBuffer byteBufferValue) {
    final String stripedName = striped + name;
    CQL3Type cqlType = abstractType.asCQL3Type();
    if (cqlType == CQL3Type.Native.TIMESTAMP) {
        Date date = (Date) abstractType.compose(byteBufferValue);
        return new NumericDocValuesField(stripedName, date.getTime());
    }//from  ww w  .  j  av  a  2  s .  c o  m
    Number value = (Number) abstractType.compose(byteBufferValue);
    if (cqlType == CQL3Type.Native.INT || cqlType == CQL3Type.Native.VARINT || cqlType == CQL3Type.Native.BIGINT
            || cqlType == CQL3Type.Native.COUNTER) {
        return new NumericDocValuesField(stripedName, value.longValue());
    } else if (cqlType == CQL3Type.Native.FLOAT) {
        return new FloatDocValuesField(stripedName, value.floatValue());
    } else if (cqlType == CQL3Type.Native.DECIMAL || cqlType == CQL3Type.Native.DOUBLE) {
        return new DoubleDocValuesField(stripedName, value.doubleValue());
    } else
        throw new IllegalArgumentException(String.format("Invalid type for numeric doc values <%s>", cqlType));
}

From source file:com.tuplejump.stargate.lucene.Properties.java

License:Apache License

public void setFromAbstractType(AbstractType type) {
    if (this.type != null)
        return;//  w w w.j a va  2  s .  c om
    CQL3Type cqlType = type.asCQL3Type();
    if (cqlType == CQL3Type.Native.INT) {
        this.type = Type.integer;
    } else if (cqlType == CQL3Type.Native.VARINT || cqlType == CQL3Type.Native.BIGINT
            || cqlType == CQL3Type.Native.COUNTER) {
        this.type = Type.bigint;
    } else if (cqlType == CQL3Type.Native.DECIMAL || cqlType == CQL3Type.Native.DOUBLE) {
        this.type = Type.bigdecimal;
    } else if (cqlType == CQL3Type.Native.FLOAT) {
        this.type = Type.decimal;
    } else if (cqlType == CQL3Type.Native.TEXT || cqlType == CQL3Type.Native.ASCII) {
        this.type = Type.text;
    } else if (cqlType == CQL3Type.Native.VARCHAR) {
        this.type = Type.string;
    } else if (cqlType == CQL3Type.Native.UUID) {
        this.type = Type.string;
    } else if (cqlType == CQL3Type.Native.TIMEUUID) {
        //TODO TimeUUID toString is not comparable.
        this.type = Type.string;
    } else if (cqlType == CQL3Type.Native.TIMESTAMP) {
        this.type = Type.date;
    } else if (cqlType == CQL3Type.Native.BOOLEAN) {
        this.type = Type.bool;
    } else {
        this.type = Type.text;
    }

}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public void buildObjectMapping(Map<String, Object> mapping, final AbstractType<?> type) throws IOException {
    CQL3Type cql3type = type.asCQL3Type();
    if (cql3type instanceof CQL3Type.Native) {
        mapping.put("type", cqlMapping.get(cql3type.toString()));
    } else if (cql3type instanceof CQL3Type.UserDefined) {
        mapping.put("type", ObjectMapper.NESTED_CONTENT_TYPE);
        mapping.put(TypeParsers.CQL_STRUCT, "udt");
        Map<String, Object> properties = Maps.newHashMap();
        TupleType tuple = (TupleType) type;
        for (int i = 0; i < tuple.size(); i++) {
            buildTypeMapping(properties, tuple.type(i));
        }//from  w  ww. j  a  v a  2 s  .  c o m
        mapping.put("properties", properties);
    }
}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public void buildTypeMapping(Map<String, Object> mapping, final AbstractType<?> type) throws IOException {
    CQL3Type cql3type = type.asCQL3Type();
    if (type.isCollection()) {
        if (type instanceof ListType) {
            mapping.put(TypeParsers.CQL_COLLECTION, "list");
            buildObjectMapping(mapping, ((ListType<?>) type).getElementsType());
        } else if (type instanceof SetType) {
            mapping.put(TypeParsers.CQL_COLLECTION, "set");
            buildObjectMapping(mapping, ((SetType<?>) type).getElementsType());
        } else if (type instanceof MapType) {
            MapType mtype = (MapType) type;
            if (mtype.getKeysType().asCQL3Type() == CQL3Type.Native.TEXT) {
                mapping.put(TypeParsers.CQL_COLLECTION, "singleton");
                mapping.put(TypeParsers.CQL_STRUCT, "map");
                mapping.put(TypeParsers.CQL_PARTIAL_UPDATE, Boolean.TRUE);
                mapping.put("type", ObjectMapper.NESTED_CONTENT_TYPE);
            } else {
                throw new IOException("Expecting a map<text,?>");
            }/*  ww  w .jav  a  2  s .c o m*/
        }
    } else {
        mapping.put(TypeParsers.CQL_COLLECTION, "singleton");
        buildObjectMapping(mapping, type);
    }
}