mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Add client version to groups table.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final Object txn = new Object();
|
||||
private final ClientId clientId;
|
||||
private final int clientVersion;
|
||||
private final GroupId groupId;
|
||||
private final Group group;
|
||||
private final Author author;
|
||||
@@ -106,7 +107,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
public DatabaseComponentImplTest() {
|
||||
clientId = getClientId();
|
||||
group = getGroup(clientId);
|
||||
clientVersion = 123;
|
||||
group = getGroup(clientId, clientVersion);
|
||||
groupId = group.getId();
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
@@ -175,7 +177,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
oneOf(database).containsGroup(txn, groupId);
|
||||
will(returnValue(true));
|
||||
// getGroups()
|
||||
oneOf(database).getGroups(txn, clientId);
|
||||
oneOf(database).getGroups(txn, clientId, clientVersion);
|
||||
will(returnValue(singletonList(group)));
|
||||
// removeGroup()
|
||||
oneOf(database).containsGroup(txn, groupId);
|
||||
@@ -215,7 +217,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
db.addGroup(transaction, group); // First time - listeners called
|
||||
db.addGroup(transaction, group); // Second time - not called
|
||||
assertEquals(singletonList(group),
|
||||
db.getGroups(transaction, clientId));
|
||||
db.getGroups(transaction, clientId, clientVersion));
|
||||
db.removeGroup(transaction, group);
|
||||
db.removeContact(transaction, contactId);
|
||||
db.removeLocalAuthor(transaction, localAuthor.getId());
|
||||
|
||||
@@ -267,7 +267,7 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
String name = "getGroups(T, ClientId)";
|
||||
benchmark(name, db -> {
|
||||
Connection txn = db.startTransaction();
|
||||
db.getGroups(txn, pickRandom(clientIds));
|
||||
db.getGroups(txn, pickRandom(clientIds), 123);
|
||||
db.commitTransaction(txn);
|
||||
});
|
||||
}
|
||||
@@ -550,7 +550,7 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
contacts.add(db.getContact(txn, c));
|
||||
contactGroups.put(c, new ArrayList<>());
|
||||
for (int j = 0; j < GROUPS_PER_CONTACT; j++) {
|
||||
Group g = getGroup(clientIds.get(j % CLIENTS));
|
||||
Group g = getGroup(clientIds.get(j % CLIENTS), 123);
|
||||
groups.add(g);
|
||||
messageMeta.put(g.getId(), new ArrayList<>());
|
||||
contactGroups.get(c).add(g);
|
||||
@@ -584,7 +584,7 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < LOCAL_GROUPS; i++) {
|
||||
Group g = getGroup(clientIds.get(i % CLIENTS));
|
||||
Group g = getGroup(clientIds.get(i % CLIENTS), 123);
|
||||
groups.add(g);
|
||||
messageMeta.put(g.getId(), new ArrayList<>());
|
||||
groupMessages.put(g.getId(), new ArrayList<>());
|
||||
|
||||
@@ -82,6 +82,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
private final GroupId groupId;
|
||||
private final ClientId clientId;
|
||||
private final int clientVersion;
|
||||
private final Group group;
|
||||
private final Author author;
|
||||
private final LocalAuthor localAuthor;
|
||||
@@ -96,7 +97,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
JdbcDatabaseTest() throws Exception {
|
||||
clientId = getClientId();
|
||||
group = getGroup(clientId);
|
||||
clientVersion = 123;
|
||||
group = getGroup(clientId, clientVersion);
|
||||
groupId = group.getId();
|
||||
author = getAuthor();
|
||||
localAuthor = getLocalAuthor();
|
||||
@@ -1460,7 +1462,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addMessage(txn, message, PENDING, true, contactId);
|
||||
|
||||
// Add a second group
|
||||
Group group1 = getGroup(clientId);
|
||||
Group group1 = getGroup(clientId, 123);
|
||||
GroupId groupId1 = group1.getId();
|
||||
db.addGroup(txn, group1);
|
||||
|
||||
@@ -1828,6 +1830,22 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetGroups() throws Exception {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
assertEquals(emptyList(), db.getGroups(txn, clientId, clientVersion));
|
||||
db.addGroup(txn, group);
|
||||
assertEquals(singletonList(group),
|
||||
db.getGroups(txn, clientId, clientVersion));
|
||||
db.removeGroup(txn, groupId);
|
||||
assertEquals(emptyList(), db.getGroups(txn, clientId, clientVersion));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionHandling() throws Exception {
|
||||
Database<Connection> db = open(false);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
context.mock(ContactGroupFactory.class);
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
|
||||
private final Group localGroup = getGroup(CLIENT_ID);
|
||||
private final Group localGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
private final LocalAuthor localAuthor = getLocalAuthor();
|
||||
private final BdfDictionary fooPropertiesDict = BdfDictionary.of(
|
||||
new BdfEntry("fooKey1", "fooValue1"),
|
||||
@@ -90,8 +90,8 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Contact contact1 = getContact(true);
|
||||
Contact contact2 = getContact(true);
|
||||
List<Contact> contacts = Arrays.asList(contact1, contact2);
|
||||
Group contactGroup1 = getGroup(CLIENT_ID);
|
||||
Group contactGroup2 = getGroup(CLIENT_ID);
|
||||
Group contactGroup1 = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
Group contactGroup2 = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).containsGroup(txn, localGroup.getId());
|
||||
@@ -144,7 +144,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
public void testCreatesContactGroupWhenAddingContact() throws Exception {
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Contact contact = getContact(true);
|
||||
Group contactGroup = getGroup(CLIENT_ID);
|
||||
Group contactGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// Create the group and share it with the contact
|
||||
@@ -172,7 +172,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
public void testRemovesGroupWhenRemovingContact() throws Exception {
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Contact contact = getContact(true);
|
||||
Group contactGroup = getGroup(CLIENT_ID);
|
||||
Group contactGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
@@ -307,7 +307,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testStoresRemotePropertiesWithVersion0() throws Exception {
|
||||
Contact contact = getContact(true);
|
||||
Group contactGroup = getGroup(CLIENT_ID);
|
||||
Group contactGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Map<TransportId, TransportProperties> properties =
|
||||
new LinkedHashMap<>();
|
||||
@@ -421,8 +421,8 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Contact contact3 = getContact(true);
|
||||
List<Contact> contacts =
|
||||
Arrays.asList(contact1, contact2, contact3);
|
||||
Group contactGroup2 = getGroup(CLIENT_ID);
|
||||
Group contactGroup3 = getGroup(CLIENT_ID);
|
||||
Group contactGroup2 = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
Group contactGroup3 = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
Map<MessageId, BdfDictionary> messageMetadata3 =
|
||||
new LinkedHashMap<>();
|
||||
// A remote update for another transport should be ignored
|
||||
@@ -524,7 +524,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
public void testMergingNewPropertiesCreatesUpdate() throws Exception {
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Contact contact = getContact(true);
|
||||
Group contactGroup = getGroup(CLIENT_ID);
|
||||
Group contactGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).startTransaction(false);
|
||||
@@ -559,7 +559,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
public void testMergingUpdatedPropertiesCreatesUpdate() throws Exception {
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Contact contact = getContact(true);
|
||||
Group contactGroup = getGroup(CLIENT_ID);
|
||||
Group contactGroup = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
BdfDictionary oldMetadata = BdfDictionary.of(
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
|
||||
@@ -18,7 +18,8 @@ import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.TransportId.MAX_TRANSPORT_ID_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestUtils.getClientId;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_VERSION;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
@@ -42,7 +43,7 @@ public class TransportPropertyValidatorTest extends BrambleMockTestCase {
|
||||
transportProperties = new TransportProperties();
|
||||
transportProperties.put("foo", "bar");
|
||||
|
||||
group = getGroup(getClientId());
|
||||
group = getGroup(CLIENT_ID, CLIENT_VERSION);
|
||||
message = getMessage(group.getId());
|
||||
|
||||
MetadataEncoder metadataEncoder = context.mock(MetadataEncoder.class);
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
private final MessageId messageId = new MessageId(getRandomId());
|
||||
private final MessageId messageId1 = new MessageId(getRandomId());
|
||||
private final MessageId messageId2 = new MessageId(getRandomId());
|
||||
private final Group group = getGroup(clientId);
|
||||
private final Group group = getGroup(clientId, 123);
|
||||
private final GroupId groupId = group.getId();
|
||||
private final long timestamp = System.currentTimeMillis();
|
||||
private final byte[] raw = new byte[123];
|
||||
|
||||
Reference in New Issue
Block a user