def follow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return None if not GateKeeper.is_switched_on('switch_friendship_to_hbase'): # create data in mysql return Friendship.objects.create( from_user_id=from_user_id, to_user_id=to_user_id, ) # create data in hbase # first we retrieve the current time and store it in a local variable # in case there are inconsistencies between the two records that we created in those two tables now = int(time.time() * 1000000) HBaseFollower.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, ) return HBaseFollowing.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, )
def follow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return None # update Redis cls.add_following_id_in_redis(from_user_id, to_user_id) # update DB if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): # create data in MySQL return Friendship.objects.create( from_user_id=from_user_id, to_user_id=to_user_id, ) # create data in hbase now = int(time.time() * 1000000) HBaseFollower.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, ) return HBaseFollowing.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, )
def test_create_and_get(self): # missing column data, can not store in hbase try: HBaseFollower.create(to_user_id=1, created_at=self.ts_now) exception_raised = False except EmptyColumnError: exception_raised = True self.assertEqual(exception_raised, True) # invalid row_key try: HBaseFollower.create(from_user_id=1, to_user_id=2) exception_raised = False except BadRowKeyError as e: exception_raised = True self.assertEqual(str(e), 'created_at is missing in row key') self.assertEqual(exception_raised, True) ts = self.ts_now HBaseFollower.create(from_user_id=1, to_user_id=2, created_at=ts) instance = HBaseFollower.get(to_user_id=2, created_at=ts) self.assertEqual(instance.from_user_id, 1) self.assertEqual(instance.to_user_id, 2) self.assertEqual(instance.created_at, ts) # can not get if row key missing try: HBaseFollower.get(to_user_id=2) exception_raised = False except BadRowKeyError as e: exception_raised = True self.assertEqual(str(e), 'created_at is missing in row key') self.assertEqual(exception_raised, True)
def follow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return None if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): # create data in mysql return Friendship.objects.create( from_user_id=from_user_id, to_user_id=to_user_id, ) # create data in hbase now = int(time.time() * 1000000) HBaseFollower.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, ) return HBaseFollowing.create( from_user_id=from_user_id, to_user_id=to_user_id, created_at=now, )