Search in sources :

Example 31 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class UpgradeFrom4To5 method upgradeMetaData.

private void upgradeMetaData(final Environment environment, final UpgradeInteractionHandler handler, final Set<Long> messagesToDiscard, Transaction transaction) {
    LOGGER.info("Message MetaData");
    if (environment.getDatabaseNames().contains(OLD_METADATA_DB_NAME)) {
        final MessageMetaDataBinding binding = new MessageMetaDataBinding();
        CursorOperation databaseOperation = new CursorOperation() {

            @Override
            public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
                StorableMessageMetaData metaData = binding.entryToObject(value);
                // get message id
                Long messageId = LongBinding.entryToLong(key);
                // ONLY copy data if message is delivered to existing queue
                if (messagesToDiscard.contains(messageId)) {
                    return;
                }
                DatabaseEntry newValue = new DatabaseEntry();
                binding.objectToEntry(metaData, newValue);
                targetDatabase.put(transaction, key, newValue);
                targetDatabase.put(transaction, key, newValue);
                deleteCurrent();
            }
        };
        new DatabaseTemplate(environment, OLD_METADATA_DB_NAME, NEW_METADATA_DB_NAME, transaction).run(databaseOperation);
        environment.removeDatabase(transaction, OLD_METADATA_DB_NAME);
        LOGGER.info(databaseOperation.getRowCount() + " Message MetaData entries");
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData)

Example 32 with Database

use of com.sleepycat.je.Database 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 33 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore method getPreferencesVersionDb.

private Database getPreferencesVersionDb() {
    Database preferencesVersionDb;
    try {
        DatabaseConfig config = new DatabaseConfig().setTransactional(true).setAllowCreate(false);
        preferencesVersionDb = getEnvironmentFacade().openDatabase(PREFERENCES_VERSION_DB_NAME, config);
    } catch (DatabaseNotFoundException e) {
        preferencesVersionDb = updateVersion(null, BrokerModel.MODEL_VERSION);
    }
    return preferencesVersionDb;
}
Also used : DatabaseNotFoundException(com.sleepycat.je.DatabaseNotFoundException) Database(com.sleepycat.je.Database) DatabaseConfig(com.sleepycat.je.DatabaseConfig)

Example 34 with Database

use of com.sleepycat.je.Database 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)

Example 35 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class OrphanConfigurationRecordPurger method purgeOrphans.

private void purgeOrphans(Environment env, final Transaction tx) throws Exception {
    try (Database configDb = env.openDatabase(tx, CONFIGURED_OBJECTS_DB_NAME, READ_WRITE_DB_CONFIG)) {
        final Set<UUID> records = new HashSet<>();
        try (Cursor configCursor = configDb.openCursor(tx, null)) {
            final DatabaseEntry key = new DatabaseEntry();
            final DatabaseEntry value = new DatabaseEntry();
            while (configCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                final UUID recId = entryToUuid(new TupleInput(key.getData()));
                records.add(recId);
            }
        }
        int configRecordDeleted = 0;
        int configHierarchyRecordsDeleted = 0;
        try (Database hierarchyDb = env.openDatabase(null, CONFIGURED_OBJECT_HIERARCHY_DB_NAME, READ_WRITE_DB_CONFIG)) {
            boolean loopAgain;
            do {
                loopAgain = false;
                try (Cursor hierarchyCursor = hierarchyDb.openCursor(tx, null)) {
                    DatabaseEntry key = new DatabaseEntry();
                    DatabaseEntry value = new DatabaseEntry();
                    boolean parentReferencingRecordFound = false;
                    while (hierarchyCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                        final TupleInput keyInput = new TupleInput(key.getData());
                        final UUID childId = entryToUuid(keyInput);
                        final String parentType = keyInput.readString();
                        final UUID parentId = entryToUuid(new TupleInput(value.getData()));
                        if (_parentRootCategory.equals(parentType)) {
                            parentReferencingRecordFound = true;
                        } else if (!records.contains(parentId)) {
                            System.out.format("Orphan UUID : %s (has unknown parent with UUID %s of type %s)\n", childId, parentId, parentType);
                            hierarchyCursor.delete();
                            configHierarchyRecordsDeleted++;
                            loopAgain = true;
                            DatabaseEntry uuidKey = new DatabaseEntry();
                            final TupleOutput tupleOutput = uuidToKey(childId);
                            TupleBase.outputToEntry(tupleOutput, uuidKey);
                            final OperationStatus delete = configDb.delete(tx, uuidKey);
                            if (delete == OperationStatus.SUCCESS) {
                                records.remove(childId);
                                configRecordDeleted++;
                            }
                        }
                    }
                    if (!parentReferencingRecordFound) {
                        throw new IllegalStateException(String.format("No hierarchy record found with root category type (%s)." + " Cannot modify store.", _parentRootCategory));
                    }
                }
            } while (loopAgain);
            System.out.format("Identified %d orphaned configured object record(s) " + "and %d hierarchy records for purging\n", configRecordDeleted, configHierarchyRecordsDeleted);
        }
    }
}
Also used : DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor) TupleInput(com.sleepycat.bind.tuple.TupleInput) OperationStatus(com.sleepycat.je.OperationStatus) Database(com.sleepycat.je.Database) UUID(java.util.UUID) HashSet(java.util.HashSet) TupleOutput(com.sleepycat.bind.tuple.TupleOutput)

Aggregations

Database (com.sleepycat.je.Database)73 DatabaseEntry (com.sleepycat.je.DatabaseEntry)46 Transaction (com.sleepycat.je.Transaction)35 DatabaseConfig (com.sleepycat.je.DatabaseConfig)27 UUID (java.util.UUID)12 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)11 Environment (com.sleepycat.je.Environment)10 OperationStatus (com.sleepycat.je.OperationStatus)8 Cursor (com.sleepycat.je.Cursor)7 HashSet (java.util.HashSet)7 StoreException (org.apache.qpid.server.store.StoreException)7 HashMap (java.util.HashMap)6 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)5 File (java.io.File)5 ArrayList (java.util.ArrayList)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 Test (org.junit.Test)5 TupleOutput (com.sleepycat.bind.tuple.TupleOutput)4