Search in sources :

Example 16 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractBDBMessageStore method addContent.

/**
 * Stores a chunk of message data.
 *
 * @param tx         The transaction for the operation.
 * @param messageId       The message to store the data for.
 * @param contentBody     The content of the data chunk.
 *
 * @throws org.apache.qpid.server.store.StoreException If the operation fails for any reason, or if the specified message does not exist.
 */
private void addContent(final Transaction tx, long messageId, QpidByteBuffer contentBody) throws StoreException {
    DatabaseEntry key = new DatabaseEntry();
    LongBinding.longToEntry(messageId, key);
    DatabaseEntry value = new DatabaseEntry();
    byte[] data = new byte[contentBody.remaining()];
    contentBody.copyTo(data);
    value.setData(data);
    try {
        OperationStatus status = getMessageContentDb().put(tx, key, value);
        if (status != OperationStatus.SUCCESS) {
            throw new StoreException("Error adding content for message id " + messageId + ": " + status);
        }
        getLogger().debug("Storing content for message {} in transaction {}", messageId, tx);
    } catch (RuntimeException e) {
        throw getEnvironmentFacade().handleDatabaseException("Error writing AMQMessage with id " + messageId + " to database: " + e.getMessage(), e);
    }
}
Also used : OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StoreException(org.apache.qpid.server.store.StoreException)

Example 17 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractBDBMessageStore method dequeueMessage.

/**
 * Extracts a message from a specified queue, in a given transaction.
 *
 * @param tx   The transaction for the operation.
 * @param queueId     The id of the queue to take the message from.
 * @param messageId The message to dequeue.
 *
 * @throws org.apache.qpid.server.store.StoreException If the operation fails for any reason, or if the specified message does not exist.
 */
private void dequeueMessage(final Transaction tx, final UUID queueId, long messageId) throws StoreException {
    DatabaseEntry key = new DatabaseEntry();
    QueueEntryKey queueEntryKey = new QueueEntryKey(queueId, messageId);
    UUID id = queueId;
    QueueEntryBinding.objectToEntry(queueEntryKey, key);
    getLogger().debug("Dequeue message id {} from queue with id {}", messageId, id);
    try {
        OperationStatus status = getDeliveryDb().delete(tx, key);
        if (status == OperationStatus.NOTFOUND) {
            throw new StoreException("Unable to find message with id " + messageId + " on queue with id " + id);
        } else if (status != OperationStatus.SUCCESS) {
            throw new StoreException("Unable to remove message with id " + messageId + " on queue with id " + id);
        }
        getLogger().debug("Removed message {} on queue with id {}", messageId, id);
    } catch (RuntimeException e) {
        getLogger().error("Failed to dequeue message " + messageId + " in transaction " + tx, e);
        throw getEnvironmentFacade().handleDatabaseException("Error accessing database while dequeuing message: " + e.getMessage(), e);
    }
}
Also used : QueueEntryKey(org.apache.qpid.server.store.berkeleydb.entry.QueueEntryKey) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) UUID(java.util.UUID) StoreException(org.apache.qpid.server.store.StoreException)

Example 18 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore method updateOrCreateInternal.

private void updateOrCreateInternal(final Transaction txn, final Collection<PreferenceRecord> preferenceRecords) {
    Database preferencesDb = getPreferencesDb();
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
    MapBinding valueBinding = MapBinding.getInstance();
    for (PreferenceRecord record : preferenceRecords) {
        keyBinding.objectToEntry(record.getId(), key);
        valueBinding.objectToEntry(record.getAttributes(), value);
        OperationStatus status = preferencesDb.put(txn, key, value);
        if (status != OperationStatus.SUCCESS) {
            throw new StoreException(String.format("Error writing preference with id '%s' (status %s)", record.getId(), status.name()));
        }
    }
}
Also used : MapBinding(org.apache.qpid.server.store.berkeleydb.tuple.MapBinding) OperationStatus(com.sleepycat.je.OperationStatus) Database(com.sleepycat.je.Database) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) UUIDTupleBinding(org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StoreException(org.apache.qpid.server.store.StoreException)

Example 19 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore method getStoredVersion.

ModelVersion getStoredVersion() {
    try (Cursor cursor = getPreferencesVersionDb().openCursor(null, null)) {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        ModelVersion storedVersion = null;
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
            String versionString = StringBinding.entryToString(key);
            ModelVersion version = ModelVersion.fromString(versionString);
            if (storedVersion == null || storedVersion.lessThan(version)) {
                storedVersion = version;
            }
        }
        if (storedVersion == null) {
            throw new StoreException("No preference version information.");
        }
        return storedVersion;
    } catch (RuntimeException e) {
        throw getEnvironmentFacade().handleDatabaseException("Cannot visit preference version", e);
    }
}
Also used : DatabaseEntry(com.sleepycat.je.DatabaseEntry) ModelVersion(org.apache.qpid.server.model.ModelVersion) Cursor(com.sleepycat.je.Cursor) StoreException(org.apache.qpid.server.store.StoreException)

Example 20 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore method openAndLoad.

@Override
public Collection<PreferenceRecord> openAndLoad(final PreferenceStoreUpdater updater) throws StoreException {
    if (!_storeState.compareAndSet(StoreState.CLOSED, StoreState.OPENING)) {
        throw new IllegalStateException(String.format("PreferenceStore cannot be opened when in state '%s'", getStoreState()));
    }
    EnvironmentFacade environmentFacade = getEnvironmentFacade();
    try {
        _storeState.set(StoreState.OPENED);
        ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
        ModelVersion storedVersion = getStoredVersion();
        if (currentVersion.lessThan(storedVersion)) {
            throw new StoreException(String.format("Cannot downgrade preference store from '%s' to '%s'", storedVersion, currentVersion));
        }
        Collection<PreferenceRecord> records = getPreferenceRecords(environmentFacade);
        if (storedVersion.lessThan(currentVersion)) {
            final Collection<UUID> ids = new HashSet<>();
            for (PreferenceRecord record : records) {
                ids.add(record.getId());
            }
            records = updater.updatePreferences(storedVersion.toString(), records);
            removeAndAdd(ids, records, transaction -> updateVersion(transaction, currentVersion.toString()));
        }
        return records;
    } catch (Exception e) {
        _storeState.set(StoreState.ERRORED);
        close();
        throw e;
    }
}
Also used : PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ModelVersion(org.apache.qpid.server.model.ModelVersion) UUID(java.util.UUID) StoreException(org.apache.qpid.server.store.StoreException) DatabaseNotFoundException(com.sleepycat.je.DatabaseNotFoundException) StoreException(org.apache.qpid.server.store.StoreException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

StoreException (org.apache.qpid.server.store.StoreException)70 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)23 PreparedStatement (java.sql.PreparedStatement)21 DatabaseEntry (com.sleepycat.je.DatabaseEntry)20 IOException (java.io.IOException)18 OperationStatus (com.sleepycat.je.OperationStatus)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ResultSet (java.sql.ResultSet)10 UUID (java.util.UUID)8 Database (com.sleepycat.je.Database)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ModelVersion (org.apache.qpid.server.model.ModelVersion)7 LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)7 HashSet (java.util.HashSet)5 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)5 Cursor (com.sleepycat.je.Cursor)4 DatabaseConfig (com.sleepycat.je.DatabaseConfig)4