Search in sources :

Example 6 with Transaction

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;
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) ArrayList(java.util.ArrayList) WebURL(edu.uci.ics.crawler4j.url.WebURL) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor)

Example 7 with Transaction

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);
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor)

Example 8 with Transaction

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;
        }
    }
}
Also used : PreparedTransaction(org.apache.qpid.server.store.berkeleydb.entry.PreparedTransaction) Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) LockConflictException(com.sleepycat.je.LockConflictException) DatabaseEntry(com.sleepycat.je.DatabaseEntry)

Example 9 with Transaction

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();
    }
}
Also used : Transaction(com.sleepycat.je.Transaction)

Example 10 with Transaction

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();
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) Database(com.sleepycat.je.Database) UUIDTupleBinding(org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding) DatabaseEntry(com.sleepycat.je.DatabaseEntry) UUID(java.util.UUID)

Aggregations

Transaction (com.sleepycat.je.Transaction)74 DatabaseEntry (com.sleepycat.je.DatabaseEntry)55 Database (com.sleepycat.je.Database)35 OperationStatus (com.sleepycat.je.OperationStatus)20 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)12 UUID (java.util.UUID)11 Cursor (com.sleepycat.je.Cursor)10 DatabaseConfig (com.sleepycat.je.DatabaseConfig)7 DatabaseException (com.sleepycat.je.DatabaseException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Environment (com.sleepycat.je.Environment)5 HashMap (java.util.HashMap)5 FieldTable (org.apache.qpid.server.protocol.v0_8.FieldTable)5 NewPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.NewPreparedTransaction)5 OldPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.OldPreparedTransaction)5 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)4 StoreException (org.apache.qpid.server.store.StoreException)4 TupleOutput (com.sleepycat.bind.tuple.TupleOutput)3 LockMode (com.sleepycat.je.LockMode)3