Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
a9b07ac4
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a9b07ac4
编写于
10月 25, 2011
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SCRIPT LOAD implemented, scripting eval command refactoring.
上级
070e3945
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
36 deletion
+61
-36
src/scripting.c
src/scripting.c
+61
-36
未找到文件。
src/scripting.c
浏览文件 @
a9b07ac4
...
...
@@ -488,6 +488,52 @@ void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
lua_setglobal
(
lua
,
var
);
}
/* Define a lua function with the specified function name and body.
* The function name musts be a 2 characters long string, since all the
* functions we defined in the Lua context are in the form:
*
* f_<hex sha1 sum>
*
* On success REDIS_OK is returned, and nothing is left on the Lua stack.
* On error REDIS_ERR is returned and an appropriate error is set in the
* client context. */
int
luaCreateFunction
(
redisClient
*
c
,
lua_State
*
lua
,
char
*
funcname
,
robj
*
body
)
{
sds
funcdef
=
sdsempty
();
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
funcdef
=
sdscat
(
funcdef
,
"function "
);
funcdef
=
sdscatlen
(
funcdef
,
funcname
,
42
);
funcdef
=
sdscatlen
(
funcdef
,
" ()
\n
"
,
4
);
funcdef
=
sdscatlen
(
funcdef
,
body
->
ptr
,
sdslen
(
body
->
ptr
));
funcdef
=
sdscatlen
(
funcdef
,
"
\n
end
\n
"
,
5
);
if
(
luaL_loadbuffer
(
lua
,
funcdef
,
sdslen
(
funcdef
),
"func definition"
))
{
addReplyErrorFormat
(
c
,
"Error compiling script (new function): %s
\n
"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
1
);
sdsfree
(
funcdef
);
return
REDIS_ERR
;
}
sdsfree
(
funcdef
);
if
(
lua_pcall
(
lua
,
0
,
0
,
0
))
{
addReplyErrorFormat
(
c
,
"Error running script (new function): %s
\n
"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
1
);
return
REDIS_ERR
;
}
/* We also save a SHA1 -> Original script map in a dictionary
* so that we can replicate / write in the AOF all the
* EVALSHA commands as EVAL using the original script. */
{
int
retval
=
dictAdd
(
server
.
lua_scripts
,
sdsnewlen
(
funcname
+
2
,
40
),
body
);
redisAssertWithInfo
(
c
,
NULL
,
retval
==
DICT_OK
);
incrRefCount
(
body
);
}
return
REDIS_OK
;
}
void
evalGenericCommand
(
redisClient
*
c
,
int
evalsha
)
{
lua_State
*
lua
=
server
.
lua
;
char
funcname
[
43
];
...
...
@@ -532,10 +578,9 @@ void evalGenericCommand(redisClient *c, int evalsha) {
funcname
[
42
]
=
'\0'
;
}
/* Try to lookup the Lua function */
lua_getglobal
(
lua
,
funcname
);
if
(
lua_isnil
(
lua
,
1
))
{
sds
funcdef
;
/* Function not defined... let's define it if we have the
* body of the funciton. If this is an EVALSHA call we can just
* return an error. */
...
...
@@ -544,41 +589,10 @@ void evalGenericCommand(redisClient *c, int evalsha) {
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
return
;
}
funcdef
=
sdsempty
();
lua_pop
(
lua
,
1
);
/* remove the nil from the stack */
funcdef
=
sdscat
(
funcdef
,
"function "
);
funcdef
=
sdscatlen
(
funcdef
,
funcname
,
42
);
funcdef
=
sdscatlen
(
funcdef
,
" ()
\n
"
,
4
);
funcdef
=
sdscatlen
(
funcdef
,
c
->
argv
[
1
]
->
ptr
,
sdslen
(
c
->
argv
[
1
]
->
ptr
));
funcdef
=
sdscatlen
(
funcdef
,
"
\n
end
\n
"
,
5
);
/* printf("Defining:\n%s\n",funcdef); */
if
(
luaL_loadbuffer
(
lua
,
funcdef
,
sdslen
(
funcdef
),
"func definition"
))
{
addReplyErrorFormat
(
c
,
"Error compiling script (new function): %s
\n
"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
1
);
sdsfree
(
funcdef
);
return
;
}
sdsfree
(
funcdef
);
if
(
lua_pcall
(
lua
,
0
,
0
,
0
))
{
addReplyErrorFormat
(
c
,
"Error running script (new function): %s
\n
"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
1
);
return
;
}
if
(
luaCreateFunction
(
c
,
lua
,
funcname
,
c
->
argv
[
1
])
==
REDIS_ERR
)
return
;
/* Now the following is guaranteed to return non nil */
lua_getglobal
(
lua
,
funcname
);
/* We also save a SHA1 -> Original script map in a dictionary
* so that we can replicate / write in the AOF all the
* EVALSHA commands as EVAL using the original script. */
{
int
retval
=
dictAdd
(
server
.
lua_scripts
,
sdsnewlen
(
funcname
+
2
,
40
),
c
->
argv
[
1
]);
redisAssertWithInfo
(
c
,
NULL
,
retval
==
DICT_OK
);
incrRefCount
(
c
->
argv
[
1
]);
}
redisAssert
(
!
lua_isnil
(
lua
,
1
));
}
/* Populate the argv and keys table accordingly to the arguments that
...
...
@@ -709,6 +723,17 @@ void scriptCommand(redisClient *c) {
else
addReply
(
c
,
shared
.
czero
);
}
}
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
];
funcname
[
0
]
=
'f'
;
funcname
[
1
]
=
'_'
;
hashScript
(
funcname
+
2
,
c
->
argv
[
2
]
->
ptr
,
sdslen
(
c
->
argv
[
2
]
->
ptr
));
if
(
luaCreateFunction
(
c
,
server
.
lua
,
funcname
,
c
->
argv
[
2
])
==
REDIS_ERR
)
return
;
addReply
(
c
,
shared
.
ok
);
}
else
{
addReplyError
(
c
,
"Unknown SCRIPT subcommand or wrong # of args."
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录