From e17c272636215faf9659105536003a3296f114a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Wed, 29 Dec 2021 18:08:52 +0100 Subject: [PATCH] feat: multiple seachers + clean end for Manager --- .vscode/settings.json | 12 +---- src/main/java/linda/search/basic/Code.java | 5 +- src/main/java/linda/search/basic/Main.java | 25 +++++++--- src/main/java/linda/search/basic/Manager.java | 50 ++++++++++++++----- .../java/linda/search/basic/Searcher.java | 49 +++++++++++------- .../linda/test/CentralizedLindaTests.java | 18 +++---- 6 files changed, 100 insertions(+), 59 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 54620aa..36c8128 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,12 +1,4 @@ { - "files.exclude": { - "**/.git": true, - "**/.svn": true, - "**/.hg": true, - "**/CVS": true, - "**/.DS_Store": true, - "**/Thumbs.db": true, - "**/*.class": true - }, - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m" } \ No newline at end of file diff --git a/src/main/java/linda/search/basic/Code.java b/src/main/java/linda/search/basic/Code.java index cb5aed1..c0e20bb 100644 --- a/src/main/java/linda/search/basic/Code.java +++ b/src/main/java/linda/search/basic/Code.java @@ -1,7 +1,8 @@ package linda.search.basic; + public enum Code { Request, // Request, UUID, String - Value, // Value, String - Result, // Result, UUID, String, Int + Value, // Value, String, UUID + Result, // Result, UUID, String, Int Searcher, // Result, "done", UUID } diff --git a/src/main/java/linda/search/basic/Main.java b/src/main/java/linda/search/basic/Main.java index 1129555..94af623 100644 --- a/src/main/java/linda/search/basic/Main.java +++ b/src/main/java/linda/search/basic/Main.java @@ -4,15 +4,24 @@ import linda.*; public class Main { + private static final int NB_MANAGER = 1; + private static final int NB_SEARCHER = 10; + public static void main(String args[]) { - if (args.length != 2) { - System.err.println("linda.search.basic.Main search file."); - return; - } + // if (args.length != 2) { + // System.err.println("linda.search.basic.Main search file."); + // return; + // } Linda linda = new linda.shm.CentralizedLinda(); - Manager manager = new Manager(linda, args[1], args[0]); - Searcher searcher = new Searcher(linda); - (new Thread(manager)).start(); - (new Thread(searcher)).start(); + + for (int i = 0; i < NB_MANAGER; i++) { + Manager manager = new Manager(linda, "/usr/share/dict/french", "agneau"); + (new Thread(manager)).start(); + } + + for (int i = 0; i < NB_SEARCHER; i++) { + Searcher searcher = new Searcher(linda); + (new Thread(searcher)).start(); + } } } diff --git a/src/main/java/linda/search/basic/Manager.java b/src/main/java/linda/search/basic/Manager.java index 9375cc2..358400b 100644 --- a/src/main/java/linda/search/basic/Manager.java +++ b/src/main/java/linda/search/basic/Manager.java @@ -22,46 +22,70 @@ public class Manager implements Runnable { this.linda = linda; this.pathname = pathname; this.search = search; + this.reqUUID = UUID.randomUUID(); + this.search = search; } - private void addSearch(String search) { - this.search = search; - this.reqUUID = UUID.randomUUID(); - System.out.println("Search " + this.reqUUID + " for " + this.search); - linda.eventRegister(Linda.eventMode.TAKE, Linda.eventTiming.IMMEDIATE, new Tuple(Code.Result, this.reqUUID, String.class, Integer.class), new CbGetResult()); + private void addSearch() { + System.out.println("Search (" + this.reqUUID + ") for " + this.search); + linda.eventRegister( + Linda.eventMode.TAKE, Linda.eventTiming.FUTURE, + new Tuple(Code.Result, this.reqUUID, String.class, Integer.class), + new CbGetResult()); linda.write(new Tuple(Code.Request, this.reqUUID, this.search)); } - private void loadData(String pathname) { + private void loadData() { try (Stream stream = Files.lines(Paths.get(pathname))) { - stream.limit(10000).forEach(s -> linda.write(new Tuple(Code.Value, s.trim()))); + stream.limit(10000).forEach(s -> linda.write(new Tuple(Code.Value, s.trim(), this.reqUUID))); } catch (java.io.IOException e) { e.printStackTrace(); } } private void waitForEndSearch() { - linda.take(new Tuple(Code.Searcher, "done", this.reqUUID)); - linda.take(new Tuple(Code.Request, this.reqUUID, String.class)); // remove query + linda.take(new Tuple(Code.Searcher, "done", this.reqUUID)); // wait for the search to be finished + linda.take(new Tuple(Code.Request, this.reqUUID, String.class)); // remove search query System.out.println("query done"); } + private void waitForResult() { + linda.take(new Tuple(Code.Searcher, "end", this.reqUUID)); + System.out.println("Final result: \"" + bestresult + '"'); + } + private class CbGetResult implements linda.Callback { - public void call(Tuple t) { // [ Result, ?UUID, ?String, ?Integer ] + public void call(Tuple t) { // [ Result, ?UUID, ?String, ?Integer ] + UUID reqUUID = (UUID) t.get(1); String s = (String) t.get(2); Integer v = (Integer) t.get(3); + if (v < bestvalue) { bestvalue = v; bestresult = s; System.out.println("New best (" + bestvalue + "): \"" + bestresult + "\""); + System.out.flush(); + } + + // Tant qu'il reste des mots à chercher, ou des résultats à traiter + if ((linda.tryRead(new Tuple(Code.Result, reqUUID, String.class, Integer.class)) != null) + || (linda.tryRead(new Tuple(Code.Value, String.class, reqUUID)) != null)) { + + linda.eventRegister( + Linda.eventMode.TAKE, Linda.eventTiming.IMMEDIATE, + new Tuple(Code.Result, reqUUID, String.class, Integer.class), + this); + } else { + System.out.println("Ending callback loop"); + linda.write(new Tuple(Code.Searcher, "end", reqUUID)); } - linda.eventRegister(Linda.eventMode.TAKE, Linda.eventTiming.IMMEDIATE, new Tuple(Code.Result, reqUUID, String.class, Integer.class), this); } } public void run() { - this.loadData(pathname); - this.addSearch(search); + this.loadData(); + this.addSearch(); this.waitForEndSearch(); + this.waitForResult(); } } diff --git a/src/main/java/linda/search/basic/Searcher.java b/src/main/java/linda/search/basic/Searcher.java index b45dc31..cdaf054 100644 --- a/src/main/java/linda/search/basic/Searcher.java +++ b/src/main/java/linda/search/basic/Searcher.java @@ -2,6 +2,7 @@ package linda.search.basic; import linda.*; import java.util.Arrays; +import java.util.Collection; import java.util.UUID; public class Searcher implements Runnable { @@ -12,23 +13,40 @@ public class Searcher implements Runnable { this.linda = linda; } - public void run() { - System.out.println("Ready to do a search"); - Tuple treq = linda.read(new Tuple(Code.Request, UUID.class, String.class)); - UUID reqUUID = (UUID)treq.get(1); + private void search(Tuple treq) { + UUID reqUUID = (UUID) treq.get(1); String req = (String) treq.get(2); - Tuple tv; System.out.println("Looking for: " + req); - while ((tv = linda.tryTake(new Tuple(Code.Value, String.class))) != null) { + System.out.flush(); + + Tuple tv; + while ((tv = linda.tryTake(new Tuple(Code.Value, String.class, reqUUID))) != null) { String val = (String) tv.get(1); int dist = getLevenshteinDistance(req, val); if (dist < 10) { // arbitrary linda.write(new Tuple(Code.Result, reqUUID, val, dist)); } } - linda.write(new Tuple(Code.Searcher, "done", reqUUID)); + + System.out.println("Ending search: " + req); + System.out.flush(); + + Tuple end = new Tuple(Code.Searcher, "done", reqUUID); + if (linda.tryRead(end) == null) + linda.write(end); } - + + public void run() { + System.out.println("Ready to search"); + System.out.flush(); + + linda.read(new Tuple(Code.Request, UUID.class, String.class)); + Collection treqs = linda.readAll(new Tuple(Code.Request, UUID.class, String.class)); + for (Tuple treq : treqs) { + search(treq); + } + } + /*****************************************************************/ /* Levenshtein distance is rather slow */ @@ -39,15 +57,13 @@ public class Searcher implements Runnable { for (int j = 0; j <= y.length(); j++) { if (i == 0) { dp[i][j] = j; - } - else if (j == 0) { + } else if (j == 0) { dp[i][j] = i; - } - else { - dp[i][j] = min(dp[i - 1][j - 1] - + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), - dp[i - 1][j] + 1, - dp[i][j - 1] + 1); + } else { + dp[i][j] = min(dp[i - 1][j - 1] + + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), + dp[i - 1][j] + 1, + dp[i][j - 1] + 1); } } } @@ -63,4 +79,3 @@ public class Searcher implements Runnable { } } - diff --git a/src/test/java/linda/test/CentralizedLindaTests.java b/src/test/java/linda/test/CentralizedLindaTests.java index 6741271..ece9ba8 100644 --- a/src/test/java/linda/test/CentralizedLindaTests.java +++ b/src/test/java/linda/test/CentralizedLindaTests.java @@ -358,15 +358,15 @@ public class CentralizedLindaTests { }); runnerExact.start(); - // Thread runnerType = new Thread(() -> { - // Tuple tuple_template = new Tuple(Integer.class); - // linda.eventRegister( - // eventMode.READ, - // eventTiming.FUTURE, - // tuple_template, - // assertCallback); - // }); - // runnerType.start(); + Thread runnerType = new Thread(() -> { + Tuple tuple_template = new Tuple(Integer.class); + linda.eventRegister( + eventMode.READ, + eventTiming.FUTURE, + tuple_template, + assertCallback); + }); + runnerType.start(); Thread.sleep(1000); linda.write(tuple_write);