use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class AstyanaxStoreManager method getRetryPolicy.
private static RetryPolicy getRetryPolicy(String serializedRetryPolicy) throws StorageException {
String[] tokens = serializedRetryPolicy.split(",");
String policyClassName = tokens[0];
int argCount = tokens.length - 1;
Integer[] args = new Integer[argCount];
for (int i = 1; i < tokens.length; i++) {
args[i - 1] = Integer.valueOf(tokens[i]);
}
try {
RetryPolicy rp = instantiate(policyClassName, args, serializedRetryPolicy);
log.debug("Instantiated RetryPolicy object {} from config string \"{}\"", rp, serializedRetryPolicy);
return rp;
} catch (Exception e) {
throw new PermanentStorageException("Failed to instantiate Astyanax Retry Policy class", e);
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class BerkeleyJEKeyValueStore method getSlice.
@Override
public RecordIterator<KeyValueEntry> getSlice(StaticBuffer keyStart, StaticBuffer keyEnd, KeySelector selector, StoreTransaction txh) throws StorageException {
log.trace("Get slice query");
Transaction tx = getTransaction(txh);
Cursor cursor = null;
final List<KeyValueEntry> result = new ArrayList<KeyValueEntry>();
try {
DatabaseEntry foundKey = keyStart.as(ENTRY_FACTORY);
DatabaseEntry foundData = new DatabaseEntry();
cursor = db.openCursor(tx, null);
OperationStatus status = cursor.getSearchKeyRange(foundKey, foundData, LockMode.DEFAULT);
// Iterate until given condition is satisfied or end of records
while (status == OperationStatus.SUCCESS) {
StaticBuffer key = getBuffer(foundKey);
if (key.compareTo(keyEnd) >= 0)
break;
if (selector.include(key)) {
result.add(new KeyValueEntry(key, getBuffer(foundData)));
}
if (selector.reachedLimit())
break;
status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
}
log.trace("Retrieved: {}", result.size());
return new RecordIterator<KeyValueEntry>() {
private final Iterator<KeyValueEntry> entries = result.iterator();
@Override
public boolean hasNext() {
return entries.hasNext();
}
@Override
public KeyValueEntry next() {
return entries.next();
}
@Override
public void close() {
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
} catch (Exception e) {
throw new PermanentStorageException(e);
} finally {
try {
if (cursor != null)
cursor.close();
} catch (Exception e) {
throw new PermanentStorageException(e);
}
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class ElasticSearchIndex method clearStorage.
@Override
public void clearStorage() throws StorageException {
try {
try {
client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();
// We wait for one second to let ES delete the river
Thread.sleep(1000);
} catch (IndexMissingException e) {
// Index does not exist... Fine
}
} catch (Exception e) {
throw new PermanentStorageException("Could not delete index " + indexName, e);
} finally {
close();
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class ElasticSearchIndex method mutate.
@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
BulkRequestBuilder brb = client.prepareBulk();
int bulkrequests = 0;
try {
for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
String storename = stores.getKey();
for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
String docid = entry.getKey();
IndexMutation mutation = entry.getValue();
Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
// Deletions first
if (mutation.hasDeletions()) {
if (mutation.isDeleted()) {
log.trace("Deleting entire document {}", docid);
brb.add(new DeleteRequest(indexName, storename, docid));
bulkrequests++;
} else {
Set<String> deletions = Sets.newHashSet(mutation.getDeletions());
if (mutation.hasAdditions()) {
for (IndexEntry ie : mutation.getAdditions()) {
deletions.remove(ie.key);
}
}
if (!deletions.isEmpty()) {
// TODO make part of batch mutation if/when possible
StringBuilder script = new StringBuilder();
for (String key : deletions) {
script.append("ctx._source.remove(\"" + key + "\"); ");
}
log.trace("Deleting individual fields [{}] for document {}", deletions, docid);
client.prepareUpdate(indexName, storename, docid).setScript(script.toString()).execute().actionGet();
}
}
}
if (mutation.hasAdditions()) {
if (mutation.isNew()) {
// Index
log.trace("Adding entire document {}", docid);
brb.add(new IndexRequest(indexName, storename, docid).source(getContent(mutation.getAdditions())));
bulkrequests++;
} else {
// Update: TODO make part of batch mutation if/when possible
boolean needUpsert = !mutation.hasDeletions();
XContentBuilder builder = getContent(mutation.getAdditions());
UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setDoc(builder);
if (needUpsert)
update.setUpsert(builder);
log.trace("Updating document {} with upsert {}", docid, needUpsert);
update.execute().actionGet();
}
}
}
}
if (bulkrequests > 0)
brb.execute().actionGet();
} catch (Exception e) {
throw convert(e);
}
}
use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.
the class CassandraThriftStoreManager method ensureColumnFamilyExists.
private void ensureColumnFamilyExists(String ksName, String cfName, String comparator) throws StorageException {
CTConnection conn = null;
try {
KsDef keyspaceDef = ensureKeyspaceExists(ksName);
conn = pool.borrowObject(ksName);
Cassandra.Client client = conn.getClient();
log.debug("Looking up metadata on keyspace {}...", ksName);
boolean foundColumnFamily = false;
for (CfDef cfDef : keyspaceDef.getCf_defs()) {
String curCfName = cfDef.getName();
if (curCfName.equals(cfName))
foundColumnFamily = true;
}
if (!foundColumnFamily) {
createColumnFamily(client, ksName, cfName, comparator);
} else {
log.debug("Keyspace {} and ColumnFamily {} were found.", ksName, cfName);
}
} catch (SchemaDisagreementException e) {
throw new TemporaryStorageException(e);
} catch (Exception e) {
throw new PermanentStorageException(e);
} finally {
pool.returnObjectUnsafe(ksName, conn);
}
}
Aggregations