use of com.thinkaurelius.titan.diskstorage.PermanentStorageException 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.PermanentStorageException in project titan by thinkaurelius.
the class BerkeleyJEKeyValueStore method get.
@Override
public StaticBuffer get(StaticBuffer key, StoreTransaction txh) throws StorageException {
Transaction tx = getTransaction(txh);
try {
DatabaseEntry dbkey = key.as(ENTRY_FACTORY);
DatabaseEntry data = new DatabaseEntry();
OperationStatus status = db.get(tx, dbkey, data, LockMode.DEFAULT);
if (status == OperationStatus.SUCCESS) {
return getBuffer(data);
} else {
return null;
}
} catch (DatabaseException e) {
throw new PermanentStorageException(e);
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class BerkeleyJEKeyValueStore method delete.
@Override
public void delete(StaticBuffer key, StoreTransaction txh) throws StorageException {
log.trace("Deletion");
Transaction tx = getTransaction(txh);
try {
OperationStatus status = db.delete(tx, key.as(ENTRY_FACTORY));
if (status != OperationStatus.SUCCESS) {
throw new PermanentStorageException("Could not remove: " + status);
}
} catch (DatabaseException e) {
throw new PermanentStorageException(e);
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class BerkeleyJEStoreManager method initialize.
private void initialize(int cachePercent) throws StorageException {
try {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(transactional);
envConfig.setCachePercent(cachePercent);
if (batchLoading) {
envConfig.setConfigParam(EnvironmentConfig.ENV_RUN_CHECKPOINTER, "false");
envConfig.setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, "false");
}
// Open the environment
environment = new Environment(directory, envConfig);
} catch (DatabaseException e) {
throw new PermanentStorageException("Error during BerkeleyJE initialization: ", e);
}
}
use of com.thinkaurelius.titan.diskstorage.PermanentStorageException in project titan by thinkaurelius.
the class BerkeleyJEStoreManager method openDatabase.
@Override
public BerkeleyJEKeyValueStore openDatabase(String name) throws StorageException {
Preconditions.checkNotNull(name);
if (stores.containsKey(name)) {
BerkeleyJEKeyValueStore store = stores.get(name);
return store;
}
try {
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setReadOnly(isReadOnly);
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(transactional);
dbConfig.setKeyPrefixing(true);
if (batchLoading) {
dbConfig.setDeferredWrite(true);
}
Database db = environment.openDatabase(null, name, dbConfig);
BerkeleyJEKeyValueStore store = new BerkeleyJEKeyValueStore(name, db, this);
stores.put(name, store);
return store;
} catch (DatabaseException e) {
throw new PermanentStorageException("Could not open BerkeleyJE data store", e);
}
}
Aggregations