From 847650f280360d9f6bc3974f732cd30309748075 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 10 Jun 2021 17:04:15 +0100 Subject: [PATCH] Replace inner classes with lambdas. --- .../bramble/sync/SimplexOutgoingSession.java | 240 +++++++----------- 1 file changed, 92 insertions(+), 148 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/sync/SimplexOutgoingSession.java b/bramble-core/src/main/java/org/briarproject/bramble/sync/SimplexOutgoingSession.java index d069d38b0..f5c7fcbb6 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/sync/SimplexOutgoingSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/sync/SimplexOutgoingSession.java @@ -99,9 +99,9 @@ class SimplexOutgoingSession implements SyncSession, EventListener { // Send our supported protocol versions recordWriter.writeVersions(new Versions(SUPPORTED_VERSIONS)); // Start a query for each type of record - dbExecutor.execute(new GenerateAck()); - if (eager) dbExecutor.execute(new LoadUnackedMessageIds()); - else dbExecutor.execute(new GenerateBatch()); + dbExecutor.execute(this::generateAck); + if (eager) dbExecutor.execute(this::loadUnackedMessageIds); + else dbExecutor.execute(this::generateBatch); // Write records until interrupted or no more records to write try { while (!interrupted) { @@ -146,166 +146,110 @@ class SimplexOutgoingSession implements SyncSession, EventListener { } } - private class LoadUnackedMessageIds implements Runnable { - - @DatabaseExecutor - @Override - public void run() { - if (interrupted) return; - try { - Map ids = - db.transactionWithResult(true, txn -> - db.getUnackedMessagesToSend(txn, contactId)); - if (LOG.isLoggable(INFO)) { - LOG.info(ids.size() + " unacked messages to send"); - } - if (ids.isEmpty()) decrementOutstandingQueries(); - else dbExecutor.execute(new GenerateEagerBatch(ids)); - } catch (DbException e) { - logException(LOG, WARNING, e); - interrupt(); + @DatabaseExecutor + private void loadUnackedMessageIds() { + if (interrupted) return; + try { + Map ids = db.transactionWithResult(true, txn -> + db.getUnackedMessagesToSend(txn, contactId)); + if (LOG.isLoggable(INFO)) { + LOG.info(ids.size() + " unacked messages to send"); } - } - } - - private class GenerateEagerBatch implements Runnable { - - private final Map ids; - - private GenerateEagerBatch(Map ids) { - this.ids = ids; - } - - @DatabaseExecutor - @Override - public void run() { - if (interrupted) return; - // Take some message IDs from `ids` to form a batch - Collection batchIds = new ArrayList<>(); - long totalLength = 0; - Iterator> it = - ids.entrySet().iterator(); - while (it.hasNext()) { - // Check whether the next message will fit in the batch - Entry e = it.next(); - int length = e.getValue(); - if (totalLength + length > MAX_RECORD_PAYLOAD_BYTES) break; - // Add the message to the batch - it.remove(); - batchIds.add(e.getKey()); - totalLength += length; - } - if (batchIds.isEmpty()) throw new AssertionError(); - try { - Collection batch = - db.transactionWithResult(false, txn -> - db.generateBatch(txn, contactId, batchIds, - maxLatency)); - writerTasks.add(new WriteEagerBatch(batch, ids)); - } catch (DbException e) { - logException(LOG, WARNING, e); - interrupt(); - } - } - } - - private class WriteEagerBatch implements ThrowingRunnable { - - private final Collection batch; - private final Map ids; - - private WriteEagerBatch(Collection batch, - Map ids) { - this.batch = batch; - this.ids = ids; - } - - @IoExecutor - @Override - public void run() throws IOException { - if (interrupted) return; - for (Message m : batch) recordWriter.writeMessage(m); - LOG.info("Sent eager batch"); if (ids.isEmpty()) decrementOutstandingQueries(); - else dbExecutor.execute(new GenerateEagerBatch(ids)); + else dbExecutor.execute(() -> generateEagerBatch(ids)); + } catch (DbException e) { + logException(LOG, WARNING, e); + interrupt(); } } - private class GenerateAck implements Runnable { - - @DatabaseExecutor - @Override - public void run() { - if (interrupted) return; - try { - Ack a = db.transactionWithNullableResult(false, txn -> - db.generateAck(txn, contactId, MAX_MESSAGE_IDS)); - if (LOG.isLoggable(INFO)) - LOG.info("Generated ack: " + (a != null)); - if (a == null) decrementOutstandingQueries(); - else writerTasks.add(new WriteAck(a)); - } catch (DbException e) { - logException(LOG, WARNING, e); - interrupt(); - } + @DatabaseExecutor + private void generateEagerBatch(Map ids) { + if (interrupted) return; + // Take some message IDs from `ids` to form a batch + Collection batchIds = new ArrayList<>(); + long totalLength = 0; + Iterator> it = ids.entrySet().iterator(); + while (it.hasNext()) { + // Check whether the next message will fit in the batch + Entry e = it.next(); + int length = e.getValue(); + if (totalLength + length > MAX_RECORD_PAYLOAD_BYTES) break; + // Add the message to the batch + it.remove(); + batchIds.add(e.getKey()); + totalLength += length; + } + if (batchIds.isEmpty()) throw new AssertionError(); + try { + Collection batch = + db.transactionWithResult(false, txn -> + db.generateBatch(txn, contactId, batchIds, + maxLatency)); + writerTasks.add(() -> writeEagerBatch(batch, ids)); + } catch (DbException e) { + logException(LOG, WARNING, e); + interrupt(); } } - private class WriteAck implements ThrowingRunnable { + @IoExecutor + private void writeEagerBatch(Collection batch, + Map ids) throws IOException { + if (interrupted) return; + for (Message m : batch) recordWriter.writeMessage(m); + LOG.info("Sent eager batch"); + if (ids.isEmpty()) decrementOutstandingQueries(); + else dbExecutor.execute(() -> generateEagerBatch(ids)); + } - private final Ack ack; - - private WriteAck(Ack ack) { - this.ack = ack; - } - - @IoExecutor - @Override - public void run() throws IOException { - if (interrupted) return; - recordWriter.writeAck(ack); - LOG.info("Sent ack"); - dbExecutor.execute(new GenerateAck()); + @DatabaseExecutor + private void generateAck() { + if (interrupted) return; + try { + Ack a = db.transactionWithNullableResult(false, txn -> + db.generateAck(txn, contactId, MAX_MESSAGE_IDS)); + if (LOG.isLoggable(INFO)) + LOG.info("Generated ack: " + (a != null)); + if (a == null) decrementOutstandingQueries(); + else writerTasks.add(() -> writeAck(a)); + } catch (DbException e) { + logException(LOG, WARNING, e); + interrupt(); } } - private class GenerateBatch implements Runnable { + @IoExecutor + private void writeAck(Ack ack) throws IOException { + if (interrupted) return; + recordWriter.writeAck(ack); + LOG.info("Sent ack"); + dbExecutor.execute(this::generateAck); + } - @DatabaseExecutor - @Override - public void run() { - if (interrupted) return; - try { - Collection b = - db.transactionWithNullableResult(false, txn -> - db.generateBatch(txn, contactId, - MAX_RECORD_PAYLOAD_BYTES, maxLatency)); - if (LOG.isLoggable(INFO)) - LOG.info("Generated batch: " + (b != null)); - if (b == null) decrementOutstandingQueries(); - else writerTasks.add(new WriteBatch(b)); - } catch (DbException e) { - logException(LOG, WARNING, e); - interrupt(); - } + @DatabaseExecutor + private void generateBatch() { + if (interrupted) return; + try { + Collection b = + db.transactionWithNullableResult(false, txn -> + db.generateBatch(txn, contactId, + MAX_RECORD_PAYLOAD_BYTES, maxLatency)); + if (LOG.isLoggable(INFO)) + LOG.info("Generated batch: " + (b != null)); + if (b == null) decrementOutstandingQueries(); + else writerTasks.add(() -> writeBatch(b)); + } catch (DbException e) { + logException(LOG, WARNING, e); + interrupt(); } } - private class WriteBatch implements ThrowingRunnable { - - private final Collection batch; - - private WriteBatch(Collection batch) { - this.batch = batch; - } - - @IoExecutor - @Override - public void run() throws IOException { - if (interrupted) return; - for (Message m : batch) recordWriter.writeMessage(m); - LOG.info("Sent batch"); - dbExecutor.execute(new GenerateBatch()); - } + @IoExecutor + private void writeBatch(Collection batch) throws IOException { + if (interrupted) return; + for (Message m : batch) recordWriter.writeMessage(m); + LOG.info("Sent batch"); + dbExecutor.execute(this::generateBatch); } }