forked from redis/redis
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HAPPEND and HAPPENDX commands #2
Open
anoakie
wants to merge
4
commits into
StumbleUponArchive:unstable
Choose a base branch
from
anoakie:unstable
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fdf1073
Add HAPPEND and HAPPENDX commands
anoakie e1afe50
Fix ziplists with HAPPEND
anoakie 3f720cd
Fix string conversion on 3rd argument in genericHappendCommand
anoakie f6f5ee5
Merge remote-tracking branch 'upstream/unstable' into unstable
anoakie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,86 @@ int hashTypeExists(robj *o, robj *field) { | |
return 0; | ||
} | ||
|
||
/* Append to an element. | ||
* Return total length of appended string. | ||
* This function will take care of incrementing the reference count of the | ||
* retained fields and value objects. */ | ||
size_t hashTypeAppend(robj *o, robj *field, robj *append) { | ||
int update = 0; | ||
long long totlen = 0; | ||
robj *value; | ||
|
||
if (o->encoding == REDIS_ENCODING_ZIPLIST) { | ||
unsigned char *zl, *fptr, *vptr; | ||
unsigned char *vstr = NULL; | ||
unsigned int vlen = UINT_MAX; | ||
long long vll = LLONG_MAX; | ||
int ret; | ||
|
||
field = getDecodedObject(field); | ||
append = getDecodedObject(append); | ||
|
||
zl = o->ptr; | ||
fptr = ziplistIndex(zl, ZIPLIST_HEAD); | ||
if (fptr != NULL) { | ||
fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1); | ||
if (fptr != NULL) { | ||
/* Grab pointer to the value (fptr points to the field) */ | ||
vptr = ziplistNext(zl, fptr); | ||
redisAssert(vptr != NULL); | ||
update = 1; | ||
|
||
/* Append to current string */ | ||
ret = ziplistGet(vptr, &vstr, &vlen, &vll); | ||
redisAssert(ret); | ||
value = createStringObject((char *)vstr, (size_t)vlen); | ||
value->ptr = sdscatlen(value->ptr, append->ptr, sdslen(append->ptr)); | ||
|
||
/* Delete value */ | ||
zl = ziplistDelete(zl, &vptr); | ||
|
||
/* Insert new value */ | ||
zl = ziplistInsert(zl, vptr, value->ptr, sdslen(value->ptr)); | ||
totlen = sdslen(value->ptr); | ||
decrRefCount(value); | ||
} | ||
} | ||
|
||
if (!update) { | ||
/* Push new field/value pair onto the tail of the ziplist */ | ||
zl = ziplistPush(zl, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL); | ||
zl = ziplistPush(zl, append->ptr, sdslen(append->ptr), ZIPLIST_TAIL); | ||
totlen = sdslen(append->ptr); | ||
} | ||
|
||
o->ptr = zl; | ||
decrRefCount(field); | ||
decrRefCount(append); | ||
|
||
/* Check if the ziplist needs to be converted to a hash table */ | ||
if (hashTypeLength(o) > server.hash_max_ziplist_entries) | ||
hashTypeConvert(o, REDIS_ENCODING_HT); | ||
} else if (o->encoding == REDIS_ENCODING_HT) { | ||
/* Append to current string */ | ||
if ((value = hashTypeGetObject(o,field)) != NULL) { | ||
value->ptr = sdscatlen(value->ptr,append->ptr,sdslen(append->ptr)); | ||
} else { | ||
value = append; | ||
incrRefCount(value); | ||
} | ||
|
||
if (dictReplace(o->ptr, field, value)) { /* Insert */ | ||
incrRefCount(field); | ||
} else { /* Update */ | ||
update = 1; | ||
} | ||
totlen = sdslen(value->ptr); | ||
} else { | ||
redisPanic("Unknown hash encoding"); | ||
} | ||
return totlen; | ||
} | ||
|
||
/* Add an element, discard the old if the key already exists. | ||
* Return 0 on insert and 1 on update. | ||
* This function will take care of incrementing the reference count of the | ||
|
@@ -752,6 +832,34 @@ void hgetallCommand(redisClient *c) { | |
genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE); | ||
} | ||
|
||
void genericHappendCommand(redisClient *c, int nx) { | ||
size_t totlen; | ||
robj *o; | ||
|
||
if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return; | ||
hashTypeTryConversion(o,c->argv,2,3); | ||
|
||
if (!hashTypeExists(o, c->argv[2]) && !nx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put the !nx check first. |
||
addReply(c, shared.czero); | ||
return; | ||
} else { | ||
hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]); | ||
totlen = hashTypeAppend(o,c->argv[2],c->argv[3]); | ||
addReplyLongLong(c, totlen); | ||
signalModifiedKey(c->db,c->argv[1]); | ||
notifyKeyspaceEvent(REDIS_NOTIFY_HASH,"happend",c->argv[1],c->db->id); | ||
server.dirty++; | ||
} | ||
} | ||
|
||
void happendxCommand(redisClient *c) { | ||
genericHappendCommand(c,0); | ||
} | ||
|
||
void happendCommand(redisClient *c) { | ||
genericHappendCommand(c,1); | ||
} | ||
|
||
void hexistsCommand(redisClient *c) { | ||
robj *o; | ||
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to be checking the hash-max-ziplist-value here.