Small improvements to DB interface.

This commit is contained in:
akwizgran
2016-01-19 10:55:46 +00:00
parent 5355951466
commit 77054cbae7
9 changed files with 99 additions and 79 deletions

View File

@@ -92,7 +92,7 @@ interface Database<T> {
* <p>
* Locking: write.
*/
void addGroup(T txn, ContactId c, Group g) throws DbException;
void addContactGroup(T txn, ContactId c, Group g) throws DbException;
/**
* Subscribes to a group, or returns false if the user already has the
@@ -225,11 +225,12 @@ interface Database<T> {
int countOfferedMessages(T txn, ContactId c) throws DbException;
/**
* Returns all groups to which the user could subscribe.
* Returns all groups belonging to the given client to which the user could
* subscribe.
* <p>
* Locking: read.
*/
Collection<Group> getAvailableGroups(T txn) throws DbException;
Collection<Group> getAvailableGroups(T txn, ClientId c) throws DbException;
/**
* Returns the contact with the given ID.
@@ -274,11 +275,12 @@ interface Database<T> {
Group getGroup(T txn, GroupId g) throws DbException;
/**
* Returns all groups to which the user subscribes.
* Returns all groups belonging to the given client to which the user
* subscribes.
* <p>
* Locking: read.
*/
Collection<Group> getGroups(T txn) throws DbException;
Collection<Group> getGroups(T txn, ClientId c) throws DbException;
/**
* Returns the local pseudonym with the given ID.

View File

@@ -9,6 +9,7 @@ import org.briarproject.api.db.ContactExistsException;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.LocalAuthorExistsException;
import org.briarproject.api.db.MessageExistsException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.NoSuchLocalAuthorException;
@@ -168,12 +169,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return c;
}
public void addGroup(ContactId c, Group g) throws DbException {
public void addContactGroup(ContactId c, Group g) throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
db.addGroup(txn, c, g);
db.addContactGroup(txn, c, g);
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
@@ -225,17 +226,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void addLocalMessage(Message m, ClientId c, Metadata meta)
throws DbException {
boolean duplicate, subscribed;
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
duplicate = db.containsMessage(txn, m.getId());
subscribed = db.containsGroup(txn, m.getGroupId());
if (!duplicate && subscribed) {
addMessage(txn, m, null);
db.mergeMessageMetadata(txn, m.getId(), meta);
}
if (db.containsMessage(txn, m.getId()))
throw new MessageExistsException();
if (!db.containsGroup(txn, m.getGroupId()))
throw new NoSuchSubscriptionException();
addMessage(txn, m, null);
db.mergeMessageMetadata(txn, m.getId(), meta);
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
@@ -244,10 +244,8 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} finally {
lock.writeLock().unlock();
}
if (!duplicate && subscribed) {
eventBus.broadcast(new MessageAddedEvent(m, null));
eventBus.broadcast(new MessageValidatedEvent(m, c, true, true));
}
eventBus.broadcast(new MessageAddedEvent(m, null));
eventBus.broadcast(new MessageValidatedEvent(m, c, true, true));
}
/**
@@ -524,12 +522,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public Collection<Group> getAvailableGroups() throws DbException {
public Collection<Group> getAvailableGroups(ClientId c) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Collection<Group> groups = db.getAvailableGroups(txn);
Collection<Group> groups = db.getAvailableGroups(txn, c);
db.commitTransaction(txn);
return groups;
} catch (DbException e) {
@@ -596,12 +594,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public Collection<Group> getGroups() throws DbException {
public Collection<Group> getGroups(ClientId c) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Collection<Group> groups = db.getGroups(txn);
Collection<Group> groups = db.getGroups(txn, c);
db.commitTransaction(txn);
return groups;
} catch (DbException e) {

View File

@@ -647,7 +647,7 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void addGroup(Connection txn, ContactId c, Group g)
public void addContactGroup(Connection txn, ContactId c, Group g)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
@@ -1155,28 +1155,28 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<Group> getAvailableGroups(Connection txn)
public Collection<Group> getAvailableGroups(Connection txn, ClientId c)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT DISTINCT"
+ " cg.groupId, cg.clientId, cg.descriptor"
String sql = "SELECT DISTINCT cg.groupId, cg.descriptor"
+ " FROM contactGroups AS cg"
+ " LEFT OUTER JOIN groups AS g"
+ " ON cg.groupId = g.groupId"
+ " WHERE g.groupId IS NULL"
+ " WHERE cg.clientId = ?"
+ " AND g.groupId IS NULL"
+ " GROUP BY cg.groupId";
ps = txn.prepareStatement(sql);
ps.setBytes(1, c.getBytes());
rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>();
Set<GroupId> ids = new HashSet<GroupId>();
while (rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
if (!ids.add(id)) throw new DbStateException();
ClientId clientId = new ClientId(rs.getBytes(2));
byte[] descriptor = rs.getBytes(3);
groups.add(new Group(id, clientId, descriptor));
byte[] descriptor = rs.getBytes(2);
groups.add(new Group(id, c, descriptor));
}
rs.close();
ps.close();
@@ -1308,19 +1308,21 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<Group> getGroups(Connection txn) throws DbException {
public Collection<Group> getGroups(Connection txn, ClientId c)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT groupId, clientId, descriptor FROM groups";
String sql = "SELECT groupId, descriptor FROM groups"
+ " WHERE clientId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, c.getBytes());
rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>();
while (rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
ClientId clientId = new ClientId(rs.getBytes(2));
byte[] descriptor = rs.getBytes(3);
groups.add(new Group(id, clientId, descriptor));
byte[] descriptor = rs.getBytes(2);
groups.add(new Group(id, c, descriptor));
}
rs.close();
ps.close();

View File

@@ -143,17 +143,13 @@ class ForumManagerImpl implements ForumManager {
@Override
public Collection<Forum> getAvailableForums() throws DbException {
// TODO: Get groups by client ID
Collection<Group> groups = db.getAvailableGroups();
Collection<Group> groups = db.getAvailableGroups(CLIENT_ID);
List<Forum> forums = new ArrayList<Forum>(groups.size());
for (Group g : groups) {
if (g.getClientId().equals(CLIENT_ID)) {
try {
forums.add(parseForum(g));
} catch (FormatException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
try {
forums.add(parseForum(g));
} catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
return Collections.unmodifiableList(forums);
@@ -193,17 +189,13 @@ class ForumManagerImpl implements ForumManager {
@Override
public Collection<Forum> getForums() throws DbException {
// TODO: Get groups by client ID
Collection<Group> groups = db.getGroups();
Collection<Group> groups = db.getGroups(CLIENT_ID);
List<Forum> forums = new ArrayList<Forum>(groups.size());
for (Group g : groups) {
if (g.getClientId().equals(CLIENT_ID)) {
try {
forums.add(parseForum(g));
} catch (FormatException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
try {
forums.add(parseForum(g));
} catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
return Collections.unmodifiableList(forums);

View File

@@ -82,7 +82,7 @@ class MessagingManagerImpl implements MessagingManager {
Group conversation = createConversationGroup(db.getContact(c));
// Subscribe to the group and share it with the contact
db.addGroup(conversation);
db.addGroup(c, conversation);
db.addContactGroup(c, conversation);
db.setVisibility(conversation.getId(), Collections.singletonList(c));
}
@@ -141,7 +141,6 @@ class MessagingManagerImpl implements MessagingManager {
@Override
public GroupId getConversationId(ContactId c) throws DbException {
// TODO: Make this more efficient
return createConversationGroup(db.getContact(c)).getId();
}