mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Where possible, do IO outside transactions and locks.
This commit is contained in:
@@ -382,29 +382,26 @@ DatabaseCleaner.Callback {
|
|||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
if(!containsContact(c)) throw new NoSuchContactException();
|
||||||
Collection<BatchId> sent = new ArrayList<BatchId>();
|
Collection<BatchId> acks, sent = new ArrayList<BatchId>();
|
||||||
messageStatusLock.readLock().lock();
|
messageStatusLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Collection<BatchId> acks = db.getBatchesToAck(txn, c);
|
acks = db.getBatchesToAck(txn, c);
|
||||||
for(BatchId b : acks) {
|
|
||||||
if(!a.writeBatchId(b)) break;
|
|
||||||
sent.add(b);
|
|
||||||
}
|
|
||||||
if(LOG.isLoggable(Level.FINE))
|
|
||||||
LOG.fine("Added " + acks.size() + " acks");
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
} catch(IOException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
messageStatusLock.readLock().unlock();
|
messageStatusLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
|
for(BatchId b : acks) {
|
||||||
|
if(!a.writeBatchId(b)) break;
|
||||||
|
sent.add(b);
|
||||||
|
}
|
||||||
|
if(LOG.isLoggable(Level.FINE))
|
||||||
|
LOG.fine("Added " + sent.size() + " batch IDs to ack");
|
||||||
// Record the contents of the ack, unless it's empty
|
// Record the contents of the ack, unless it's empty
|
||||||
if(sent.isEmpty()) return false;
|
if(sent.isEmpty()) return false;
|
||||||
a.finish();
|
a.finish();
|
||||||
@@ -557,6 +554,7 @@ DatabaseCleaner.Callback {
|
|||||||
|
|
||||||
public Collection<MessageId> generateOffer(ContactId c, OfferWriter o)
|
public Collection<MessageId> generateOffer(ContactId c, OfferWriter o)
|
||||||
throws DbException, IOException {
|
throws DbException, IOException {
|
||||||
|
Collection<MessageId> sendable, sent = new ArrayList<MessageId>();
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
if(!containsContact(c)) throw new NoSuchContactException();
|
||||||
@@ -566,24 +564,12 @@ DatabaseCleaner.Callback {
|
|||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Collection<MessageId> sendable =
|
sendable = db.getSendableMessages(txn, c,
|
||||||
db.getSendableMessages(txn, c, Integer.MAX_VALUE);
|
Integer.MAX_VALUE);
|
||||||
Iterator<MessageId> it = sendable.iterator();
|
|
||||||
Collection<MessageId> sent = new ArrayList<MessageId>();
|
|
||||||
while(it.hasNext()) {
|
|
||||||
MessageId m = it.next();
|
|
||||||
if(!o.writeMessageId(m)) break;
|
|
||||||
sent.add(m);
|
|
||||||
}
|
|
||||||
if(!sent.isEmpty()) o.finish();
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
return sent;
|
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
} catch(IOException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
messageStatusLock.readLock().unlock();
|
messageStatusLock.readLock().unlock();
|
||||||
@@ -594,10 +580,19 @@ DatabaseCleaner.Callback {
|
|||||||
} finally {
|
} finally {
|
||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
|
for(MessageId m : sendable) {
|
||||||
|
if(!o.writeMessageId(m)) break;
|
||||||
|
sent.add(m);
|
||||||
|
}
|
||||||
|
if(!sent.isEmpty()) o.finish();
|
||||||
|
if(LOG.isLoggable(Level.FINE))
|
||||||
|
LOG.fine("Added " + sent.size() + " message IDs to offer");
|
||||||
|
return sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateSubscriptionUpdate(ContactId c, SubscriptionWriter s)
|
public void generateSubscriptionUpdate(ContactId c, SubscriptionWriter s)
|
||||||
throws DbException, IOException {
|
throws DbException, IOException {
|
||||||
|
Map<Group, Long> subs;
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
if(!containsContact(c)) throw new NoSuchContactException();
|
||||||
@@ -605,17 +600,11 @@ DatabaseCleaner.Callback {
|
|||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Map<Group, Long> subs = db.getVisibleSubscriptions(txn, c);
|
subs = db.getVisibleSubscriptions(txn, c);
|
||||||
s.writeSubscriptions(subs, System.currentTimeMillis());
|
|
||||||
if(LOG.isLoggable(Level.FINE))
|
|
||||||
LOG.fine("Added " + subs.size() + " subscriptions");
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
} catch(IOException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
subscriptionLock.readLock().unlock();
|
subscriptionLock.readLock().unlock();
|
||||||
@@ -623,10 +612,14 @@ DatabaseCleaner.Callback {
|
|||||||
} finally {
|
} finally {
|
||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
|
s.writeSubscriptions(subs, System.currentTimeMillis());
|
||||||
|
if(LOG.isLoggable(Level.FINE))
|
||||||
|
LOG.fine("Added " + subs.size() + " subscriptions to update");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateTransportUpdate(ContactId c, TransportWriter t)
|
public void generateTransportUpdate(ContactId c, TransportWriter t)
|
||||||
throws DbException, IOException {
|
throws DbException, IOException {
|
||||||
|
Map<String, Map<String, String>> transports;
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
if(!containsContact(c)) throw new NoSuchContactException();
|
if(!containsContact(c)) throw new NoSuchContactException();
|
||||||
@@ -634,18 +627,11 @@ DatabaseCleaner.Callback {
|
|||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
Map<String, Map<String, String>> transports =
|
transports = db.getTransports(txn);
|
||||||
db.getTransports(txn);
|
|
||||||
t.writeTransports(transports, System.currentTimeMillis());
|
|
||||||
if(LOG.isLoggable(Level.FINE))
|
|
||||||
LOG.fine("Added " + transports.size() + " transports");
|
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
} catch(IOException e) {
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
transportLock.readLock().unlock();
|
transportLock.readLock().unlock();
|
||||||
@@ -653,6 +639,9 @@ DatabaseCleaner.Callback {
|
|||||||
} finally {
|
} finally {
|
||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
|
t.writeTransports(transports, System.currentTimeMillis());
|
||||||
|
if(LOG.isLoggable(Level.FINE))
|
||||||
|
LOG.fine("Added " + transports.size() + " transports to update");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionWindow getConnectionWindow(ContactId c, int transportId)
|
public ConnectionWindow getConnectionWindow(ContactId c, int transportId)
|
||||||
|
|||||||
Reference in New Issue
Block a user