mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
If no messages are added to a batch, don't call BatchWriter.finish() -
this allows the caller to avoid creating an empty packet by delaying creation of the packet's header and trailer until something's written to the packet's body. Changed the return semantics of DatabaseComponent.generateBatch(ContactId, BatchWriter, Collection<MessageId>) so that the IDs of messages considered for inclusion in the batch but no longer sendable are also returned - this allows the caller to remove them from the set of requested IDs.
This commit is contained in:
@@ -79,8 +79,9 @@ public interface DatabaseComponent {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a batch of messages for the given contact from the given
|
* Generates a batch of messages for the given contact from the given
|
||||||
* collection of requested messages, and returns the IDs of the messages
|
* collection of requested messages, and returns the IDs of any messages
|
||||||
* added to the bacth.
|
* that were either added to the batch, or were considered for inclusion
|
||||||
|
* but are no longer sendable to the contact.
|
||||||
*/
|
*/
|
||||||
Collection<MessageId> generateBatch(ContactId c, BatchWriter b,
|
Collection<MessageId> generateBatch(ContactId c, BatchWriter b,
|
||||||
Collection<MessageId> requested) throws DbException, IOException;
|
Collection<MessageId> requested) throws DbException, IOException;
|
||||||
|
|||||||
@@ -300,13 +300,11 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
try {
|
try {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
|
sent = new ArrayList<MessageId>();
|
||||||
int capacity = b.getCapacity();
|
int capacity = b.getCapacity();
|
||||||
Collection<MessageId> sendable =
|
Collection<MessageId> sendable =
|
||||||
db.getSendableMessages(txn, c, capacity);
|
db.getSendableMessages(txn, c, capacity);
|
||||||
Iterator<MessageId> it = sendable.iterator();
|
for(MessageId m : sendable) {
|
||||||
sent = new ArrayList<MessageId>();
|
|
||||||
while(it.hasNext()) {
|
|
||||||
MessageId m = it.next();
|
|
||||||
byte[] raw = db.getMessage(txn, m);
|
byte[] raw = db.getMessage(txn, m);
|
||||||
if(!b.writeMessage(raw)) break;
|
if(!b.writeMessage(raw)) break;
|
||||||
bytesSent += raw.length;
|
bytesSent += raw.length;
|
||||||
@@ -326,9 +324,9 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
} finally {
|
} finally {
|
||||||
messageStatusLock.readLock().unlock();
|
messageStatusLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
BatchId id = b.finish();
|
|
||||||
// Record the contents of the batch, unless it's empty
|
// Record the contents of the batch, unless it's empty
|
||||||
if(sent.isEmpty()) return;
|
if(sent.isEmpty()) return;
|
||||||
|
BatchId id = b.finish();
|
||||||
messageStatusLock.writeLock().lock();
|
messageStatusLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
@@ -357,7 +355,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
if(!containsContact(c)) throw new NoSuchContactException();
|
||||||
messageLock.readLock().lock();
|
messageLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
Collection<MessageId> sent;
|
Collection<MessageId> sent, considered;
|
||||||
messageStatusLock.readLock().lock();
|
messageStatusLock.readLock().lock();
|
||||||
try{
|
try{
|
||||||
subscriptionLock.readLock().lock();
|
subscriptionLock.readLock().lock();
|
||||||
@@ -365,13 +363,20 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
sent = new ArrayList<MessageId>();
|
sent = new ArrayList<MessageId>();
|
||||||
|
considered = new ArrayList<MessageId>();
|
||||||
int bytesSent = 0;
|
int bytesSent = 0;
|
||||||
for(MessageId m : requested) {
|
for(MessageId m : requested) {
|
||||||
byte[] raw = db.getMessageIfSendable(txn, c, m);
|
byte[] raw = db.getMessageIfSendable(txn, c, m);
|
||||||
if(raw == null) continue;
|
// If the message is still sendable, try to add
|
||||||
if(!b.writeMessage(raw)) break;
|
// it to the batch. If the batch is full, don't
|
||||||
bytesSent += raw.length;
|
// treat the message as considered, and don't
|
||||||
sent.add(m);
|
// try to add any further messages.
|
||||||
|
if(raw != null) {
|
||||||
|
if(!b.writeMessage(raw)) break;
|
||||||
|
bytesSent += raw.length;
|
||||||
|
sent.add(m);
|
||||||
|
}
|
||||||
|
considered.add(m);
|
||||||
}
|
}
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
@@ -387,16 +392,16 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
} finally {
|
} finally {
|
||||||
messageStatusLock.readLock().unlock();
|
messageStatusLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
BatchId id = b.finish();
|
|
||||||
// Record the contents of the batch, unless it's empty
|
// Record the contents of the batch, unless it's empty
|
||||||
if(sent.isEmpty()) return sent;
|
if(sent.isEmpty()) return considered;
|
||||||
|
BatchId id = b.finish();
|
||||||
messageStatusLock.writeLock().lock();
|
messageStatusLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
db.addOutstandingBatch(txn, c, id, sent);
|
db.addOutstandingBatch(txn, c, id, sent);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
return sent;
|
return considered;
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -223,23 +223,24 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
synchronized(subscriptionLock) {
|
synchronized(subscriptionLock) {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
int capacity = b.getCapacity();
|
|
||||||
Collection<MessageId> sendable =
|
|
||||||
db.getSendableMessages(txn, c, capacity);
|
|
||||||
Iterator<MessageId> it = sendable.iterator();
|
|
||||||
Collection<MessageId> sent =
|
Collection<MessageId> sent =
|
||||||
new ArrayList<MessageId>();
|
new ArrayList<MessageId>();
|
||||||
int bytesSent = 0;
|
int bytesSent = 0;
|
||||||
while(it.hasNext()) {
|
int capacity = b.getCapacity();
|
||||||
MessageId m = it.next();
|
Collection<MessageId> sendable =
|
||||||
|
db.getSendableMessages(txn, c, capacity);
|
||||||
|
for(MessageId m : sendable) {
|
||||||
byte[] raw = db.getMessage(txn, m);
|
byte[] raw = db.getMessage(txn, m);
|
||||||
if(!b.writeMessage(raw)) break;
|
if(!b.writeMessage(raw)) break;
|
||||||
bytesSent += raw.length;
|
bytesSent += raw.length;
|
||||||
sent.add(m);
|
sent.add(m);
|
||||||
}
|
}
|
||||||
BatchId id = b.finish();
|
// If the batch is not empty, calculate its ID and
|
||||||
if(!sent.isEmpty())
|
// record it as outstanding
|
||||||
|
if(!sent.isEmpty()) {
|
||||||
|
BatchId id = b.finish();
|
||||||
db.addOutstandingBatch(txn, c, id, sent);
|
db.addOutstandingBatch(txn, c, id, sent);
|
||||||
|
}
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
@@ -265,19 +266,30 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
try {
|
try {
|
||||||
Collection<MessageId> sent =
|
Collection<MessageId> sent =
|
||||||
new ArrayList<MessageId>();
|
new ArrayList<MessageId>();
|
||||||
|
Collection<MessageId> considered =
|
||||||
|
new ArrayList<MessageId>();
|
||||||
int bytesSent = 0;
|
int bytesSent = 0;
|
||||||
for(MessageId m : requested) {
|
for(MessageId m : requested) {
|
||||||
byte[] raw = db.getMessageIfSendable(txn, c, m);
|
byte[] raw = db.getMessageIfSendable(txn, c, m);
|
||||||
if(raw == null) continue;
|
// If the message is still sendable, try to add
|
||||||
if(!b.writeMessage(raw)) break;
|
// it to the batch. If the batch is full, don't
|
||||||
bytesSent += raw.length;
|
// treat the message as considered, and don't
|
||||||
sent.add(m);
|
// try to add any further messages.
|
||||||
|
if(raw != null) {
|
||||||
|
if(!b.writeMessage(raw)) break;
|
||||||
|
bytesSent += raw.length;
|
||||||
|
sent.add(m);
|
||||||
|
}
|
||||||
|
considered.add(m);
|
||||||
}
|
}
|
||||||
BatchId id = b.finish();
|
// If the batch is not empty, calculate its ID and
|
||||||
if(!sent.isEmpty())
|
// record it as outstanding
|
||||||
|
if(!sent.isEmpty()) {
|
||||||
|
BatchId id = b.finish();
|
||||||
db.addOutstandingBatch(txn, c, id, sent);
|
db.addOutstandingBatch(txn, c, id, sent);
|
||||||
|
}
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
return sent;
|
return considered;
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
Reference in New Issue
Block a user