-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterAPIRedis.java
87 lines (71 loc) · 2.36 KB
/
TwitterAPIRedis.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.*;
/**
* Created by Tim on 2/3/2018.
*/
public class TwitterAPIRedis implements TwitterAPI {
// server client to be used with Redis
Jedis jedis;
// constructor
public TwitterAPIRedis(){
this.jedis = new Jedis("localhost");
}
// increments and returns the Tweet ID of the next tweet
public long getNextTweetId(){
long tweetId = jedis.incr("nextTweetId");
return tweetId;
}
// posts a single tweet
public void postTweet(Tweet t, boolean broadcast){
String key = "Tweet:" + t.getUserId() + ":" + Long.toString(this.getNextTweetId());
String value = t.getTweetTs() + t.getTweetText();
// add tweet, regardless of broadcast
this.jedis.set(key, value);
// add tweet if broadcast is on
if (broadcast)
{
Set<String> followers = jedis.smembers("Followers:"+t.getUserID());
for (String f : followers)
addToTimeline(t, f);
}
}
// adds a follower to the followee
public void addFollower(String followerId, String followeeId){
String key = "Followers:" + followeeId;
this.jedis.sadd(key, followerId);
}
// add tweet to a follower's timeline
public void addToTimeline(Tweet t, String followerId){
String key = "Timeline:" + followerId;
String value = t.toString();
this.jedis.lpush(key, value);
}
// returns all tweets from who the user follows
public List<Tweet> getTimeline(String userId){
String key = "Timeline:" + userId;
List<Tweet> timeline = this.jedis.get(key);
return timeline;
}
// returns all followers of the user
public List<String> getFollowers(String userId){
String key = "Followers:" + userId;
List<String> followers = this.jedis.get(key);
return followers;
}
// return all users the user follows
public List<String> getFollowees(String userId){
// TODO
}
// return all tweets by the user
public List<Tweet> getTweets(String userId){
String key = "Tweet:" + userId + "*";
List<Tweet> tweets = this.jedis.keys(key);
return tweets;
}
// clears all of the data from the database
public void reset(){
this.jedis.flushAll();
}
}