Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add library level rename/renamenx commands

  • Loading branch information...
commit 02bdbb22ce41149468c8cafe85ec228831ccbe9d 1 parent e5d5318
@salimane authored
Showing with 44 additions and 10 deletions.
  1. +44 −0 rediscluster/cluster_client.py
  2. +0 −10 tests/cluster_commands.py
View
44 rediscluster/cluster_client.py
@@ -380,3 +380,47 @@ def _rc_mget(self, keys, *args):
for key in args:
result.append(self.get(key))
return result
+
+ def _rc_rename(self, src, dst):
+ """
+ Rename key ``src`` to ``dst``
+ """
+ if src == dst:
+ return self.rename(src + "{" + src + "}", src)
+ if not self.exists(src):
+ return self.rename(src + "{" + src + "}", src)
+
+ self.delete(dst)
+ ktype = self.type(src)
+ kttl = self.ttl(src)
+
+ if ktype == b('none'):
+ return False
+
+ if ktype == b('string'):
+ self.set(dst, self.get(src))
+ elif ktype == b('hash'):
+ self.hmset(dst, self.hgetall(src))
+ elif ktype == b('list'):
+ for k in self.lrange(src, 0, -1):
+ self.rpush(dst, k)
+ elif ktype == b('set'):
+ for k in self.smembers(src):
+ self.sadd(dst, k)
+ elif ktype == b('zset'):
+ for k, v in self.zrange(src, 0, -1, withscores=True):
+ self.zadd(dst, v, k)
+
+ # Handle keys with an expire time set
+ kttl = -1 if kttl is None or kttl < 0 else int(kttl)
+ if kttl != -1:
+ dst.expire(dst, kttl)
+
+ return self.delete(src)
+
+ def _rc_renamenx(self, src, dst):
+ "Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"
+ if self.exists(dst):
+ return False
+
+ return self.rename(src, dst)
View
10 tests/cluster_commands.py
@@ -505,22 +505,12 @@ def test_randomkey(self):
self.assert_(self.client.randomkey() in (b('a'), b('b'), b('c')))
def test_rename(self):
- # CLUSTER
- try:
- raise unittest.SkipTest()
- except AttributeError:
- return
self.client['a'] = '1'
self.assert_(self.client.rename('a', 'b'))
self.assertEquals(self.client.get('a'), None)
self.assertEquals(self.client['b'], b('1'))
def test_renamenx(self):
- # CLUSTER
- try:
- raise unittest.SkipTest()
- except AttributeError:
- return
self.client['a'] = '1'
self.client['b'] = '2'
self.assert_(not self.client.renamenx('a', 'b'))
Please sign in to comment.
Something went wrong with that request. Please try again.