Exemplo n.º 1
0
    def test_filter(self):
        HBaseFollowing.create(from_user_id=1,
                              to_user_id=2,
                              created_at=self.ts_now)
        HBaseFollowing.create(from_user_id=1,
                              to_user_id=3,
                              created_at=self.ts_now)
        HBaseFollowing.create(from_user_id=1,
                              to_user_id=4,
                              created_at=self.ts_now)

        followings = HBaseFollowing.filter(prefix=(1, None))
        self.assertEqual(3, len(followings))
        self.assertEqual(followings[0].from_user_id, 1)
        self.assertEqual(followings[0].to_user_id, 2)
        self.assertEqual(followings[1].from_user_id, 1)
        self.assertEqual(followings[1].to_user_id, 3)
        self.assertEqual(followings[2].from_user_id, 1)
        self.assertEqual(followings[2].to_user_id, 4)

        # test limit
        results = HBaseFollowing.filter(prefix=(1, None), limit=1)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].to_user_id, 2)

        results = HBaseFollowing.filter(prefix=(1, None), limit=2)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].to_user_id, 2)
        self.assertEqual(results[1].to_user_id, 3)

        results = HBaseFollowing.filter(prefix=(1, None), limit=4)
        self.assertEqual(len(results), 3)
        self.assertEqual(results[0].to_user_id, 2)
        self.assertEqual(results[1].to_user_id, 3)
        self.assertEqual(results[2].to_user_id, 4)

        results = HBaseFollowing.filter(start=(1, results[1].created_at),
                                        limit=2)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].to_user_id, 3)
        self.assertEqual(results[1].to_user_id, 4)

        # test reverse
        results = HBaseFollowing.filter(prefix=(1, None),
                                        limit=2,
                                        reverse=True)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].to_user_id, 4)
        self.assertEqual(results[1].to_user_id, 3)

        results = HBaseFollowing.filter(start=(1, results[1].created_at),
                                        limit=2,
                                        reverse=True)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0].to_user_id, 3)
        self.assertEqual(results[1].to_user_id, 2)
Exemplo n.º 2
0
    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,
        )
Exemplo n.º 3
0
    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,
        )
Exemplo n.º 4
0
    def test_filter(self):
        now = self.ts_now()
        # dummy friendships
        for i in range(1, 5):
            HBaseFollowing.create(from_user_id=i,
                                  created_at=now,
                                  to_user_id=999)
        for i in range(1, 4):
            HBaseFollowing.create(from_user_id=999,
                                  created_at=now + i,
                                  to_user_id=i)

        # single instance filter
        following = HBaseFollowing.filter(start=(1, now), stop=(1, now))
        self.assertEqual(len(following), 1)
        self.assertEqual(following[0].from_user_id, 1)
        self.assertEqual(following[0].to_user_id, 999)
        self.assertEqual(following[0].created_at, now)

        # start from non-exist id
        following = HBaseFollowing.filter(start=(0, now))
        self.assertEqual(len(following), 7)

        # range query
        followings = HBaseFollowing.filter(start=(1, now))
        self.assertEqual(len(followings), 7)
        followings = HBaseFollowing.filter(start=(1, now), limit=2)
        self.assertEqual(len(followings), 2)
        self.assertEqual(followings[0].from_user_id, 1)
        self.assertEqual(followings[0].to_user_id, 999)
        self.assertEqual(followings[1].from_user_id, 2)
        self.assertEqual(followings[1].to_user_id, 999)

        # reverse range query
        followings = HBaseFollowing.filter(start=(4, now), reverse=True)
        self.assertEqual(len(followings), 4)
        followings = HBaseFollowing.filter(start=(4, now),
                                           limit=3,
                                           reverse=True)
        self.assertEqual(len(followings), 3)
        self.assertEqual(followings[-1].from_user_id, 2)
        self.assertEqual(followings[-1].to_user_id, 999)
        self.assertEqual(followings[0].from_user_id, 4)
        self.assertEqual(followings[0].to_user_id, 999)

        # part to row_keys range query
        followings = HBaseFollowing.filter(start=(5, None),
                                           limit=3,
                                           reverse=True)
        self.assertEqual(len(followings), 3)
        self.assertEqual(followings[-1].from_user_id, 2)
        self.assertEqual(followings[-1].to_user_id, 999)
        self.assertEqual(followings[0].from_user_id, 4)
        self.assertEqual(followings[0].to_user_id, 999)
        followings = HBaseFollowing.filter(start=(4, None),
                                           limit=3,
                                           reverse=True)
        self.assertEqual(len(followings), 3)
        # None stays behind all values, In Table: (1, 999), (2, 299), (3, 999), ((4, None)), (4, 999)
        self.assertEqual(followings[-1].from_user_id, 1)
        self.assertEqual(followings[-1].to_user_id, 999)
        self.assertEqual(followings[0].from_user_id, 3)
        self.assertEqual(followings[0].to_user_id, 999)

        # prefix range query
        followings = HBaseFollowing.filter(prefix=(999, None),
                                           limit=4,
                                           reverse=True)
        self.assertEqual(len(followings), 3)
        self.assertEqual(followings[0].from_user_id, 999)
        self.assertEqual(followings[0].to_user_id, 3)
        self.assertEqual(followings[1].from_user_id, 999)
        self.assertEqual(followings[1].to_user_id, 2)
        self.assertEqual(followings[-1].from_user_id, 999)
        self.assertEqual(followings[-1].to_user_id, 1)

        # extra redundant field
        followings = HBaseFollowing.filter(start=(1, now, None))
        self.assertEqual(len(followings), 7)