Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zadd syntax and add support for dealing with partially corrupted datasets #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions redisdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,22 @@ def dump(fp, host='localhost', port=6379, password=None, db=0, pretty=False,
fp.write('{')
first = True
for key, type, ttl, value in _reader(r, pretty, encoding, keys):
key = encoder.encode(key)
type = encoder.encode(type)
value = encoder.encode(value)
if ttl:
expireat = encoder.encode(_time.time() + ttl)
ttl = encoder.encode(ttl)
item = '%s:{"type":%s,"value":%s,"ttl":%s,"expireat":%s}' % (
key, type, value, ttl, expireat)
else:
item = '%s:{"type":%s,"value":%s}' % (key, type, value)
if first:
first = False
else:
fp.write(',')
fp.write(item)
if type is not None and value is not None:
key = encoder.encode(key)
type = encoder.encode(type)
value = encoder.encode(value)
if ttl:
expireat = encoder.encode(_time.time() + ttl)
ttl = encoder.encode(ttl)
item = '%s:{"type":%s,"value":%s,"ttl":%s,"expireat":%s}' % (
key, type, value, ttl, expireat)
else:
item = '%s:{"type":%s,"value":%s}' % (key, type, value)
if first:
first = False
else:
fp.write(',')
fp.write(item)
fp.write('}')

class StringReader(object):
Expand Down Expand Up @@ -300,6 +301,10 @@ def _reader(r, pretty, encoding, keys='*'):
except KeyTypeChangedError:
# retry reading type again
pass
except:
# Pass None values since retries are not going to help
handled = True
yield key, None, None, None
if not handled:
# ran out of retries
raise ConcurrentModificationError('Key %s is being concurrently modified' % key)
Expand Down Expand Up @@ -479,9 +484,12 @@ def _writer(r, p, key, type, value, ttl, expireat, use_expireat):
p.sadd(key, element)
elif type == 'zset':
for element, score in value:
p.zadd(key, element, score)
p.zadd(key, {element: float(score)})
elif type == 'hash':
p.hmset(key, value)
elif type is None:
# Ignore None types
pass
else:
raise UnknownTypeError("Unknown key type: %s" % type)

Expand Down