Contact IDs are now auto-generated.

This commit is contained in:
akwizgran
2011-07-06 14:53:35 +01:00
parent 5e0d580d00
commit 9fbf0f21de
7 changed files with 133 additions and 34 deletions

View File

@@ -74,6 +74,13 @@ interface Database<T> {
*/
void addBatchToAck(T txn, ContactId c, BatchId b) throws DbException;
/**
* Adds a new contact to the database and returns an ID for the contact.
* <p>
* Locking: contacts write, messageStatuses write.
*/
ContactId addContact(T txn) throws DbException;
/**
* Returns false if the given message is already in the database. Otherwise
* stores the message and returns true.
@@ -82,13 +89,6 @@ interface Database<T> {
*/
boolean addMessage(T txn, Message m) throws DbException;
/**
* Adds a new contact to the database.
* <p>
* Locking: contacts write, messageStatuses write.
*/
void addContact(T txn, ContactId c) throws DbException;
/**
* Records a sent batch as needing to be acknowledged.
* <p>

View File

@@ -126,7 +126,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " (bundleId XXXX NOT NULL,"
+ " contactId INT NOT NULL,"
+ " timestamp BIGINT NOT NULL,"
+ " PRIMARY KEY (bundleId),"
+ " PRIMARY KEY (bundleId, contactId),"
+ " FOREIGN KEY (contactId) REFERENCES contacts (contactId)"
+ " ON DELETE CASCADE)";
@@ -363,10 +363,24 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void addContact(Connection txn, ContactId c) throws DbException {
public ContactId addContact(Connection txn) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "INSERT INTO contacts"
// Get the highest existing contact ID
String 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);
boolean more = rs.next();
assert !more;
rs.close();
ps.close();
// Create a new contact row
sql = "INSERT INTO contacts"
+ " (contactId, lastBundleReceived)"
+ " VALUES (?, ?)";
ps = txn.prepareStatement(sql);
@@ -375,6 +389,7 @@ abstract class JdbcDatabase implements Database<Connection> {
int rowsAffected = ps.executeUpdate();
assert rowsAffected == 1;
ps.close();
// Create a dummy received bundle row for BundleId.NONE
sql = "INSERT INTO receivedBundles"
+ " (bundleId, contactId, timestamp)"
+ " VALUES (?, ?, ?)";
@@ -385,6 +400,7 @@ abstract class JdbcDatabase implements Database<Connection> {
rowsAffected = ps.executeUpdate();
assert rowsAffected == 1;
ps.close();
return c;
} catch(SQLException e) {
tryToClose(ps);
tryToClose(txn);

View File

@@ -83,16 +83,19 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public void addContact(ContactId c) throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact " + c);
public ContactId addContact() throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
contactLock.writeLock().lock();
try {
messageStatusLock.writeLock().lock();
try {
Txn txn = db.startTransaction();
try {
db.addContact(txn, c);
ContactId c = db.addContact(txn);
db.commitTransaction(txn);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added contact " + c);
return c;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
@@ -307,6 +310,28 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public Set<ContactId> getContacts() throws DbException {
contactLock.readLock().lock();
try {
messageStatusLock.readLock().lock();
try {
Txn txn = db.startTransaction();
try {
Set<ContactId> contacts = db.getContacts(txn);
db.commitTransaction(txn);
return contacts;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
messageStatusLock.readLock().unlock();
}
} finally {
contactLock.readLock().unlock();
}
}
public Rating getRating(AuthorId a) throws DbException {
ratingLock.readLock().lock();
try {
@@ -329,8 +354,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
Txn txn = db.startTransaction();
try {
HashSet<GroupId> subs = new HashSet<GroupId>();
for(GroupId g : db.getSubscriptions(txn)) subs.add(g);
Set<GroupId> subs = db.getSubscriptions(txn);
db.commitTransaction(txn);
return subs;
} catch(DbException e) {

View File

@@ -62,14 +62,17 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public void addContact(ContactId c) throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact " + c);
public ContactId addContact() throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
synchronized(contactLock) {
synchronized(messageStatusLock) {
Txn txn = db.startTransaction();
try {
db.addContact(txn, c);
ContactId c = db.addContact(txn);
db.commitTransaction(txn);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added contact " + c);
return c;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
@@ -224,6 +227,22 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public Set<ContactId> getContacts() throws DbException {
synchronized(contactLock) {
synchronized(messageStatusLock) {
Txn txn = db.startTransaction();
try {
Set<ContactId> contacts = db.getContacts(txn);
db.commitTransaction(txn);
return contacts;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
}
}
}
public Rating getRating(AuthorId a) throws DbException {
synchronized(ratingLock) {
Txn txn = db.startTransaction();
@@ -242,8 +261,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
synchronized(subscriptionLock) {
Txn txn = db.startTransaction();
try {
HashSet<GroupId> subs = new HashSet<GroupId>();
for(GroupId g : db.getSubscriptions(txn)) subs.add(g);
Set<GroupId> subs = db.getSubscriptions(txn);
db.commitTransaction(txn);
return subs;
} catch(DbException e) {