From 6cdf68d6cb3cd193f41ab65dc36987be8a8734d5 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Tue, 15 Nov 2011 13:45:57 +0000 Subject: [PATCH] Initialise all connection windows when a contact is added. --- components/net/sf/briar/db/JdbcDatabase.java | 88 ++++++++++++-------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java index 0ec9015392..458d39edc2 100644 --- a/components/net/sf/briar/db/JdbcDatabase.java +++ b/components/net/sf/briar/db/JdbcDatabase.java @@ -550,6 +550,44 @@ abstract class JdbcDatabase implements Database<Connection> { affected = ps.executeUpdate(); if(affected != 1) throw new DbStateException(); 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; } catch(SQLException e) { tryToClose(rs); @@ -903,42 +941,26 @@ abstract class JdbcDatabase implements Database<Connection> { PreparedStatement ps = null; ResultSet rs = null; 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 = ?"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); ps.setInt(2, i.getInt()); rs = ps.executeQuery(); - if(rs.next()) { - // A connection row exists - update it - long outgoing = rs.getLong(1); - if(rs.next()) throw new DbStateException(); - rs.close(); - ps.close(); - 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; - } + if(!rs.next()) throw new DbStateException(); + long outgoing = rs.getLong(1); + if(rs.next()) throw new DbStateException(); + rs.close(); + ps.close(); + return outgoing; } catch(SQLException e) { tryToClose(rs); tryToClose(ps); @@ -961,9 +983,7 @@ abstract class JdbcDatabase implements Database<Connection> { while(rs.next()) unseen.add(rs.getLong(1)); rs.close(); ps.close(); - if(unseen.isEmpty()) - return connectionWindowFactory.createConnectionWindow(); - else return connectionWindowFactory.createConnectionWindow(unseen); + return connectionWindowFactory.createConnectionWindow(unseen); } catch(SQLException e) { tryToClose(rs); tryToClose(ps); -- GitLab