def remove_node(self, node): """Removes `node` from the hash ring and its replicas. """ self.nodes.remove(node) for x in xrange(self.replicas): crckey = zlib.crc32(b("%s:%d" % (node, x))) self.ring.remove(crckey) self.sorted_keys.remove(crckey)
def test_pipeline_script(self): pipe = self.client.pipeline() for i in range(100): pipe.eval(""" redis.call('set', KEYS[1], ARGV[1]) """, 1, 'testx%d' % i, i) pipe.execute() for i in range(100): eq_(self.client.get('testx%d' % i), b('%d' % i))
def add_node(self, node): """Adds a `node` to the hash ring (including a number of replicas). """ self.nodes.append(node) for x in xrange(self.replicas): crckey = zlib.crc32(b("%s:%d" % (node, x))) self.ring[crckey] = node self.sorted_keys.append(crckey) self.sorted_keys.sort()
def get_node_pos(self, key): """Given a string key a corresponding node in the hash ring is returned along with it's position in the ring. If the hash ring is empty, (`None`, `None`) is returned. """ if len(self.ring) == 0: return [None, None] crc = zlib.crc32(b(key)) idx = bisect.bisect(self.sorted_keys, crc) idx = min(idx, (self.replicas * len(self.nodes)) - 1) # prevents out of range index return [self.ring[self.sorted_keys[idx]], idx]
def test_evalsha(self): sha = self.client.script_load(""" return redis.call('set', KEYS[1], ARGV[1]) """) eq_(self.client.evalsha(sha, 1, 'test8', b('8')), b('OK')) eq_(self.client.get('test8'), b('8'))
def test_eval(self): self.client.eval(""" return redis.call('set', KEYS[1], ARGV[1]) """, 1, 'test7', '7') eq_(self.client.get('test7'), b('7'))
def test_mset(self): self.client.mset({'test4': 4, 'test5': 5, 'test6': 6}) eq_(self.client.get('test4'), b('4')) eq_(self.client.mget('test4', 'test5', 'test6'), [b('4'), b('5'), b('6')])
def test_mget(self): self.client.set('test1', 1) self.client.set('test2', 2) self.client.set('test3', 3) eq_(self.client.mget('test1', 'test2', 'test3'), [b('1'), b('2'), b('3')])