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
42f72210
Commit
42f72210
authored
Apr 25, 2016
by
antirez
Browse files
Modules: Hash API defines made more uniform.
parent
9b0556cf
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/module.c
View file @
42f72210
...
@@ -1481,35 +1481,35 @@ int RM_ZsetRangePrev(RedisModuleKey *key) {
...
@@ -1481,35 +1481,35 @@ int RM_ZsetRangePrev(RedisModuleKey *key) {
*
*
* Example to set the hash argv[1] to the value argv[2]:
* Example to set the hash argv[1] to the value argv[2]:
*
*
* RedisModule_HashSet(key,REDISMODULE_H
SET
_NONE,argv[1],argv[2],NULL);
* RedisModule_HashSet(key,REDISMODULE_H
ASH
_NONE,argv[1],argv[2],NULL);
*
*
* The function can also be used in order to delete fields (if they exist)
* The function can also be used in order to delete fields (if they exist)
* by setting them to the specified value of REDISMODULE_H
SET
_DELETE:
* by setting them to the specified value of REDISMODULE_H
ASH
_DELETE:
*
*
* RedisModule_HashSet(key,REDISMODULE_H
SET
_NONE,argv[1],
* RedisModule_HashSet(key,REDISMODULE_H
ASH
_NONE,argv[1],
* REDISMODULE_H
SET
_DELETE,NULL);
* REDISMODULE_H
ASH
_DELETE,NULL);
*
*
* The behavior of the command changes with the specified flags, that can be
* The behavior of the command changes with the specified flags, that can be
* set to REDISMODULE_H
SET
_NONE if no special behavior is needed.
* set to REDISMODULE_H
ASH
_NONE if no special behavior is needed.
*
*
* REDISMODULE_H
SET
_NX: The operation is performed only if the field was not
* REDISMODULE_H
ASH
_NX: The operation is performed only if the field was not
* already existing in the hash.
* already existing in the hash.
* REDISMODULE_H
SET
_XX: The operation is performed only if the field was
* REDISMODULE_H
ASH
_XX: The operation is performed only if the field was
* already existing, so that a new value could be
* already existing, so that a new value could be
* associated to an existing filed, but no new fields
* associated to an existing filed, but no new fields
* are created.
* are created.
* REDISMODULE_H
SET
_CFIELDS: The field names passed are null terminated C
* REDISMODULE_H
ASH
_CFIELDS: The field names passed are null terminated C
* strings instead of RedisModuleString objects.
* strings instead of RedisModuleString objects.
*
*
* Unless NX is specified, the command overwrites the old field value with
* Unless NX is specified, the command overwrites the old field value with
* the new one.
* the new one.
*
*
* When using REDISMODULE_H
SET
_CFIELDS, field names are reported using
* When using REDISMODULE_H
ASH
_CFIELDS, field names are reported using
* normal C strings, so for example to delete the field "foo" the following
* normal C strings, so for example to delete the field "foo" the following
* code can be used:
* code can be used:
*
*
* RedisModule_HashSet(key,REDISMODULE_H
SET
_CFIELDS,"foo",
* RedisModule_HashSet(key,REDISMODULE_H
ASH
_CFIELDS,"foo",
* REDISMODULE_H
SET
_DELETE,NULL);
* REDISMODULE_H
ASH
_DELETE,NULL);
*
*
* Return value:
* Return value:
*
*
...
@@ -1532,7 +1532,7 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
...
@@ -1532,7 +1532,7 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
while
(
1
)
{
while
(
1
)
{
RedisModuleString
*
field
,
*
value
;
RedisModuleString
*
field
,
*
value
;
/* Get the field and value objects. */
/* Get the field and value objects. */
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
{
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
{
char
*
cfield
=
va_arg
(
ap
,
char
*
);
char
*
cfield
=
va_arg
(
ap
,
char
*
);
if
(
cfield
==
NULL
)
break
;
if
(
cfield
==
NULL
)
break
;
field
=
createRawStringObject
(
cfield
,
strlen
(
cfield
));
field
=
createRawStringObject
(
cfield
,
strlen
(
cfield
));
...
@@ -1543,20 +1543,20 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
...
@@ -1543,20 +1543,20 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
value
=
va_arg
(
ap
,
RedisModuleString
*
);
value
=
va_arg
(
ap
,
RedisModuleString
*
);
/* Handle XX and NX */
/* Handle XX and NX */
if
(
flags
&
(
REDISMODULE_H
SET
_XX
|
REDISMODULE_H
SET
_NX
))
{
if
(
flags
&
(
REDISMODULE_H
ASH
_XX
|
REDISMODULE_H
ASH
_NX
))
{
int
exists
=
hashTypeExists
(
key
->
value
,
field
->
ptr
);
int
exists
=
hashTypeExists
(
key
->
value
,
field
->
ptr
);
if
(((
flags
&
REDISMODULE_H
SET
_XX
)
&&
!
exists
)
||
if
(((
flags
&
REDISMODULE_H
ASH
_XX
)
&&
!
exists
)
||
((
flags
&
REDISMODULE_H
SET
_NX
)
&&
exists
))
((
flags
&
REDISMODULE_H
ASH
_NX
)
&&
exists
))
{
{
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
decrRefCount
(
field
);
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
decrRefCount
(
field
);
continue
;
continue
;
}
}
}
}
/* Handle deletion if value is REDISMODULE_H
SET
_DELETE. */
/* Handle deletion if value is REDISMODULE_H
ASH
_DELETE. */
if
(
value
==
REDISMODULE_H
SET
_DELETE
)
{
if
(
value
==
REDISMODULE_H
ASH
_DELETE
)
{
updated
+=
hashTypeDelete
(
key
->
value
,
field
->
ptr
);
updated
+=
hashTypeDelete
(
key
->
value
,
field
->
ptr
);
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
decrRefCount
(
field
);
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
decrRefCount
(
field
);
continue
;
continue
;
}
}
...
@@ -1564,13 +1564,13 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
...
@@ -1564,13 +1564,13 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
* SDS object to the low level function that sets the field
* SDS object to the low level function that sets the field
* to avoid a useless copy. */
* to avoid a useless copy. */
int
low_flags
=
HASH_SET_COPY
;
int
low_flags
=
HASH_SET_COPY
;
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
low_flags
|=
HASH_SET_TAKE_FIELD
;
low_flags
|=
HASH_SET_TAKE_FIELD
;
updated
+=
hashTypeSet
(
key
->
value
,
field
->
ptr
,
value
->
ptr
,
low_flags
);
updated
+=
hashTypeSet
(
key
->
value
,
field
->
ptr
,
value
->
ptr
,
low_flags
);
field
->
ptr
=
NULL
;
/* Ownership is now of hashTypeSet() */
field
->
ptr
=
NULL
;
/* Ownership is now of hashTypeSet() */
/* Cleanup */
/* Cleanup */
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
decrRefCount
(
field
);
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
decrRefCount
(
field
);
}
}
va_end
(
ap
);
va_end
(
ap
);
moduleDelKeyIfEmpty
(
key
);
moduleDelKeyIfEmpty
(
key
);
...
@@ -1587,25 +1587,25 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
...
@@ -1587,25 +1587,25 @@ int RM_HashSet(RedisModuleKey *key, int flags, ...) {
* This is an example usage:
* This is an example usage:
*
*
* RedisModuleString *first, *second;
* RedisModuleString *first, *second;
* RedisModule_HashGet(mykey,REDISMODULE_H
GET
_NONE,argv[1],&first,
* RedisModule_HashGet(mykey,REDISMODULE_H
ASH
_NONE,argv[1],&first,
* argv[2],&second,NULL);
* argv[2],&second,NULL);
*
*
* As with RedisModule_HashSet() the behavior of the command can be specified
* As with RedisModule_HashSet() the behavior of the command can be specified
* passing flags different than REDISMODULE_H
GET
_NONE:
* passing flags different than REDISMODULE_H
ASH
_NONE:
*
*
* REDISMODULE_H
GET
_CFIELD: field names as null terminated C strings.
* REDISMODULE_H
ASH
_CFIELD: field names as null terminated C strings.
*
*
* REDISMODULE_H
GET
_EXISTS: instead of setting the value of the field
* REDISMODULE_H
ASH
_EXISTS: instead of setting the value of the field
* expecting a RedisModuleString pointer to pointer, the function just
* expecting a RedisModuleString pointer to pointer, the function just
* reports if the field esists or not and expects an integer pointer
* reports if the field esists or not and expects an integer pointer
* as the second element of each pair.
* as the second element of each pair.
*
*
* Example of REDISMODULE_H
GET
_CFIELD:
* Example of REDISMODULE_H
ASH
_CFIELD:
*
*
* RedisModuleString *username, *hashedpass;
* RedisModuleString *username, *hashedpass;
* RedisModule_HashGet(mykey,"username",&username,"hp",&hashedpass);
* RedisModule_HashGet(mykey,"username",&username,"hp",&hashedpass);
*
*
* Example of REDISMODULE_H
GET
_EXISTS:
* Example of REDISMODULE_H
ASH
_EXISTS:
*
*
* int exists;
* int exists;
* RedisModule_HashGet(mykey,argv[1],&exists,NULL);
* RedisModule_HashGet(mykey,argv[1],&exists,NULL);
...
@@ -1627,7 +1627,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
...
@@ -1627,7 +1627,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
RedisModuleString
*
field
,
**
valueptr
;
RedisModuleString
*
field
,
**
valueptr
;
int
*
existsptr
;
int
*
existsptr
;
/* Get the field object and the value pointer to pointer. */
/* Get the field object and the value pointer to pointer. */
if
(
flags
&
REDISMODULE_H
GET
_CFIELDS
)
{
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
{
char
*
cfield
=
va_arg
(
ap
,
char
*
);
char
*
cfield
=
va_arg
(
ap
,
char
*
);
if
(
cfield
==
NULL
)
break
;
if
(
cfield
==
NULL
)
break
;
field
=
createRawStringObject
(
cfield
,
strlen
(
cfield
));
field
=
createRawStringObject
(
cfield
,
strlen
(
cfield
));
...
@@ -1637,7 +1637,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
...
@@ -1637,7 +1637,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
}
}
/* Query the hash for existence or value object. */
/* Query the hash for existence or value object. */
if
(
flags
&
REDISMODULE_H
GET
_EXISTS
)
{
if
(
flags
&
REDISMODULE_H
ASH
_EXISTS
)
{
existsptr
=
va_arg
(
ap
,
int
*
);
existsptr
=
va_arg
(
ap
,
int
*
);
if
(
key
->
value
)
if
(
key
->
value
)
*
existsptr
=
hashTypeExists
(
key
->
value
,
field
->
ptr
);
*
existsptr
=
hashTypeExists
(
key
->
value
,
field
->
ptr
);
...
@@ -1660,7 +1660,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
...
@@ -1660,7 +1660,7 @@ int RM_HashGet(RedisModuleKey *key, int flags, ...) {
}
}
/* Cleanup */
/* Cleanup */
if
(
flags
&
REDISMODULE_H
SET
_CFIELDS
)
decrRefCount
(
field
);
if
(
flags
&
REDISMODULE_H
ASH
_CFIELDS
)
decrRefCount
(
field
);
}
}
va_end
(
ap
);
va_end
(
ap
);
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
...
...
src/modules/helloworld.c
View file @
42f72210
...
@@ -440,9 +440,9 @@ int HelloHCopy_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int a
...
@@ -440,9 +440,9 @@ int HelloHCopy_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int a
/* Get the old field value. */
/* Get the old field value. */
RedisModuleString
*
oldval
;
RedisModuleString
*
oldval
;
RedisModule_HashGet
(
key
,
REDISMODULE_H
GET
_NONE
,
argv
[
2
],
&
oldval
,
NULL
);
RedisModule_HashGet
(
key
,
REDISMODULE_H
ASH
_NONE
,
argv
[
2
],
&
oldval
,
NULL
);
if
(
oldval
)
{
if
(
oldval
)
{
RedisModule_HashSet
(
key
,
REDISMODULE_H
SET
_NONE
,
argv
[
3
],
oldval
,
NULL
);
RedisModule_HashSet
(
key
,
REDISMODULE_H
ASH
_NONE
,
argv
[
3
],
oldval
,
NULL
);
}
}
RedisModule_ReplyWithLongLong
(
ctx
,
oldval
!=
NULL
);
RedisModule_ReplyWithLongLong
(
ctx
,
oldval
!=
NULL
);
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
...
...
src/redismodule.h
View file @
42f72210
...
@@ -51,19 +51,15 @@
...
@@ -51,19 +51,15 @@
#define REDISMODULE_ZADD_NOP (1<<4)
#define REDISMODULE_ZADD_NOP (1<<4)
/* Hash API flags. */
/* Hash API flags. */
#define REDISMODULE_HSET_NONE 0
#define REDISMODULE_HASH_NONE 0
#define REDISMODULE_HGET_NONE 0
#define REDISMODULE_HASH_NX (1<<0)
#define REDISMODULE_HSET_NX (1<<0)
#define REDISMODULE_HASH_XX (1<<1)
#define REDISMODULE_HSET_XX (1<<1)
#define REDISMODULE_HASH_CFIELDS (1<<2)
#define REDISMODULE_HSET_CFIELDS (1<<2)
#define REDISMODULE_HASH_EXISTS (1<<3)
/* Set GET_CFIELD to the same value as SET_CFIELD so that misuses will not
* result into surprising behaviors. */
#define REDISMODULE_HGET_CFIELDS REDISMODULE_HSET_CFIELDS
#define REDISMODULE_HGET_EXISTS (1<<3)
/* A special pointer that we can use between the core and the module to signal
/* A special pointer that we can use between the core and the module to signal
* field deletion, and that is impossible to be a valid pointer. */
* field deletion, and that is impossible to be a valid pointer. */
#define REDISMODULE_H
SET
_DELETE ((RedisModuleString*)(long)1)
#define REDISMODULE_H
ASH
_DELETE ((RedisModuleString*)(long)1)
/* Error messages. */
/* Error messages. */
#define REDISMODULE_ERRORMSG_WRONGTYPE "WRONGTYPE Operation against a key holding the wrong kind of value"
#define REDISMODULE_ERRORMSG_WRONGTYPE "WRONGTYPE Operation against a key holding the wrong kind of value"
...
...
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