Search in sources :

Example 81 with DatabaseEntry

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

Example 82 with DatabaseEntry

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

the class UpgradeFrom8To9 method performUpgrade.

@Override
public void performUpgrade(final Environment environment, final UpgradeInteractionHandler handler, final ConfiguredObject<?> parent) {
    reportStarting(environment, 8);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    final Transaction transaction = environment.beginTransaction(null, null);
    try {
        Database userPreferencesDb = environment.openDatabase(transaction, "USER_PREFERENCES", dbConfig);
        userPreferencesDb.close();
        try (Database userPreferencesVersionDb = environment.openDatabase(transaction, "USER_PREFERENCES_VERSION", dbConfig)) {
            if (userPreferencesVersionDb.count() == 0L) {
                DatabaseEntry key = new DatabaseEntry();
                DatabaseEntry value = new DatabaseEntry();
                StringBinding.stringToEntry(DEFAULT_VERSION, key);
                LongBinding.longToEntry(System.currentTimeMillis(), value);
                OperationStatus status = userPreferencesVersionDb.put(transaction, key, value);
                if (status != OperationStatus.SUCCESS) {
                    throw new StoreException("Error initialising user preference version: " + status);
                }
            }
        }
        transaction.commit();
        reportFinished(environment, 9);
    } catch (RuntimeException e) {
        try {
            if (transaction.isValid()) {
                transaction.abort();
            }
        } finally {
            throw e;
        }
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseConfig(com.sleepycat.je.DatabaseConfig) StoreException(org.apache.qpid.server.store.StoreException)

Example 83 with DatabaseEntry

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

the class BDBLinkStore method getLinkDefinitions.

private Collection<LinkDefinition<Source, Target>> getLinkDefinitions(final LinkStoreUpdater updater) {
    Database linksDatabase = getEnvironmentFacade().openDatabase(LINKS_DB_NAME, DEFAULT_DATABASE_CONFIG);
    Collection<LinkDefinition<Source, Target>> links = new HashSet<>();
    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));
    }
    try (Cursor cursor = linksDatabase.openCursor(null, null)) {
        final DatabaseEntry key = new DatabaseEntry();
        final DatabaseEntry value = new DatabaseEntry();
        LinkKeyEntryBinding keyEntryBinding = LinkKeyEntryBinding.getInstance();
        LinkValueEntryBinding linkValueEntryBinding = LinkValueEntryBinding.getInstance();
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
            LinkKey linkKey = keyEntryBinding.entryToObject(key);
            LinkValue linkValue = linkValueEntryBinding.entryToObject(value);
            LinkDefinition<Source, Target> link = new LinkDefinitionImpl<>(linkKey.getRemoteContainerId(), linkKey.getLinkName(), linkKey.getRole(), linkValue.getSource(), linkValue.getTarget());
            links.add(link);
        }
    }
    if (storedVersion.lessThan(currentVersion)) {
        links = updater.update(storedVersion.toString(), links);
        final Transaction txn = getEnvironmentFacade().beginTransaction(null);
        try {
            linksDatabase = getEnvironmentFacade().clearDatabase(txn, LINKS_DB_NAME, DEFAULT_DATABASE_CONFIG);
            for (LinkDefinition<Source, Target> link : links) {
                save(linksDatabase, txn, link);
            }
            updateVersion(txn, currentVersion.toString());
            txn.commit();
            linksDatabase.close();
        } catch (Exception e) {
            txn.abort();
            throw e;
        }
    }
    return links;
}
Also used : LinkKey(org.apache.qpid.server.protocol.v1_0.LinkKey) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor) Source(org.apache.qpid.server.protocol.v1_0.type.messaging.Source) StoreException(org.apache.qpid.server.store.StoreException) DatabaseNotFoundException(com.sleepycat.je.DatabaseNotFoundException) StoreException(org.apache.qpid.server.store.StoreException) LinkDefinition(org.apache.qpid.server.protocol.v1_0.LinkDefinition) Target(org.apache.qpid.server.protocol.v1_0.type.messaging.Target) LinkDefinitionImpl(org.apache.qpid.server.protocol.v1_0.LinkDefinitionImpl) Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) ModelVersion(org.apache.qpid.server.model.ModelVersion) HashSet(java.util.HashSet)

Example 84 with DatabaseEntry

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

the class BDBLinkStore method doDeleteLink.

@Override
protected void doDeleteLink(final LinkDefinition<Source, Target> linkDefinition) {
    LinkKey linkKey = new LinkKey(linkDefinition);
    try {
        Database linksDatabase = getEnvironmentFacade().openDatabase(LINKS_DB_NAME, DEFAULT_DATABASE_CONFIG);
        final DatabaseEntry databaseEntry = new DatabaseEntry();
        LinkKeyEntryBinding.getInstance().objectToEntry(linkKey, databaseEntry);
        OperationStatus status = linksDatabase.delete(null, databaseEntry);
        if (status != OperationStatus.SUCCESS) {
            LOGGER.debug(String.format("Unexpected status '%s' for deletion of '%s'", status, linkKey));
        }
    } catch (RuntimeException e) {
        throw getEnvironmentFacade().handleDatabaseException(String.format("Failed deletion of link '%s'", linkKey), e);
    }
}
Also used : LinkKey(org.apache.qpid.server.protocol.v1_0.LinkKey) OperationStatus(com.sleepycat.je.OperationStatus) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry)

Example 85 with DatabaseEntry

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

the class BDBLinkStore method getStoredVersion.

private ModelVersion getStoredVersion() throws RuntimeException {
    try (Cursor cursor = getLinksVersionDb().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 link version information.");
        }
        return storedVersion;
    } catch (RuntimeException e) {
        throw getEnvironmentFacade().handleDatabaseException("Cannot visit link 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)

Aggregations

DatabaseEntry (com.sleepycat.je.DatabaseEntry)153 OperationStatus (com.sleepycat.je.OperationStatus)70 Transaction (com.sleepycat.je.Transaction)58 Database (com.sleepycat.je.Database)46 Cursor (com.sleepycat.je.Cursor)31 DatabaseException (com.sleepycat.je.DatabaseException)26 StoreException (org.apache.qpid.server.store.StoreException)20 UUID (java.util.UUID)17 ArrayList (java.util.ArrayList)13 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)12 DatabaseConfig (com.sleepycat.je.DatabaseConfig)11 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)11 LockConflictException (com.sleepycat.je.LockConflictException)9 HashMap (java.util.HashMap)9 Versioned (voldemort.versioning.Versioned)9 HashSet (java.util.HashSet)8 UUIDTupleBinding (org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding)8 TupleOutput (com.sleepycat.bind.tuple.TupleOutput)6 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)6 SirixIOException (org.sirix.exception.SirixIOException)6