def test_zadd(self): zset_obj = self.zset_obj zset_obj.zadd(elements=ZsetNode('d', 1)) self.assertEqual(4, zset_obj.zcard()) zset_obj.zadd(ZsetNode('d', 1), 'nx') self.assertEqual(4, zset_obj.zcard()) zset_obj.zadd(ZsetNode('e', 2), 'nx') self.assertEqual(5, zset_obj.zcard())
def zrange(): zset_obj = ZsetObj() for i in RANDOMLONGS_E4: zset_obj.zadd(ZsetNode(str(i), i)) with timeit('search range inside SortedSet'): for i in range(0, 10000): zset_obj.zrange(1, 10000, 1)
def zrange_generic(self, reverse, start, end, withscores): """ :param reverse: :param start: :param end: :param withscores: :return: """ assert (-maxsize - 1) <= start <= maxsize llen = self.zset.zset_length() if start < 0: start = llen + start if end < 0: end = llen + end if start < 0: start = 0 if start > end or start >= llen: return None if end >= llen: end = llen - 1 rangelen = (end - start) + 1 zsl = self.zset.zsl rets = zsl.zsl_range_generic(reverse=reverse, start=start, rangelen=rangelen) result = [] for ret in rets: result.append( ZsetNode(ele=ret[0], score=ret[1] if withscores else None)) return result
def zscore(): zset_obj = ZsetObj() for i in RANDOMLONGS_E4: zset_obj.zadd(ZsetNode(str(i), i)) with timeit('search inside SortedSet'): for i in RANDOMLONGS_E3: zset_obj.zscore(str(i))
def zadd(): zset_obj = ZsetObj() with timeit('add operation of SortedSet'): for i in RANDOMLONGS_E4: zset_obj.zadd(ZsetNode(str(i), i)) with timeit('range operation of SortedSet'): result = zset_obj.zrange(0, 100000, 1)
def zlower(self, score): """ :param score: :return: """ ret = self.zset.zset_get_lower_element_by_score(score=score) if not ret: return None return ZsetNode(ele=ret[0], score=ret[1])
def zrange_generic(self, reverse, start, end, withscores): """ :param reverse: :param start: :param end: :param withscores: :return: """ assert (-maxsize - 1) <= start <= maxsize llen = self.zset.zset_length() if start < 0: start = llen + start if end < 0: end = llen + end if start < 0: start = 0 if start > end or start >= llen: return None if end >= llen: end = llen - 1 rangelen = (end - start) + 1 zsl = self.zset.zsl if reverse: ln = zsl.tail if start > 0: ln = zsl.zsl_get_element_by_rank(rank=llen - start) else: ln = zsl.header.level[0].forward if start > 0: ln = zsl.zsl_get_element_by_rank(rank=start + 1) result = [] while rangelen > 0: assert ln is not None node = ZsetNode(ele=ln.ele, score=ln.score if withscores else None) result.append(node) ln = ln.backward if reverse else ln.level[0].forward rangelen -= 1 return result
def zrange_generic_by_score(self, reverse, min_, minex, max_, maxex, limit, withscores): """ :param reverse: :param min_: :param minex: :param max_: :param maxex: :param limit: :param withscores: :return: """ zsl = self.zset.zsl rets = zsl.zsl_range_generic_by_score(reverse=reverse, min_=min_, minex=minex, max_=max_, maxex=maxex, limit=limit) result = [] for ret in rets: result.append( ZsetNode(ele=ret[0], score=ret[1] if withscores else None)) return result
def setUp(self): self.zset_obj = ZsetObj() elements = [ZsetNode('a', 1), ZsetNode('b', 3), ZsetNode('c', 2)] self.zset_obj.zadd(elements=elements)
def test_zincrby(self): zset_obj = self.zset_obj zset_obj.zincrby(ZsetNode('a', 1), 'incr') score = zset_obj.zscore('a') self.assertEqual(2, score)
def zadd(): zset_obj = ZsetObj() with timeit('add operation of SortedSet'): for i in RANDOMLONGS_E4: zset_obj.zadd(ZsetNode(str(i), i))