Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
2564031a
Commit
2564031a
authored
Sep 26, 2016
by
antirez
Browse files
Merge branch 'unstable' of github.com:/antirez/redis into unstable
parents
6d9f8e24
6e866ee0
Changes
4
Show whitespace changes
Inline
Side-by-side
src/module.c
View file @
2564031a
...
@@ -681,13 +681,33 @@ void autoMemoryCollect(RedisModuleCtx *ctx) {
...
@@ -681,13 +681,33 @@ void autoMemoryCollect(RedisModuleCtx *ctx) {
*
*
* The string is created by copying the `len` bytes starting
* The string is created by copying the `len` bytes starting
* at `ptr`. No reference is retained to the passed buffer. */
* at `ptr`. No reference is retained to the passed buffer. */
RedisModuleString
*
RM_CreateString
(
RedisModuleCtx
*
ctx
,
const
char
*
ptr
,
size_t
len
)
RedisModuleString
*
RM_CreateString
(
RedisModuleCtx
*
ctx
,
const
char
*
ptr
,
size_t
len
)
{
{
RedisModuleString
*
o
=
createStringObject
(
ptr
,
len
);
RedisModuleString
*
o
=
createStringObject
(
ptr
,
len
);
autoMemoryAdd
(
ctx
,
REDISMODULE_AM_STRING
,
o
);
autoMemoryAdd
(
ctx
,
REDISMODULE_AM_STRING
,
o
);
return
o
;
return
o
;
}
}
/* Create a new module string object from a printf format and arguments.
* The returned string must be freed with RedisModule_FreeString(), unless automatic
* memory is enabled.
*
* The string is created using the sds formatter function sdscatvprintf() */
RedisModuleString
*
RM_CreateStringPrintf
(
RedisModuleCtx
*
ctx
,
const
char
*
fmt
,
...)
{
sds
s
=
sdsempty
();
va_list
ap
;
va_start
(
ap
,
fmt
);
s
=
sdscatvprintf
(
s
,
fmt
,
ap
);
va_end
(
ap
);
RedisModuleString
*
o
=
createObject
(
OBJ_STRING
,
s
);
autoMemoryAdd
(
ctx
,
REDISMODULE_AM_STRING
,
o
);
return
o
;
}
/* Like RedisModule_CreatString(), but creates a string starting from a long long
/* Like RedisModule_CreatString(), but creates a string starting from a long long
* integer instead of taking a buffer and its length.
* integer instead of taking a buffer and its length.
*
*
...
@@ -3194,6 +3214,7 @@ void moduleRegisterCoreAPI(void) {
...
@@ -3194,6 +3214,7 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API
(
CreateString
);
REGISTER_API
(
CreateString
);
REGISTER_API
(
CreateStringFromLongLong
);
REGISTER_API
(
CreateStringFromLongLong
);
REGISTER_API
(
CreateStringFromString
);
REGISTER_API
(
CreateStringFromString
);
REGISTER_API
(
CreateStringPrintf
);
REGISTER_API
(
FreeString
);
REGISTER_API
(
FreeString
);
REGISTER_API
(
StringPtrLen
);
REGISTER_API
(
StringPtrLen
);
REGISTER_API
(
AutoMemory
);
REGISTER_API
(
AutoMemory
);
...
...
src/modules/testmodule.c
View file @
2564031a
...
@@ -93,6 +93,25 @@ int TestStringAppendAM(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
...
@@ -93,6 +93,25 @@ int TestStringAppendAM(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
}
}
/* TEST.STRING.PRINTF -- Test string formatting. */
int
TestStringPrintf
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
RedisModule_AutoMemory
(
ctx
);
if
(
argc
<
3
)
{
return
RedisModule_WrongArity
(
ctx
);
}
RedisModuleString
*
s
=
RedisModule_CreateStringPrintf
(
ctx
,
"Got %d args. argv[1]: %s, argv[2]: %s"
,
argc
,
RedisModule_StringPtrLen
(
argv
[
1
],
NULL
),
RedisModule_StringPtrLen
(
argv
[
2
],
NULL
)
);
RedisModule_ReplyWithString
(
ctx
,
s
);
return
REDISMODULE_OK
;
}
/* ----------------------------- Test framework ----------------------------- */
/* ----------------------------- Test framework ----------------------------- */
/* Return 1 if the reply matches the specified string, otherwise log errors
/* Return 1 if the reply matches the specified string, otherwise log errors
...
@@ -163,6 +182,9 @@ int TestIt(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
...
@@ -163,6 +182,9 @@ int TestIt(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
T
(
"test.string.append.am"
,
""
);
T
(
"test.string.append.am"
,
""
);
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"foobar"
,
6
))
goto
fail
;
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"foobar"
,
6
))
goto
fail
;
T
(
"test.string.printf"
,
"cc"
,
"foo"
,
"bar"
);
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"Got 3 args. argv[1]: foo, argv[2]: bar"
,
38
))
goto
fail
;
RedisModule_ReplyWithSimpleString
(
ctx
,
"ALL TESTS PASSED"
);
RedisModule_ReplyWithSimpleString
(
ctx
,
"ALL TESTS PASSED"
);
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
...
@@ -188,6 +210,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
...
@@ -188,6 +210,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
TestStringAppendAM
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
TestStringAppendAM
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.string.printf"
,
TestStringPrintf
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.it"
,
if
(
RedisModule_CreateCommand
(
ctx
,
"test.it"
,
TestIt
,
"readonly"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
TestIt
,
"readonly"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
...
...
src/object.c
View file @
2564031a
...
@@ -75,7 +75,7 @@ robj *makeObjectShared(robj *o) {
...
@@ -75,7 +75,7 @@ robj *makeObjectShared(robj *o) {
/* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain
/* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain
* string object where o->ptr points to a proper sds string. */
* string object where o->ptr points to a proper sds string. */
robj
*
createRawStringObject
(
const
char
*
ptr
,
size_t
len
)
{
robj
*
createRawStringObject
(
const
char
*
ptr
,
size_t
len
)
{
return
createObject
(
OBJ_STRING
,
sdsnewlen
(
ptr
,
len
));
return
createObject
(
OBJ_STRING
,
sdsnewlen
(
ptr
,
len
));
}
}
/* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is
/* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is
...
...
src/redismodule.h
View file @
2564031a
...
@@ -125,6 +125,7 @@ RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_CallReplyArrayElement)(Re
...
@@ -125,6 +125,7 @@ RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_CallReplyArrayElement)(Re
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateString
)(
RedisModuleCtx
*
ctx
,
const
char
*
ptr
,
size_t
len
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateString
)(
RedisModuleCtx
*
ctx
,
const
char
*
ptr
,
size_t
len
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateStringFromLongLong
)(
RedisModuleCtx
*
ctx
,
long
long
ll
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateStringFromLongLong
)(
RedisModuleCtx
*
ctx
,
long
long
ll
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateStringFromString
)(
RedisModuleCtx
*
ctx
,
const
RedisModuleString
*
str
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateStringFromString
)(
RedisModuleCtx
*
ctx
,
const
RedisModuleString
*
str
);
RedisModuleString
*
REDISMODULE_API_FUNC
(
RedisModule_CreateStringPrintf
)(
RedisModuleCtx
*
ctx
,
const
char
*
fmt
,
...);
void
REDISMODULE_API_FUNC
(
RedisModule_FreeString
)(
RedisModuleCtx
*
ctx
,
RedisModuleString
*
str
);
void
REDISMODULE_API_FUNC
(
RedisModule_FreeString
)(
RedisModuleCtx
*
ctx
,
RedisModuleString
*
str
);
const
char
*
REDISMODULE_API_FUNC
(
RedisModule_StringPtrLen
)(
const
RedisModuleString
*
str
,
size_t
*
len
);
const
char
*
REDISMODULE_API_FUNC
(
RedisModule_StringPtrLen
)(
const
RedisModuleString
*
str
,
size_t
*
len
);
int
REDISMODULE_API_FUNC
(
RedisModule_ReplyWithError
)(
RedisModuleCtx
*
ctx
,
const
char
*
err
);
int
REDISMODULE_API_FUNC
(
RedisModule_ReplyWithError
)(
RedisModuleCtx
*
ctx
,
const
char
*
err
);
...
@@ -234,6 +235,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
...
@@ -234,6 +235,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API
(
CreateString
);
REDISMODULE_GET_API
(
CreateString
);
REDISMODULE_GET_API
(
CreateStringFromLongLong
);
REDISMODULE_GET_API
(
CreateStringFromLongLong
);
REDISMODULE_GET_API
(
CreateStringFromString
);
REDISMODULE_GET_API
(
CreateStringFromString
);
REDISMODULE_GET_API
(
CreateStringPrintf
);
REDISMODULE_GET_API
(
FreeString
);
REDISMODULE_GET_API
(
FreeString
);
REDISMODULE_GET_API
(
StringPtrLen
);
REDISMODULE_GET_API
(
StringPtrLen
);
REDISMODULE_GET_API
(
AutoMemory
);
REDISMODULE_GET_API
(
AutoMemory
);
...
...
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