ENH: Replaces transport config with namespaced settings

This commit is contained in:
Santiago Torres
2016-01-17 15:42:45 -05:00
parent 2b02db3023
commit 190bb12964
27 changed files with 206 additions and 238 deletions

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -223,13 +222,6 @@ interface Database<T> {
*/
Collection<Group> getAvailableGroups(T txn) throws DbException;
/**
* Returns the configuration for the given transport.
* <p>
* Locking: read.
*/
TransportConfig getConfig(T txn, TransportId t) throws DbException;
/**
* Returns the contact with the given ID.
* <p>
@@ -419,11 +411,11 @@ interface Database<T> {
int maxLength) throws DbException;
/**
* Returns all settings.
* Returns all settings that belong to a namespace.
* <p>
* Locking: read.
*/
Settings getSettings(T txn) throws DbException;
Settings getSettings(T txn, String namespace) throws DbException;
/**
* Returns all contacts who subscribe to the given group.
@@ -524,15 +516,6 @@ interface Database<T> {
void lowerRequestedFlag(T txn, ContactId c, Collection<MessageId> requested)
throws DbException;
/**
* Merges the given configuration with the existing configuration for the
* given transport.
* <p>
* Locking: write.
*/
void mergeConfig(T txn, TransportId t, TransportConfig config)
throws DbException;
/**
* Merges the given properties with the existing local properties for the
* given transport.
@@ -547,7 +530,7 @@ interface Database<T> {
* <p>
* Locking: write.
*/
void mergeSettings(T txn, Settings s) throws DbException;
void mergeSettings(T txn, Settings s, String Namespace) throws DbException;
/**
* Marks a message as needing to be acknowledged to the given contact.

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -524,25 +523,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public TransportConfig getConfig(TransportId t) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
TransportConfig config = db.getConfig(txn, t);
db.commitTransaction(txn);
return config;
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.readLock().unlock();
}
}
public Contact getContact(ContactId c) throws DbException {
lock.readLock().lock();
try {
@@ -808,12 +788,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public Settings getSettings() throws DbException {
public Settings getSettings(String namespace) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Settings s = db.getSettings(txn);
Settings s = db.getSettings(txn, namespace);
db.commitTransaction(txn);
return s;
} catch (DbException e) {
@@ -939,25 +919,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public void mergeConfig(TransportId t, TransportConfig c)
throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
db.mergeConfig(txn, t, c);
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public void mergeLocalProperties(TransportId t, TransportProperties p)
throws DbException {
boolean changed = false;
@@ -982,14 +943,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
if (changed) eventBus.broadcast(new LocalTransportsUpdatedEvent());
}
public void mergeSettings(Settings s) throws DbException {
public void mergeSettings(Settings s, String namespace) throws DbException {
boolean changed = false;
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!s.equals(db.getSettings(txn))) {
db.mergeSettings(txn, s);
if (!s.equals(db.getSettings(txn, namespace))) {
db.mergeSettings(txn, s, namespace);
changed = true;
}
db.commitTransaction(txn);

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -64,14 +63,15 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 11;
private static final int MIN_SCHEMA_VERSION = 10;
private static final int SCHEMA_VERSION = 12;
private static final int MIN_SCHEMA_VERSION = 12;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"
+ " (key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL,"
+ " PRIMARY KEY (key))";
+ " namespace VARCHAR NOT NULL,"
+ " PRIMARY KEY (key, namespace))";
private static final String CREATE_LOCAL_AUTHORS =
"CREATE TABLE localAuthors"
@@ -343,7 +343,7 @@ abstract class JdbcDatabase implements Database<Connection> {
Settings s = new Settings();
s.put("schemaVersion", String.valueOf(SCHEMA_VERSION));
s.put("minSchemaVersion", String.valueOf(MIN_SCHEMA_VERSION));
mergeSettings(txn, s);
mergeSettings(txn, s, "db");
}
commitTransaction(txn);
} catch (DbException e) {
@@ -354,7 +354,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private boolean checkSchemaVersion(Connection txn) throws DbException {
try {
Settings s = getSettings(txn);
Settings s = getSettings(txn, "db");
int schemaVersion = Integer.valueOf(s.get("schemaVersion"));
if (schemaVersion == SCHEMA_VERSION) return true;
if (schemaVersion < MIN_SCHEMA_VERSION) return false;
@@ -1179,28 +1179,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public TransportConfig getConfig(Connection txn, TransportId t)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT key, value FROM transportConfigs"
+ " WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
TransportConfig c = new TransportConfig();
while (rs.next()) c.put(rs.getString(1), rs.getString(2));
rs.close();
ps.close();
return c;
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Contact getContact(Connection txn, ContactId c) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
@@ -1921,12 +1899,13 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Settings getSettings(Connection txn) throws DbException {
public Settings getSettings(Connection txn, String namespace) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT key, value FROM settings";
String sql = "SELECT key, value FROM settings WHERE namespace = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, namespace);
rs = ps.executeQuery();
Settings s = new Settings();
while (rs.next()) s.put(rs.getString(1), rs.getString(2));
@@ -2373,12 +2352,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void mergeConfig(Connection txn, TransportId t, TransportConfig c)
throws DbException {
// Merge the new configuration with the existing one
mergeStringMap(txn, t, c, "transportConfigs");
}
public void mergeLocalProperties(Connection txn, TransportId t,
TransportProperties p) throws DbException {
// Merge the new properties with the existing ones
@@ -2446,15 +2419,16 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void mergeSettings(Connection txn, Settings s) throws DbException {
public void mergeSettings(Connection txn, Settings s, String namespace) throws DbException {
PreparedStatement ps = null;
try {
// Update any settings that already exist
String sql = "UPDATE settings SET value = ? WHERE key = ?";
String sql = "UPDATE settings SET value = ? WHERE key = ? AND namespace = ?";
ps = txn.prepareStatement(sql);
for (Entry<String, String> e : s.entrySet()) {
ps.setString(1, e.getValue());
ps.setString(2, e.getKey());
ps.setString(3, namespace);
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
@@ -2464,13 +2438,14 @@ abstract class JdbcDatabase implements Database<Connection> {
if (batchAffected[i] > 1) throw new DbStateException();
}
// Insert any settings that don't already exist
sql = "INSERT INTO settings (key, value) VALUES (?, ?)";
sql = "INSERT INTO settings (key, value, namespace) VALUES (?, ?, ?)";
ps = txn.prepareStatement(sql);
int updateIndex = 0, inserted = 0;
for (Entry<String, String> e : s.entrySet()) {
if (batchAffected[updateIndex] == 0) {
ps.setString(1, e.getKey());
ps.setString(2, e.getValue());
ps.setString(3, namespace);
ps.addBatch();
inserted++;
}