-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaryLoader.java
73 lines (63 loc) · 2.37 KB
/
DictionaryLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* Created by ajay on 4/21/17.
*/
public class DictionaryLoader {
private final String dictSource;
private final String ip;
private final int port;
public static void main(String args[]) {
if (args.length!=3) {
System.err.println("Usage: java DictionaryLoader ipaddr<str> port<int> dict-srcfile<str-path>");
System.exit(1);
}
new DictionaryLoader(args[0], Integer.parseInt(args[1]), args[2]).load();
}
public DictionaryLoader(String ip, int primaryPort, String srcFile) {
this.ip = ip;
this.port = primaryPort;
this.dictSource = srcFile;
}
public void load() {
try(Stream<String> contentStream = Files.lines(Paths.get(dictSource))) {
contentStream.forEach(this::insertToChord);
} catch (IOException ioe) { ioe.printStackTrace();}
}
private void insertToChord(String line) {
String parts[] = line.split(":");
int hash = ChordUtility.hash(parts[0].trim());
NodeRef successor = null;
TTransport transport = new TSocket(ip, port);
try {
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
ChordService.Client chordNode = new ChordService.Client(protocol);
successor = chordNode.findSuccessor(hash);
transport.close();
} catch (TException e) {
System.err.println("exception at findPredecessor");
e.printStackTrace();
}
if(successor!=null) {
try {
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
ChordService.Client chordNode = new ChordService.Client(protocol);
chordNode.insert(hash, parts[1].trim());
transport.close();
} catch (TException e) {
System.err.println("exception at findPredecessor");
e.printStackTrace();
}
}
}
}