feat: multiple seachers + clean end for Manager

This commit is contained in:
Laureηt 2021-12-29 18:08:52 +01:00
parent 24810c2250
commit e17c272636
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
6 changed files with 100 additions and 59 deletions

12
.vscode/settings.json vendored
View file

@ -1,12 +1,4 @@
{ {
"files.exclude": { "java.configuration.updateBuildConfiguration": "automatic",
"**/.git": true, "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m"
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/*.class": true
},
"java.configuration.updateBuildConfiguration": "automatic"
} }

View file

@ -1,7 +1,8 @@
package linda.search.basic; package linda.search.basic;
public enum Code { public enum Code {
Request, // Request, UUID, String Request, // Request, UUID, String
Value, // Value, String Value, // Value, String, UUID
Result, // Result, UUID, String, Int Result, // Result, UUID, String, Int
Searcher, // Result, "done", UUID Searcher, // Result, "done", UUID
} }

View file

@ -4,15 +4,24 @@ import linda.*;
public class Main { public class Main {
private static final int NB_MANAGER = 1;
private static final int NB_SEARCHER = 10;
public static void main(String args[]) { public static void main(String args[]) {
if (args.length != 2) { // if (args.length != 2) {
System.err.println("linda.search.basic.Main search file."); // System.err.println("linda.search.basic.Main search file.");
return; // return;
} // }
Linda linda = new linda.shm.CentralizedLinda(); Linda linda = new linda.shm.CentralizedLinda();
Manager manager = new Manager(linda, args[1], args[0]);
Searcher searcher = new Searcher(linda); for (int i = 0; i < NB_MANAGER; i++) {
Manager manager = new Manager(linda, "/usr/share/dict/french", "agneau");
(new Thread(manager)).start(); (new Thread(manager)).start();
}
for (int i = 0; i < NB_SEARCHER; i++) {
Searcher searcher = new Searcher(linda);
(new Thread(searcher)).start(); (new Thread(searcher)).start();
} }
} }
}

View file

@ -22,46 +22,70 @@ public class Manager implements Runnable {
this.linda = linda; this.linda = linda;
this.pathname = pathname; this.pathname = pathname;
this.search = search; this.search = search;
this.reqUUID = UUID.randomUUID();
this.search = search;
} }
private void addSearch(String search) { private void addSearch() {
this.search = search; System.out.println("Search (" + this.reqUUID + ") for " + this.search);
this.reqUUID = UUID.randomUUID(); linda.eventRegister(
System.out.println("Search " + this.reqUUID + " for " + this.search); Linda.eventMode.TAKE, Linda.eventTiming.FUTURE,
linda.eventRegister(Linda.eventMode.TAKE, Linda.eventTiming.IMMEDIATE, new Tuple(Code.Result, this.reqUUID, String.class, Integer.class), new CbGetResult()); new Tuple(Code.Result, this.reqUUID, String.class, Integer.class),
new CbGetResult());
linda.write(new Tuple(Code.Request, this.reqUUID, this.search)); linda.write(new Tuple(Code.Request, this.reqUUID, this.search));
} }
private void loadData(String pathname) { private void loadData() {
try (Stream<String> stream = Files.lines(Paths.get(pathname))) { try (Stream<String> 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) { } catch (java.io.IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void waitForEndSearch() { private void waitForEndSearch() {
linda.take(new Tuple(Code.Searcher, "done", this.reqUUID)); 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 query linda.take(new Tuple(Code.Request, this.reqUUID, String.class)); // remove search query
System.out.println("query done"); 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 { 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); String s = (String) t.get(2);
Integer v = (Integer) t.get(3); Integer v = (Integer) t.get(3);
if (v < bestvalue) { if (v < bestvalue) {
bestvalue = v; bestvalue = v;
bestresult = s; bestresult = s;
System.out.println("New best (" + bestvalue + "): \"" + bestresult + "\""); 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() { public void run() {
this.loadData(pathname); this.loadData();
this.addSearch(search); this.addSearch();
this.waitForEndSearch(); this.waitForEndSearch();
this.waitForResult();
} }
} }

View file

@ -2,6 +2,7 @@ package linda.search.basic;
import linda.*; import linda.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.UUID; import java.util.UUID;
public class Searcher implements Runnable { public class Searcher implements Runnable {
@ -12,21 +13,38 @@ public class Searcher implements Runnable {
this.linda = linda; this.linda = linda;
} }
public void run() { private void search(Tuple treq) {
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); UUID reqUUID = (UUID) treq.get(1);
String req = (String) treq.get(2); String req = (String) treq.get(2);
Tuple tv;
System.out.println("Looking for: " + req); 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); String val = (String) tv.get(1);
int dist = getLevenshteinDistance(req, val); int dist = getLevenshteinDistance(req, val);
if (dist < 10) { // arbitrary if (dist < 10) { // arbitrary
linda.write(new Tuple(Code.Result, reqUUID, val, dist)); 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<Tuple> treqs = linda.readAll(new Tuple(Code.Request, UUID.class, String.class));
for (Tuple treq : treqs) {
search(treq);
}
} }
/*****************************************************************/ /*****************************************************************/
@ -39,11 +57,9 @@ public class Searcher implements Runnable {
for (int j = 0; j <= y.length(); j++) { for (int j = 0; j <= y.length(); j++) {
if (i == 0) { if (i == 0) {
dp[i][j] = j; dp[i][j] = j;
} } else if (j == 0) {
else if (j == 0) {
dp[i][j] = i; dp[i][j] = i;
} } else {
else {
dp[i][j] = min(dp[i - 1][j - 1] dp[i][j] = min(dp[i - 1][j - 1]
+ costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
dp[i - 1][j] + 1, dp[i - 1][j] + 1,
@ -63,4 +79,3 @@ public class Searcher implements Runnable {
} }
} }

View file

@ -358,15 +358,15 @@ public class CentralizedLindaTests {
}); });
runnerExact.start(); runnerExact.start();
// Thread runnerType = new Thread(() -> { Thread runnerType = new Thread(() -> {
// Tuple tuple_template = new Tuple(Integer.class); Tuple tuple_template = new Tuple(Integer.class);
// linda.eventRegister( linda.eventRegister(
// eventMode.READ, eventMode.READ,
// eventTiming.FUTURE, eventTiming.FUTURE,
// tuple_template, tuple_template,
// assertCallback); assertCallback);
// }); });
// runnerType.start(); runnerType.start();
Thread.sleep(1000); Thread.sleep(1000);
linda.write(tuple_write); linda.write(tuple_write);