feat: multiple seachers + clean end for Manager
This commit is contained in:
parent
24810c2250
commit
e17c272636
12
.vscode/settings.json
vendored
12
.vscode/settings.json
vendored
|
@ -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"
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
package linda.search.basic;
|
||||
|
||||
public enum Code {
|
||||
Request, // Request, UUID, String
|
||||
Value, // Value, String
|
||||
Value, // Value, String, UUID
|
||||
Result, // Result, UUID, String, Int
|
||||
Searcher, // Result, "done", UUID
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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) {
|
||||
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 ]
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,21 +13,38 @@ 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));
|
||||
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<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++) {
|
||||
if (i == 0) {
|
||||
dp[i][j] = j;
|
||||
}
|
||||
else if (j == 0) {
|
||||
} else if (j == 0) {
|
||||
dp[i][j] = i;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
dp[i][j] = min(dp[i - 1][j - 1]
|
||||
+ costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
|
||||
dp[i - 1][j] + 1,
|
||||
|
@ -63,4 +79,3 @@ public class Searcher implements Runnable {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue