-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendDB.java
More file actions
executable file
·70 lines (50 loc) · 1.64 KB
/
BackendDB.java
File metadata and controls
executable file
·70 lines (50 loc) · 1.64 KB
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
import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
public class BackendDB {
private static Logger log = Logger.getLogger(BackendDB.class);
private static BackendDB instance = null;
private ConcurrentHashMap<Integer, BackendVirtualDB> database = null;
public static BackendDB getInstance() {
if (instance == null) {
synchronized (BackendDB.class) {
if (instance == null) {
instance = new BackendDB();
}
}
}
return instance;
}
private BackendDB() {
database = new ConcurrentHashMap<Integer, BackendVirtualDB>();
}
public void initNew (int numVirtual) {
log.info("Initializing system database");
for (int i = 0; i < numVirtual; i++) {
database.put(i, new BackendVirtualDB());
}
}
public void putNode(int VirtualNode, BackendVirtualDB vdb) {
database.put(VirtualNode, vdb);
}
public String getNode(int VirtualNode) {
BackendVirtualDB vdb = database.get(VirtualNode);
return Util.g.toJson(vdb, BackendVirtualDB.class);
}
public void addTweet(int VirtualNode, String hash, String tweet) {
BackendVirtualDB vdb = database.get(VirtualNode);
vdb.addTweet(hash, tweet);
}
public Row getTweets(int VirtualNode, String hash) {
BackendVirtualDB vdb = database.get(VirtualNode);
Row retval = new Row(vdb.getTweets(hash), vdb.getMaxClock(hash));
return retval;
}
public static void main (String[] args) {
BackendDB bdb = BackendDB.getInstance();
bdb.initNew(1);
bdb.addTweet(0, "apple", "I have 1 #apple");
bdb.addTweet(0, "apple", "I have two apples");
Row r = bdb.getTweets(0, "apple");
System.out.println(Util.g.toJson(r, Row.class));
}
}