Search in sources :

Example 6 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion 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 7 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion 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)

Example 8 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion in project qpid-broker-j by apache.

the class DerbyPreferenceStoreTest method testVersionAfterUpgrade.

public void testVersionAfterUpgrade() throws Exception {
    ModelVersion storeVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION - 1, BrokerModel.MODEL_MINOR_VERSION);
    _testConnection = DriverManager.getConnection(_connectionUrl);
    try (Statement stmt = _testConnection.createStatement()) {
        stmt.execute("CREATE TABLE PREFERENCES_VERSION ( version VARCHAR(20) NOT NULL )");
    }
    try (PreparedStatement pstmt = _testConnection.prepareStatement("INSERT INTO PREFERENCES_VERSION ( version ) VALUES ( ? )")) {
        pstmt.setString(1, storeVersion.toString());
        pstmt.execute();
    }
    _preferenceStore.openAndLoad(_updater);
    ModelVersion storedVersion = _preferenceStore.getPreferencesVersion();
    assertEquals("Unexpected version", BrokerModel.MODEL_VERSION, storedVersion.toString());
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) ModelVersion(org.apache.qpid.server.model.ModelVersion)

Example 9 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion in project qpid-broker-j by apache.

the class JDBCLinkStore method doOpenAndLoad.

@Override
protected Collection<LinkDefinition<Source, Target>> doOpenAndLoad(final LinkStoreUpdater updater) throws StoreException {
    Collection<LinkDefinition<Source, Target>> linkDefinitions;
    try {
        checkTransactionIsolationLevel();
        createOrOpenStoreDatabase();
        linkDefinitions = getLinks();
        ModelVersion storedVersion = getStoredVersion();
        ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
        if (storedVersion.lessThan(currentVersion)) {
            linkDefinitions = performUpdate(updater, linkDefinitions, storedVersion, currentVersion);
        } else if (currentVersion.lessThan(storedVersion)) {
            throw new StoreException(String.format("Cannot downgrade the store from %s to %s", storedVersion, currentVersion));
        }
    } catch (SQLException e) {
        throw new StoreException("Cannot open link store", e);
    }
    return linkDefinitions;
}
Also used : LinkDefinition(org.apache.qpid.server.protocol.v1_0.LinkDefinition) SQLException(java.sql.SQLException) ModelVersion(org.apache.qpid.server.model.ModelVersion) StoreException(org.apache.qpid.server.store.StoreException)

Aggregations

ModelVersion (org.apache.qpid.server.model.ModelVersion)9 StoreException (org.apache.qpid.server.store.StoreException)7 Cursor (com.sleepycat.je.Cursor)3 DatabaseEntry (com.sleepycat.je.DatabaseEntry)3 HashSet (java.util.HashSet)3 DatabaseNotFoundException (com.sleepycat.je.DatabaseNotFoundException)2 SQLException (java.sql.SQLException)2 LinkedHashSet (java.util.LinkedHashSet)2 UUID (java.util.UUID)2 LinkDefinition (org.apache.qpid.server.protocol.v1_0.LinkDefinition)2 PreferenceRecord (org.apache.qpid.server.store.preferences.PreferenceRecord)2 Database (com.sleepycat.je.Database)1 Transaction (com.sleepycat.je.Transaction)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 LinkDefinitionImpl (org.apache.qpid.server.protocol.v1_0.LinkDefinitionImpl)1 LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)1 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)1