From 03a76b904aec141dd306b772abeda5bb89ce857d Mon Sep 17 00:00:00 2001 From: Michael Huebler Date: Mon, 9 Nov 2020 06:55:14 +0100 Subject: [PATCH] Some additional safety checks, so that we don't skip any Diagnosis Keys. --- .../coronawarncompanion/MainActivity.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/corona-warn-companion/src/main/java/org/tosl/coronawarncompanion/MainActivity.java b/corona-warn-companion/src/main/java/org/tosl/coronawarncompanion/MainActivity.java index 1d989ad..5e74ea4 100644 --- a/corona-warn-companion/src/main/java/org/tosl/coronawarncompanion/MainActivity.java +++ b/corona-warn-companion/src/main/java/org/tosl/coronawarncompanion/MainActivity.java @@ -555,6 +555,9 @@ public void onComplete() { int dkListLen = diagnosisKeysList.size(); if ((rpiList != null) && (dkListLen != 0)) { int desiredThreads = Runtime.getRuntime().availableProcessors(); + if (desiredThreads < 1) { + desiredThreads = 1; + } Log.d(TAG, "Matching: Trying to split into " + desiredThreads + " threads."); if (desiredThreads > dkListLen) { desiredThreads = dkListLen; @@ -562,24 +565,29 @@ public void onComplete() { } ArrayList> ranges = new ArrayList<>(); int lastEndExclusive = 0; - for (int i = 1; i <= desiredThreads; i++) { - int newEndExclusive = dkListLen * i / desiredThreads; + int newEndExclusive = 0; + int i = 1; + while (newEndExclusive < dkListLen) { + newEndExclusive = dkListLen * i / desiredThreads; if (newEndExclusive < lastEndExclusive) { newEndExclusive = lastEndExclusive; } - ranges.add(new Pair<>(lastEndExclusive, newEndExclusive)); - Log.d(TAG, "Matching: Range " + lastEndExclusive + ".." + newEndExclusive); - lastEndExclusive = newEndExclusive; - if (newEndExclusive >= dkListLen) { - break; + if (newEndExclusive > dkListLen) { + newEndExclusive = dkListLen; } + if (newEndExclusive > lastEndExclusive) { + ranges.add(new Pair<>(lastEndExclusive, newEndExclusive)); + Log.d(TAG, "Matching: Range " + lastEndExclusive + ".." + newEndExclusive); + } + lastEndExclusive = newEndExclusive; + i++; } numMatchingThreads = ranges.size(); ArrayList> observables = new ArrayList<>(); - for (int i = 0; i < numMatchingThreads; i++) { + for (int threadNum = 0; threadNum < numMatchingThreads; threadNum++) { Matcher matcher = new Matcher(rpiList, - diagnosisKeysList.subList(ranges.get(i).first, ranges.get(i).second), i); + diagnosisKeysList.subList(ranges.get(threadNum).first, ranges.get(threadNum).second), threadNum); observables.add( matcher.getMatchingObservable() .subscribeOn(Schedulers.computation())