mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Update DB method that gets total size of messages to send.
This commit is contained in:
@@ -314,16 +314,6 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
*/
|
||||
Message getMessage(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the total length, including headers, of any messages that are
|
||||
* eligible to be sent to the given contact via a transport with the given
|
||||
* max latency.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
long getMessageBytesToSend(Transaction txn, ContactId c, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs of all delivered messages in the given group.
|
||||
* <p/>
|
||||
@@ -469,6 +459,16 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
Map<MessageId, Integer> getUnackedMessagesToSend(Transaction txn,
|
||||
ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the total length, including headers, of all messages that are
|
||||
* eligible to be sent to the given contact. This may include messages
|
||||
* that have already been sent and are not yet due for retransmission.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
long getUnackedMessageBytesToSend(Transaction txn, ContactId c)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the next time (in milliseconds since the Unix epoch) when a
|
||||
* message is due to be deleted, or {@link #NO_CLEANUP_DEADLINE}
|
||||
|
||||
@@ -357,16 +357,6 @@ interface Database<T> {
|
||||
*/
|
||||
Message getMessage(T txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the total length, including headers, of any messages that are
|
||||
* eligible to be sent to the given contact via a transport with the given
|
||||
* max latency.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
long getMessageBytesToSend(T txn, ContactId c, int maxLatency)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs and states of all dependencies of the given message.
|
||||
* For missing dependencies and dependencies in other groups, the state
|
||||
@@ -518,6 +508,15 @@ interface Database<T> {
|
||||
Map<MessageId, Integer> getUnackedMessagesToSend(T txn, ContactId c)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the total length, including headers, of all messages that are
|
||||
* eligible to be sent to the given contact. This may include messages
|
||||
* that have already been sent and are not yet due for retransmission.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
long getUnackedMessageBytesToSend(T txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the IDs of any messages that need to be validated.
|
||||
* <p/>
|
||||
|
||||
@@ -608,15 +608,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
return db.getMessage(txn, m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMessageBytesToSend(Transaction transaction, ContactId c,
|
||||
int maxLatency) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
return db.getMessageBytesToSend(txn, c, maxLatency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MessageId> getMessageIds(Transaction transaction,
|
||||
GroupId g) throws DbException {
|
||||
@@ -750,6 +741,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
return db.getUnackedMessagesToSend(txn, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getUnackedMessageBytesToSend(Transaction transaction,
|
||||
ContactId c) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
return db.getUnackedMessageBytesToSend(txn, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<MessageId, MessageState> getMessageDependencies(
|
||||
Transaction transaction, MessageId m) throws DbException {
|
||||
|
||||
@@ -1788,37 +1788,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMessageBytesToSend(Connection txn, ContactId c,
|
||||
int maxLatency) throws DbException {
|
||||
long now = clock.currentTimeMillis();
|
||||
long eta = now + maxLatency;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT SUM(length) FROM statuses"
|
||||
+ " WHERE contactId = ? AND state = ?"
|
||||
+ " AND groupShared = TRUE AND messageShared = TRUE"
|
||||
+ " AND deleted = FALSE AND seen = FALSE"
|
||||
+ " AND (expiry <= ? OR eta > ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, DELIVERED.getValue());
|
||||
ps.setLong(3, now);
|
||||
ps.setLong(4, eta);
|
||||
rs = ps.executeQuery();
|
||||
rs.next();
|
||||
long total = rs.getInt(1);
|
||||
rs.close();
|
||||
ps.close();
|
||||
return total;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MessageId> getMessageIds(Connection txn, GroupId g)
|
||||
throws DbException {
|
||||
@@ -2297,6 +2266,32 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getUnackedMessageBytesToSend(Connection txn, ContactId c)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT SUM(length) FROM statuses"
|
||||
+ " WHERE contactId = ? AND state = ?"
|
||||
+ " AND groupShared = TRUE AND messageShared = TRUE"
|
||||
+ " AND deleted = FALSE AND seen = FALSE";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, DELIVERED.getValue());
|
||||
rs = ps.executeQuery();
|
||||
rs.next();
|
||||
long total = rs.getInt(1);
|
||||
rs.close();
|
||||
ps.close();
|
||||
return total;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MessageId> getMessagesToValidate(Connection txn)
|
||||
throws DbException {
|
||||
|
||||
@@ -60,10 +60,9 @@ class RemovableDriveWriterTask extends RemovableDriveTaskImpl
|
||||
setSuccess(false);
|
||||
return;
|
||||
}
|
||||
int maxLatency = plugin.getMaxLatency();
|
||||
try {
|
||||
setTotal(db.transactionWithResult(true, txn ->
|
||||
db.getMessageBytesToSend(txn, contactId, maxLatency)));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId)));
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
registry.removeWriter(this);
|
||||
|
||||
@@ -358,7 +358,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
try {
|
||||
db.transaction(true, transaction ->
|
||||
db.getMessageBytesToSend(transaction, contactId, 123));
|
||||
db.getUnackedMessageBytesToSend(transaction, contactId));
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
|
||||
@@ -228,7 +228,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertEquals(singletonList(messageId), ids);
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Changing the status to seen = true should make the message unsendable
|
||||
db.raiseSeenFlag(txn, contactId, messageId);
|
||||
@@ -236,7 +236,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -261,7 +261,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Marking the message delivered should make it sendable
|
||||
db.setMessageState(txn, messageId, DELIVERED);
|
||||
@@ -270,7 +270,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertEquals(singletonList(messageId), ids);
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Marking the message invalid should make it unsendable
|
||||
db.setMessageState(txn, messageId, INVALID);
|
||||
@@ -278,7 +278,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Marking the message pending should make it unsendable
|
||||
db.setMessageState(txn, messageId, PENDING);
|
||||
@@ -286,7 +286,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -310,7 +310,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Making the group visible should not make the message sendable
|
||||
db.addGroupVisibility(txn, contactId, groupId, false);
|
||||
@@ -318,7 +318,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Sharing the group should make the message sendable
|
||||
db.setGroupVisibility(txn, contactId, groupId, true);
|
||||
@@ -327,7 +327,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertEquals(singletonList(messageId), ids);
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Unsharing the group should make the message unsendable
|
||||
db.setGroupVisibility(txn, contactId, groupId, false);
|
||||
@@ -335,7 +335,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Making the group invisible should make the message unsendable
|
||||
db.removeGroupVisibility(txn, contactId, groupId);
|
||||
@@ -343,7 +343,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -368,7 +368,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertTrue(ids.isEmpty());
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(0, db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
assertEquals(0, db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// Sharing the message should make it sendable
|
||||
db.setMessageShared(txn, messageId, true);
|
||||
@@ -377,7 +377,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
ids = db.getMessagesToOffer(txn, contactId, 100, MAX_LATENCY);
|
||||
assertEquals(singletonList(messageId), ids);
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -402,14 +402,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
MAX_LATENCY);
|
||||
assertTrue(ids.isEmpty());
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
// The message is just the right size to send
|
||||
ids = db.getMessagesToSend(txn, contactId, message.getRawLength(),
|
||||
MAX_LATENCY);
|
||||
assertEquals(singletonList(messageId), ids);
|
||||
assertEquals(message.getRawLength(),
|
||||
db.getMessageBytesToSend(txn, contactId, MAX_LATENCY));
|
||||
db.getUnackedMessageBytesToSend(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
|
||||
Reference in New Issue
Block a user