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));
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations