def unfollow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return 0 # Queryset 的 delete 操作返回两个值,一个是删了多少数据,一个是具体每种类型删了多少 # 为什么会出现多种类型数据的删掉?因为可能因为 foreign key 设置了 cascade 出现级联 # 删除,也就是比如 A model 的某个属性是 B model 的 foreign key,并且设置了 # on_delete = models.CASCADE,那么当 B 的某个数据被删除的时候,A 中的关联也会被删除。 # 所以 CASCADE 是很危险的,一般最好不要用,而是用 on_delete = models.SET_NULL # 取而代之,这样至少可以避免误删除操作带来的多米诺效应 if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): deleted, _ = Friendship.objects.filter( from_user_id=from_user_id, to_user_id=to_user_id, ).delete() return deleted instance = cls.get_follow_instance(from_user_id, to_user_id) if instance is None: return 0 HBaseFollowing.delete(from_user_id=from_user_id, created_at=instance.created_at) HBaseFollower.delete(to_user_id=to_user_id, created_at=instance.created_at) return 1
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 unfollow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return 0 if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): deleted, _ = Friendship.objects.filter( from_user_id=from_user_id, to_user_id=to_user_id, ).delete() return deleted instance = cls.get_follow_instance(from_user_id, to_user_id) if instance is None: return 0 HBaseFollowing.delete(from_user_id=from_user_id, created_at=instance.created_at) HBaseFollower.delete(to_user_id=to_user_id, created_at=instance.created_at) return 1
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'): return Friendship.objects.create( from_user_id=from_user_id, to_user_id=to_user_id, ) now = ts_now_as_int() HBaseFollower.create( from_user_id=from_user_id, created_at=now, to_user_id=to_user_id, ) return HBaseFollowing.create( from_user_id=from_user_id, created_at=now, to_user_id=to_user_id, )
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, )
def unfollow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return 0 if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): delete, _ = Friendship.objects.filter( from_user_id=from_user_id, to_user_id=to_user_id, ).delete() return delete following = cls.get_following_instance(from_user_id=from_user_id, to_user_id=to_user_id) # fs = HBaseFollowing.filter(prefix=(from_user_id, None)) # for f in fs: # print(f.from_user_id, f.created_at, '->', f.to_user_id) # print(following) if following is None: return 0 HBaseFollowing.delete(from_user_id=from_user_id, created_at=following.created_at) HBaseFollower.delete(to_user_id=to_user_id, created_at=following.created_at) return 1
def get_follower_count(cls, to_user_id): if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): return Friendship.objects.filter(to_user_id=to_user_id).count() followers = HBaseFollower.filter(prefix=(to_user_id, None)) return len(followers)
def get_follower_ids(cls, to_user_id): if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): friendships = Friendship.objects.filter(to_user_id=to_user_id) else: friendships = HBaseFollower.filter(prefix=(to_user_id, None)) return [friendship.from_user_id for friendship in friendships]
def test_create_and_get(self): now = self.ts_now() HBaseFollower.create( created_at=now, to_user_id=456, from_user_id=123, ) follower = HBaseFollower.get(to_user_id=456, created_at=now) self.assertEqual(follower.from_user_id, 123) self.assertEqual(follower.to_user_id, 456) self.assertEqual(follower.created_at, now) # invalid row_key get try: HBaseFollower.get(to_user_id=456, from_user_id=123) exception_raised = False except BadRowKeyException as e: exception_raised = True self.assertEqual(str(e), "created_at not defined") self.assertEqual(exception_raised, True) # invalid row_keys creation try: HBaseFollower.create(from_user_id=123, create_at=now) exception_raised = False except BadRowKeyException as e: exception_raised = True self.assertEqual(str(e), 'to_user_id not defined') self.assertEqual(exception_raised, True) # no column_key value try: HBaseFollower.create(to_user_id=123, created_at=now) exception_raised = False except EmptyColumnException as e: exception_raised = True self.assertEqual(exception_raised, True) # wrong order input try: HBaseFollower.create(from_user_id=0, to_user_id=123, created_at=now) exception_raised = False except EmptyColumnException as e: exception_raised = True self.assertEqual(exception_raised, True) # row_key should not have ':' try: HBaseFollower.create( from_user_id=123, created_at=now, to_user_id='456:', ) exception_raised = False except BadRowKeyException as e: exception_raised = True self.assertEqual(str(e), "to_user_id must not have ':' in value 456:") self.assertEqual(exception_raised, True)