-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataNode.java
More file actions
51 lines (43 loc) · 1.2 KB
/
DataNode.java
File metadata and controls
51 lines (43 loc) · 1.2 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
import java.util.ArrayList;
import java.util.Collections;
import org.apache.log4j.Logger;
public class DataNode {
private static Logger log = Logger.getLogger(DataNode.class);
ArrayList<Tweet> tweets;
public DataNode() {
tweets = new ArrayList<Tweet>();
}
public VectorClock getVersion() {
Tweet max = tweets.get(0);
for (Tweet t : tweets) {
if (t.compareTo(max) > 0) {
max = t;
}
}
return max.getClock();
}
public void addTweet(String tweet) {
ServerStateManager.incrementMyClock();
VectorClock c = ServerStateManager.getMyClockVal();
log.info("Adding tweet '" + tweet + "' at time: " + c);
tweets.add(new Tweet(tweet, c));
}
public ArrayList<Tweet> getTweetsList() {
Collections.sort(tweets);
return tweets;
}
public void mergeTweet(Tweet t, VectorClock c) {
log.info("Merging tweet '" + t.getTweet() + "' at time: " + t.getClock() + " with time " + c);
VectorClock newClock = t.getClock();
newClock.mergeIn(c);
tweets.add(new Tweet(t.getTweet(), newClock));
}
public ArrayList<String> getTweets() {
ArrayList<String> retval = new ArrayList<String>();
for (Tweet t : tweets) {
retval.add(t.getTweet());
}
Collections.sort(retval);
return retval;
}
}