Never reuse contact IDs.

This commit is contained in:
akwizgran
2011-10-19 12:43:44 +01:00
parent c8b2cc38de
commit dbdbb1d8d4
3 changed files with 22 additions and 19 deletions

View File

@@ -38,7 +38,8 @@ class H2Database extends JdbcDatabase {
@DatabaseMaxSize long maxSize,
ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory) {
super(connectionWindowFactory, groupFactory, "BINARY(32)", "BINARY");
super(connectionWindowFactory, groupFactory, "BINARY(32)", "BINARY",
"INT NOT NULL AUTO_INCREMENT");
home = new File(dir, "db");
this.password = password;
url = "jdbc:h2:split:" + home.getPath()

View File

@@ -55,7 +55,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_CONTACTS =
"CREATE TABLE contacts"
+ " (contactId INT NOT NULL,"
+ " (contactId COUNTER,"
+ " secret BINARY NOT NULL,"
+ " PRIMARY KEY (contactId))";
@@ -228,7 +228,7 @@ abstract class JdbcDatabase implements Database<Connection> {
Logger.getLogger(JdbcDatabase.class.getName());
// Different database libraries use different names for certain types
private final String hashType, binaryType;
private final String hashType, binaryType, counterType;
private final ConnectionWindowFactory connectionWindowFactory;
private final GroupFactory groupFactory;
private final LinkedList<Connection> connections =
@@ -240,11 +240,13 @@ abstract class JdbcDatabase implements Database<Connection> {
protected abstract Connection createConnection() throws SQLException;
JdbcDatabase(ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory, String hashType, String binaryType) {
GroupFactory groupFactory, String hashType, String binaryType,
String counterType) {
this.connectionWindowFactory = connectionWindowFactory;
this.groupFactory = groupFactory;
this.hashType = hashType;
this.binaryType = binaryType;
this.counterType = counterType;
}
protected void open(boolean resume, File dir, String driverClass)
@@ -321,6 +323,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private String insertTypeNames(String s) {
s = s.replaceAll("HASH", hashType);
s = s.replaceAll("BINARY", binaryType);
s = s.replaceAll("COUNTER", counterType);
return s;
}
@@ -467,25 +470,24 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Get the highest existing contact ID
String sql = "SELECT contactId FROM contacts"
// Create a new contact row
String sql = "INSERT INTO contacts (secret) VALUES (?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, secret);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
// Get the new (highest) contact ID
sql = "SELECT contactId FROM contacts"
+ " ORDER BY contactId DESC LIMIT ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, 1);
rs = ps.executeQuery();
int nextId = rs.next() ? rs.getInt(1) + 1 : 1;
ContactId c = new ContactId(nextId);
if(!rs.next()) throw new DbStateException();
ContactId c = new ContactId(rs.getInt(1));
if(rs.next()) throw new DbStateException();
rs.close();
ps.close();
// Create a new contact row
sql = "INSERT INTO contacts (contactId, secret) VALUES (?, ?)";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, secret);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
// Store the contact's transport properties
sql = "INSERT INTO contactTransports"
+ " (contactId, transportId, key, value)"

View File

@@ -186,9 +186,9 @@ public class H2DatabaseTest extends TestCase {
assertFalse(db.containsContact(txn, contactId2));
assertEquals(contactId2, db.addContact(txn, transports, secret));
assertTrue(db.containsContact(txn, contactId2));
// Delete one of the contacts
db.removeContact(txn, contactId1);
assertFalse(db.containsContact(txn, contactId1));
// Delete the contact with the highest ID
db.removeContact(txn, contactId2);
assertFalse(db.containsContact(txn, contactId2));
// Add another contact - a new ID should be created
assertFalse(db.containsContact(txn, contactId3));
assertEquals(contactId3, db.addContact(txn, transports, secret));