AstyanaxKeyColumnValueStore(String columnFamilyName, Keyspace keyspace, AstyanaxStoreManager storeManager, RetryPolicy retryPolicy) { this.keyspace = keyspace; this.columnFamilyName = columnFamilyName; this.retryPolicy = retryPolicy; this.storeManager = storeManager; entryGetter = new AstyanaxGetter(storeManager.getMetaDataSchema(columnFamilyName)); columnFamily = new ColumnFamily<ByteBuffer, ByteBuffer>( this.columnFamilyName, ByteBufferSerializer.get(), ByteBufferSerializer.get()); }
/** * Check if the column family exists. If it dosn't create it */ private void testAndCreateColumnFamilyDef( MultiTenantColumnFamilyDefinition columnFamily ) throws ConnectionException { final KeyspaceDefinition keyspaceDefinition = keyspace.describeKeyspace(); final ColumnFamilyDefinition existing = keyspaceDefinition.getColumnFamily( columnFamily.getColumnFamily().getName() ); if ( existing != null ) { logger.info("Not creating columnfamily {}, it already exists.", columnFamily.getColumnFamily().getName()); return; } keyspace.createColumnFamily( columnFamily.getColumnFamily(), columnFamily.getOptions() ); // the CF def creation uses Asytanax, so manually check the schema agreement astyanaxWaitForSchemaAgreement(); logger.info( "Created column family {}", columnFamily.getColumnFamily().getName() ); }
private ColumnFamily getLocksColumnFamily() { if ( columnFamily == null ) { columnFamily = ColumnFamily.newColumnFamily( CF_NAME, StringSerializer.get(), StringSerializer.get() ); if ( logger.isDebugEnabled() ) { try { final KeyspaceDefinition kd = keyspace.describeKeyspace(); final ColumnFamilyDefinition cfd = kd.getColumnFamily( columnFamily.getName() ); Map<String, Object> options = new HashMap<>( 1 ); options.put( "gc_grace_seconds", cfd.getGcGraceSeconds() ); options.put( "caching", cfd.getCaching() ); options.put( "compaction_strategy", cfd.getCompactionStrategy() ); options.put( "compaction_strategy_options", cfd.getCompactionStrategyOptions() ); logger.debug( "Locks column family {} exists with options: {}", cfd.getName(), options); } catch ( ConnectionException ce ) { logger.warn("Error connecting to Cassandra for debug column family info", ce); } } } return columnFamily; }
public CqlColumnFamilyDefinitionImpl(Session session, String keyspace, ColumnFamily<?, ?> columnFamily, Map<String, Object> options) { this.session = session; Preconditions.checkArgument(columnFamily != null, "ColumnFamily cannot be null"); if (options == null) { options = new HashMap<String, Object>(); } keyspaceName = keyspace; cfName = columnFamily.getName(); optionsMap.put("key_validator", columnFamily.getKeySerializer().getComparatorType().getClassName()); optionsMap.put("comparator", columnFamily.getColumnSerializer().getComparatorType().getClassName()); optionsMap.put("default_validator", columnFamily.getDefaultValueSerializer().getComparatorType().getClassName()); if (columnFamily.getColumnSerializer() instanceof AnnotatedCompositeSerializer) { compositeSerializer = (AnnotatedCompositeSerializer<?>) columnFamily.getColumnSerializer(); } initFromMap(options); }
protected <C> ColumnFamily<ByteBuffer, C> getColumnFamily(KeyspaceDefinition keyspaceDef, String prefix, String suffix, String placement, Serializer<C> columnSerializer) throws IllegalArgumentException { // Create the column family object. It must be keyed by a ByteBuffer because that's what // the AstyanaxTable.getRowKey() method returns. ColumnFamily<ByteBuffer, C> cf = new ColumnFamily<>(prefix + "_" + suffix, ByteBufferSerializer.get(), columnSerializer); // Verify that the column family exists in the Cassandra schema. ColumnFamilyDefinition cfDef = keyspaceDef.getColumnFamily(cf.getName()); if (cfDef == null) { throw new UnknownPlacementException(format( "Placement string '%s' refers to unknown Cassandra %s column family in keyspace '%s': %s", placement, suffix, keyspaceDef.getName(), cf.getName()), placement); } return cf; } }
public ReverseIndexQuery(Keyspace ks, ColumnFamily<K, C> cfData, String indexCf, Serializer<V> valSerializer) { this.ks = ks; this.cfData = cfData; this.valSerializer = valSerializer; this.startValue = null; this.endValue = null; this.cfIndex = ColumnFamily.newColumnFamily(indexCf, ByteBufferSerializer.get(), ByteBufferSerializer.get()); }
timestamp = clock.getCurrentTime(); ByteBuffer bbKey = columnFamily.getKeySerializer().toByteBuffer(rowKey); if (!bbKey.hasRemaining()) { throw new RuntimeException("Row key cannot be empty"); KeyAndColumnFamily kacf = new KeyAndColumnFamily(columnFamily.getName(), bbKey); ColumnListMutation<C> clm = (ColumnListMutation<C>) rowLookup.get(kacf); if (clm == null) { List<Mutation> innerMutationList = innerMutationMap.get(columnFamily.getName()); if (innerMutationList == null) { innerMutationList = Lists.newArrayList(); innerMutationMap.put(columnFamily.getName(), innerMutationList); clm = new ThriftColumnFamilyMutationImpl<C>(timestamp, innerMutationList, columnFamily.getColumnSerializer()); rowLookup.put(kacf, clm);
@Override public <K, C> ColumnListMutation<C> withRow(ColumnFamily<K, C> columnFamily, K rowKey) { Preconditions.checkNotNull(columnFamily, "columnFamily cannot be null"); Preconditions.checkNotNull(rowKey, "Row key cannot be null"); ByteBuffer bbKey = columnFamily.getKeySerializer().toByteBuffer(rowKey); if (!bbKey.hasRemaining()) { throw new RuntimeException("Row key cannot be empty"); } KeyAndColumnFamily kacf = new KeyAndColumnFamily(columnFamily.getName(), bbKey); ColumnListMutation<C> clm = (ColumnListMutation<C>) rowLookup.get(kacf); if (clm == null) { Map<String, ColumnListMutation<?>> innerMutationMap = mutationMap.get(bbKey); if (innerMutationMap == null) { innerMutationMap = Maps.newHashMap(); mutationMap.put(bbKey, innerMutationMap); } ColumnListMutation<?> innerMutationList = innerMutationMap.get(columnFamily.getName()); if (innerMutationList == null) { innerMutationList = createColumnListMutation(keyspace, columnFamily, rowKey); innerMutationMap.put(columnFamily.getName(), innerMutationList); } rowLookup.put(kacf, innerMutationList); clm = (ColumnListMutation<C>) innerMutationList; } return clm; }
public String getDeleteEntireRowQuery() { ColumnFamily<?,?> cf = cfContext.getColumnFamily(); CqlColumnFamilyDefinitionImpl cfDef = (CqlColumnFamilyDefinitionImpl)cf.getColumnFamilyDefinition(); return "DELETE FROM " + ksContext.getKeyspace() + "." + cf.getName() + " WHERE " + cfDef.getPartitionKeyColumnDefinition().getName() + " = ?;"; }
@Override public <K, C> Rows<K, C> getRows(ColumnFamily<K, C> columnFamily) { if (!result.isSetRows()) throw new RuntimeException("CQL reponse doesn't contain rows"); return new ThriftCqlRowsImpl<K, C>(result.getRows(), columnFamily.getKeySerializer(), columnFamily.getColumnSerializer()); }
/** * Generate the expire time value to put in the column value. * @param timeout */ private ByteBuffer generateTimeoutValue(long timeout) { if (columnFamily.getDefaultValueSerializer() == ByteBufferSerializer.get() || columnFamily.getDefaultValueSerializer() == LongSerializer.get()) { return LongSerializer.get().toByteBuffer(timeout); } else { return columnFamily.getDefaultValueSerializer().fromString(Long.toString(timeout)); } }
@Override public IndexQuery<K, C> withColumnRange(C startColumn, C endColumn, boolean reversed, int count) { predicate.setSlice_range(ThriftUtils.createSliceRange(columnFamily.getColumnSerializer(), startColumn, endColumn, reversed, count)); return this; }
private Object getRowKey(com.datastax.driver.core.Row row, ColumnFamily<K, C> cf) { return CqlTypeMapping.getDynamicColumn(row, cf.getKeySerializer(), 0, cf); } }
public CqlRowListIterator(ResultSet rs, ColumnFamily<K,C> cf) { this.resultSet = rs; this.cf = cf; this.keySerializer = cf.getKeySerializer(); CqlColumnFamilyDefinitionImpl cfDef = (CqlColumnFamilyDefinitionImpl) cf.getColumnFamilyDefinition(); this.isClusteringKey = cfDef.getClusteringKeyColumnDefinitionList().size() > 0; }
/** * Convert a Map of options to an internal thrift column family definition * @param options */ private ThriftColumnFamilyDefinitionImpl toThriftColumnFamilyDefinition(Map<String, Object> options, ColumnFamily columnFamily) { ThriftColumnFamilyDefinitionImpl def = new ThriftColumnFamilyDefinitionImpl(); Map<String, Object> internalOptions = Maps.newHashMap(); if (options != null) internalOptions.putAll(options); internalOptions.put("keyspace", getKeyspaceName()); if (columnFamily != null) { internalOptions.put("name", columnFamily.getName()); if (!internalOptions.containsKey("comparator_type")) internalOptions.put("comparator_type", columnFamily.getColumnSerializer().getComparatorType().getTypeName()); if (!internalOptions.containsKey("key_validation_class")) internalOptions.put("key_validation_class", columnFamily.getKeySerializer().getComparatorType().getTypeName()); if (columnFamily.getDefaultValueSerializer() != null && !internalOptions.containsKey("default_validation_class")) internalOptions.put("default_validation_class", columnFamily.getDefaultValueSerializer().getComparatorType().getTypeName()); } def.setFields(internalOptions); return def; }
public AstyanaxCheckpointManager(Keyspace keyspace, String columnFamily, ByteBuffer bbKey) { this.keyspace = keyspace; this.bbKey = bbKey; this.columnFamily = ColumnFamily.newColumnFamily(columnFamily, ByteBufferSerializer.get(), StringSerializer.get()); }
timestamp = clock.getCurrentTime(); ByteBuffer bbKey = columnFamily.getKeySerializer().toByteBuffer(rowKey); if (!bbKey.hasRemaining()) { throw new RuntimeException("Row key cannot be empty"); KeyAndColumnFamily kacf = new KeyAndColumnFamily(columnFamily.getName(), bbKey); ColumnListMutation<C> clm = (ColumnListMutation<C>) rowLookup.get(kacf); if (clm == null) { List<Mutation> innerMutationList = innerMutationMap.get(columnFamily.getName()); if (innerMutationList == null) { innerMutationList = Lists.newArrayList(); innerMutationMap.put(columnFamily.getName(), innerMutationList); clm = new ThriftColumnFamilyMutationImpl<C>(timestamp, innerMutationList, columnFamily.getColumnSerializer()); rowLookup.put(kacf, clm);
@Override public <K, C> Rows<K, C> getRows(ColumnFamily<K, C> columnFamily) { if (!result.isSetRows()) throw new RuntimeException("CQL reponse doesn't contain rows"); return new ThriftCqlRowsImpl<K, C>(result.getRows(), columnFamily.getKeySerializer(), columnFamily.getColumnSerializer()); }
/** * Generate the expire time value to put in the column value. * @param timeout */ private ByteBuffer generateTimeoutValue(long timeout) { if (columnFamily.getDefaultValueSerializer() == ByteBufferSerializer.get() || columnFamily.getDefaultValueSerializer() == LongSerializer.get()) { return LongSerializer.get().toByteBuffer(timeout); } else { return columnFamily.getDefaultValueSerializer().fromString(Long.toString(timeout)); } }
@Override public IndexOperationExpression<K, C> whereColumn(C columnName) { internalExpression.setColumn_name(columnFamily.getColumnSerializer().toBytes(columnName)); return this; }