diff --git a/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy b/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy index 1af73c6b..23f60815 100644 --- a/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy +++ b/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy @@ -19,6 +19,8 @@ import java.util.zip.GZIPInputStream @Log class BrowseManager { + private static final int BATCH_SIZE = 32 + private final I2PConnector connector private final EventBus eventBus private final Persona me @@ -58,19 +60,34 @@ class BrowseManager { boolean chat = headers.containsKey("Chat") && Boolean.parseBoolean(headers['Chat']) // at this stage, start pulling the results - eventBus.publish(new BrowseStatusEvent(host: e.host, status : BrowseStatus.FETCHING, totalResults : results)) + UUID uuid = UUID.randomUUID() + eventBus.publish(new BrowseStatusEvent(host: e.host, status : BrowseStatus.FETCHING, + totalResults : results, uuid : uuid)) + log.info("Starting to fetch $results results with uuid $uuid") JsonSlurper slurper = new JsonSlurper() DataInputStream dis = new DataInputStream(new GZIPInputStream(is)) - UUID uuid = UUID.randomUUID() + UIResultEvent[] batch = new UIResultEvent[Math.min(BATCH_SIZE, results)] + int j = 0 for (int i = 0; i < results; i++) { + log.fine("parsing result $i at batch position $j") + int size = dis.readUnsignedShort() byte [] tmp = new byte[size] dis.readFully(tmp) def json = slurper.parse(tmp) UIResultEvent result = ResultsParser.parse(e.host, uuid, json) result.chat = chat - eventBus.publish(result) + batch[j++] = result + + + // publish piecemally + if (j == batch.length) { + eventBus.publish(new UIResultBatchEvent(results : batch, uuid : uuid)) + j = 0 + batch = new UIResultEvent[Math.min(results - i - 1, BATCH_SIZE)] + log.fine("publishing batch, next batch size ${batch.length}") + } } eventBus.publish(new BrowseStatusEvent(host: e.host, status : BrowseStatus.FINISHED)) diff --git a/core/src/main/groovy/com/muwire/core/search/BrowseStatusEvent.groovy b/core/src/main/groovy/com/muwire/core/search/BrowseStatusEvent.groovy index 5703574c..e1de5fb6 100644 --- a/core/src/main/groovy/com/muwire/core/search/BrowseStatusEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/search/BrowseStatusEvent.groovy @@ -7,4 +7,5 @@ class BrowseStatusEvent extends Event { Persona host BrowseStatus status int totalResults + UUID uuid } diff --git a/gui/griffon-app/controllers/com/muwire/gui/BrowseController.groovy b/gui/griffon-app/controllers/com/muwire/gui/BrowseController.groovy index 8c98d6c4..7723c5a3 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/BrowseController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/BrowseController.groovy @@ -14,6 +14,7 @@ import com.muwire.core.download.UIDownloadEvent import com.muwire.core.search.BrowseStatus import com.muwire.core.search.BrowseStatusEvent import com.muwire.core.search.UIBrowseEvent +import com.muwire.core.search.UIResultBatchEvent import com.muwire.core.search.UIResultEvent @ArtifactProviderFor(GriffonController) @@ -28,27 +29,31 @@ class BrowseController { void register() { core.eventBus.register(BrowseStatusEvent.class, this) - core.eventBus.register(UIResultEvent.class, this) + core.eventBus.register(UIResultBatchEvent.class, this) core.eventBus.publish(new UIBrowseEvent(host : model.host)) } void mvcGroupDestroy() { core.eventBus.unregister(BrowseStatusEvent.class, this) - core.eventBus.unregister(UIResultEvent.class, this) + core.eventBus.unregister(UIResultBatchEvent.class, this) } void onBrowseStatusEvent(BrowseStatusEvent e) { runInsideUIAsync { model.status = e.status - if (e.status == BrowseStatus.FETCHING) + if (e.status == BrowseStatus.FETCHING) { model.totalResults = e.totalResults + model.uuid = e.uuid + } } } - void onUIResultEvent(UIResultEvent e) { + void onUIResultBatchEvent(UIResultBatchEvent e) { runInsideUIAsync { - model.chatActionEnabled = e.chat - model.results << e + if (e.uuid != model.uuid) + return + model.chatActionEnabled = e.results[0].chat + model.results.addAll(e.results.toList()) model.resultCount = model.results.size() view.resultsTable.model.fireTableDataChanged() } diff --git a/gui/griffon-app/models/com/muwire/gui/BrowseModel.groovy b/gui/griffon-app/models/com/muwire/gui/BrowseModel.groovy index c902e554..e33d389b 100644 --- a/gui/griffon-app/models/com/muwire/gui/BrowseModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/BrowseModel.groovy @@ -18,6 +18,7 @@ class BrowseModel { @Observable boolean chatActionEnabled @Observable int totalResults @Observable int resultCount + UUID uuid def results = [] } \ No newline at end of file