Retrieve all remote transports from the DB in a single call.

This commit is contained in:
akwizgran
2011-10-10 22:35:46 +01:00
parent 4059fbf863
commit 68b82ae826
8 changed files with 207 additions and 176 deletions

View File

@@ -211,6 +211,14 @@ interface Database<T> {
*/
MessageId getGroupMessageParent(T txn, MessageId m) throws DbException;
/**
* Returns all local transport properties.
* <p>
* Locking: transports read.
*/
Map<TransportId, Map<String, String>> getLocalTransports(T txn)
throws DbException;
/**
* Returns the IDs of any batches sent to the given contact that should now
* be considered lost.
@@ -269,6 +277,14 @@ interface Database<T> {
*/
Rating getRating(T txn, AuthorId a) throws DbException;
/**
* Returns all remote transport properties.
* <p>
* Locking: contacts read, transports read.
*/
Map<TransportId, Map<ContactId, Map<String, String>>>
getRemoteTransports(T txn) throws DbException;
/**
* Returns the sendability score of the given group message.
* <p>
@@ -325,22 +341,6 @@ interface Database<T> {
Map<String, String> getTransportConfig(T txn, TransportId t)
throws DbException;
/**
* Returns all local transport properties.
* <p>
* Locking: transports read.
*/
Map<TransportId, Map<String, String>> getTransports(T txn)
throws DbException;
/**
* Returns all transport properties for the given contact.
* <p>
* Locking: contacts read, transports read.
*/
Map<TransportId, Map<String, String>> getTransports(T txn, ContactId c)
throws DbException;
/**
* Returns the contacts to which the given group is visible.
* <p>

View File

@@ -615,7 +615,7 @@ DatabaseCleaner.Callback {
try {
T txn = db.startTransaction();
try {
transports = db.getTransports(txn);
transports = db.getLocalTransports(txn);
timestamp = System.currentTimeMillis();
db.setTransportTimestamp(txn, c, timestamp);
db.commitTransaction(txn);
@@ -699,6 +699,25 @@ DatabaseCleaner.Callback {
}
}
public Map<TransportId, Map<String, String>> getLocalTransports()
throws DbException {
transportLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<TransportId, Map<String, String>> transports =
db.getLocalTransports(txn);
db.commitTransaction(txn);
return transports;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
transportLock.readLock().unlock();
}
}
public Rating getRating(AuthorId a) throws DbException {
ratingLock.readLock().lock();
try {
@@ -716,6 +735,30 @@ DatabaseCleaner.Callback {
}
}
public Map<TransportId, Map<ContactId, Map<String, String>>>
getRemoteTransports() throws DbException {
contactLock.readLock().lock();
try {
transportLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<TransportId, Map<ContactId, Map<String, String>>>
transports = db.getRemoteTransports(txn);
db.commitTransaction(txn);
return transports;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
transportLock.readLock().unlock();
}
} finally {
contactLock.readLock().unlock();
}
}
public byte[] getSharedSecret(ContactId c) throws DbException {
contactLock.readLock().lock();
try {
@@ -769,50 +812,6 @@ DatabaseCleaner.Callback {
}
}
public Map<TransportId, Map<String, String>> getTransports()
throws DbException {
transportLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<TransportId, Map<String, String>> transports =
db.getTransports(txn);
db.commitTransaction(txn);
return transports;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
transportLock.readLock().unlock();
}
}
public Map<TransportId, Map<String, String>> getTransports(ContactId c)
throws DbException {
contactLock.readLock().lock();
try {
if(!containsContact(c)) throw new NoSuchContactException();
transportLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<TransportId, Map<String, String>> transports =
db.getTransports(txn, c);
db.commitTransaction(txn);
return transports;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
transportLock.readLock().unlock();
}
} finally {
contactLock.readLock().unlock();
}
}
public Collection<ContactId> getVisibility(GroupId g) throws DbException {
contactLock.readLock().lock();
try {
@@ -1252,7 +1251,7 @@ DatabaseCleaner.Callback {
T txn = db.startTransaction();
try {
Map<TransportId, Map<String, String>> transports =
db.getTransports(txn);
db.getLocalTransports(txn);
Map<String, String> old = transports.get(t);
if(!properties.equals(old)) {
db.setTransportProperties(txn, t, properties);

View File

@@ -916,6 +916,37 @@ abstract class JdbcDatabase implements Database<Connection> {
} else return f.length();
}
public Map<TransportId, Map<String, String>> getLocalTransports(
Connection txn) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT transportId, key, value FROM transports"
+ " ORDER BY transportId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
Map<TransportId, Map<String, String>> transports =
new TreeMap<TransportId, Map<String, String>>();
Map<String, String> properties = null;
TransportId lastId = null;
while(rs.next()) {
TransportId id = new TransportId(rs.getInt(1));
if(!id.equals(lastId)) {
properties = new TreeMap<String, String>();
transports.put(id, properties);
}
properties.put(rs.getString(2), rs.getString(3));
}
rs.close();
ps.close();
return transports;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Collection<BatchId> getLostBatches(Connection txn, ContactId c)
throws DbException {
PreparedStatement ps = null;
@@ -1181,6 +1212,46 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Map<TransportId, Map<ContactId, Map<String, String>>>
getRemoteTransports(Connection txn) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT transportId, contactId, key, value"
+ " FROM contactTransports"
+ " ORDER BY transportId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
Map<TransportId, Map<ContactId, Map<String, String>>> transports =
new TreeMap<TransportId, Map<ContactId, Map<String, String>>>();
Map<ContactId, Map<String, String>> contacts = null;
Map<String, String> properties = null;
TransportId lastTransportId = null;
ContactId lastContactId = null;
while(rs.next()) {
TransportId transportId = new TransportId(rs.getInt(1));
if(!transportId.equals(lastTransportId)) {
contacts = new HashMap<ContactId, Map<String, String>>();
transports.put(transportId, contacts);
lastContactId = null;
}
ContactId contactId = new ContactId(rs.getInt(2));
if(!contactId.equals(lastContactId)) {
properties = new TreeMap<String, String>();
contacts.put(contactId, properties);
}
properties.put(rs.getString(3), rs.getString(4));
}
rs.close();
ps.close();
return transports;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public byte[] getSharedSecret(Connection txn, ContactId c)
throws DbException {
PreparedStatement ps = null;
@@ -1418,70 +1489,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Map<TransportId, Map<String, String>> getTransports(Connection txn)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT transportId, key, value FROM transports"
+ " ORDER BY transportId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
Map<TransportId, Map<String, String>> transports =
new TreeMap<TransportId, Map<String, String>>();
Map<String, String> properties = null;
TransportId lastId = null;
while(rs.next()) {
TransportId id = new TransportId(rs.getInt(1));
if(!id.equals(lastId)) {
properties = new TreeMap<String, String>();
transports.put(id, properties);
}
properties.put(rs.getString(2), rs.getString(3));
}
rs.close();
ps.close();
return transports;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Map<TransportId, Map<String, String>> getTransports(Connection txn,
ContactId c) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT transportId, key, value FROM contactTransports"
+ " WHERE contactId = ?"
+ " ORDER BY transportId";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
rs = ps.executeQuery();
Map<TransportId, Map<String, String>> transports =
new TreeMap<TransportId, Map<String, String>>();
Map<String, String> properties = null;
TransportId lastId = null;
while(rs.next()) {
TransportId id = new TransportId(rs.getInt(1));
if(!id.equals(lastId)) {
properties = new TreeMap<String, String>();
transports.put(id, properties);
}
properties.put(rs.getString(2), rs.getString(3));
}
rs.close();
ps.close();
return transports;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
throws DbException {
PreparedStatement ps = null;

View File

@@ -73,7 +73,7 @@ class InvitationWorker implements Runnable {
// FIXME: Create a real invitation
Map<TransportId, Map<String, String>> transports;
try {
transports = db.getTransports();
transports = db.getLocalTransports();
} catch(DbException e) {
throw new IOException(e.getMessage());
}