public Tweet getTweet(String tweetid) { Map<String, String> map = cassandra.listColumns(tweetid, TWEETS, false); return new Tweet( tweetid.getBytes(), map.get("uname"), map.get("body"), map.get("timestamp") == null ? null : Long.parseLong(map.get("timestamp"))); }
// Data Reading public User getUserByUsername(String uname) { Args.notNull(uname, "Name"); String password = cassandra.readColumn(uname, "password", USERS); if (null == password || password.equals("")) { return null; } return new User(uname.getBytes(), password); }
public void removeFriends(String from_uname, List<String> to_unames) { Mutator<String> mutator = cassandra.getMutator(); for (String uname : to_unames) { mutator .addDeletion(from_uname, FRIENDS, uname, SE) .addDeletion(uname, FOLLOWERS, from_uname, SE); mutator.decrementCounter(from_uname, MISC_COUNTS, "friends", 1); mutator.decrementCounter(uname, MISC_COUNTS, "followers", 1); } mutator.execute(); }
public void addFriends(String from_uname, List<String> to_unames) { long timestamp = System.currentTimeMillis(); Mutator<String> mutator = cassandra.getMutator(); for (String uname : to_unames) { mutator .addInsertion( from_uname, FRIENDS, HFactory.createStringColumn(uname, String.valueOf(timestamp))) .addInsertion( uname, FOLLOWERS, HFactory.createStringColumn(from_uname, String.valueOf(timestamp))) .addCounter(from_uname, MISC_COUNTS, HFactory.createCounterColumn("friends", 1)) .addCounter(uname, MISC_COUNTS, HFactory.createCounterColumn("followers", 1)); } mutator.execute(); }
private Timeline getLine( String COL_FAM, String uname, String startkey, int count, final boolean reversed) { Map<String, String> map = cassandra.listColumns(uname, COL_FAM, startkey, count, reversed); if (null == map || 0 == map.size()) { return null; } List<String> tweetids = ImmutableList.<String>copyOf(map.values()); List<Tweet> tweets = getTweetsForTweetids(tweetids); return new Timeline(tweets, Long.valueOf(String.valueOf(getLast(map.keySet(), 0)))); /* * 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); */ // return null; }
public void saveTweet(Tweet tweet) { long timestamp = System.currentTimeMillis(); String key = bToS(tweet.getKey()); Mutator<String> mutator = cassandra.getMutator(); mutator .addInsertion(key, TWEETS, createStringColumn("uname", tweet.getUname())) .addInsertion(key, TWEETS, createStringColumn("body", tweet.getBody())) .addInsertion(key, TWEETS, createStringColumn("timestamp", String.valueOf(timestamp))) .addInsertion( tweet.getUname(), USERLINE, createStringColumn(String.valueOf(timestamp), key)) .addInsertion(PUBLIC, USERLINE, createStringColumn(String.valueOf(timestamp), key)) .addCounter(tweet.getUname(), MISC_COUNTS, HFactory.createCounterColumn("tweets", 1)) .addCounter(PUBLIC, MISC_COUNTS, HFactory.createCounterColumn("tweets", 1)); ArrayList<String> followerUnames = new ArrayList<String>(getFollowerUnames(tweet.getUname())); for (String follower : followerUnames) { mutator.addInsertion(follower, TIMELINE, createColumn(timestamp, key)); } mutator.execute(); }
// Data Writing public void saveUser(User user) { cassandra.updateColumn(bToS(user.getKey()), user.getPassword(), "password", USERS); }
public long getCounter(String uname, String counter) { return cassandra.countColumns(uname, counter, MISC_COUNTS); }
// Helpers private List<String> getFriendOrFollowerUnames(String COL_FAM, String uname, int count) { Map<String, String> map = cassandra.listColumns(uname, COL_FAM, null, count, false); return ImmutableList.<String>copyOf(map.keySet()); }