add data migration support for data type Set

This commit is contained in:
holys 2014-10-08 11:33:43 +08:00
parent 87f19c8591
commit f560c0533f
3 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,5 @@
## Notice ## Notice
1. The tool doesn't support `set` data type.
2. The tool doesn't support `bitmap` data type. 2. The tool doesn't support `bitmap` data type.
2. Our `zset` use integer instead of double, so the zset float score in Redis 2. Our `zset` use integer instead of double, so the zset float score in Redis
will be **converted to integer**. will be **converted to integer**.

View File

@ -35,7 +35,7 @@ def set_ttl(redis_client, ledis_client, key, k_type):
"string": ledis_client.expire, "string": ledis_client.expire,
"list": ledis_client.lexpire, "list": ledis_client.lexpire,
"hash": ledis_client.hexpire, "hash": ledis_client.hexpire,
"set": ledis_client.zexpire, "set": ledis_client.sexpire,
"zset": ledis_client.zexpire "zset": ledis_client.zexpire
} }
timeout = redis_client.ttl(key) timeout = redis_client.ttl(key)
@ -46,6 +46,7 @@ def set_ttl(redis_client, ledis_client, key, k_type):
def copy_key(redis_client, ledis_client, key, convert=False): def copy_key(redis_client, ledis_client, key, convert=False):
global entries global entries
k_type = redis_client.type(key) k_type = redis_client.type(key)
if k_type == "string": if k_type == "string":
value = redis_client.get(key) value = redis_client.get(key)
ledis_client.set(key, value) ledis_client.set(key, value)
@ -66,6 +67,7 @@ def copy_key(redis_client, ledis_client, key, convert=False):
entries += 1 entries += 1
elif k_type == "zset": elif k_type == "zset":
# dangerous to do this?
out = redis_client.zrange(key, 0, -1, withscores=True) out = redis_client.zrange(key, 0, -1, withscores=True)
pieces = od() pieces = od()
for i in od(out).iteritems(): for i in od(out).iteritems():
@ -74,6 +76,14 @@ def copy_key(redis_client, ledis_client, key, convert=False):
set_ttl(redis_client, ledis_client, key, k_type) set_ttl(redis_client, ledis_client, key, k_type)
entries += 1 entries += 1
elif k_type == "set":
mbs = list(redis_client.smembers(key))
if mbs is not None:
ledis_client.sadd(key, *mbs)
set_ttl(redis_client, ledis_client, key, k_type)
entries += 1
else: else:
print "KEY %s of TYPE %s is not supported by LedisDB." % (key, k_type) print "KEY %s of TYPE %s is not supported by LedisDB." % (key, k_type)

View File

@ -47,6 +47,9 @@ def random_zset(client, words, length=1000):
client.zadd("zsetName", **d) client.zadd("zsetName", **d)
def random_set(client, words, length=1000):
client.sadd("setName", *words)
def test(): def test():
words = get_words() words = get_words()
print "Flush all redis data before insert new." print "Flush all redis data before insert new."
@ -64,10 +67,9 @@ def test():
random_zset(rds, words) random_zset(rds, words)
print "random_zset done" print "random_zset done"
random_set(rds, words)
print "random_set done"
lds.lclear("listName")
lds.hclear("hashName")
lds.zclear("zsetName")
copy(rds, lds, convert=True) copy(rds, lds, convert=True)
# for all keys # for all keys
@ -101,6 +103,7 @@ def ledis_ttl(ledis_client, key, k_type):
"list": lds.lttl, "list": lds.lttl,
"hash": lds.httl, "hash": lds.httl,
"zset": lds.zttl, "zset": lds.zttl,
"set": lds.sttl,
} }
return ttls[k_type](key) return ttls[k_type](key)
@ -115,6 +118,9 @@ def test_ttl():
assert ledis_ttl(lds, key, k_type) > 0 assert ledis_ttl(lds, key, k_type) > 0
if __name__ == "__main__": if __name__ == "__main__":
rds.flushdb()
lds.flushdb()
test() test()
test_ttl() test_ttl()
print "Test passed." print "Test passed."