Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
e8c993f0
Commit
e8c993f0
authored
Oct 25, 2011
by
antirez
Browse files
Fixes for the scripting refactoring and new commands. Tests for the new features.
parent
a9b07ac4
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/scripting.c
View file @
e8c993f0
...
@@ -500,7 +500,6 @@ void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
...
@@ -500,7 +500,6 @@ void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
int
luaCreateFunction
(
redisClient
*
c
,
lua_State
*
lua
,
char
*
funcname
,
robj
*
body
)
{
int
luaCreateFunction
(
redisClient
*
c
,
lua_State
*
lua
,
char
*
funcname
,
robj
*
body
)
{
sds
funcdef
=
sdsempty
();
sds
funcdef
=
sdsempty
();
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
funcdef
=
sdscat
(
funcdef
,
"function "
);
funcdef
=
sdscat
(
funcdef
,
"function "
);
funcdef
=
sdscatlen
(
funcdef
,
funcname
,
42
);
funcdef
=
sdscatlen
(
funcdef
,
funcname
,
42
);
funcdef
=
sdscatlen
(
funcdef
,
" ()
\n
"
,
4
);
funcdef
=
sdscatlen
(
funcdef
,
" ()
\n
"
,
4
);
...
@@ -581,12 +580,12 @@ void evalGenericCommand(redisClient *c, int evalsha) {
...
@@ -581,12 +580,12 @@ void evalGenericCommand(redisClient *c, int evalsha) {
/* Try to lookup the Lua function */
/* Try to lookup the Lua function */
lua_getglobal
(
lua
,
funcname
);
lua_getglobal
(
lua
,
funcname
);
if
(
lua_isnil
(
lua
,
1
))
{
if
(
lua_isnil
(
lua
,
1
))
{
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
/* Function not defined... let's define it if we have the
/* Function not defined... let's define it if we have the
* body of the funciton. If this is an EVALSHA call we can just
* body of the funciton. If this is an EVALSHA call we can just
* return an error. */
* return an error. */
if
(
evalsha
)
{
if
(
evalsha
)
{
addReply
(
c
,
shared
.
noscripterr
);
addReply
(
c
,
shared
.
noscripterr
);
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
return
;
return
;
}
}
if
(
luaCreateFunction
(
c
,
lua
,
funcname
,
c
->
argv
[
1
])
==
REDIS_ERR
)
return
;
if
(
luaCreateFunction
(
c
,
lua
,
funcname
,
c
->
argv
[
1
])
==
REDIS_ERR
)
return
;
...
@@ -724,15 +723,21 @@ void scriptCommand(redisClient *c) {
...
@@ -724,15 +723,21 @@ void scriptCommand(redisClient *c) {
addReply
(
c
,
shared
.
czero
);
addReply
(
c
,
shared
.
czero
);
}
}
}
else
if
(
c
->
argc
==
3
&&
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"load"
))
{
}
else
if
(
c
->
argc
==
3
&&
!
strcasecmp
(
c
->
argv
[
1
]
->
ptr
,
"load"
))
{
/* We obtain the script SHA1, then check if this function is already
* defined into the Lua state */
char
funcname
[
43
];
char
funcname
[
43
];
sds
sha
;
funcname
[
0
]
=
'f'
;
funcname
[
0
]
=
'f'
;
funcname
[
1
]
=
'_'
;
funcname
[
1
]
=
'_'
;
hashScript
(
funcname
+
2
,
c
->
argv
[
2
]
->
ptr
,
sdslen
(
c
->
argv
[
2
]
->
ptr
));
hashScript
(
funcname
+
2
,
c
->
argv
[
2
]
->
ptr
,
sdslen
(
c
->
argv
[
2
]
->
ptr
));
if
(
luaCreateFunction
(
c
,
server
.
lua
,
funcname
,
c
->
argv
[
2
])
==
REDIS_ERR
)
sha
=
sdsnewlen
(
funcname
+
2
,
40
);
return
;
if
(
dictFind
(
server
.
lua_scripts
,
sha
)
==
NULL
)
{
if
(
luaCreateFunction
(
c
,
server
.
lua
,
funcname
,
c
->
argv
[
2
])
==
REDIS_ERR
)
{
sdsfree
(
sha
);
return
;
}
}
sdsfree
(
sha
);
addReply
(
c
,
shared
.
ok
);
addReply
(
c
,
shared
.
ok
);
}
else
{
}
else
{
addReplyError
(
c
,
"Unknown SCRIPT subcommand or wrong # of args."
);
addReplyError
(
c
,
"Unknown SCRIPT subcommand or wrong # of args."
);
...
...
tests/unit/scripting.tcl
View file @
e8c993f0
...
@@ -47,8 +47,13 @@ start_server {tags {"scripting"}} {
...
@@ -47,8 +47,13 @@ start_server {tags {"scripting"}} {
r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
}
{
myval
}
}
{
myval
}
test
{
EVALSHA - Do we get an error on invalid SHA1?
}
{
catch
{
r evalsha NotValidShaSUM 0
}
e
set _ $e
}
{
NOSCRIPT*
}
test
{
EVALSHA - Do we get an error on non defined SHA1?
}
{
test
{
EVALSHA - Do we get an error on non defined SHA1?
}
{
catch
{
r evalsha ff
ffffffffffffffffffffffffffffffffffffff
0
}
e
catch
{
r evalsha ff
d632c7d33e571e9f24556ebed26c3479a87130
0
}
e
set _ $e
set _ $e
}
{
NOSCRIPT*
}
}
{
NOSCRIPT*
}
...
@@ -162,6 +167,27 @@ start_server {tags {"scripting"}} {
...
@@ -162,6 +167,27 @@ start_server {tags {"scripting"}} {
}
e
}
e
set e
set e
}
{
*against a key*
}
}
{
*against a key*
}
test
{
SCRIPTING FLUSH - is able to clear the scripts cache?
}
{
r set mykey myval
set v
[
r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
]
assert_equal $v myval
set e
""
r script flush
catch
{
r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
}
e
set e
}
{
NOSCRIPT*
}
test
{
SCRIPT EXISTS - can detect already defined scripts?
}
{
r eval
"return 1+1"
0
r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
}
{
1 0
}
test
{
SCRIPT LOAD - is able to register scripts in the scripting cache
}
{
list
\
[
r script load
"return 'loaded'"
]
\
[
r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0
]
}
{
OK loaded
}
}
}
start_server
{
tags
{
"scripting repl"
}}
{
start_server
{
tags
{
"scripting repl"
}}
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment