Add a contactExists() method to the contactManager

This requires exposing the `containsContact()` method to the `DatabaseComponent`
and is needed for finding out efficiently whether a contact already exists.
This commit is contained in:
Torsten Grote
2016-02-18 14:46:43 -02:00
parent e2d64e0a8c
commit 5bde14c694
4 changed files with 44 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction; import org.briarproject.api.db.Transaction;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId; import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.identity.LocalAuthor;
import java.util.Collection; import java.util.Collection;
@@ -44,6 +45,14 @@ public interface ContactManager {
/** Marks a contact as active or inactive. */ /** Marks a contact as active or inactive. */
void setContactActive(ContactId c, boolean active) throws DbException; void setContactActive(ContactId c, boolean active) throws DbException;
/** Return true if a contact with this name and public key already exists */
boolean contactExists(Transaction txn, AuthorId remoteAuthorID,
AuthorId localAuthorId) throws DbException;
/** Return true if a contact with this name and public key already exists */
boolean contactExists(AuthorId remoteAuthorID, AuthorId localAuthorId)
throws DbException;
interface AddContactHook { interface AddContactHook {
void addingContact(Transaction txn, Contact c) throws DbException; void addingContact(Transaction txn, Contact c) throws DbException;
} }

View File

@@ -162,6 +162,13 @@ public interface DatabaseComponent {
Collection<ContactId> getContacts(Transaction txn, AuthorId a) Collection<ContactId> getContacts(Transaction txn, AuthorId a)
throws DbException; throws DbException;
/**
* Returns true if the database contains the given contact for the given
* local pseudonym.
*/
boolean containsContact(Transaction txn, AuthorId remote, AuthorId local)
throws DbException;
/** /**
* Returns the unique ID for this device. * Returns the unique ID for this device.
* <p/> * <p/>

View File

@@ -125,6 +125,26 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
} }
} }
@Override
public boolean contactExists(Transaction txn, AuthorId remoteAuthorID,
AuthorId localAuthorId) throws DbException {
return db.containsContact(txn, remoteAuthorID, localAuthorId);
}
@Override
public boolean contactExists(AuthorId remoteAuthorID,
AuthorId localAuthorId) throws DbException {
boolean exists = false;
Transaction txn = db.startTransaction(true);
try {
exists = contactExists(txn, remoteAuthorID, localAuthorId);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return exists;
}
private void removeContact(Transaction txn, ContactId c) private void removeContact(Transaction txn, ContactId c)
throws DbException { throws DbException {
Contact contact = db.getContact(txn, c); Contact contact = db.getContact(txn, c);

View File

@@ -342,6 +342,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return db.getContacts(txn, a); return db.getContacts(txn, a);
} }
public boolean containsContact(Transaction transaction, AuthorId remote,
AuthorId local) throws DbException {
T txn = unbox(transaction);
if (!db.containsLocalAuthor(txn, local))
throw new NoSuchLocalAuthorException();
return db.containsContact(txn, remote, local);
}
public DeviceId getDeviceId(Transaction transaction) throws DbException { public DeviceId getDeviceId(Transaction transaction) throws DbException {
T txn = unbox(transaction); T txn = unbox(transaction);
return db.getDeviceId(txn); return db.getDeviceId(txn);