Removed last connection time from Contact class, tightened up layouts.

This commit is contained in:
akwizgran
2013-04-15 14:44:42 +01:00
parent 2ef06f8564
commit c5fa3d1841
25 changed files with 115 additions and 125 deletions

View File

@@ -237,7 +237,7 @@ interface Database<T> {
/**
* Returns the contact with the given ID.
* <p>
* Locking: contact read, window read.
* Locking: contact read.
*/
Contact getContact(T txn, ContactId c) throws DbException;
@@ -292,12 +292,12 @@ interface Database<T> {
MessageId getGroupMessageParent(T txn, MessageId m) throws DbException;
/**
* Returns the time at which a connection to the given contact was last
* made.
* Returns the time at which a connection to each contact was last opened
* or closed.
* <p>
* Locking: window read.
*/
long getLastConnected(T txn, ContactId c) throws DbException;
Map<ContactId, Long> getLastConnected(T txn) throws DbException;
/**
* Returns the pseudonym with the given ID.

View File

@@ -926,21 +926,16 @@ DatabaseCleaner.Callback {
public Contact getContact(ContactId c) throws DbException {
contactLock.readLock().lock();
try {
windowLock.readLock().lock();
T txn = db.startTransaction();
try {
T txn = db.startTransaction();
try {
if(!db.containsContact(txn, c))
throw new NoSuchContactException();
Contact contact = db.getContact(txn, c);
db.commitTransaction(txn);
return contact;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
windowLock.readLock().unlock();
if(!db.containsContact(txn, c))
throw new NoSuchContactException();
Contact contact = db.getContact(txn, c);
db.commitTransaction(txn);
return contact;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
contactLock.readLock().unlock();
@@ -1019,6 +1014,23 @@ DatabaseCleaner.Callback {
}
}
public Map<ContactId, Long> getLastConnected() throws DbException {
windowLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Map<ContactId, Long> times = db.getLastConnected(txn);
db.commitTransaction(txn);
return times;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
windowLock.readLock().unlock();
}
}
public LocalAuthor getLocalAuthor(AuthorId a) throws DbException {
identityLock.readLock().lock();
try {

View File

@@ -1200,12 +1200,9 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT authorId, name, publicKey, localAuthorId,"
+ " lastConnected"
+ " FROM contacts AS c"
+ " JOIN connectionTimes AS ct"
+ " ON c.contactId = ct.contactId"
+ " WHERE c.contactId = ?";
String sql = "SELECT authorId, name, publicKey, localAuthorId"
+ " FROM contacts"
+ " WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
rs = ps.executeQuery();
@@ -1214,11 +1211,10 @@ abstract class JdbcDatabase implements Database<Connection> {
String name = rs.getString(2);
byte[] publicKey = rs.getBytes(3);
AuthorId localAuthorId = new AuthorId(rs.getBytes(4));
long lastConnected = rs.getLong(5);
rs.close();
ps.close();
Author author = new Author(authorId, name, publicKey);
return new Contact(c, author, localAuthorId, lastConnected);
return new Contact(c, author, localAuthorId);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
@@ -1251,11 +1247,9 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT c.contactId, authorId, name, publicKey,"
+ " localAuthorId, lastConnected"
+ " FROM contacts AS c"
+ " JOIN connectionTimes AS ct"
+ " ON c.contactId = ct.contactId";
String sql = "SELECT contactId, authorId, name, publicKey,"
+ " localAuthorId"
+ " FROM contacts";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
List<Contact> contacts = new ArrayList<Contact>();
@@ -1264,11 +1258,9 @@ abstract class JdbcDatabase implements Database<Connection> {
AuthorId authorId = new AuthorId(rs.getBytes(2));
String name = rs.getString(3);
byte[] publicKey = rs.getBytes(4);
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
long lastConnected = rs.getLong(6);
Author author = new Author(authorId, name, publicKey);
contacts.add(new Contact(contactId, author, localAuthorId,
lastConnected));
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
contacts.add(new Contact(contactId, author, localAuthorId));
}
rs.close();
ps.close();
@@ -1409,22 +1401,20 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public long getLastConnected(Connection txn, ContactId c)
public Map<ContactId, Long> getLastConnected(Connection txn)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT lastConnected FROM connectionTimes"
+ " WHERE contactId = ?";
String sql = "SELECT contactId, lastConnected FROM connectionTimes";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
rs = ps.executeQuery();
if(!rs.next()) throw new DbStateException();
long lastConnected = rs.getLong(1);
if(rs.next()) throw new DbStateException();
Map<ContactId, Long> times = new HashMap<ContactId, Long>();
while(rs.next())
times.put(new ContactId(rs.getInt(1)), rs.getLong(2));
rs.close();
ps.close();
return lastConnected;
return Collections.unmodifiableMap(times);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);