-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendVirtualDB.java
More file actions
executable file
·111 lines (90 loc) · 2.45 KB
/
BackendVirtualDB.java
File metadata and controls
executable file
·111 lines (90 loc) · 2.45 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.log4j.Logger;
public class BackendVirtualDB {
private static Logger log = Logger.getLogger(BackendVirtualDB.class);
private HashMap<String, VirtualDataNode> index;
private transient ReentrantReadWriteLock lock;
private int clock;
public BackendVirtualDB() {
index = new HashMap<String, VirtualDataNode>();
lock = new ReentrantReadWriteLock();
clock = 0;
}
public BackendVirtualDB(HashMap<String, VirtualDataNode> i, int c) {
index = i;
lock = new ReentrantReadWriteLock();
clock = c;
}
public void addTweet(String hash, String tweet) {
log.info("Adding tweet '" + tweet + "' to #" + hash);
lock.writeLock().lock();
VirtualDataNode tweetList = null;
if ((tweetList = index.get(hash)) == null) {
index.put(hash, new VirtualDataNode());
tweetList = index.get(hash);
}
tweetList.addTweet(tweet, clock++);
lock.writeLock().unlock();
}
public ArrayList<String> getTweets(String hash) {
lock.readLock().lock();
ArrayList<String> retval = index.get(hash).getTweets();
lock.readLock().unlock();
return retval;
}
public int getMaxClock(String hash) {
lock.readLock().lock();
int retval = index.get(hash).getMaxClock();
lock.readLock().unlock();
return retval;
}
private class VirtualDataNode {
public ArrayList<Tweet> tweetList;
public VirtualDataNode() {
tweetList = new ArrayList<Tweet>();
}
@SuppressWarnings("unused")
public VirtualDataNode(ArrayList<Tweet> t) {
tweetList = t;
}
public void addTweet(String tweet, int c) {
tweetList.add(new Tweet(tweet, c));
}
public ArrayList<String> getTweets() {
Collections.sort(tweetList);
ArrayList<String> retval = new ArrayList<String>();
for (Tweet t : tweetList) {
retval.add(t.toString());
}
return retval;
}
public int getMaxClock() {
Collections.sort(tweetList);
return tweetList.get(0).clock;
}
private class Tweet implements Comparable<Tweet> {
private String tweet;
private int clock;
public Tweet(String t, int c) {
tweet = t;
clock = c;
}
public int getClock() {
return clock;
}
public String toString() {
return tweet + " [" + clock + "]";
}
public int compareTo(Tweet t) {
if (clock < t.getClock())
return 1;
else if (clock > t.getClock())
return -1;
return 0;
}
}
}
}