Merged flags table into messages table (they now use the same lock).

This commit is contained in:
akwizgran
2013-01-29 17:51:54 +00:00
parent ea339e76d6
commit fe153b0815

View File

@@ -116,6 +116,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " raw BLOB NOT NULL," + " raw BLOB NOT NULL,"
+ " sendability INT," // Null for private messages + " sendability INT," // Null for private messages
+ " contactId INT," // Null for group messages + " contactId INT," // Null for group messages
+ " read BOOLEAN NOT NULL,"
+ " starred BOOLEAN NOT NULL,"
+ " PRIMARY KEY (messageId)," + " PRIMARY KEY (messageId),"
+ " FOREIGN KEY (groupId)" + " FOREIGN KEY (groupId)"
+ " REFERENCES groups (groupId)" + " REFERENCES groups (groupId)"
@@ -166,17 +168,6 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String INDEX_STATUSES_BY_CONTACT = private static final String INDEX_STATUSES_BY_CONTACT =
"CREATE INDEX statusesByContact ON statuses (contactId)"; "CREATE INDEX statusesByContact ON statuses (contactId)";
// Locking: message
private static final String CREATE_FLAGS =
"CREATE TABLE flags"
+ " (messageId HASH NOT NULL,"
+ " read BOOLEAN NOT NULL,"
+ " starred BOOLEAN NOT NULL,"
+ " PRIMARY KEY (messageId),"
+ " FOREIGN KEY (messageId)"
+ " REFERENCES messages (messageId)"
+ " ON DELETE CASCADE)";
// Locking: rating // Locking: rating
private static final String CREATE_RATINGS = private static final String CREATE_RATINGS =
"CREATE TABLE ratings" "CREATE TABLE ratings"
@@ -364,7 +355,6 @@ abstract class JdbcDatabase implements Database<Connection> {
s.executeUpdate(insertTypeNames(CREATE_STATUSES)); s.executeUpdate(insertTypeNames(CREATE_STATUSES));
s.executeUpdate(INDEX_STATUSES_BY_MESSAGE); s.executeUpdate(INDEX_STATUSES_BY_MESSAGE);
s.executeUpdate(INDEX_STATUSES_BY_CONTACT); s.executeUpdate(INDEX_STATUSES_BY_CONTACT);
s.executeUpdate(insertTypeNames(CREATE_FLAGS));
s.executeUpdate(insertTypeNames(CREATE_RATINGS)); s.executeUpdate(insertTypeNames(CREATE_RATINGS));
s.executeUpdate(insertTypeNames(CREATE_RETENTION_VERSIONS)); s.executeUpdate(insertTypeNames(CREATE_RETENTION_VERSIONS));
s.executeUpdate(insertTypeNames(CREATE_TRANSPORTS)); s.executeUpdate(insertTypeNames(CREATE_TRANSPORTS));
@@ -593,8 +583,9 @@ abstract class JdbcDatabase implements Database<Connection> {
try { try {
String sql = "INSERT INTO messages (messageId, parentId, groupId," String sql = "INSERT INTO messages (messageId, parentId, groupId,"
+ " authorId, subject, timestamp, length, bodyStart," + " authorId, subject, timestamp, length, bodyStart,"
+ " bodyLength, raw, sendability)" + " bodyLength, raw, sendability, read, starred)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ZERO())"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ZERO(), FALSE,"
+ " FALSE)";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getId().getBytes()); ps.setBytes(1, m.getId().getBytes());
if(m.getParent() == null) ps.setNull(2, BINARY); if(m.getParent() == null) ps.setNull(2, BINARY);
@@ -686,8 +677,8 @@ abstract class JdbcDatabase implements Database<Connection> {
try { try {
String sql = "INSERT INTO messages" String sql = "INSERT INTO messages"
+ " (messageId, parentId, subject, timestamp, length," + " (messageId, parentId, subject, timestamp, length,"
+ " bodyStart, bodyLength, raw, contactId)" + " bodyStart, bodyLength, raw, contactId, read, starred)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, FALSE, FALSE)";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getId().getBytes()); ps.setBytes(1, m.getId().getBytes());
if(m.getParent() == null) ps.setNull(2, BINARY); if(m.getParent() == null) ps.setNull(2, BINARY);
@@ -1140,8 +1131,6 @@ abstract class JdbcDatabase implements Database<Connection> {
String sql = "SELECT m.messageId, parentId, authorId," String sql = "SELECT m.messageId, parentId, authorId,"
+ " subject, timestamp, read, starred" + " subject, timestamp, read, starred"
+ " FROM messages AS m" + " FROM messages AS m"
+ " LEFT OUTER JOIN flags AS f"
+ " ON m.messageId = f.messageId"
+ " WHERE groupId = ?"; + " WHERE groupId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, g.getBytes()); ps.setBytes(1, g.getBytes());
@@ -1421,7 +1410,7 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT read FROM flags WHERE messageId = ?"; String sql = "SELECT read FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes()); ps.setBytes(1, m.getBytes());
rs = ps.executeQuery(); rs = ps.executeQuery();
@@ -1667,7 +1656,7 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT starred FROM flags WHERE messageId = ?"; String sql = "SELECT starred FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes()); ps.setBytes(1, m.getBytes());
rs = ps.executeQuery(); rs = ps.executeQuery();
@@ -1894,9 +1883,7 @@ abstract class JdbcDatabase implements Database<Connection> {
try { try {
String sql = "SELECT groupId, COUNT(*)" String sql = "SELECT groupId, COUNT(*)"
+ " FROM messages AS m" + " FROM messages AS m"
+ " LEFT OUTER JOIN flags AS f" + " WHERE read = FALSE"
+ " ON m.messageId = f.messageId"
+ " WHERE (NOT read) OR (read IS NULL)"
+ " GROUP BY groupId"; + " GROUP BY groupId";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
rs = ps.executeQuery(); rs = ps.executeQuery();
@@ -2432,44 +2419,24 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT read FROM flags WHERE messageId = ?"; String sql = "SELECT read FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes()); ps.setBytes(1, m.getBytes());
rs = ps.executeQuery(); rs = ps.executeQuery();
boolean old; if(!rs.next()) throw new DbStateException();
if(rs.next()) { boolean wasRead = rs.getBoolean(1);
// A flag row exists - update it if necessary if(rs.next()) throw new DbStateException();
old = rs.getBoolean(1); rs.close();
if(rs.next()) throw new DbStateException(); ps.close();
rs.close(); if(wasRead == read) return read;
ps.close(); sql = "UPDATE messages SET read = ? WHERE messageId = ?";
if(old != read) { ps = txn.prepareStatement(sql);
sql = "UPDATE flags SET read = ? WHERE messageId = ?"; ps.setBoolean(1, read);
ps = txn.prepareStatement(sql); ps.setBytes(2, m.getBytes());
ps.setBoolean(1, read); int affected = ps.executeUpdate();
ps.setBytes(2, m.getBytes()); if(affected != 1) throw new DbStateException();
int affected = ps.executeUpdate(); ps.close();
if(affected != 1) throw new DbStateException(); return !read;
ps.close();
}
} else {
// No flag row exists - create one if necessary
ps.close();
rs.close();
old = false;
if(old != read) {
sql = "INSERT INTO flags (messageId, read, starred)"
+ " VALUES (?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
ps.setBoolean(2, read);
ps.setBoolean(3, false);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
}
}
return old;
} catch(SQLException e) { } catch(SQLException e) {
tryToClose(rs); tryToClose(rs);
tryToClose(ps); tryToClose(ps);
@@ -2580,44 +2547,24 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT starred FROM flags WHERE messageId = ?"; String sql = "SELECT starred FROM messages WHERE messageId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes()); ps.setBytes(1, m.getBytes());
rs = ps.executeQuery(); rs = ps.executeQuery();
boolean old; if(!rs.next()) throw new DbStateException();
if(rs.next()) { boolean wasStarred = rs.getBoolean(1);
// A flag row exists - update it if necessary if(rs.next()) throw new DbStateException();
old = rs.getBoolean(1); rs.close();
if(rs.next()) throw new DbStateException(); ps.close();
rs.close(); if(wasStarred == starred) return starred;
ps.close(); sql = "UPDATE messages SET starred = ? WHERE messageId = ?";
if(old != starred) { ps = txn.prepareStatement(sql);
sql = "UPDATE flags SET starred = ? WHERE messageId = ?"; ps.setBoolean(1, starred);
ps = txn.prepareStatement(sql); ps.setBytes(2, m.getBytes());
ps.setBoolean(1, starred); int affected = ps.executeUpdate();
ps.setBytes(2, m.getBytes()); if(affected != 1) throw new DbStateException();
int affected = ps.executeUpdate(); ps.close();
if(affected != 1) throw new DbStateException(); return !starred;
ps.close();
}
} else {
// No flag row exists - create one if necessary
ps.close();
rs.close();
old = false;
if(old != starred) {
sql = "INSERT INTO flags (messageId, read, starred)"
+ " VALUES (?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes());
ps.setBoolean(2, false);
ps.setBoolean(3, starred);
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
}
}
return old;
} catch(SQLException e) { } catch(SQLException e) {
tryToClose(rs); tryToClose(rs);
tryToClose(ps); tryToClose(ps);