List of usage examples for org.apache.cassandra.cql3 CQL3Type getType
public AbstractType<?> getType();
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 ww w. j ava 2 s .c om * @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 www . j ava 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:kina.entity.CellValidator.java
License:Apache License
/** * Generates the cassandra marshaller ({@link org.apache.cassandra.db.marshal.AbstractType}) for this CellValidator. * * @return an instance of the cassandra marshaller for this CellValidator. *//*from w w w . j av a 2 s . co m*/ public AbstractType<?> getAbstractType() { if (abstractType != null) { return abstractType; } try { if (validatorKind == Kind.NOT_A_COLLECTION) { abstractType = AnnotationUtils.MAP_ABSTRACT_TYPE_CLASS_TO_ABSTRACT_TYPE .get(forName(validatorClassName)); } else { Iterator<String> types = validatorTypes.iterator(); switch (validatorKind) { case SET: CQL3Type cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase()); abstractType = SetType.getInstance(cql3Type.getType()); break; case LIST: cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase()); abstractType = ListType.getInstance(cql3Type.getType()); break; case MAP: cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase()); CQL3Type cql3Type2 = CQL3Type.Native.valueOf(types.next().toUpperCase()); abstractType = MapType.getInstance(cql3Type.getType(), cql3Type2.getType()); break; default: throw new GenericException("Cannot determine collection kind for " + validatorKind); } } } catch (ClassNotFoundException e) { throw new GenericException(e); } return abstractType; }