diff options
author | sundb <sundbcn@gmail.com> | 2021-08-10 14:18:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-10 09:18:49 +0300 |
commit | 02fd76b97cbc5b8ad6f4c81c8538f02c76cbed46 (patch) | |
tree | 7b40fbf03f5003c9993451dfe5b8fd902570ac51 /src/listpack.h | |
parent | cbda492909cd2fff25263913cd2e1f00bc48a541 (diff) | |
download | redis-02fd76b97cbc5b8ad6f4c81c8538f02c76cbed46.tar.gz |
Replace all usage of ziplist with listpack for t_hash (#8887)
Part one of implementing #8702 (taking hashes first before other types)
## Description of the feature
1. Change ziplist encoded hash objects to listpack encoding.
2. Convert existing ziplists on RDB loading time. an O(n) operation.
## Rdb format changes
1. Add RDB_TYPE_HASH_LISTPACK rdb type.
2. Bump RDB_VERSION to 10
## Interface changes
1. New `hash-max-listpack-entries` config is an alias for `hash-max-ziplist-entries` (same with `hash-max-listpack-value`)
2. OBJECT ENCODING will return `listpack` instead of `ziplist`
## Listpack improvements:
1. Support direct insert, replace integer element (rather than convert back and forth from string)
3. Add more listpack capabilities to match the ziplist ones (like `lpFind`, `lpRandomPairs` and such)
4. Optimize element length fetching, avoid multiple calculations
5. Use inline to avoid function call overhead.
## Tests
1. Add a new test to the RDB load time conversion
2. Adding the listpack unit tests. (based on the one in ziplist.c)
3. Add a few "corrupt payload: fuzzer findings" tests, and slightly modify existing ones.
Co-authored-by: Oran Agra <oran@redislabs.com>
Diffstat (limited to 'src/listpack.h')
-rw-r--r-- | src/listpack.h | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/listpack.h b/src/listpack.h index 9a5115bad..57bd4ecf3 100644 --- a/src/listpack.h +++ b/src/listpack.h @@ -45,22 +45,47 @@ #define LP_AFTER 1 #define LP_REPLACE 2 +/* Each entry in the listpack is either a string or an integer. */ +typedef struct { + /* When string is used, it is provided with the length (slen). */ + unsigned char *sval; + uint32_t slen; + /* When integer is used, 'sval' is NULL, and lval holds the value. */ + long long lval; +} listpackEntry; + unsigned char *lpNew(size_t capacity); void lpFree(unsigned char *lp); unsigned char* lpShrinkToFit(unsigned char *lp); -unsigned char *lpInsert(unsigned char *lp, unsigned char *ele, uint32_t size, unsigned char *p, int where, unsigned char **newp); -unsigned char *lpAppend(unsigned char *lp, unsigned char *ele, uint32_t size); +unsigned char *lpPrepend(unsigned char *lp, unsigned char *s, uint32_t slen); +unsigned char *lpPrependInteger(unsigned char *lp, long long lval); +unsigned char *lpAppend(unsigned char *lp, unsigned char *s, uint32_t slen); +unsigned char *lpAppendInteger(unsigned char *lp, long long lval); +unsigned char *lpReplace(unsigned char *lp, unsigned char **p, unsigned char *s, uint32_t slen); +unsigned char *lpReplaceInteger(unsigned char *lp, unsigned char **p, long long lval); unsigned char *lpDelete(unsigned char *lp, unsigned char *p, unsigned char **newp); -uint32_t lpLength(unsigned char *lp); +unsigned long lpLength(unsigned char *lp); unsigned char *lpGet(unsigned char *p, int64_t *count, unsigned char *intbuf); +unsigned char *lpGetValue(unsigned char *p, unsigned int *slen, long long *lval); +unsigned char *lpFind(unsigned char *lp, unsigned char *p, unsigned char *s, uint32_t slen, unsigned int skip); unsigned char *lpFirst(unsigned char *lp); unsigned char *lpLast(unsigned char *lp); unsigned char *lpNext(unsigned char *lp, unsigned char *p); unsigned char *lpPrev(unsigned char *lp, unsigned char *p); -uint32_t lpBytes(unsigned char *lp); +size_t lpBytes(unsigned char *lp); unsigned char *lpSeek(unsigned char *lp, long index); -int lpValidateIntegrity(unsigned char *lp, size_t size, int deep); +typedef int (*listpackValidateEntryCB)(unsigned char *p, void *userdata); +int lpValidateIntegrity(unsigned char *lp, size_t size, int deep, + listpackValidateEntryCB entry_cb, void *cb_userdata); unsigned char *lpValidateFirst(unsigned char *lp); int lpValidateNext(unsigned char *lp, unsigned char **pp, size_t lpbytes); +unsigned int lpCompare(unsigned char *p, unsigned char *s, uint32_t slen); +void lpRandomPair(unsigned char *lp, unsigned long total_count, listpackEntry *key, listpackEntry *val); +void lpRandomPairs(unsigned char *lp, unsigned int count, listpackEntry *keys, listpackEntry *vals); +unsigned int lpRandomPairsUnique(unsigned char *lp, unsigned int count, listpackEntry *keys, listpackEntry *vals); + +#ifdef REDIS_TEST +int listpackTest(int argc, char *argv[], int accurate); +#endif #endif |