-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBase.java
More file actions
341 lines (309 loc) · 12.5 KB
/
Base.java
File metadata and controls
341 lines (309 loc) · 12.5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package example;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.*;
import example.models.Timeline;
import example.models.Tweet;
import example.models.User;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.Column;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebSession;
import org.wyki.cassandra.pelops.Mutator;
import org.wyki.cassandra.pelops.NumberHelper;
import org.wyki.cassandra.pelops.Pelops;
import org.wyki.cassandra.pelops.Selector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.wicket.markup.html.basic.Label;
/**
* Base contains both the default header/footer things for the UI as
* well as all the shared code for all the child controllers.
*/
public abstract class Base extends WebPage {
final static Logger log = LoggerFactory.getLogger(Base.class);
//CLs
final ConsistencyLevel WCL = ConsistencyLevel.ONE;
final ConsistencyLevel RCL = ConsistencyLevel.ONE;
//Column Family names
final static String USERS = "User";
final static String FRIENDS = "Friends";
final static String FOLLOWERS = "Followers";
final static String TWEETS = "Tweet";
final static String TIMELINE = "Timeline";
final static String USERLINE = "Userline";
//UI settings
public Base(final PageParameters parameters) {
add(CSSPackageResource.getHeaderContribution(Base.class, "960.css"));
add(CSSPackageResource.getHeaderContribution(Base.class, "reset.css"));
add(CSSPackageResource.getHeaderContribution(Base.class, "screen.css"));
add(CSSPackageResource.getHeaderContribution(Base.class, "text.css"));
String condauth = "Log";
String username = ((TwissSession)WebSession.get()).getUname();
if (username == null) {
condauth += "in";
}
else {
condauth += "out: " + username;
}
add(new Label("loginout", condauth));
}
//
// SHARED CODE
//
//Space-savers
private String bToS(byte[] bytes) {
return new String(bytes, Charset.forName("UTF-8"));
}
private Selector makeSel() {
return Pelops.createSelector("Twissjava Pool", "Twissandra");
}
private Mutator makeMut() {
return Pelops.createMutator("Twissjava Pool", "Twissandra");
}
private SlicePredicate SPall(){
return Selector.newColumnsPredicateAll(false,5000);
}
private Tweet makeTweet(byte[] key, List<Column> tweetcols) {
return new Tweet(key, bToS(tweetcols.get(1).value), bToS(tweetcols.get(0).value));
}
//Helpers
private List<String> getFriendOrFollowerUnames(String COL_FAM, String uname, int count) {
Selector selector = makeSel();
List<Column> row;
try {
row = selector.getColumnsFromRow(uname, COL_FAM, Selector.newColumnsPredicateAll(false, count), RCL);
}
catch (Exception e) {
log.error("No record found for uname: " + uname + ", COL_FAM: " + COL_FAM);
return Collections.emptyList();
}
ArrayList<String> unames = new ArrayList<String>(row.size());
for(Column c : row) {
unames.add(bToS(c.name));
}
return unames;
}
private Timeline getLine(String COL_FAM, String uname, String startkey, int count) {
Selector selector = makeSel();
List<Column> timeline;
byte[] longTypeStartKey = (startkey.equals("") ? new byte[0] : NumberHelper.toBytes(Long.parseLong(startkey)));
try {
timeline = selector.getColumnsFromRow(uname, COL_FAM, Selector.newColumnsPredicate(longTypeStartKey,new byte[0],true,count+1), RCL);
}
catch (Exception e) {
log.error("Unable to retrieve timeline for uname: " + uname);
return null;
}
Long mintimestamp = null;
if (timeline.size() > count) {
//find min timestamp
mintimestamp = Long.MAX_VALUE;
Column removeme = timeline.get(0); //This cannot fail. Count is 0+, and size is thus 1+. Only needed for initialization.
for (Column c : timeline) {
long ctime = ByteBuffer.wrap(c.name).getLong();
if (ctime < mintimestamp) {
mintimestamp = ctime;
removeme = c;
}
}
//eject column from list after saving the timestamp
timeline.remove(removeme);
}
ArrayList<String> tweetids = new ArrayList<String>(timeline.size());
for (Column c : timeline) {
tweetids.add(bToS(c.value));
}
Map<String, List<Column>> unordered_tweets = Collections.emptyMap();
try {
unordered_tweets = selector.getColumnsFromRows(tweetids, TWEETS, SPall(), RCL);
}
catch (Exception e) {
log.error("Unable to retrieve tweets from timeline for uname: " + uname);
return null;
}
//Order the tweets by the ordered tweetids
ArrayList<Tweet> ordered_tweets = new ArrayList<Tweet>(tweetids.size());
for (String tweetid : tweetids) {
ordered_tweets.add(makeTweet(tweetid.getBytes(),unordered_tweets.get(tweetid)));
}
return new Timeline(ordered_tweets, mintimestamp);
}
//Data Reading
public User getUserByUsername(String uname) {
Selector selector = makeSel();
List<Column> usercols;
try {
usercols = selector.getColumnsFromRow(uname, USERS, Selector.newColumnsPredicateAll(false,5000), RCL);
}
catch (Exception e) {
log.error("Cannot find user by uname: " + uname);
return null;
}
if (usercols.size() == 0) {
log.error("User does not exist: " + uname);
return null;
}
return new User(uname.getBytes(), bToS(usercols.get(0).value));
}
public List<String> getFriendUnames(String uname) {
return getFriendUnames(uname, 5000);
}
public List<String> getFriendUnames(String uname, int count) {
return getFriendOrFollowerUnames(FRIENDS, uname, count);
}
public List<String> getFollowerUnames(String uname) {
return getFollowerUnames(uname, 5000);
}
public List<String> getFollowerUnames(String uname, int count) {
return getFriendOrFollowerUnames(FOLLOWERS, uname, count);
}
public List<User> getUsersForUnames(List<String> unames) {
Selector selector = makeSel();
ArrayList<User> users = new ArrayList<User>();
Map<String, List<Column>> data;
try {
data = selector.getColumnsFromRows(unames, USERS, SPall(), RCL);
}
catch (Exception e) {
log.error("Cannot get users for unames: " + unames);
return users;
}
for (Map.Entry<String,List<Column>> row : data.entrySet()) {
users.add(new User(row.getKey().getBytes(), bToS(row.getValue().get(0).value)));
}
return users;
}
public List<User> getFriends(String uname) {
return getFriends(uname, 5000);
}
public List<User> getFriends(String uname, int count) {
List<String> friendUnames = getFriendUnames(uname, count);
return getUsersForUnames(friendUnames);
}
public List<User> getFollowers(String uname) {
return getFollowers(uname, 5000);
}
public List<User> getFollowers(String uname, int count) {
List<String> followerUnames = getFollowerUnames(uname, count);
return getUsersForUnames(followerUnames);
}
public Timeline getTimeline(String uname) {
return getTimeline(uname, "", 40);
}
public Timeline getTimeline(String uname, Long startkey) {
String longAsStr = (startkey == null) ? "" : String.valueOf(startkey);
return getTimeline(uname, longAsStr, 40);
}
public Timeline getTimeline(String uname, String startkey, int limit) {
return getLine(TIMELINE, uname, startkey, limit);
}
public Timeline getUserline(String uname) {
return getUserline(uname, "", 40);
}
public Timeline getUserline(String uname, Long startkey) {
String longAsStr = (startkey == null) ? "" : String.valueOf(startkey);
return getUserline(uname, longAsStr, 40);
}
public Timeline getUserline(String uname, String startkey, int limit) {
return getLine(USERLINE, uname, startkey, limit);
}
public Tweet getTweet(String tweetid) {
Selector selector = makeSel();
List<Column> tweetcols;
try {
tweetcols = selector.getColumnsFromRow(tweetid, TWEETS, SPall(), RCL);
}
catch (Exception e) {
log.error("Could not locate tweet for id: " + tweetid);
return null;
}
//maketweet from cols and return
return makeTweet(tweetid.getBytes(),tweetcols);
}
public List<Tweet> getTweetsForTweetids(List<String> tweetids) {
Selector selector = makeSel();
Map<String, List<Column>> data;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
try {
data = selector.getColumnsFromRows(tweetids, TWEETS, SPall(), RCL);
}
catch (Exception e) {
log.error("Cannot get tweets for tweetids: " + tweetids);
return tweets;
}
//loop maketweet from cols and return
for (Map.Entry<String, List<Column>> datarow : data.entrySet()) {
tweets.add(makeTweet(datarow.getKey().getBytes(), datarow.getValue()));
}
return tweets;
}
//Data Writing
public void saveUser(User user) {
Mutator mutator = makeMut();
mutator.writeColumn(bToS(user.getKey()), USERS, mutator.newColumn("password",user.getPassword()));
try {
mutator.execute(WCL);
}
catch (Exception e) {
log.error("Unable to save user: " + bToS(user.getKey()));
}
}
public void saveTweet(Tweet tweet) {
long timestamp = System.currentTimeMillis();
Mutator mutator = makeMut();
//Insert the tweet into tweets cf
String key = bToS(tweet.getKey());
mutator.writeColumn(key, TWEETS, mutator.newColumn("uname",tweet.getUname()));
mutator.writeColumn(key, TWEETS, mutator.newColumn("body",tweet.getBody()));
//Insert into the user's timeline
mutator.writeColumn(tweet.getUname(), USERLINE, mutator.newColumn(NumberHelper.toBytes(timestamp), key));
//Insert into the public timeline
mutator.writeColumn("!PUBLIC!", USERLINE, mutator.newColumn(NumberHelper.toBytes(timestamp), key));
//Insert into all followers streams
ArrayList<String> followerUnames = new ArrayList<String>(getFollowerUnames(tweet.getUname()));
followerUnames.add(tweet.getUname());
for (String follower : followerUnames) {
mutator.writeColumn(follower, TIMELINE, mutator.newColumn(NumberHelper.toBytes(timestamp), key));
}
try {
mutator.execute(WCL);
}
catch (Exception e) {
log.error("Unable to save tweet: " + tweet.getUname() + ": " + tweet.getBody());
}
}
public void addFriends(String from_uname, List<String> to_unames) {
long timestamp = System.currentTimeMillis();
Mutator mutator = makeMut();
ArrayList<Column> friends = new ArrayList<Column>();
for (String uname : to_unames) {
friends.add(mutator.newColumn(uname, String.valueOf(timestamp)));
mutator.writeColumn(uname, FOLLOWERS, mutator.newColumn(from_uname, String.valueOf(timestamp)));
}
mutator.writeColumns(from_uname, FRIENDS, friends);
try {
mutator.execute(WCL);
}
catch (Exception e) {
log.error("Unable to add friendship from: " + from_uname + ", to: " + to_unames);
}
}
public void removeFriends(String from_uname, List<String> to_unames) {
Mutator mutator = makeMut();
for (String uname : to_unames) {
mutator.deleteColumn(from_uname, FRIENDS, uname);
mutator.deleteColumn(uname, FOLLOWERS, from_uname);
}
try {
mutator.execute(WCL);
}
catch (Exception e) {
log.error("Unable to remove friendship from: " + from_uname + ", to: " + to_unames);
}
}
}