Search in sources :

Example 1 with PreferenceRecord

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

the class DerbyPreferenceStoreTest method testOpenAndLoadEmptyStore.

public void testOpenAndLoadEmptyStore() throws Exception {
    Collection<PreferenceRecord> records = _preferenceStore.openAndLoad(_updater);
    assertEquals("Unexpected number of records", 0, records.size());
    _testConnection = DriverManager.getConnection(_connectionUrl);
    DerbyUtils.tableExists("PREFERENCES", _testConnection);
    DerbyUtils.tableExists("PREFERENCES_VERSION", _testConnection);
    List<String> versions = new ArrayList<>();
    try (PreparedStatement selectStatement = _testConnection.prepareStatement("select version from PREFERENCES_VERSION")) {
        try (ResultSet resultSet = selectStatement.executeQuery()) {
            while (resultSet.next()) {
                versions.add(resultSet.getString(1));
            }
        }
    }
    assertEquals("Unexpected versions size", 1, versions.size());
    assertEquals("Unexpected version", BrokerModel.MODEL_VERSION, versions.get(0));
}
Also used : PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with PreferenceRecord

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

the class DerbyPreferenceStoreTest method getPreferenceRecords.

private List<PreferenceRecord> getPreferenceRecords() throws SQLException, java.io.IOException {
    List<PreferenceRecord> records = new ArrayList<>();
    ObjectMapper objectMapper = new ObjectMapper();
    try (PreparedStatement selectStatement = _testConnection.prepareStatement("select id,attributes from PREFERENCES")) {
        try (ResultSet resultSet = selectStatement.executeQuery()) {
            while (resultSet.next()) {
                records.add(new PreferenceRecordImpl(UUID.fromString(resultSet.getString(1)), objectMapper.readValue(DerbyUtils.getBlobAsString(resultSet, 2), Map.class)));
            }
        }
    }
    return records;
}
Also used : PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PreferenceRecordImpl(org.apache.qpid.server.store.preferences.PreferenceRecordImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with PreferenceRecord

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

the class AbstractJDBCPreferenceStore 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'", _storeState.get()));
    }
    try {
        _storeState.set(StoreState.OPENED);
        Collection<PreferenceRecord> records;
        try (Connection connection = getConnection()) {
            createVersionTable(connection);
            createPreferencesTable(connection);
            ModelVersion preferencesVersion = getPreferencesVersion(connection);
            ModelVersion brokerModelVersion = ModelVersion.fromString(BrokerModel.MODEL_VERSION);
            if (brokerModelVersion.lessThan(preferencesVersion)) {
                throw new StoreException(String.format("Cannot downgrade preference store from '%s' to '%s'", preferencesVersion, brokerModelVersion));
            }
            records = getPreferenceRecords(connection);
            if (preferencesVersion.lessThan(brokerModelVersion)) {
                final Collection<UUID> ids = new HashSet<>();
                for (PreferenceRecord record : records) {
                    ids.add(record.getId());
                }
                records = updater.updatePreferences(preferencesVersion.toString(), records);
                removeAndAdd(ids, records, transactedConnection -> updateVersion(transactedConnection, brokerModelVersion.toString()));
            }
        }
        return records;
    } catch (SQLException e) {
        _storeState.set(StoreState.ERRORED);
        close();
        throw new StoreException(e);
    }
}
Also used : SQLException(java.sql.SQLException) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) Connection(java.sql.Connection) ModelVersion(org.apache.qpid.server.model.ModelVersion) UUID(java.util.UUID) StoreException(org.apache.qpid.server.store.StoreException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with PreferenceRecord

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

the class AbstractJDBCPreferenceStore method getPreferenceRecords.

private Collection<PreferenceRecord> getPreferenceRecords(final Connection connection) throws SQLException {
    Collection<PreferenceRecord> records = new LinkedHashSet<>();
    final ObjectMapper objectMapper = new ObjectMapper();
    try (PreparedStatement stmt = connection.prepareStatement(String.format(SELECT_FROM_PREFERENCES, getPreferencesTableName()))) {
        try (ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                String id = rs.getString(1);
                String attributes = getBlobAsString(rs, 2);
                final PreferenceRecord preferenceRecord = new PreferenceRecordImpl(UUID.fromString(id), objectMapper.readValue(attributes, Map.class));
                records.add(preferenceRecord);
            }
        } catch (IOException e) {
            throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
        }
    }
    return records;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PreferenceRecordImpl(org.apache.qpid.server.store.preferences.PreferenceRecordImpl) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreException(org.apache.qpid.server.store.StoreException)

Example 5 with PreferenceRecord

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

the class AbstractBDBPreferenceStore method getPreferenceRecords.

private Collection<PreferenceRecord> getPreferenceRecords(final EnvironmentFacade environmentFacade) {
    Collection<PreferenceRecord> records = new LinkedHashSet<>();
    try (Cursor cursor = getPreferencesDb().openCursor(null, null)) {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
        MapBinding valueBinding = MapBinding.getInstance();
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
            UUID preferenceId = keyBinding.entryToObject(key);
            Map<String, Object> preferenceAttributes = valueBinding.entryToObject(value);
            PreferenceRecord record = new PreferenceRecordImpl(preferenceId, preferenceAttributes);
            records.add(record);
        }
    } catch (RuntimeException e) {
        throw environmentFacade.handleDatabaseException("Cannot visit preferences", e);
    }
    return records;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MapBinding(org.apache.qpid.server.store.berkeleydb.tuple.MapBinding) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) PreferenceRecordImpl(org.apache.qpid.server.store.preferences.PreferenceRecordImpl) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor) UUIDTupleBinding(org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding) UUID(java.util.UUID)

Aggregations

PreferenceRecord (org.apache.qpid.server.store.preferences.PreferenceRecord)16 PreferenceRecordImpl (org.apache.qpid.server.store.preferences.PreferenceRecordImpl)6 UUID (java.util.UUID)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 LinkedHashSet (java.util.LinkedHashSet)4 StoreException (org.apache.qpid.server.store.StoreException)4 DatabaseEntry (com.sleepycat.je.DatabaseEntry)3 HashSet (java.util.HashSet)3 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)3 MapBinding (org.apache.qpid.server.store.berkeleydb.tuple.MapBinding)3 UUIDTupleBinding (org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Database (com.sleepycat.je.Database)2 TaskExecutorImpl (org.apache.qpid.server.configuration.updater.TaskExecutorImpl)2 ModelVersion (org.apache.qpid.server.model.ModelVersion)2 PreferenceStoreUpdaterImpl (org.apache.qpid.server.store.preferences.PreferenceStoreUpdaterImpl)2 PreferencesRecoverer (org.apache.qpid.server.store.preferences.PreferencesRecoverer)2 Cursor (com.sleepycat.je.Cursor)1