mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Merge branch 'db-code-cleanup' into 'master'
Clean up some database code See merge request briar/briar!1071
This commit is contained in:
@@ -10,6 +10,4 @@ public interface DatabaseConfig {
|
||||
File getDatabaseDirectory();
|
||||
|
||||
File getDatabaseKeyDirectory();
|
||||
|
||||
long getMaxSize();
|
||||
}
|
||||
|
||||
@@ -237,13 +237,6 @@ interface Database<T> {
|
||||
*/
|
||||
Collection<ContactId> getContacts(T txn, AuthorId a) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the amount of free storage space available to the database, in
|
||||
* bytes. This is based on the minimum of the space available on the device
|
||||
* where the database is stored and the database's configured size.
|
||||
*/
|
||||
long getFreeSpace() throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the group with the given ID.
|
||||
* <p/>
|
||||
|
||||
@@ -76,30 +76,6 @@ class H2Database extends JdbcDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFreeSpace() {
|
||||
File dir = config.getDatabaseDirectory();
|
||||
long maxSize = config.getMaxSize();
|
||||
long free = dir.getFreeSpace();
|
||||
long used = getDiskSpace(dir);
|
||||
long quota = maxSize - used;
|
||||
return Math.min(free, quota);
|
||||
}
|
||||
|
||||
private long getDiskSpace(File f) {
|
||||
if (f.isDirectory()) {
|
||||
long total = 0;
|
||||
File[] children = f.listFiles();
|
||||
if (children != null)
|
||||
for (File child : children) total += getDiskSpace(child);
|
||||
return total;
|
||||
} else if (f.isFile()) {
|
||||
return f.length();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws SQLException {
|
||||
SecretKey key = this.key;
|
||||
|
||||
@@ -86,30 +86,6 @@ class HyperSqlDatabase extends JdbcDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFreeSpace() {
|
||||
File dir = config.getDatabaseDirectory();
|
||||
long maxSize = config.getMaxSize();
|
||||
long free = dir.getFreeSpace();
|
||||
long used = getDiskSpace(dir);
|
||||
long quota = maxSize - used;
|
||||
return Math.min(free, quota);
|
||||
}
|
||||
|
||||
private long getDiskSpace(File f) {
|
||||
if (f.isDirectory()) {
|
||||
long total = 0;
|
||||
File[] children = f.listFiles();
|
||||
if (children != null)
|
||||
for (File child : children) total += getDiskSpace(child);
|
||||
return total;
|
||||
} else if (f.isFile()) {
|
||||
return f.length();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws SQLException {
|
||||
SecretKey key = this.key;
|
||||
|
||||
@@ -337,7 +337,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
protected void open(String driverClass, boolean reopen, SecretKey key,
|
||||
protected void open(String driverClass, boolean reopen,
|
||||
@SuppressWarnings("unused") SecretKey key,
|
||||
@Nullable MigrationListener listener) throws DbException {
|
||||
// Load the JDBC driver
|
||||
try {
|
||||
@@ -766,7 +767,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
for (Entry<ContactId, Boolean> e : visibility.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
boolean offered = removeOfferedMessage(txn, c, m.getId());
|
||||
boolean seen = offered || (sender != null && c.equals(sender));
|
||||
boolean seen = offered || c.equals(sender);
|
||||
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
|
||||
raw.length, state, e.getValue(), messageShared,
|
||||
false, seen);
|
||||
|
||||
@@ -45,8 +45,7 @@ public abstract class DatabaseMigrationTest extends BrambleMockTestCase {
|
||||
private final Migration<Connection> migration1 =
|
||||
context.mock(Migration.class, "migration1");
|
||||
|
||||
protected final DatabaseConfig config =
|
||||
new TestDatabaseConfig(testDir, 1024 * 1024);
|
||||
protected final DatabaseConfig config = new TestDatabaseConfig(testDir);
|
||||
protected final MessageFactory messageFactory = new TestMessageFactory();
|
||||
protected final SecretKey key = getSecretKey();
|
||||
protected final Clock clock = new SystemClock();
|
||||
|
||||
@@ -76,8 +76,8 @@ public abstract class DatabasePerformanceComparisonTest
|
||||
private Database<Connection> openDatabase(boolean conditionA)
|
||||
throws DbException {
|
||||
Database<Connection> db = createDatabase(conditionA,
|
||||
new TestDatabaseConfig(testDir, MAX_SIZE),
|
||||
new TestMessageFactory(), new SystemClock());
|
||||
new TestDatabaseConfig(testDir), new TestMessageFactory(),
|
||||
new SystemClock());
|
||||
db.open(databaseKey, null);
|
||||
return db;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,6 @@ import static org.junit.Assert.assertTrue;
|
||||
public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
|
||||
private static final int ONE_MEGABYTE = 1024 * 1024;
|
||||
static final int MAX_SIZE = 100 * ONE_MEGABYTE;
|
||||
|
||||
/**
|
||||
* How many contacts to simulate.
|
||||
|
||||
@@ -48,8 +48,8 @@ public abstract class DatabaseTraceTest extends DatabasePerformanceTest {
|
||||
|
||||
private Database<Connection> openDatabase() throws DbException {
|
||||
Database<Connection> db = createDatabase(
|
||||
new TestDatabaseConfig(testDir, MAX_SIZE),
|
||||
new TestMessageFactory(), new SystemClock());
|
||||
new TestDatabaseConfig(testDir), new TestMessageFactory(),
|
||||
new SystemClock());
|
||||
db.open(databaseKey, null);
|
||||
return db;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.INVALID;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
|
||||
@@ -88,7 +87,6 @@ import static org.junit.Assert.fail;
|
||||
public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
private static final int ONE_MEGABYTE = 1024 * 1024;
|
||||
private static final int MAX_SIZE = 5 * ONE_MEGABYTE;
|
||||
// All our transports use a maximum latency of 30 seconds
|
||||
private static final int MAX_LATENCY = 30 * 1000;
|
||||
|
||||
@@ -454,29 +452,6 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFreeSpace() throws Exception {
|
||||
Message message = getMessage(groupId, MAX_MESSAGE_BODY_LENGTH);
|
||||
Database<Connection> db = open(false);
|
||||
|
||||
// Sanity check: there should be enough space on disk for this test
|
||||
assertTrue(testDir.getFreeSpace() > MAX_SIZE);
|
||||
|
||||
// The free space should not be more than the allowed maximum size
|
||||
long free = db.getFreeSpace();
|
||||
assertTrue(free <= MAX_SIZE);
|
||||
assertTrue(free > 0);
|
||||
|
||||
// Storing a message should reduce the free space
|
||||
Connection txn = db.startTransaction();
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.commitTransaction(txn);
|
||||
assertTrue(db.getFreeSpace() < free);
|
||||
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloseWaitsForCommit() throws Exception {
|
||||
CountDownLatch closing = new CountDownLatch(1);
|
||||
@@ -1996,9 +1971,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
private Database<Connection> open(boolean resume,
|
||||
MessageFactory messageFactory, Clock clock) throws Exception {
|
||||
Database<Connection> db =
|
||||
createDatabase(new TestDatabaseConfig(testDir, MAX_SIZE),
|
||||
messageFactory, clock);
|
||||
Database<Connection> db = createDatabase(
|
||||
new TestDatabaseConfig(testDir), messageFactory, clock);
|
||||
if (!resume) deleteTestDirectory(testDir);
|
||||
db.open(key, null);
|
||||
return db;
|
||||
|
||||
@@ -45,8 +45,8 @@ public abstract class SingleDatabasePerformanceTest
|
||||
|
||||
private Database<Connection> openDatabase() throws DbException {
|
||||
Database<Connection> db = createDatabase(
|
||||
new TestDatabaseConfig(testDir, MAX_SIZE),
|
||||
new TestMessageFactory(), new SystemClock());
|
||||
new TestDatabaseConfig(testDir), new TestMessageFactory(),
|
||||
new SystemClock());
|
||||
db.open(databaseKey, null);
|
||||
return db;
|
||||
}
|
||||
|
||||
@@ -9,12 +9,10 @@ import java.io.File;
|
||||
public class TestDatabaseConfig implements DatabaseConfig {
|
||||
|
||||
private final File dbDir, keyDir;
|
||||
private final long maxSize;
|
||||
|
||||
public TestDatabaseConfig(File testDir, long maxSize) {
|
||||
public TestDatabaseConfig(File testDir) {
|
||||
dbDir = new File(testDir, "db");
|
||||
keyDir = new File(testDir, "key");
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,9 +24,4 @@ public class TestDatabaseConfig implements DatabaseConfig {
|
||||
public File getDatabaseKeyDirectory() {
|
||||
return keyDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return maxSize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public class TestDatabaseModule {
|
||||
}
|
||||
|
||||
public TestDatabaseModule(File dir) {
|
||||
config = new TestDatabaseConfig(dir, Long.MAX_VALUE);
|
||||
config = new TestDatabaseConfig(dir);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@@ -24,9 +24,4 @@ class AndroidDatabaseConfig implements DatabaseConfig {
|
||||
public File getDatabaseKeyDirectory() {
|
||||
return keyDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.headless
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig
|
||||
import java.io.File
|
||||
import java.lang.Long.MAX_VALUE
|
||||
|
||||
internal class HeadlessDatabaseConfig(private val dbDir: File, private val keyDir: File) :
|
||||
DatabaseConfig {
|
||||
@@ -14,8 +13,4 @@ internal class HeadlessDatabaseConfig(private val dbDir: File, private val keyDi
|
||||
override fun getDatabaseKeyDirectory(): File {
|
||||
return keyDir
|
||||
}
|
||||
|
||||
override fun getMaxSize(): Long {
|
||||
return MAX_VALUE
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user