use of com.sleepycat.je.Transaction in project crawler4j by yasserg.
the class WorkQueues method get.
public List<WebURL> get(int max) {
synchronized (mutex) {
List<WebURL> results = new ArrayList<>(max);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
Transaction txn = beginTransaction();
try (Cursor cursor = openCursor(txn)) {
OperationStatus result = cursor.getFirst(key, value, null);
int matches = 0;
while ((matches < max) && (result == OperationStatus.SUCCESS)) {
if (value.getData().length > 0) {
results.add(webURLBinding.entryToObject(value));
matches++;
}
result = cursor.getNext(key, value, null);
}
}
commit(txn);
return results;
}
}
use of com.sleepycat.je.Transaction in project crawler4j by yasserg.
the class WorkQueues method delete.
public void delete(int count) {
synchronized (mutex) {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
Transaction txn = beginTransaction();
try (Cursor cursor = openCursor(txn)) {
OperationStatus result = cursor.getFirst(key, value, null);
int matches = 0;
while ((matches < count) && (result == OperationStatus.SUCCESS)) {
cursor.delete();
matches++;
result = cursor.getNext(key, value, null);
}
}
commit(txn);
}
}
use of com.sleepycat.je.Transaction in project qpid-broker-j by apache.
the class AbstractBDBMessageStore method removeMessage.
void removeMessage(long messageId, boolean sync) throws StoreException {
boolean complete = false;
Transaction tx = null;
int attempts = 0;
try {
do {
tx = null;
try {
tx = getEnvironmentFacade().beginTransaction(null);
// remove the message meta data from the store
DatabaseEntry key = new DatabaseEntry();
LongBinding.longToEntry(messageId, key);
getLogger().debug("Removing message id {}", messageId);
OperationStatus status = getMessageMetaDataDb().delete(tx, key);
if (status == OperationStatus.NOTFOUND) {
getLogger().debug("Message id {} not found (attempt to remove failed - probably application initiated rollback)", messageId);
}
getLogger().debug("Deleted metadata for message {}", messageId);
// now remove the content data from the store if there is any.
DatabaseEntry contentKeyEntry = new DatabaseEntry();
LongBinding.longToEntry(messageId, contentKeyEntry);
getMessageContentDb().delete(tx, contentKeyEntry);
getLogger().debug("Deleted content for message {}", messageId);
getEnvironmentFacade().commit(tx, sync);
complete = true;
tx = null;
} catch (LockConflictException e) {
try {
if (tx != null) {
tx.abort();
}
} catch (RuntimeException e2) {
getLogger().warn("Unable to abort transaction after LockConflictException on removal of message with id {}", messageId, e2);
// been logged.
throw getEnvironmentFacade().handleDatabaseException("Cannot remove message with id " + messageId, e);
}
sleepOrThrowOnLockConflict(attempts++, "Cannot remove messages", e);
}
} while (!complete);
} catch (RuntimeException e) {
getLogger().error("Unexpected BDB exception", e);
try {
abortTransactionSafely(tx, getEnvironmentFacade());
} finally {
tx = null;
}
throw getEnvironmentFacade().handleDatabaseException("Error removing message with id " + messageId + " from database: " + e.getMessage(), e);
} finally {
try {
abortTransactionSafely(tx, getEnvironmentFacade());
} finally {
tx = null;
}
}
}
use of com.sleepycat.je.Transaction in project qpid-broker-j by apache.
the class AbstractBDBPreferenceStore method updateOrCreate.
@Override
public void updateOrCreate(final Collection<PreferenceRecord> preferenceRecords) {
_useOrCloseRWLock.readLock().lock();
try {
if (!getStoreState().equals(StoreState.OPENED)) {
throw new IllegalStateException("PreferenceStore is not opened");
}
if (preferenceRecords.isEmpty()) {
return;
}
EnvironmentFacade environmentFacade = getEnvironmentFacade();
Transaction txn = null;
try {
txn = environmentFacade.beginTransaction(null);
updateOrCreateInternal(txn, preferenceRecords);
txn.commit();
txn = null;
} catch (RuntimeException e) {
throw environmentFacade.handleDatabaseException("Error on preferences updateOrCreate: " + e.getMessage(), e);
} finally {
if (txn != null) {
abortTransactionSafely(txn, environmentFacade);
}
}
} finally {
_useOrCloseRWLock.readLock().unlock();
}
}
use of com.sleepycat.je.Transaction in project qpid-broker-j by apache.
the class AbstractBDBPreferenceStore method removeAndAdd.
private void removeAndAdd(final Collection<UUID> preferenceRecordsToRemove, final Collection<PreferenceRecord> preferenceRecordsToAdd, final Action<Transaction> preCommitAction) {
_useOrCloseRWLock.readLock().lock();
try {
final StoreState storeState = getStoreState();
if (!storeState.equals(StoreState.OPENED)) {
throw new IllegalStateException(String.format("PreferenceStore is not opened. Actual state : %s", storeState));
}
if (preferenceRecordsToRemove.isEmpty() && preferenceRecordsToAdd.isEmpty()) {
return;
}
EnvironmentFacade environmentFacade = getEnvironmentFacade();
Transaction txn = null;
try {
txn = environmentFacade.beginTransaction(null);
Database preferencesDb = getPreferencesDb();
DatabaseEntry key = new DatabaseEntry();
UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
for (UUID id : preferenceRecordsToRemove) {
getLogger().debug("Removing preference {}", id);
keyBinding.objectToEntry(id, key);
OperationStatus status = preferencesDb.delete(txn, key);
if (status == OperationStatus.NOTFOUND) {
getLogger().debug("Preference {} not found", id);
}
}
updateOrCreateInternal(txn, preferenceRecordsToAdd);
if (preCommitAction != null) {
preCommitAction.performAction(txn);
}
txn.commit();
txn = null;
} catch (RuntimeException e) {
throw environmentFacade.handleDatabaseException("Error on replacing of preferences: " + e.getMessage(), e);
} finally {
if (txn != null) {
abortTransactionSafely(txn, environmentFacade);
}
}
} finally {
_useOrCloseRWLock.readLock().unlock();
}
}
Aggregations