mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Small improvements to DB interface.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user