Add client version to groups table.

This commit is contained in:
akwizgran
2018-04-13 13:10:02 +01:00
parent a38f39207f
commit 8c00f2417b
23 changed files with 116 additions and 67 deletions

View File

@@ -266,7 +266,8 @@ interface Database<T> {
* <p/>
* Read-only.
*/
Collection<Group> getGroups(T txn, ClientId c) throws DbException;
Collection<Group> getGroups(T txn, ClientId c, int clientVersion)
throws DbException;
/**
* Returns the given group's visibility to the given contact, or

View File

@@ -435,10 +435,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
@Override
public Collection<Group> getGroups(Transaction transaction, ClientId c)
throws DbException {
public Collection<Group> getGroups(Transaction transaction, ClientId c,
int clientVersion) throws DbException {
T txn = unbox(transaction);
return db.getGroups(txn, c);
return db.getGroups(txn, c, clientVersion);
}
@Override

View File

@@ -117,6 +117,7 @@ abstract class JdbcDatabase implements Database<Connection> {
"CREATE TABLE groups"
+ " (groupId _HASH NOT NULL,"
+ " clientId _STRING NOT NULL,"
+ " clientVersion INT NOT NULL,"
+ " descriptor _BINARY NOT NULL,"
+ " PRIMARY KEY (groupId))";
@@ -275,9 +276,9 @@ abstract class JdbcDatabase implements Database<Connection> {
"CREATE INDEX IF NOT EXISTS contactsByAuthorId"
+ " ON contacts (authorId)";
private static final String INDEX_GROUPS_BY_CLIENT_ID =
"CREATE INDEX IF NOT EXISTS groupsByClientId"
+ " ON groups (clientId)";
private static final String INDEX_GROUPS_BY_CLIENT_ID_CLIENT_VERSION =
"CREATE INDEX IF NOT EXISTS groupsByClientIdClientVersion"
+ " ON groups (clientId, clientVersion)";
private static final String INDEX_MESSAGE_METADATA_BY_GROUP_ID_STATE =
"CREATE INDEX IF NOT EXISTS messageMetadataByGroupIdState"
@@ -444,7 +445,7 @@ abstract class JdbcDatabase implements Database<Connection> {
try {
s = txn.createStatement();
s.executeUpdate(INDEX_CONTACTS_BY_AUTHOR_ID);
s.executeUpdate(INDEX_GROUPS_BY_CLIENT_ID);
s.executeUpdate(INDEX_GROUPS_BY_CLIENT_ID_CLIENT_VERSION);
s.executeUpdate(INDEX_MESSAGE_METADATA_BY_GROUP_ID_STATE);
s.executeUpdate(INDEX_MESSAGE_DEPENDENCIES_BY_DEPENDENCY_ID);
s.executeUpdate(INDEX_STATUSES_BY_CONTACT_ID_GROUP_ID);
@@ -612,12 +613,14 @@ abstract class JdbcDatabase implements Database<Connection> {
public void addGroup(Connection txn, Group g) throws DbException {
PreparedStatement ps = null;
try {
String sql = "INSERT INTO groups (groupId, clientId, descriptor)"
+ " VALUES (?, ?, ?)";
String sql = "INSERT INTO groups"
+ " (groupId, clientId, clientVersion, descriptor)"
+ " VALUES (?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, g.getId().getBytes());
ps.setString(2, g.getClientId().getString());
ps.setBytes(3, g.getDescriptor());
ps.setInt(3, g.getClientVersion());
ps.setBytes(4, g.getDescriptor());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
@@ -1346,17 +1349,18 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT clientId, descriptor FROM groups"
+ " WHERE groupId = ?";
String sql = "SELECT clientId, clientVersion, descriptor"
+ " FROM groups WHERE groupId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, g.getBytes());
rs = ps.executeQuery();
if (!rs.next()) throw new DbStateException();
ClientId clientId = new ClientId(rs.getString(1));
byte[] descriptor = rs.getBytes(2);
int clientVersion = rs.getInt(2);
byte[] descriptor = rs.getBytes(3);
rs.close();
ps.close();
return new Group(g, clientId, descriptor);
return new Group(g, clientId, clientVersion, descriptor);
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
@@ -1365,21 +1369,22 @@ abstract class JdbcDatabase implements Database<Connection> {
}
@Override
public Collection<Group> getGroups(Connection txn, ClientId c)
throws DbException {
public Collection<Group> getGroups(Connection txn, ClientId c,
int clientVersion) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT groupId, descriptor FROM groups"
+ " WHERE clientId = ?";
+ " WHERE clientId = ? AND clientVersion = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, c.getString());
ps.setInt(2, clientVersion);
rs = ps.executeQuery();
List<Group> groups = new ArrayList<>();
while (rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
byte[] descriptor = rs.getBytes(2);
groups.add(new Group(id, c, descriptor));
groups.add(new Group(id, c, clientVersion, descriptor));
}
rs.close();
ps.close();

View File

@@ -34,6 +34,6 @@ class GroupFactoryImpl implements GroupFactory {
byte[] hash = crypto.hash(LABEL, new byte[] {FORMAT_VERSION},
StringUtils.toUtf8(c.getString()), clientVersionBytes,
descriptor);
return new Group(new GroupId(hash), c, descriptor);
return new Group(new GroupId(hash), c, clientVersion, descriptor);
}
}