mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Initialise all connection windows when a contact is added.
This commit is contained in:
@@ -550,6 +550,44 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
affected = ps.executeUpdate();
|
affected = ps.executeUpdate();
|
||||||
if(affected != 1) throw new DbStateException();
|
if(affected != 1) throw new DbStateException();
|
||||||
ps.close();
|
ps.close();
|
||||||
|
// Initialise the connection numbers for all transports
|
||||||
|
sql = "INSERT INTO connections (contactId, index, outgoing)"
|
||||||
|
+ " VALUES (?, ?, ZERO())";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
for(int i = 0; i < ProtocolConstants.MAX_TRANSPORTS; i++) {
|
||||||
|
ps.setInt(2, i);
|
||||||
|
ps.addBatch();
|
||||||
|
}
|
||||||
|
int[] batchAffected = ps.executeBatch();
|
||||||
|
if(batchAffected.length != ProtocolConstants.MAX_TRANSPORTS)
|
||||||
|
throw new DbStateException();
|
||||||
|
for(int i = 0; i < batchAffected.length; i++) {
|
||||||
|
if(batchAffected[i] != 1) throw new DbStateException();
|
||||||
|
}
|
||||||
|
ps.close();
|
||||||
|
// Initialise the connection windows for all transports
|
||||||
|
sql = "INSERT INTO connectionWindows (contactId, index, unseen)"
|
||||||
|
+ " VALUES (?, ?, ?)";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
int batchSize = 0;
|
||||||
|
for(int i = 0; i < ProtocolConstants.MAX_TRANSPORTS; i++) {
|
||||||
|
ps.setInt(2, i);
|
||||||
|
ConnectionWindow w =
|
||||||
|
connectionWindowFactory.createConnectionWindow();
|
||||||
|
for(long l : w.getUnseen()) {
|
||||||
|
ps.setLong(3, l);
|
||||||
|
ps.addBatch();
|
||||||
|
batchSize++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
batchAffected = ps.executeBatch();
|
||||||
|
if(batchAffected.length != batchSize) throw new DbStateException();
|
||||||
|
for(int i = 0; i < batchAffected.length; i++) {
|
||||||
|
if(batchAffected[i] != 1) throw new DbStateException();
|
||||||
|
}
|
||||||
|
ps.close();
|
||||||
return c;
|
return c;
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
@@ -903,42 +941,26 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT outgoing FROM connections"
|
String sql = "UPDATE connections SET outgoing = outgoing + 1"
|
||||||
|
+ " WHERE contactId = ? AND index = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
ps.setInt(2, i.getInt());
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if(affected != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
sql = "SELECT outgoing FROM connections"
|
||||||
+ " WHERE contactId = ? AND index = ?";
|
+ " WHERE contactId = ? AND index = ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setInt(1, c.getInt());
|
ps.setInt(1, c.getInt());
|
||||||
ps.setInt(2, i.getInt());
|
ps.setInt(2, i.getInt());
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
if(rs.next()) {
|
if(!rs.next()) throw new DbStateException();
|
||||||
// A connection row exists - update it
|
long outgoing = rs.getLong(1);
|
||||||
long outgoing = rs.getLong(1);
|
if(rs.next()) throw new DbStateException();
|
||||||
if(rs.next()) throw new DbStateException();
|
rs.close();
|
||||||
rs.close();
|
ps.close();
|
||||||
ps.close();
|
return outgoing;
|
||||||
sql = "UPDATE connections SET outgoing = ?"
|
|
||||||
+ " WHERE contactId = ? AND index = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setLong(1, outgoing + 1);
|
|
||||||
ps.setInt(2, c.getInt());
|
|
||||||
ps.setInt(3, i.getInt());
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if(affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
return outgoing;
|
|
||||||
} else {
|
|
||||||
// No connection row exists - create one
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
sql = "INSERT INTO connections (contactId, index, outgoing)"
|
|
||||||
+ " VALUES(?, ?, ZERO())";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
ps.setInt(2, i.getInt());
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if(affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
return 0L;
|
|
||||||
}
|
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
tryToClose(ps);
|
tryToClose(ps);
|
||||||
@@ -961,9 +983,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
while(rs.next()) unseen.add(rs.getLong(1));
|
while(rs.next()) unseen.add(rs.getLong(1));
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
if(unseen.isEmpty())
|
return connectionWindowFactory.createConnectionWindow(unseen);
|
||||||
return connectionWindowFactory.createConnectionWindow();
|
|
||||||
else return connectionWindowFactory.createConnectionWindow(unseen);
|
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
tryToClose(ps);
|
tryToClose(ps);
|
||||||
|
|||||||
Reference in New Issue
Block a user