@Override public StoreTransaction beginTransaction(final BaseTransactionConfig config) { return new CassandraTransaction(config); }
@Override public String toString() { return "thriftCassandra" + super.toString(); }
public final Partitioner getPartitioner() { if (partitioner == null) { try { partitioner = Partitioner.getPartitioner(getCassandraPartitioner()); } catch (BackendException e) { throw new TitanException("Could not connect to Cassandra to read partitioner information. Please check the connection", e); } } assert partitioner != null; return partitioner; }
@Override public KeyIterator getKeys(KeyRangeQuery query, StoreTransaction txh) throws BackendException { // this query could only be done when byte-ordering partitioner is used // because Cassandra operates on tokens internally which means that even contiguous // range of keys (e.g. time slice) with random partitioner could produce disjoint set of tokens // returning ambiguous results to the user. Partitioner partitioner = storeManager.getPartitioner(); if (partitioner != Partitioner.BYTEORDER) throw new PermanentBackendException("getKeys(KeyRangeQuery could only be used with byte-ordering partitioner."); ByteBuffer start = query.getKeyStart().asByteBuffer(), end = query.getKeyEnd().asByteBuffer(); RowSliceQuery rowSlice = keyspace.prepareQuery(columnFamily) .setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()) .withRetryPolicy(retryPolicy.duplicate()) .getKeyRange(start, end, null, null, Integer.MAX_VALUE); // Astyanax is bad at builder pattern :( rowSlice.withColumnRange(query.getSliceStart().asByteBuffer(), query.getSliceEnd().asByteBuffer(), false, query.getLimit()); // Omit final the query's keyend from the result, if present in result final Rows<ByteBuffer, ByteBuffer> r; try { r = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) rowSlice.execute()).getResult(); } catch (ConnectionException e) { throw new TemporaryBackendException(e); } Iterator<Row<ByteBuffer, ByteBuffer>> i = Iterators.filter(r.iterator(), new KeySkipPredicate(query.getKeyEnd().asByteBuffer())); return new RowIterator(i, query); }
mutate(new ArrayList<org.apache.cassandra.db.Mutation>(rowMutations.values()), getTx(txh).getWriteConsistencyLevel().getDB()); sleepAfterWrite(txh, commitTime);
@Override public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws BackendException { MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch) .setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax()) .withRetryPolicy(retryPolicy.duplicate()); Preconditions.checkArgument(openStores.containsKey(storeName), "Store cannot be found: " + storeName); ColumnFamily<ByteBuffer, ByteBuffer> columnFamily = openStores.get(storeName).getColumnFamily(); sleepAfterWrite(txh, commitTime);
@Test public void testWriteConsistencyLevel() { int levelsChecked = 0; // Test whether CassandraTransaction honors the write consistency level option for (CLevel writeLevel : CLevel.values()) { StandardBaseTransactionConfig.Builder b = new StandardBaseTransactionConfig.Builder(); ModifiableConfiguration mc = GraphDatabaseConfiguration.buildGraphConfiguration(); mc.set(CASSANDRA_WRITE_CONSISTENCY, writeLevel.name()); b.customOptions(mc); b.timestampProvider(TimestampProviders.MICRO); CassandraTransaction ct = new CassandraTransaction(b.build()); assertEquals(writeLevel, ct.getWriteConsistencyLevel()); levelsChecked++; } // Sanity check: if CLevel.values was empty, something is wrong with the test Preconditions.checkState(0 < levelsChecked); }
@Test public void testReadConsistencyLevel() { int levelsChecked = 0; // Test whether CassandraTransaction honors the write consistency level option for (CLevel writeLevel : CLevel.values()) { StandardBaseTransactionConfig.Builder b = new StandardBaseTransactionConfig.Builder(); ModifiableConfiguration mc = GraphDatabaseConfiguration.buildGraphConfiguration(); mc.set(CASSANDRA_READ_CONSISTENCY, writeLevel.name()); b.timestampProvider(TimestampProviders.MICRO); b.customOptions(mc); CassandraTransaction ct = new CassandraTransaction(b.build()); assertEquals(writeLevel, ct.getReadConsistencyLevel()); levelsChecked++; } // Sanity check: if CLevel.values was empty, something is wrong with the test Preconditions.checkState(0 < levelsChecked); }
public CassandraTransaction(BaseTransactionConfig c) { super(c); read = CLevel.parse(getConfiguration().getCustomOption(CASSANDRA_READ_CONSISTENCY)); write = CLevel.parse(getConfiguration().getCustomOption(CASSANDRA_WRITE_CONSISTENCY)); log.debug("Created {}", this.toString()); }
@Test public void testTimestampProvider() { BaseTransactionConfig txcfg = StandardBaseTransactionConfig.of(TimestampProviders.NANO); CassandraTransaction ct = new CassandraTransaction(txcfg); assertEquals(TimestampProviders.NANO, ct.getConfiguration().getTimestampProvider()); txcfg = StandardBaseTransactionConfig.of(TimestampProviders.MICRO); ct = new CassandraTransaction(txcfg); assertEquals(TimestampProviders.MICRO, ct.getConfiguration().getTimestampProvider()); txcfg = StandardBaseTransactionConfig.of(TimestampProviders.MILLI); ct = new CassandraTransaction(txcfg); assertEquals(TimestampProviders.MILLI, ct.getConfiguration().getTimestampProvider()); } }
public static CLevel parse(String value) { Preconditions.checkArgument(value != null && !value.isEmpty()); value = value.trim(); if (value.equals("1")) return ONE; else if (value.equals("2")) return TWO; else if (value.equals("3")) return THREE; else { for (CLevel c : values()) { if (c.toString().equalsIgnoreCase(value) || ("CL_" + c.toString()).equalsIgnoreCase(value)) return c; } } throw new IllegalArgumentException("Unrecognized cassandra consistency level: " + value); } }
@Test public void testDisableCFCompressor() throws BackendException { final String cf = TEST_CF_NAME + "_nocompress"; ModifiableConfiguration config = getBaseStorageConfiguration(); config.set(AbstractCassandraStoreManager.CF_COMPRESSION,false); AbstractCassandraStoreManager mgr = openStorageManager(config); // N.B.: clearStorage() truncates CFs but does not delete them mgr.openDatabase(cf); assertEquals(Collections.emptyMap(), mgr.getCompressionOptions(cf)); }
switch (getPartitioner()) { case RANDOM: keyOrdered = false; throw new IllegalArgumentException("Unrecognized partitioner: " + getPartitioner()); switch (getDeployment()) { case REMOTE: fb.multiQuery(true); throw new IllegalArgumentException("Unrecognized deployment mode: " + getDeployment());
@Test public void testDefaultCFCompressor() throws BackendException { final String cf = TEST_CF_NAME + "_snappy"; AbstractCassandraStoreManager mgr = openStorageManager(); mgr.openDatabase(cf); Map<String, String> defaultCfCompressionOps = new ImmutableMap.Builder<String, String>() .put("sstable_compression", DEFAULT_COMPRESSOR_PACKAGE + "." + AbstractCassandraStoreManager.CF_COMPRESSION_TYPE.getDefaultValue()) .put("chunk_length_kb", "64") .build(); assertEquals(defaultCfCompressionOps, mgr.getCompressionOptions(cf)); }
@Override public AbstractCassandraStoreManager openStorageManager() throws BackendException { return openStorageManager(getBaseStorageConfiguration()); } }
public static Partitioner getPartitioner(IPartitioner partitioner) { return getPartitioner(partitioner.getClass().getSimpleName()); }
@Test public void testCustomCFCompressor() throws BackendException { final String cname = "DeflateCompressor"; final int ckb = 128; final String cf = TEST_CF_NAME + "_gzip"; ModifiableConfiguration config = getBaseStorageConfiguration(); config.set(AbstractCassandraStoreManager.CF_COMPRESSION_TYPE,cname); config.set(AbstractCassandraStoreManager.CF_COMPRESSION_BLOCK_SIZE,ckb); AbstractCassandraStoreManager mgr = openStorageManager(config); // N.B.: clearStorage() truncates CFs but does not delete them mgr.openDatabase(cf); final Map<String, String> expected = ImmutableMap .<String, String> builder() .put("sstable_compression", DEFAULT_COMPRESSOR_PACKAGE + "." + cname) .put("chunk_length_kb", String.valueOf(ckb)).build(); assertEquals(expected, mgr.getCompressionOptions(cf)); }
@Override public String toString() { return "embeddedCassandra" + super.toString(); }
@Override public String toString() { return "astyanax" + super.toString(); }