-
Notifications
You must be signed in to change notification settings - Fork 4
Support StrictRedis.eval for Lua scripts #9
Changes from 11 commits
b8d997d
da8a820
0845484
7ea39cd
74389b7
58d0763
77685dc
0dd661f
507a125
06e02b5
0fd83e6
f2086fd
6e0c1b3
73cc1d7
87c2686
e0e7718
121de8c
9de8754
9e6b32f
15232fa
9bf4173
bfa38c9
63949ba
975f88c
71a2c1e
1def833
dd23fb1
c91f0b1
ee53358
a30fdef
b426f0f
6153787
7fb69f4
49cf337
392206c
d5e4ce2
fe9065d
296c2ff
95762e2
fde2b75
773f0f4
b1a0402
856a031
acc44b1
23a43af
525a29b
267e338
6377669
244056b
8f869b4
b2b6786
fa252bc
f917498
82e7e65
fdf9ecb
a6a7cd6
9f4d910
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,7 +203,6 @@ scripting | |
* script kill | ||
* script load | ||
* evalsha | ||
* eval | ||
* script exists | ||
|
||
|
||
|
@@ -266,6 +265,11 @@ they have all been tagged as 'slow' so you can skip them by running:: | |
Revision history | ||
================ | ||
|
||
0.9.5 | ||
----- | ||
Add support for StrictRedis.eval for Lua scripts | ||
- `#9 <https://github.com/ska-sa/fakenewsredis/pull/9>`_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put the description into the bullet point (to make the format of the other changelog entries). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
0.9.4 | ||
----- | ||
This is a minor bugfix and optimization release: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
import types | ||
import re | ||
import functools | ||
from itertools import count | ||
|
||
from lupa import LuaRuntime, lua_type | ||
|
||
import redis | ||
from redis.exceptions import ResponseError | ||
|
@@ -701,6 +704,55 @@ def sort(self, name, start=None, num=None, by=None, get=None, desc=False, | |
except KeyError: | ||
return [] | ||
|
||
def eval(self, script, numkeys, *keys_and_args): | ||
""" | ||
Execute the Lua ``script``, specifying the ``numkeys`` the script | ||
will touch and the key names and argument values in ``keys_and_args``. | ||
Returns the result of the script. | ||
In practice, use the object returned by ``register_script``. This | ||
function exists purely for Redis API completion. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can delete the docstring. It's assumed that people will refer to redis-py for docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
lua_runtime = LuaRuntime(unpack_returned_tuples=True) | ||
|
||
raw_lua = """ | ||
function(KEYS, ARGV, callback) | ||
redis = {{}} | ||
redis.call = callback | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latter two are done. |
||
{body} | ||
end | ||
""".format(body=script) | ||
keys = (None,) + keys_and_args[:numkeys] | ||
args = (None,) + keys_and_args[numkeys:] | ||
|
||
lua_func = lua_runtime.eval(raw_lua) | ||
result = lua_func( | ||
keys, | ||
args, | ||
self._lua_callback | ||
) | ||
if lua_type(result) == 'table': | ||
# Convert Lua tables into lists, starting from index 1, mimicking the behavior of StrictRedis. | ||
result_list = [] | ||
for index in count(1): | ||
if index not in result: | ||
break | ||
item = result[index] | ||
result_list.append( | ||
item.encode() if isinstance(item, str) and not isinstance(item, bytes) else item | ||
) | ||
return result_list | ||
return result | ||
|
||
def _lua_callback(self, op, *args): | ||
special_cases = { | ||
'del': self.delete, | ||
'decrby': self.decr, | ||
'incrby': self.incr | ||
} | ||
op = op.lower() | ||
func = special_cases[op] if op in special_cases else getattr(self, op) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are presumably attributes of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably yes. This is a bit of a hack, but I figure the tradeoff is that it doesn't have a huge mapping from Redis commands to Python functions that needs to be maintained as more Redis functions get added. I'll make it a bit more selective. |
||
return func(*args) | ||
|
||
def _retrive_data_from_sort(self, data, get): | ||
if get is not None: | ||
if isinstance(get, string_types): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
flake8<3.0.0 | ||
nose==1.3.4 | ||
redis==2.10.6 | ||
lupa==1.6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setup( | ||
name='fakenewsredis', | ||
version='0.9.4', | ||
version='0.9.5', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't update the version yet; I'll do it when I make a new release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
description="Fake implementation of redis API for testing purposes.", | ||
long_description=open(os.path.join(os.path.dirname(__file__), | ||
'README.rst')).read(), | ||
|
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.
Can you change this to "Development version". There is some possibility of re-merging with fakeredis, so I don't want to make assumptions about version numbers yet.
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.
Done.