use of com.thinkaurelius.titan.diskstorage.BackendException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method getCompressionOptions.
@Override
public Map<String, String> getCompressionOptions(String cf) throws BackendException {
CTConnection conn = null;
Map<String, String> result = null;
try {
conn = pool.borrowObject(keySpaceName);
Cassandra.Client client = conn.getClient();
KsDef ksDef = client.describe_keyspace(keySpaceName);
for (CfDef cfDef : ksDef.getCf_defs()) {
if (null != cfDef && cfDef.getName().equals(cf)) {
result = cfDef.getCompression_options();
break;
}
}
return result;
} catch (InvalidRequestException e) {
log.debug("Keyspace {} does not exist", keySpaceName);
return null;
} catch (Exception e) {
throw new TemporaryBackendException(e);
} finally {
pool.returnObjectUnsafe(keySpaceName, conn);
}
}
use of com.thinkaurelius.titan.diskstorage.BackendException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method mutateMany.
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
Preconditions.checkNotNull(mutations);
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
ConsistencyLevel consistency = getTx(txh).getWriteConsistencyLevel().getThrift();
// Generate Thrift-compatible batch_mutate() datastructure
// key -> cf -> cassmutation
int size = 0;
for (Map<StaticBuffer, KCVMutation> mutation : mutations.values()) size += mutation.size();
Map<ByteBuffer, Map<String, List<org.apache.cassandra.thrift.Mutation>>> batch = new HashMap<ByteBuffer, Map<String, List<org.apache.cassandra.thrift.Mutation>>>(size);
for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> keyMutation : mutations.entrySet()) {
String columnFamily = keyMutation.getKey();
for (Map.Entry<StaticBuffer, KCVMutation> mutEntry : keyMutation.getValue().entrySet()) {
ByteBuffer keyBB = mutEntry.getKey().asByteBuffer();
// Get or create the single Cassandra Mutation object responsible for this key
Map<String, List<org.apache.cassandra.thrift.Mutation>> cfmutation = batch.get(keyBB);
if (cfmutation == null) {
// Most mutations only modify the edgeStore and indexStore
cfmutation = new HashMap<String, List<org.apache.cassandra.thrift.Mutation>>(3);
batch.put(keyBB, cfmutation);
}
KCVMutation mutation = mutEntry.getValue();
List<org.apache.cassandra.thrift.Mutation> thriftMutation = new ArrayList<org.apache.cassandra.thrift.Mutation>(mutations.size());
if (mutation.hasDeletions()) {
for (StaticBuffer buf : mutation.getDeletions()) {
Deletion d = new Deletion();
SlicePredicate sp = new SlicePredicate();
sp.addToColumn_names(buf.as(StaticBuffer.BB_FACTORY));
d.setPredicate(sp);
d.setTimestamp(commitTime.getDeletionTime(times));
org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
m.setDeletion(d);
thriftMutation.add(m);
}
}
if (mutation.hasAdditions()) {
for (Entry ent : mutation.getAdditions()) {
ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
Column column = new Column(ent.getColumnAs(StaticBuffer.BB_FACTORY));
column.setValue(ent.getValueAs(StaticBuffer.BB_FACTORY));
column.setTimestamp(commitTime.getAdditionTime(times));
Integer ttl = (Integer) ent.getMetaData().get(EntryMetaData.TTL);
if (null != ttl && ttl > 0) {
column.setTtl(ttl);
}
cosc.setColumn(column);
org.apache.cassandra.thrift.Mutation m = new org.apache.cassandra.thrift.Mutation();
m.setColumn_or_supercolumn(cosc);
thriftMutation.add(m);
}
}
cfmutation.put(columnFamily, thriftMutation);
}
}
CTConnection conn = null;
try {
conn = pool.borrowObject(keySpaceName);
Cassandra.Client client = conn.getClient();
if (atomicBatch) {
client.atomic_batch_mutate(batch, consistency);
} else {
client.batch_mutate(batch, consistency);
}
} catch (Exception ex) {
throw CassandraThriftKeyColumnValueStore.convertException(ex);
} finally {
pool.returnObjectUnsafe(keySpaceName, conn);
}
sleepAfterWrite(txh, commitTime);
}
use of com.thinkaurelius.titan.diskstorage.BackendException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method ensureKeyspaceExists.
private KsDef ensureKeyspaceExists(String keyspaceName) throws TException, BackendException {
CTConnection connection = null;
try {
connection = pool.borrowObject(SYSTEM_KS);
Cassandra.Client client = connection.getClient();
try {
// Side effect: throws Exception if keyspaceName doesn't exist
// Don't remove
client.set_keyspace(keyspaceName);
client.set_keyspace(SYSTEM_KS);
log.debug("Found existing keyspace {}", keyspaceName);
} catch (InvalidRequestException e) {
// Keyspace didn't exist; create it
log.debug("Creating keyspace {}...", keyspaceName);
KsDef ksdef = new KsDef().setName(keyspaceName).setCf_defs(// cannot be null but can be empty
new LinkedList<CfDef>()).setStrategy_class(storageConfig.get(REPLICATION_STRATEGY)).setStrategy_options(strategyOptions);
client.set_keyspace(SYSTEM_KS);
try {
client.system_add_keyspace(ksdef);
retrySetKeyspace(keyspaceName, client);
log.debug("Created keyspace {}", keyspaceName);
} catch (InvalidRequestException ire) {
log.error("system_add_keyspace failed for keyspace=" + keyspaceName, ire);
throw ire;
}
}
return client.describe_keyspace(keyspaceName);
} catch (Exception e) {
throw new TemporaryBackendException(e);
} finally {
pool.returnObjectUnsafe(SYSTEM_KS, connection);
}
}
use of com.thinkaurelius.titan.diskstorage.BackendException in project titan by thinkaurelius.
the class KCVSConfiguration method set.
public <O> void set(String key, O value, O expectedValue, final boolean checkExpectedValue) {
final StaticBuffer column = string2StaticBuffer(key);
final List<Entry> additions;
final List<StaticBuffer> deletions;
if (value != null) {
//Addition
additions = new ArrayList<Entry>(1);
deletions = KeyColumnValueStore.NO_DELETIONS;
StaticBuffer val = object2StaticBuffer(value);
additions.add(StaticArrayEntry.of(column, val));
} else {
//Deletion
additions = KeyColumnValueStore.NO_ADDITIONS;
deletions = Lists.newArrayList(column);
}
final StaticBuffer expectedValueBuffer;
if (checkExpectedValue && expectedValue != null) {
expectedValueBuffer = object2StaticBuffer(expectedValue);
} else {
expectedValueBuffer = null;
}
BackendOperation.execute(new BackendOperation.Transactional<Boolean>() {
@Override
public Boolean call(StoreTransaction txh) throws BackendException {
if (checkExpectedValue)
store.acquireLock(rowKey, column, expectedValueBuffer, txh);
store.mutate(rowKey, additions, deletions, txh);
return true;
}
@Override
public String toString() {
return "setConfiguration";
}
}, txProvider, times, maxOperationWaitTime);
}
use of com.thinkaurelius.titan.diskstorage.BackendException in project titan by thinkaurelius.
the class TitanGraphBaseTest method openLog.
private Log openLog(String logManagerName, String logName) {
try {
ModifiableConfiguration configuration = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, config.copy(), BasicConfiguration.Restriction.NONE);
configuration.set(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID, "reader");
configuration.set(GraphDatabaseConfiguration.LOG_READ_INTERVAL, Duration.ofMillis(500L), logManagerName);
if (logStoreManager == null) {
logStoreManager = Backend.getStorageManager(configuration);
}
StoreFeatures f = logStoreManager.getFeatures();
boolean part = f.isDistributed() && f.isKeyOrdered();
if (part) {
for (String logname : new String[] { USER_LOG, TRANSACTION_LOG, MANAGEMENT_LOG }) configuration.set(KCVSLogManager.LOG_MAX_PARTITIONS, 8, logname);
}
assert logStoreManager != null;
if (!logManagers.containsKey(logManagerName)) {
//Open log manager - only supports KCVSLog
Configuration logConfig = configuration.restrictTo(logManagerName);
Preconditions.checkArgument(logConfig.get(LOG_BACKEND).equals(LOG_BACKEND.getDefaultValue()));
logManagers.put(logManagerName, new KCVSLogManager(logStoreManager, logConfig));
}
assert logManagers.containsKey(logManagerName);
return logManagers.get(logManagerName).openLog(logName);
} catch (BackendException e) {
throw new TitanException("Could not open log: " + logName, e);
}
}
Aggregations