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
4a505700
Commit
4a505700
authored
Apr 15, 2019
by
antirez
Browse files
Merge branch 'unstable' of github.com:/antirez/redis into unstable
parents
487601d8
d044e33c
Changes
6
Hide whitespace changes
Inline
Side-by-side
runtest-moduleapi
0 → 100755
View file @
4a505700
#!/bin/sh
TCL_VERSIONS
=
"8.5 8.6"
TCLSH
=
""
for
VERSION
in
$TCL_VERSIONS
;
do
TCL
=
`
which tclsh
$VERSION
2>/dev/null
`
&&
TCLSH
=
$TCL
done
if
[
-z
$TCLSH
]
then
echo
"You need tcl 8.5 or newer in order to run the Redis test"
exit
1
fi
make
-C
tests/modules
&&
\
$TCLSH
tests/test_helper.tcl
--single
unit/moduleapi/commandfilter
"
${
@
}
"
src/module.c
View file @
4a505700
...
...
@@ -753,6 +753,7 @@ void RM_SetModuleAttribs(RedisModuleCtx *ctx, const char *name, int ver, int api
module
->
usedby
=
listCreate
();
module
->
using
=
listCreate
();
module
->
filters
=
listCreate
();
module
->
in_call
=
0
;
ctx
->
module
=
module
;
}
...
...
src/modules/Makefile
View file @
4a505700
...
...
@@ -13,7 +13,7 @@ endif
.SUFFIXES
:
.c .so .xo .o
all
:
helloworld.so hellotype.so helloblock.so testmodule.so hellocluster.so hellotimer.so hellodict.so
hellofilter.so
all
:
helloworld.so hellotype.so helloblock.so testmodule.so hellocluster.so hellotimer.so hellodict.so
.c.xo
:
$(CC)
-I
.
$(CFLAGS)
$(SHOBJ_CFLAGS)
-fPIC
-c
$<
-o
$@
...
...
@@ -47,11 +47,6 @@ hellodict.xo: ../redismodule.h
hellodict.so
:
hellodict.xo
hellofilter.xo
:
../redismodule.h
hellofilter.so
:
hellofilter.xo
$(LD)
-o
$@
$<
$(SHOBJ_LDFLAGS)
$(LIBS)
-lc
testmodule.xo
:
../redismodule.h
testmodule.so
:
testmodule.xo
...
...
tests/modules/Makefile
0 → 100644
View file @
4a505700
# find the OS
uname_S
:=
$(
shell
sh
-c
'uname -s 2>/dev/null || echo not'
)
# Compile flags for linux / osx
ifeq
($(uname_S),Linux)
SHOBJ_CFLAGS
?=
-W
-Wall
-fno-common
-g
-ggdb
-std
=
c99
-O2
SHOBJ_LDFLAGS
?=
-shared
else
SHOBJ_CFLAGS
?=
-W
-Wall
-dynamic
-fno-common
-g
-ggdb
-std
=
c99
-O2
SHOBJ_LDFLAGS
?=
-bundle
-undefined
dynamic_lookup
endif
.SUFFIXES
:
.c .so .xo .o
all
:
commandfilter.so
.c.xo
:
$(CC)
-I
../../src
$(CFLAGS)
$(SHOBJ_CFLAGS)
-fPIC
-c
$<
-o
$@
commandfilter.xo
:
../../src/redismodule.h
commandfilter.so
:
commandfilter.xo
$(LD)
-o
$@
$<
$(SHOBJ_LDFLAGS)
$(LIBS)
-lc
src
/modules/
hello
filter.c
→
tests
/modules/
command
filter.c
View file @
4a505700
#define REDISMODULE_EXPERIMENTAL_API
#include "
../
redismodule.h"
#include "redismodule.h"
#include <string.h>
static
RedisModuleString
*
log_key_name
;
static
const
char
log_command_name
[]
=
"
hello
filter.log"
;
static
const
char
ping_command_name
[]
=
"
hello
filter.ping"
;
static
const
char
unregister_command_name
[]
=
"
hello
filter.unregister"
;
static
const
char
log_command_name
[]
=
"
command
filter.log"
;
static
const
char
ping_command_name
[]
=
"
command
filter.ping"
;
static
const
char
unregister_command_name
[]
=
"
command
filter.unregister"
;
static
int
in_log_command
=
0
;
static
RedisModuleCommandFilter
*
filter
=
NULL
;
int
Hello
Filter_UnregisterCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_UnregisterCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
(
void
)
argc
;
(
void
)
argv
;
...
...
@@ -23,7 +23,7 @@ int HelloFilter_UnregisterCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
return
REDISMODULE_OK
;
}
int
Hello
Filter_PingCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_PingCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
(
void
)
argc
;
(
void
)
argv
;
...
...
@@ -39,7 +39,7 @@ int HelloFilter_PingCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int a
return
REDISMODULE_OK
;
}
int
Hello
Filter_LogCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_LogCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
RedisModuleString
*
s
=
RedisModule_CreateString
(
ctx
,
""
,
0
);
...
...
@@ -74,9 +74,9 @@ int HelloFilter_LogCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
return
REDISMODULE_OK
;
}
void
Hello
Filter_CommandFilter
(
RedisModuleCommandFilterCtx
*
filter
)
void
Command
Filter_CommandFilter
(
RedisModuleCommandFilterCtx
*
filter
)
{
if
(
in_log_command
)
return
;
/* don't process our own RM_Call() from
Hello
Filter_LogCommand() */
if
(
in_log_command
)
return
;
/* don't process our own RM_Call() from
Command
Filter_LogCommand() */
/* Fun manipulations:
* - Remove @delme
...
...
@@ -117,7 +117,7 @@ void HelloFilter_CommandFilter(RedisModuleCommandFilterCtx *filter)
}
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
RedisModule_Init
(
ctx
,
"
hello
filter"
,
1
,
REDISMODULE_APIVER_1
)
if
(
RedisModule_Init
(
ctx
,
"
command
filter"
,
1
,
REDISMODULE_APIVER_1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
argc
!=
2
)
{
...
...
@@ -130,18 +130,18 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
RedisModule_StringToLongLong
(
argv
[
1
],
&
noself
);
if
(
RedisModule_CreateCommand
(
ctx
,
log_command_name
,
Hello
Filter_LogCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_LogCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
ping_command_name
,
Hello
Filter_PingCommand
,
"deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_PingCommand
,
"deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
unregister_command_name
,
Hello
Filter_UnregisterCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_UnregisterCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
((
filter
=
RedisModule_RegisterCommandFilter
(
ctx
,
Hello
Filter_CommandFilter
,
if
((
filter
=
RedisModule_RegisterCommandFilter
(
ctx
,
Command
Filter_CommandFilter
,
noself
?
REDISMODULE_CMDFILTER_NOSELF
:
0
))
==
NULL
)
return
REDISMODULE_ERR
;
...
...
tests/module
s
/commandfilter.tcl
→
tests/
unit/
module
api
/commandfilter.tcl
View file @
4a505700
set testmodule
[
file normalize
src
/modules/
hello
filter.so
]
set testmodule
[
file normalize
tests
/modules/
command
filter.so
]
start_server
{
tags
{
"modules"
}}
{
r module load $testmodule log-key 0
...
...
@@ -27,7 +27,7 @@ start_server {tags {"modules"}} {
test
{
Command Filter applies on RM_Call
()
commands
}
{
r del log-key
r
hello
filter.ping
r
command
filter.ping
r lrange log-key 0 -1
}
"{ping @log}"
...
...
@@ -39,13 +39,13 @@ start_server {tags {"modules"}} {
test
{
Command Filter applies on Lua redis.call
()
that calls a module
}
{
r del log-key
r eval
"redis.call('
hello
filter.ping')"
0
r eval
"redis.call('
command
filter.ping')"
0
r lrange log-key 0 -1
}
"{ping @log}"
test
{
Command Filter is unregistered implicitly on module unload
}
{
r del log-key
r module unload
hello
filter
r module unload
command
filter
r set mykey @log
r lrange log-key 0 -1
}
{}
...
...
@@ -59,14 +59,14 @@ start_server {tags {"modules"}} {
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
# Unregister
r
hello
filter.unregister
r
command
filter.unregister
r del log-key
r set mykey @log
r lrange log-key 0 -1
}
{}
r module unload
hello
filter
r module unload
command
filter
r module load $testmodule log-key 1
test
{
Command Filter REDISMODULE_CMDFILTER_NOSELF works as expected
}
{
...
...
@@ -74,10 +74,10 @@ start_server {tags {"modules"}} {
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
r del log-key
r
hello
filter.ping
r
command
filter.ping
assert_equal
{}
[
r lrange log-key 0 -1
]
r eval
"redis.call('
hello
filter.ping')"
0
r eval
"redis.call('
command
filter.ping')"
0
assert_equal
{}
[
r lrange log-key 0 -1
]
}
...
...
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