Add database method for retrieving a contact by author ID

and use it for retreiving the status of an author faster.
Also add tests for both.
This commit is contained in:
Torsten Grote
2016-08-31 16:49:02 -03:00
parent 61c05c1dd4
commit 6f0ffa8439
7 changed files with 197 additions and 11 deletions

View File

@@ -216,6 +216,14 @@ interface Database<T> {
*/
Collection<Contact> getContacts(T txn) throws DbException;
/**
* Returns a possibly empty collection of contacts with the given author ID.
* <p/>
* Read-only.
*/
Collection<Contact> getContactsByAuthorId(T txn, AuthorId remote)
throws DbException;
/**
* Returns all contacts associated with the given local pseudonym.
* <p/>

View File

@@ -359,6 +359,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return db.getContacts(txn);
}
public Collection<Contact> getContactsByAuthorId(Transaction transaction,
AuthorId remote) throws DbException {
T txn = unbox(transaction);
return db.getContactsByAuthorId(txn, remote);
}
public Collection<ContactId> getContacts(Transaction transaction,
AuthorId a) throws DbException {
T txn = unbox(transaction);

View File

@@ -1020,7 +1020,7 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<ContactId> getContacts(Connection txn, AuthorId a)
public Collection<ContactId> getContacts(Connection txn, AuthorId local)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
@@ -1028,7 +1028,7 @@ abstract class JdbcDatabase implements Database<Connection> {
String sql = "SELECT contactId FROM contacts"
+ " WHERE localAuthorId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, a.getBytes());
ps.setBytes(1, local.getBytes());
rs = ps.executeQuery();
List<ContactId> ids = new ArrayList<ContactId>();
while (rs.next()) ids.add(new ContactId(rs.getInt(1)));
@@ -1042,6 +1042,40 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<Contact> getContactsByAuthorId(Connection txn,
AuthorId remote) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT contactId, name, publicKey,"
+ " localAuthorId, verified, active"
+ " FROM contacts"
+ " WHERE authorId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, remote.getBytes());
rs = ps.executeQuery();
List<Contact> contacts = new ArrayList<Contact>();
while (rs.next()) {
ContactId c = new ContactId(rs.getInt(1));
String name = rs.getString(2);
byte[] publicKey = rs.getBytes(3);
AuthorId localAuthorId = new AuthorId(rs.getBytes(4));
boolean verified = rs.getBoolean(5);
boolean active = rs.getBoolean(6);
Author author = new Author(remote, name, publicKey);
contacts.add(new Contact(c, author, localAuthorId, verified,
active));
}
rs.close();
ps.close();
return Collections.unmodifiableList(contacts);
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Group getGroup(Connection txn, GroupId g) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;

View File

@@ -130,17 +130,18 @@ class IdentityManagerImpl implements IdentityManager {
@Override
public Status getAuthorStatus(Transaction txn, AuthorId authorId)
throws DbException {
// Compare to the IDs of the user's identities
for (LocalAuthor a : db.getLocalAuthors(txn))
for (LocalAuthor a : db.getLocalAuthors(txn)) {
if (a.getId().equals(authorId)) return OURSELVES;
// Compare to the IDs of contacts' identities
for (Contact c : db.getContacts(txn)) {
if (c.getAuthor().getId().equals(authorId)) {
if (c.isVerified()) return VERIFIED;
else return UNVERIFIED;
}
}
return UNKNOWN;
Collection<Contact> contacts = db.getContactsByAuthorId(txn, authorId);
if (contacts.isEmpty()) return UNKNOWN;
for (Contact c : contacts) {
if (c.isVerified()) return VERIFIED;
}
return UNVERIFIED;
}
}