-
Notifications
You must be signed in to change notification settings - Fork 179
Add support for DUMP and RESTORE commands. #285
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
Conversation
a803ed7 to
cf03464
Compare
|
Thanks. I'm going to have to think about this for a bit. In my use of fakeredis at work we have actually implemented DUMP with the redis format, but only for a few types (string, hash and zset). You can see it here. The rdbtools package contains all the code for decoding the redis dump format, as well as documentation of it. So it would be nice to implement a DUMP that uses the redis format, but it would still require a lot more work, and maybe it's worth getting something going in the meantime. I also try to avoid pickle where possible just because of the security implications (loading a pickle can execute arbitrary code). That's probably not such an issue for a unit testing library, but maybe something like msgpack would work just as well? |
That's my general feeling as well. It makes sense to use RDB if that's the actual data storage of your server but otherwise it feels like there's too much extra work involved for the sake of achieving this property It also brings the question of which version of RDB should be used, what if it changes between Redis versions? Do do you now support both format? Require a version flag to be specified on client creation?
I also think this isn't an issue for a library that advertise itself as having a single purpose: to write unittests. I guess other formats could work as well (e.g. base64 encoded |
Just because one builds a tool for a purpose doesn't mean people won't find other uses for it (e.g. #221) 😄 But I think it probably falls into the category of "good enough for now". I'm a little swamped at the moment, but I'll try to look at this in the next week - it does look like a nice small change. |
|
I've decided I'm happy with the pickle approach for now. The code you've written so far looks good, but there are a few gaps in the implementation, and a few changes I'd like:
|
cf03464 to
81ea715
Compare
|
@bmerry thanks for the quick feedback. All of above points were addressed except for support for I have a commit ready for |
bmerry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the work. Just a few more niggles I missed the first time.
fakeredis/_server.py
Outdated
| else: | ||
| raise SimpleError(SYNTAX_ERROR_MSG) | ||
| if key and not replace: | ||
| raise redis.ResponseError(RESTORE_KEY_EXISTS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these redis.ResponseErrors should all be SimpleErrors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh looks like SimpleError are turned into ResponseError automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. fakeredis also support aioredis, where SimpleError gets turned into an aioredis exception.
fakeredis/_server.py
Outdated
| now_us %= 1000000 | ||
| return [str(now_s).encode(), str(now_us).encode()] | ||
|
|
||
| # Generic commands |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line, and move the new code to the "Key commands" section (because that's the category they're listed under in the redis command reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any thoughts about adjusting the README to match the Redis command reference? That's where I got the order from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what's happened is that the data that backs the command reference has two names for each section e.g. the URL for the "Keys" section is https://redis.io/commands#generic. The script that generates that section of the README is using the short names (hence "generic") but the comments in _server.py use the long names.
fakeredis/_server.py
Outdated
|
|
||
| # Generic commands | ||
|
|
||
| @command((Key(),)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need the following to make this do the right thing if the key doesn't exist (which also needs a test):
| @command((Key(),)) | |
| @command((Key(missing_return=None),)) |
One caveat is that they don't use the RDB format but pickle seemed like a good candidate to mimic what is expected to be an opaque and non-standard format.
81ea715 to
d6e89d2
Compare
|
@bmerry all comments should be addressed. |
|
Thanks for the work - all looks good now! |
|
Thanks for the merge @bmerry. Any idea when a new release containing these changes will be released? We're currently using a monkeypatch of |
There's one PR in progress which I hope to get merged this week before a release. I just have to remember to make time in my day job. If you haven't seen anything by next Thursday then feel free to remind me again. |
|
Thanks for the release @bmerry, appreciate 🙇 |



One caveat is that they don't use the RDB format but pickle seemed like a good candidate to mimic what is expected to be an opaque and non-standard format.
https://redis.io/commands/dump
DUMP keyOpted for
sha1here.This could unfortunately not be achieve as it would be impractical.
https://redis.io/commands/restore
RESTORE key ttl serialized-value