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
c7d9d662
Commit
c7d9d662
authored
May 30, 2010
by
Pieter Noordhuis
Browse files
generic push function that supports the dual encoding
parent
0f3dfa87
Changes
1
Hide whitespace changes
Inline
Side-by-side
redis.c
View file @
c7d9d662
...
@@ -75,6 +75,7 @@
...
@@ -75,6 +75,7 @@
#include "lzf.h" /* LZF compression library */
#include "lzf.h" /* LZF compression library */
#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
#include "zipmap.h" /* Compact dictionary-alike data structure */
#include "zipmap.h" /* Compact dictionary-alike data structure */
#include "ziplist.h" /* Compact list data structure */
#include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
#include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
#include "release.h" /* Release and/or git repository information */
#include "release.h" /* Release and/or git repository information */
...
@@ -124,10 +125,12 @@
...
@@ -124,10 +125,12 @@
/* Objects encoding. Some kind of objects like Strings and Hashes can be
/* Objects encoding. Some kind of objects like Strings and Hashes can be
* internally represented in multiple ways. The 'encoding' field of the object
* internally represented in multiple ways. The 'encoding' field of the object
* is set to one of this fields for this object. */
* is set to one of this fields for this object. */
#define REDIS_ENCODING_RAW 0
/* Raw representation */
#define REDIS_ENCODING_RAW 0 /* Raw representation */
#define REDIS_ENCODING_INT 1
/* Encoded as integer */
#define REDIS_ENCODING_INT 1 /* Encoded as integer */
#define REDIS_ENCODING_ZIPMAP 2
/* Encoded as zipmap */
#define REDIS_ENCODING_HT 2 /* Encoded as hash table */
#define REDIS_ENCODING_HT 3
/* Encoded as an hash table */
#define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
#define REDIS_ENCODING_LIST 4 /* Encoded as zipmap */
#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
static char* strencoding[] = {
static char* strencoding[] = {
"raw", "int", "zipmap", "hashtable"
"raw", "int", "zipmap", "hashtable"
...
@@ -3016,7 +3019,16 @@ static void freeStringObject(robj *o) {
...
@@ -3016,7 +3019,16 @@ static void freeStringObject(robj *o) {
}
}
static void freeListObject(robj *o) {
static void freeListObject(robj *o) {
listRelease
((
list
*
)
o
->
ptr
);
switch (o->encoding) {
case REDIS_ENCODING_LIST:
listRelease((list*) o->ptr);
break;
case REDIS_ENCODING_ZIPLIST:
zfree(o->ptr);
break;
default:
redisPanic("Unknown list encoding type");
}
}
}
static void freeSetObject(robj *o) {
static void freeSetObject(robj *o) {
...
@@ -4760,26 +4772,36 @@ static void moveCommand(redisClient *c) {
...
@@ -4760,26 +4772,36 @@ static void moveCommand(redisClient *c) {
}
}
/* =================================== Lists ================================ */
/* =================================== Lists ================================ */
static
void
pushGenericCommand
(
redisClient
*
c
,
int
where
)
{
static void lPush(robj *subject, robj *value, int where) {
robj
*
lobj
;
if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
list
*
list
;
int pos = (where == REDIS_HEAD) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
value = getDecodedObject(value);
subject->ptr = ziplistPush(subject->ptr,value->ptr,sdslen(value->ptr),pos);
decrRefCount(value);
} else if (subject->encoding == REDIS_ENCODING_LIST) {
if (where == REDIS_HEAD) {
listAddNodeHead(subject->ptr,value);
} else {
listAddNodeTail(subject->ptr,value);
}
incrRefCount(value);
} else {
redisPanic("Unknown list encoding");
}
}
lobj
=
lookupKeyWrite
(
c
->
db
,
c
->
argv
[
1
]);
static void pushGenericCommand(redisClient *c, int where) {
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
if (lobj == NULL) {
if (lobj == NULL) {
if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
addReply(c,shared.cone);
addReply(c,shared.cone);
return;
return;
}
}
lobj
=
createListObject
();
lobj = createObject(REDIS_LIST,ziplistNew());
list
=
lobj
->
ptr
;
lobj->encoding = REDIS_ENCODING_ZIPLIST;
if
(
where
==
REDIS_HEAD
)
{
listAddNodeHead
(
list
,
c
->
argv
[
2
]);
}
else
{
listAddNodeTail
(
list
,
c
->
argv
[
2
]);
}
dictAdd(c->db->dict,c->argv[1],lobj);
dictAdd(c->db->dict,c->argv[1],lobj);
incrRefCount(c->argv[1]);
incrRefCount(c->argv[1]);
incrRefCount
(
c
->
argv
[
2
]);
} else {
} else {
if (lobj->type != REDIS_LIST) {
if (lobj->type != REDIS_LIST) {
addReply(c,shared.wrongtypeerr);
addReply(c,shared.wrongtypeerr);
...
@@ -4789,16 +4811,10 @@ static void pushGenericCommand(redisClient *c, int where) {
...
@@ -4789,16 +4811,10 @@ static void pushGenericCommand(redisClient *c, int where) {
addReply(c,shared.cone);
addReply(c,shared.cone);
return;
return;
}
}
list
=
lobj
->
ptr
;
if
(
where
==
REDIS_HEAD
)
{
listAddNodeHead
(
list
,
c
->
argv
[
2
]);
}
else
{
listAddNodeTail
(
list
,
c
->
argv
[
2
]);
}
incrRefCount
(
c
->
argv
[
2
]);
}
}
lPush(lobj,c->argv[2],where);
addReplyLongLong(c,lLength(lobj));
server.dirty++;
server.dirty++;
addReplyLongLong
(
c
,
listLength
(
list
));
}
}
static void lpushCommand(redisClient *c) {
static void lpushCommand(redisClient *c) {
...
...
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