def removeFollower(self, username, follower): """removeFollower username follower""" following_fn = "following:" + follower with Transaction() as fs: yield fs.begin() content = (yield fs.read(following_fn)) if content is None: yield Op.FollowerChangeResult.FAILURE content = Utility.byteArrayToString(content) out = StringBuffer() absent = True for line in lines(content): followed_user, timestamp = line.split("\t") if followed_user == username: absent = False else: out.append(line + "\n") fs.overwrite(following_fn, Utility.stringToByteArray(out.toString())) if not (yield fs.commit()): yield Op.FollowerChangeResult.FAILURE elif absent: yield Op.FollowerChangeResult.DOES_NOT_EXIST else: yield Op.FollowerChangeResult.SUCCESS
def addFollower(self, username, follower): """addFollower username follower""" following_fn = "following:" + follower with Transaction() as fs: yield fs.begin() content = (yield fs.read(following_fn)) if content is None: yield Op.FollowerChangeResult.FAILURE content = Utility.byteArrayToString(content) for line in lines(content): followed_user, timestamp = line.split("\t") if followed_user == username: yield Op.FollowerChangeResult.ALREADY_EXISTS line = "%s\t%d\n" % (username, System.currentTimeMillis()) if not (yield fs.append(following_fn, Utility.stringToByteArray(line))): yield Op.FollowerChangeResult.FAILURE if not (yield fs.commit()): yield Op.FollowerChangeResult.FAILURE yield Op.FollowerChangeResult.SUCCESS
def getTimeline(self, username): """getTimeline username""" result = [] user_fn = "users:" + username with Transaction() as fs: yield fs.begin() content = (yield fs.read(user_fn)) if content is None: yield None content = Utility.byteArrayToString(content) timestamp = long(content) following_fn = "following:" + username followings = [] content = (yield fs.read(following_fn)) if content is None: yield None for line in lines(Utility.byteArrayToString(content)): followed, follow_timestamp = line.split("\t") followings.append(Pair(followed, Long(follow_timestamp))) results = [] for user_followed, follow_timestamp in followings: tweets = (yield fs.read("tweets:" + user_followed)) if tweets is None: yield None else: results.append(tweets) for content in results: chits = self._content_to_chits(content) if chits is not None: cutoff = max(follow_timestamp, timestamp) result.extend(chit for chit in chits if chit.timestamp > cutoff) timestamp = System.currentTimeMillis() payload = Utility.stringToByteArray(str(timestamp)) if not (yield fs.overwrite(user_fn, payload)): yield None if not (yield fs.commit()): yield None result.sort() yield result
def createUser(self, username): """createUser username""" if not valid_username(username): yield False tweets_fn = "tweets:" + username following_fn = "following:" + username user_fn = "users:" + username with Transaction() as fs: yield fs.begin() if not (yield fs.create(tweets_fn)): yield False if not (yield fs.create(following_fn)): yield False if not (yield fs.create(user_fn)): yield False content = "%d" % (0,) if not (yield fs.overwrite(user_fn, Utility.stringToByteArray(content))): yield False yield (yield fs.commit())