mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
DB interface cleanup, removed unnecessary exceptions.
This commit is contained in:
@@ -124,20 +124,20 @@ interface Database<T> {
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a transport and returns true if the transport was not previously
|
||||
* in the database.
|
||||
* Stores a transport.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
boolean addTransport(T txn, TransportId t, int maxLatency)
|
||||
void addTransport(T txn, TransportId t, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given transport keys for a newly added contact.
|
||||
* Stores transport keys for a newly added contact.
|
||||
* <p>
|
||||
* Locking: write.
|
||||
*/
|
||||
void addTransportKeys(T txn, ContactId c, TransportKeys k) throws DbException;
|
||||
void addTransportKeys(T txn, ContactId c, TransportKeys k)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Makes a group visible to the given contact.
|
||||
|
||||
@@ -7,8 +7,6 @@ import org.briarproject.api.contact.ContactId;
|
||||
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.NoSuchGroupException;
|
||||
@@ -168,8 +166,8 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (!db.containsGroup(txn, g.getId())) {
|
||||
added = true;
|
||||
db.addGroup(txn, g);
|
||||
added = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
@@ -187,9 +185,9 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (db.containsLocalAuthor(txn, a.getId()))
|
||||
throw new LocalAuthorExistsException();
|
||||
db.addLocalAuthor(txn, a);
|
||||
if (!db.containsLocalAuthor(txn, a.getId())) {
|
||||
db.addLocalAuthor(txn, a);
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -202,15 +200,17 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
|
||||
public void addLocalMessage(Message m, ClientId c, Metadata meta,
|
||||
boolean shared) throws DbException {
|
||||
boolean added = false;
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if (db.containsMessage(txn, m.getId()))
|
||||
throw new MessageExistsException();
|
||||
if (!db.containsGroup(txn, m.getGroupId()))
|
||||
throw new NoSuchGroupException();
|
||||
addMessage(txn, m, VALID, shared, null);
|
||||
if (!db.containsMessage(txn, m.getId())) {
|
||||
addMessage(txn, m, VALID, shared, null);
|
||||
added = true;
|
||||
}
|
||||
db.mergeMessageMetadata(txn, m.getId(), meta);
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
@@ -220,8 +220,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
eventBus.broadcast(new MessageAddedEvent(m, null));
|
||||
eventBus.broadcast(new MessageValidatedEvent(m, c, true, true));
|
||||
if (added) {
|
||||
eventBus.broadcast(new MessageAddedEvent(m, null));
|
||||
eventBus.broadcast(new MessageValidatedEvent(m, c, true, true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,14 +250,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addTransport(TransportId t, int maxLatency)
|
||||
throws DbException {
|
||||
boolean added;
|
||||
public void addTransport(TransportId t, int maxLatency) throws DbException {
|
||||
boolean added = false;
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
added = db.addTransport(txn, t, maxLatency);
|
||||
if (!db.containsTransport(txn, t)) {
|
||||
db.addTransport(txn, t, maxLatency);
|
||||
added = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -265,7 +269,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
if (added) eventBus.broadcast(new TransportAddedEvent(t, maxLatency));
|
||||
return added;
|
||||
}
|
||||
|
||||
public void addTransportKeys(ContactId c, TransportKeys k)
|
||||
@@ -909,8 +912,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
if (visible) {
|
||||
if (!duplicate)
|
||||
eventBus.broadcast(new MessageAddedEvent(m, c));
|
||||
if (!duplicate) eventBus.broadcast(new MessageAddedEvent(m, c));
|
||||
eventBus.broadcast(new MessageToAckEvent(c));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -661,23 +661,11 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addTransport(Connection txn, TransportId t, int maxLatency)
|
||||
public void addTransport(Connection txn, TransportId t, int maxLatency)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
// Return false if the transport is already in the database
|
||||
String sql = "SELECT NULL FROM transports WHERE transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
rs = ps.executeQuery();
|
||||
boolean found = rs.next();
|
||||
if (rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
if (found) return false;
|
||||
// Create a transport row
|
||||
sql = "INSERT INTO transports (transportId, maxLatency)"
|
||||
String sql = "INSERT INTO transports (transportId, maxLatency)"
|
||||
+ " VALUES (?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, t.getString());
|
||||
@@ -685,10 +673,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
tryToClose(rs);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user