private ModelVersion getStoredVersion() throws SQLException { ModelVersion version = null; try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(String.format("SELECT version FROM %s", getVersionTableName()))) { while (resultSet.next()) { ModelVersion storedVersion = ModelVersion.fromString(resultSet.getString(1)); if (version == null || version.lessThan(storedVersion)) { version = storedVersion; } } } if (version == null) { throw new StoreException("Version of links is not found"); } return version; }
private void updateVersion(final Connection connection, final ModelVersion currentVersion) throws SQLException { String version = currentVersion.toString(); try (PreparedStatement statement = connection.prepareStatement(String.format( "INSERT INTO %s (version, version_time) VALUES (?,?)", getVersionTableName()))) { statement.setString(1, version); statement.setDate(2, new java.sql.Date(System.currentTimeMillis())); if (statement.executeUpdate() != 1) { throw new StoreException(String.format("Cannot insert version '%s' into version table", version)); } } }
public static ModelVersion fromString(String versionString) { if (versionString == null) { throw new IllegalArgumentException("Cannot parse null"); } final Matcher matcher = MODEL_VERSION_PATTERN.matcher(versionString); if (!matcher.matches()) { throw new IllegalArgumentException(String.format("Could not parse model version string '%s'", versionString)); } return new ModelVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))); }
ModelVersion storedVersion = ModelVersion.fromString(storeContent.getVersion()); ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION); if (currentVersion.lessThan(storedVersion)) currentVersion.toString(), BrokerModel.MODEL_VERSION)); if (storedVersion.lessThan(currentVersion)) records = updater.updatePreferences(storedVersion.toString(), records); storeContent.setVersion(BrokerModel.MODEL_VERSION); storeContent.setPreferences(records.toArray(new StoredPreferenceRecord[records.size()]));
new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION); ModelVersion storedVersion = getStoredVersion(); if (currentVersion.lessThan(storedVersion)) if (storedVersion.lessThan(currentVersion)) records = updater.updatePreferences(storedVersion.toString(), records); removeAndAdd(ids, records, transaction -> updateVersion(transaction, currentVersion.toString()));
createPreferencesTable(connection); ModelVersion preferencesVersion = getPreferencesVersion(connection); ModelVersion brokerModelVersion = ModelVersion.fromString(BrokerModel.MODEL_VERSION); if (brokerModelVersion.lessThan(preferencesVersion)) if (preferencesVersion.lessThan(brokerModelVersion)) records = updater.updatePreferences(preferencesVersion.toString(), records); brokerModelVersion.toString()));
@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; }
private void createVersionTable(final Connection conn) throws SQLException { String versionTableName = getVersionTableName(); if (!JdbcUtils.tableExists(versionTableName, conn)) { try (Statement stmt = conn.createStatement()) { stmt.execute(String.format("CREATE TABLE %s" + " (version varchar(10) PRIMARY KEY ," + " version_time %s)", versionTableName, _sqlTimestampType)); } updateVersion(conn, ModelVersion.fromString(BrokerModel.MODEL_VERSION)); } }
new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION); ModelVersion storedVersion = getStoredVersion(); if (currentVersion.lessThan(storedVersion)) if (storedVersion.lessThan(currentVersion)) links = updater.update(storedVersion.toString(), links); final Transaction txn = getEnvironmentFacade().beginTransaction(null); try updateVersion(txn, currentVersion.toString()); txn.commit(); linksDatabase.close();
try ModelVersion version = ModelVersion.fromString(versionString); if (storedVersion == null || storedVersion.lessThan(version))
throws SQLException linkDefinitions = updater.update(storedVersion.toString(), linkDefinitions); Connection connection = getConnection(); try
ModelVersion getStoredVersion() { try(Cursor cursor = getPreferencesVersionDb().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 preference version information."); } return storedVersion; } catch (RuntimeException e) { throw getEnvironmentFacade().handleDatabaseException("Cannot visit preference version", e); } }
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); } }