2010-08-13 13:28:49 -04:00
|
|
|
/* The ziplist is a specially encoded dually linked list that is designed
|
|
|
|
* to be very memory efficient. It stores both strings and integer values,
|
|
|
|
* where integers are encoded as actual integers instead of a series of
|
|
|
|
* characters. It allows push and pop operations on either side of the list
|
|
|
|
* in O(1) time. However, because every operation requires a reallocation of
|
|
|
|
* the memory used by the ziplist, the actual complexity is related to the
|
|
|
|
* amount of memory used by the ziplist.
|
2010-05-21 06:54:01 -04:00
|
|
|
*
|
2010-08-13 13:28:49 -04:00
|
|
|
* ----------------------------------------------------------------------------
|
2010-05-21 06:54:01 -04:00
|
|
|
*
|
2010-08-13 13:28:49 -04:00
|
|
|
* ZIPLIST OVERALL LAYOUT:
|
|
|
|
* The general layout of the ziplist is as follows:
|
|
|
|
* <zlbytes><zltail><zllen><entry><entry><zlend>
|
2010-05-21 06:54:01 -04:00
|
|
|
*
|
2010-08-13 13:28:49 -04:00
|
|
|
* <zlbytes> is an unsigned integer to hold the number of bytes that the
|
|
|
|
* ziplist occupies. This value needs to be stored to be able to resize the
|
|
|
|
* entire structure without the need to traverse it first.
|
|
|
|
*
|
|
|
|
* <zltail> is the offset to the last entry in the list. This allows a pop
|
|
|
|
* operation on the far side of the list without the need for full traversal.
|
|
|
|
*
|
|
|
|
* <zllen> is the number of entries.When this value is larger than 2**16-2,
|
|
|
|
* we need to traverse the entire list to know how many items it holds.
|
|
|
|
*
|
|
|
|
* <zlend> is a single byte special value, equal to 255, which indicates the
|
|
|
|
* end of the list.
|
|
|
|
*
|
|
|
|
* ZIPLIST ENTRIES:
|
|
|
|
* Every entry in the ziplist is prefixed by a header that contains two pieces
|
|
|
|
* of information. First, the length of the previous entry is stored to be
|
|
|
|
* able to traverse the list from back to front. Second, the encoding with an
|
|
|
|
* optional string length of the entry itself is stored.
|
|
|
|
*
|
|
|
|
* The length of the previous entry is encoded in the following way:
|
|
|
|
* If this length is smaller than 254 bytes, it will only consume a single
|
|
|
|
* byte that takes the length as value. When the length is greater than or
|
|
|
|
* equal to 254, it will consume 5 bytes. The first byte is set to 254 to
|
|
|
|
* indicate a larger value is following. The remaining 4 bytes take the
|
|
|
|
* length of the previous entry as value.
|
|
|
|
*
|
|
|
|
* The other header field of the entry itself depends on the contents of the
|
|
|
|
* entry. When the entry is a string, the first 2 bits of this header will hold
|
|
|
|
* the type of encoding used to store the length of the string, followed by the
|
|
|
|
* actual length of the string. When the entry is an integer the first 2 bits
|
|
|
|
* are both set to 1. The following 2 bits are used to specify what kind of
|
|
|
|
* integer will be stored after this header. An overview of the different
|
|
|
|
* types and encodings is as follows:
|
|
|
|
*
|
|
|
|
* |00pppppp| - 1 byte
|
|
|
|
* String value with length less than or equal to 63 bytes (6 bits).
|
|
|
|
* |01pppppp|qqqqqqqq| - 2 bytes
|
|
|
|
* String value with length less than or equal to 16383 bytes (14 bits).
|
|
|
|
* |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
|
|
|
|
* String value with length greater than or equal to 16384 bytes.
|
|
|
|
* |1100____| - 1 byte
|
|
|
|
* Integer encoded as int16_t (2 bytes).
|
|
|
|
* |1101____| - 1 byte
|
|
|
|
* Integer encoded as int32_t (4 bytes).
|
|
|
|
* |1110____| - 1 byte
|
|
|
|
* Integer encoded as int64_t (8 bytes).
|
2010-05-21 06:54:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-05-22 09:07:12 -04:00
|
|
|
#include <stdlib.h>
|
2010-05-21 06:54:01 -04:00
|
|
|
#include <string.h>
|
2010-06-01 12:55:37 -04:00
|
|
|
#include <stdint.h>
|
2010-05-21 06:54:01 -04:00
|
|
|
#include <assert.h>
|
2010-05-22 09:07:12 -04:00
|
|
|
#include <limits.h>
|
2010-05-21 06:54:01 -04:00
|
|
|
#include "zmalloc.h"
|
2011-04-27 07:30:32 -04:00
|
|
|
#include "util.h"
|
2010-05-21 06:54:01 -04:00
|
|
|
#include "ziplist.h"
|
2012-02-14 10:11:46 -05:00
|
|
|
#include "endianconv.h"
|
2010-05-21 06:54:01 -04:00
|
|
|
|
2010-05-22 09:18:30 -04:00
|
|
|
#define ZIP_END 255
|
2010-05-22 13:35:26 -04:00
|
|
|
#define ZIP_BIGLEN 254
|
2010-05-22 09:18:30 -04:00
|
|
|
|
2010-08-13 13:28:49 -04:00
|
|
|
/* Different encoding/length possibilities */
|
2012-01-03 18:48:55 -05:00
|
|
|
#define ZIP_STR_MASK (0xc0)
|
|
|
|
#define ZIP_INT_MASK (0x30)
|
2010-08-13 13:28:49 -04:00
|
|
|
#define ZIP_STR_06B (0 << 6)
|
|
|
|
#define ZIP_STR_14B (1 << 6)
|
|
|
|
#define ZIP_STR_32B (2 << 6)
|
|
|
|
#define ZIP_INT_16B (0xc0 | 0<<4)
|
|
|
|
#define ZIP_INT_32B (0xc0 | 1<<4)
|
|
|
|
#define ZIP_INT_64B (0xc0 | 2<<4)
|
2012-04-20 10:38:42 -04:00
|
|
|
#define ZIP_INT_24B (0xc0 | 3<<4)
|
|
|
|
|
|
|
|
#define INT24_MAX 0x7fffff
|
|
|
|
#define INT24_MIN (-INT24_MAX - 1)
|
2010-05-22 09:18:30 -04:00
|
|
|
|
2012-01-03 18:48:55 -05:00
|
|
|
/* Macro to determine type */
|
|
|
|
#define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
|
2010-05-22 09:18:30 -04:00
|
|
|
|
|
|
|
/* Utility macros */
|
2010-06-01 12:55:37 -04:00
|
|
|
#define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
|
|
|
|
#define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
|
|
|
|
#define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
|
|
|
|
#define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
|
|
|
|
#define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
|
2012-02-08 17:20:39 -05:00
|
|
|
#define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
|
|
|
|
#define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
|
2010-06-01 12:55:37 -04:00
|
|
|
|
|
|
|
/* We know a positive increment can only be 1 because entries can only be
|
|
|
|
* pushed one at a time. */
|
2010-05-22 08:58:58 -04:00
|
|
|
#define ZIPLIST_INCR_LENGTH(zl,incr) { \
|
2012-02-08 16:59:35 -05:00
|
|
|
if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
|
2012-02-09 10:28:35 -05:00
|
|
|
ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
|
2012-02-08 16:59:35 -05:00
|
|
|
}
|
2010-05-21 06:54:01 -04:00
|
|
|
|
2010-05-29 10:20:01 -04:00
|
|
|
typedef struct zlentry {
|
|
|
|
unsigned int prevrawlensize, prevrawlen;
|
|
|
|
unsigned int lensize, len;
|
|
|
|
unsigned int headersize;
|
|
|
|
unsigned char encoding;
|
2010-05-29 11:38:23 -04:00
|
|
|
unsigned char *p;
|
2010-05-29 10:20:01 -04:00
|
|
|
} zlentry;
|
|
|
|
|
2012-01-03 18:48:55 -05:00
|
|
|
#define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
|
|
|
|
(encoding) = (ptr[0]) & (ZIP_STR_MASK | ZIP_INT_MASK); \
|
|
|
|
if (((encoding) & ZIP_STR_MASK) < ZIP_STR_MASK) { \
|
|
|
|
/* String encoding: 2 MSBs */ \
|
|
|
|
(encoding) &= ZIP_STR_MASK; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2010-08-13 13:28:49 -04:00
|
|
|
|
2010-05-22 09:18:30 -04:00
|
|
|
/* Return bytes needed to store integer encoded by 'encoding' */
|
2010-08-13 13:28:49 -04:00
|
|
|
static unsigned int zipIntSize(unsigned char encoding) {
|
|
|
|
switch(encoding) {
|
|
|
|
case ZIP_INT_16B: return sizeof(int16_t);
|
2012-04-20 10:38:42 -04:00
|
|
|
case ZIP_INT_24B: return sizeof(int32_t)-sizeof(int8_t);
|
2010-08-13 13:28:49 -04:00
|
|
|
case ZIP_INT_32B: return sizeof(int32_t);
|
|
|
|
case ZIP_INT_64B: return sizeof(int64_t);
|
2010-05-22 09:18:30 -04:00
|
|
|
}
|
|
|
|
assert(NULL);
|
2010-12-23 06:25:56 -05:00
|
|
|
return 0;
|
2010-05-22 09:18:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
|
|
|
|
* the amount of bytes required to encode such a length. */
|
2010-08-13 13:28:49 -04:00
|
|
|
static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
|
|
|
|
unsigned char len = 1, buf[5];
|
|
|
|
|
|
|
|
if (ZIP_IS_STR(encoding)) {
|
|
|
|
/* Although encoding is given it may not be set for strings,
|
|
|
|
* so we determine it here using the raw length. */
|
|
|
|
if (rawlen <= 0x3f) {
|
2010-05-22 09:18:30 -04:00
|
|
|
if (!p) return len;
|
2010-08-13 13:28:49 -04:00
|
|
|
buf[0] = ZIP_STR_06B | rawlen;
|
|
|
|
} else if (rawlen <= 0x3fff) {
|
|
|
|
len += 1;
|
2010-05-22 09:18:30 -04:00
|
|
|
if (!p) return len;
|
2010-08-13 13:28:49 -04:00
|
|
|
buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
|
|
|
|
buf[1] = rawlen & 0xff;
|
2010-05-22 09:18:30 -04:00
|
|
|
} else {
|
|
|
|
len += 4;
|
|
|
|
if (!p) return len;
|
2010-08-13 13:28:49 -04:00
|
|
|
buf[0] = ZIP_STR_32B;
|
|
|
|
buf[1] = (rawlen >> 24) & 0xff;
|
|
|
|
buf[2] = (rawlen >> 16) & 0xff;
|
|
|
|
buf[3] = (rawlen >> 8) & 0xff;
|
|
|
|
buf[4] = rawlen & 0xff;
|
2010-05-22 09:18:30 -04:00
|
|
|
}
|
2010-08-13 13:28:49 -04:00
|
|
|
} else {
|
|
|
|
/* Implies integer encoding, so length is always 1. */
|
|
|
|
if (!p) return len;
|
|
|
|
buf[0] = encoding;
|
2010-05-22 09:18:30 -04:00
|
|
|
}
|
|
|
|
|
2010-08-13 13:28:49 -04:00
|
|
|
/* Store this length at p */
|
2010-05-22 09:18:30 -04:00
|
|
|
memcpy(p,buf,len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-01-03 18:48:55 -05:00
|
|
|
/* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
|
|
|
|
* entries encoding, the 'lensize' variable will hold the number of bytes
|
|
|
|
* required to encode the entries length, and the 'len' variable will hold the
|
|
|
|
* entries length. */
|
|
|
|
#define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
|
|
|
|
ZIP_ENTRY_ENCODING((ptr), (encoding)); \
|
|
|
|
if ((encoding) < ZIP_STR_MASK) { \
|
|
|
|
if ((encoding) == ZIP_STR_06B) { \
|
|
|
|
(lensize) = 1; \
|
|
|
|
(len) = (ptr)[0] & 0x3f; \
|
|
|
|
} else if ((encoding) == ZIP_STR_14B) { \
|
|
|
|
(lensize) = 2; \
|
|
|
|
(len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
|
|
|
|
} else if (encoding == ZIP_STR_32B) { \
|
|
|
|
(lensize) = 5; \
|
|
|
|
(len) = ((ptr)[1] << 24) | \
|
|
|
|
((ptr)[2] << 16) | \
|
|
|
|
((ptr)[3] << 8) | \
|
|
|
|
((ptr)[4]); \
|
|
|
|
} else { \
|
|
|
|
assert(NULL); \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
(lensize) = 1; \
|
|
|
|
(len) = zipIntSize(encoding); \
|
|
|
|
} \
|
|
|
|
} while(0);
|
2010-05-29 13:26:35 -04:00
|
|
|
|
|
|
|
/* Encode the length of the previous entry and write it to "p". Return the
|
|
|
|
* number of bytes needed to encode this length if "p" is NULL. */
|
|
|
|
static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
|
|
|
|
if (p == NULL) {
|
|
|
|
return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
|
|
|
|
} else {
|
|
|
|
if (len < ZIP_BIGLEN) {
|
|
|
|
p[0] = len;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
p[0] = ZIP_BIGLEN;
|
|
|
|
memcpy(p+1,&len,sizeof(len));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev32ifbe(p+1);
|
2010-05-29 13:26:35 -04:00
|
|
|
return 1+sizeof(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-06 17:12:41 -04:00
|
|
|
/* Encode the length of the previous entry and write it to "p". This only
|
|
|
|
* uses the larger encoding (required in __ziplistCascadeUpdate). */
|
|
|
|
static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
|
|
|
|
if (p == NULL) return;
|
|
|
|
p[0] = ZIP_BIGLEN;
|
|
|
|
memcpy(p+1,&len,sizeof(len));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev32ifbe(p+1);
|
2010-09-06 17:12:41 -04:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:48:55 -05:00
|
|
|
/* Decode the number of bytes required to store the length of the previous
|
|
|
|
* element, from the perspective of the entry pointed to by 'ptr'. */
|
|
|
|
#define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
|
|
|
|
if ((ptr)[0] < ZIP_BIGLEN) { \
|
|
|
|
(prevlensize) = 1; \
|
|
|
|
} else { \
|
|
|
|
(prevlensize) = 5; \
|
|
|
|
} \
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
/* Decode the length of the previous element, from the perspective of the entry
|
|
|
|
* pointed to by 'ptr'. */
|
|
|
|
#define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
|
|
|
|
ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
|
|
|
|
if ((prevlensize) == 1) { \
|
|
|
|
(prevlen) = (ptr)[0]; \
|
|
|
|
} else if ((prevlensize) == 5) { \
|
|
|
|
assert(sizeof((prevlensize)) == 4); \
|
|
|
|
memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
|
2012-03-23 07:42:20 -04:00
|
|
|
memrev32ifbe(&prevlen); \
|
2012-01-03 18:48:55 -05:00
|
|
|
} \
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
/* Return the difference in number of bytes needed to store the length of the
|
|
|
|
* previous element 'len', in the entry pointed to by 'p'. */
|
2010-05-29 09:40:40 -04:00
|
|
|
static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
|
|
|
|
unsigned int prevlensize;
|
2012-01-03 18:48:55 -05:00
|
|
|
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
|
|
|
|
return zipPrevEncodeLength(NULL, len) - prevlensize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the total number of bytes used by the entry pointed to by 'p'. */
|
|
|
|
static unsigned int zipRawEntryLength(unsigned char *p) {
|
|
|
|
unsigned int prevlensize, encoding, lensize, len;
|
|
|
|
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
|
|
|
|
ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
|
|
|
|
return prevlensize + lensize + len;
|
2010-05-29 09:40:40 -04:00
|
|
|
}
|
|
|
|
|
2010-05-22 09:18:30 -04:00
|
|
|
/* Check if string pointed to by 'entry' can be encoded as an integer.
|
2010-07-27 09:26:08 -04:00
|
|
|
* Stores the integer value in 'v' and its encoding in 'encoding'. */
|
|
|
|
static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
|
2010-05-22 09:18:30 -04:00
|
|
|
long long value;
|
|
|
|
|
2010-07-27 09:26:08 -04:00
|
|
|
if (entrylen >= 32 || entrylen == 0) return 0;
|
2011-04-27 07:30:32 -04:00
|
|
|
if (string2ll((char*)entry,entrylen,&value)) {
|
2010-07-27 09:26:08 -04:00
|
|
|
/* Great, the string can be encoded. Check what's the smallest
|
|
|
|
* of our encoding types that can hold this value. */
|
2010-06-01 12:55:37 -04:00
|
|
|
if (value >= INT16_MIN && value <= INT16_MAX) {
|
2010-08-13 13:28:49 -04:00
|
|
|
*encoding = ZIP_INT_16B;
|
2012-04-20 10:38:42 -04:00
|
|
|
} else if (value >= INT24_MIN && value <= INT24_MAX) {
|
|
|
|
*encoding = ZIP_INT_24B;
|
2010-06-01 12:55:37 -04:00
|
|
|
} else if (value >= INT32_MIN && value <= INT32_MAX) {
|
2010-08-13 13:28:49 -04:00
|
|
|
*encoding = ZIP_INT_32B;
|
2010-05-22 09:18:30 -04:00
|
|
|
} else {
|
2010-08-13 13:28:49 -04:00
|
|
|
*encoding = ZIP_INT_64B;
|
2010-05-22 09:18:30 -04:00
|
|
|
}
|
|
|
|
*v = value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store integer 'value' at 'p', encoded as 'encoding' */
|
2010-06-01 12:55:37 -04:00
|
|
|
static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
|
|
|
|
int16_t i16;
|
|
|
|
int32_t i32;
|
|
|
|
int64_t i64;
|
2010-08-13 13:28:49 -04:00
|
|
|
if (encoding == ZIP_INT_16B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
i16 = value;
|
|
|
|
memcpy(p,&i16,sizeof(i16));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev16ifbe(p);
|
2012-04-20 10:38:42 -04:00
|
|
|
} else if (encoding == ZIP_INT_24B) {
|
|
|
|
i32 = value<<8;
|
|
|
|
memrev32ifbe(&i32);
|
|
|
|
memcpy(p,((unsigned char*)&i32)+1,sizeof(i32)-sizeof(int8_t));
|
2010-08-13 13:28:49 -04:00
|
|
|
} else if (encoding == ZIP_INT_32B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
i32 = value;
|
|
|
|
memcpy(p,&i32,sizeof(i32));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev32ifbe(p);
|
2010-08-13 13:28:49 -04:00
|
|
|
} else if (encoding == ZIP_INT_64B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
i64 = value;
|
|
|
|
memcpy(p,&i64,sizeof(i64));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev64ifbe(p);
|
2010-05-22 09:18:30 -04:00
|
|
|
} else {
|
|
|
|
assert(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read integer encoded as 'encoding' from 'p' */
|
2010-06-01 12:55:37 -04:00
|
|
|
static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
|
|
|
|
int16_t i16;
|
|
|
|
int32_t i32;
|
2010-12-23 06:25:56 -05:00
|
|
|
int64_t i64, ret = 0;
|
2010-08-13 13:28:49 -04:00
|
|
|
if (encoding == ZIP_INT_16B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
memcpy(&i16,p,sizeof(i16));
|
2011-03-09 12:49:59 -05:00
|
|
|
memrev16ifbe(&i16);
|
2010-06-01 12:55:37 -04:00
|
|
|
ret = i16;
|
2010-08-13 13:28:49 -04:00
|
|
|
} else if (encoding == ZIP_INT_32B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
memcpy(&i32,p,sizeof(i32));
|
2012-02-09 11:09:01 -05:00
|
|
|
memrev32ifbe(&i32);
|
2010-06-01 12:55:37 -04:00
|
|
|
ret = i32;
|
2012-04-20 10:38:42 -04:00
|
|
|
} else if (encoding == ZIP_INT_24B) {
|
|
|
|
i32 = 0;
|
|
|
|
memcpy(((unsigned char*)&i32)+1,p,sizeof(i32)-sizeof(int8_t));
|
|
|
|
memrev32ifbe(&i32);
|
|
|
|
ret = i32>>8;
|
2010-08-13 13:28:49 -04:00
|
|
|
} else if (encoding == ZIP_INT_64B) {
|
2010-06-01 12:55:37 -04:00
|
|
|
memcpy(&i64,p,sizeof(i64));
|
2012-02-09 10:28:35 -05:00
|
|
|
memrev64ifbe(&i64);
|
2010-06-01 12:55:37 -04:00
|
|
|
ret = i64;
|
2010-05-22 09:18:30 -04:00
|
|
|
} else {
|
|
|
|
assert(NULL);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-29 10:20:01 -04:00
|
|
|
/* Return a struct with all information about an entry. */
|
|
|
|
static zlentry zipEntry(unsigned char *p) {
|
|
|
|
zlentry e;
|
2012-01-03 18:48:55 -05:00
|
|
|
|
|
|
|
ZIP_DECODE_PREVLEN(p, e.prevrawlensize, e.prevrawlen);
|
|
|
|
ZIP_DECODE_LENGTH(p + e.prevrawlensize, e.encoding, e.lensize, e.len);
|
|
|
|
e.headersize = e.prevrawlensize + e.lensize;
|
2010-05-29 11:38:23 -04:00
|
|
|
e.p = p;
|
2010-05-29 10:20:01 -04:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2010-05-21 06:54:01 -04:00
|
|
|
/* Create a new empty ziplist. */
|
|
|
|
unsigned char *ziplistNew(void) {
|
|
|
|
unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
|
|
|
|
unsigned char *zl = zmalloc(bytes);
|
2012-02-08 16:59:35 -05:00
|
|
|
ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);
|
|
|
|
ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);
|
2010-05-21 06:54:01 -04:00
|
|
|
ZIPLIST_LENGTH(zl) = 0;
|
|
|
|
zl[bytes-1] = ZIP_END;
|
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-05-22 09:18:30 -04:00
|
|
|
/* Resize the ziplist. */
|
2010-05-21 06:54:01 -04:00
|
|
|
static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
|
2010-05-22 09:18:30 -04:00
|
|
|
zl = zrealloc(zl,len);
|
2012-02-08 16:59:35 -05:00
|
|
|
ZIPLIST_BYTES(zl) = intrev32ifbe(len);
|
2010-05-21 06:54:01 -04:00
|
|
|
zl[len-1] = ZIP_END;
|
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-09-06 17:12:41 -04:00
|
|
|
/* When an entry is inserted, we need to set the prevlen field of the next
|
|
|
|
* entry to equal the length of the inserted entry. It can occur that this
|
|
|
|
* length cannot be encoded in 1 byte and the next entry needs to be grow
|
|
|
|
* a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
|
|
|
|
* because this only happens when an entry is already being inserted (which
|
|
|
|
* causes a realloc and memmove). However, encoding the prevlen may require
|
|
|
|
* that this entry is grown as well. This effect may cascade throughout
|
|
|
|
* the ziplist when there are consecutive entries with a size close to
|
|
|
|
* ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
|
|
|
|
* consecutive entry.
|
|
|
|
*
|
|
|
|
* Note that this effect can also happen in reverse, where the bytes required
|
|
|
|
* to encode the prevlen field can shrink. This effect is deliberately ignored,
|
|
|
|
* because it can cause a "flapping" effect where a chain prevlen fields is
|
|
|
|
* first grown and then shrunk again after consecutive inserts. Rather, the
|
|
|
|
* field is allowed to stay larger than necessary, because a large prevlen
|
|
|
|
* field implies the ziplist is holding large entries anyway.
|
|
|
|
*
|
|
|
|
* The pointer "p" points to the first entry that does NOT need to be
|
|
|
|
* updated, i.e. consecutive fields MAY need an update. */
|
|
|
|
static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
|
2012-02-08 16:59:35 -05:00
|
|
|
size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), rawlen, rawlensize;
|
2011-03-13 13:15:57 -04:00
|
|
|
size_t offset, noffset, extra;
|
2010-09-06 17:12:41 -04:00
|
|
|
unsigned char *np;
|
|
|
|
zlentry cur, next;
|
|
|
|
|
|
|
|
while (p[0] != ZIP_END) {
|
|
|
|
cur = zipEntry(p);
|
|
|
|
rawlen = cur.headersize + cur.len;
|
|
|
|
rawlensize = zipPrevEncodeLength(NULL,rawlen);
|
|
|
|
|
|
|
|
/* Abort if there is no next entry. */
|
|
|
|
if (p[rawlen] == ZIP_END) break;
|
|
|
|
next = zipEntry(p+rawlen);
|
|
|
|
|
|
|
|
/* Abort when "prevlen" has not changed. */
|
|
|
|
if (next.prevrawlen == rawlen) break;
|
|
|
|
|
|
|
|
if (next.prevrawlensize < rawlensize) {
|
|
|
|
/* The "prevlen" field of "next" needs more bytes to hold
|
|
|
|
* the raw length of "cur". */
|
|
|
|
offset = p-zl;
|
|
|
|
extra = rawlensize-next.prevrawlensize;
|
|
|
|
zl = ziplistResize(zl,curlen+extra);
|
|
|
|
p = zl+offset;
|
|
|
|
|
2011-05-05 08:55:51 -04:00
|
|
|
/* Current pointer and offset for next element. */
|
2010-09-06 17:12:41 -04:00
|
|
|
np = p+rawlen;
|
|
|
|
noffset = np-zl;
|
2011-05-05 08:55:51 -04:00
|
|
|
|
|
|
|
/* Update tail offset when next element is not the tail element. */
|
2012-02-09 10:28:35 -05:00
|
|
|
if ((zl+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) != np) {
|
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
|
|
|
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);
|
|
|
|
}
|
2011-05-05 08:55:51 -04:00
|
|
|
|
|
|
|
/* Move the tail to the back. */
|
2010-09-06 17:12:41 -04:00
|
|
|
memmove(np+rawlensize,
|
|
|
|
np+next.prevrawlensize,
|
|
|
|
curlen-noffset-next.prevrawlensize-1);
|
|
|
|
zipPrevEncodeLength(np,rawlen);
|
|
|
|
|
|
|
|
/* Advance the cursor */
|
|
|
|
p += rawlen;
|
2010-10-14 15:11:42 -04:00
|
|
|
curlen += extra;
|
2010-09-06 17:12:41 -04:00
|
|
|
} else {
|
|
|
|
if (next.prevrawlensize > rawlensize) {
|
|
|
|
/* This would result in shrinking, which we want to avoid.
|
|
|
|
* So, set "rawlen" in the available bytes. */
|
|
|
|
zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
|
|
|
|
} else {
|
|
|
|
zipPrevEncodeLength(p+rawlen,rawlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop here, as the raw length of "next" has not changed. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-05-29 11:38:23 -04:00
|
|
|
/* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
|
2010-05-31 15:44:12 -04:00
|
|
|
static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
|
2010-05-29 11:38:23 -04:00
|
|
|
unsigned int i, totlen, deleted = 0;
|
2011-03-13 13:15:57 -04:00
|
|
|
size_t offset;
|
|
|
|
int nextdiff = 0;
|
2010-09-06 17:12:41 -04:00
|
|
|
zlentry first, tail;
|
|
|
|
|
|
|
|
first = zipEntry(p);
|
2010-05-29 11:38:23 -04:00
|
|
|
for (i = 0; p[0] != ZIP_END && i < num; i++) {
|
|
|
|
p += zipRawEntryLength(p);
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
|
|
|
|
totlen = p-first.p;
|
|
|
|
if (totlen > 0) {
|
|
|
|
if (p[0] != ZIP_END) {
|
|
|
|
/* Tricky: storing the prevlen in this entry might reduce or
|
|
|
|
* increase the number of bytes needed, compared to the current
|
|
|
|
* prevlen. Note that we can always store this length because
|
|
|
|
* it was previously stored by an entry that is being deleted. */
|
|
|
|
nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
|
2010-05-29 13:26:35 -04:00
|
|
|
zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
|
2010-05-29 11:38:23 -04:00
|
|
|
|
|
|
|
/* Update offset for tail */
|
2012-02-09 10:28:35 -05:00
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
|
|
|
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen);
|
2010-09-06 17:12:41 -04:00
|
|
|
|
|
|
|
/* When the tail contains more than one entry, we need to take
|
|
|
|
* "nextdiff" in account as well. Otherwise, a change in the
|
|
|
|
* size of prevlen doesn't have an effect on the *tail* offset. */
|
|
|
|
tail = zipEntry(p);
|
2012-02-09 10:28:35 -05:00
|
|
|
if (p[tail.headersize+tail.len] != ZIP_END) {
|
2012-02-09 10:36:25 -05:00
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
2012-02-09 10:28:35 -05:00
|
|
|
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
|
|
|
|
}
|
2010-05-29 11:38:23 -04:00
|
|
|
|
|
|
|
/* Move tail to the front of the ziplist */
|
2012-02-08 16:59:35 -05:00
|
|
|
memmove(first.p,p-nextdiff,
|
|
|
|
intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1+nextdiff);
|
2010-05-29 11:38:23 -04:00
|
|
|
} else {
|
|
|
|
/* The entire tail was deleted. No need to move memory. */
|
2012-02-08 16:59:35 -05:00
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
|
|
|
intrev32ifbe((first.p-zl)-first.prevrawlen);
|
2010-05-29 11:38:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize and update length */
|
2010-09-06 17:12:41 -04:00
|
|
|
offset = first.p-zl;
|
2012-02-08 16:59:35 -05:00
|
|
|
zl = ziplistResize(zl, intrev32ifbe(ZIPLIST_BYTES(zl))-totlen+nextdiff);
|
2010-05-29 11:38:23 -04:00
|
|
|
ZIPLIST_INCR_LENGTH(zl,-deleted);
|
2010-09-06 17:12:41 -04:00
|
|
|
p = zl+offset;
|
|
|
|
|
|
|
|
/* When nextdiff != 0, the raw length of the next entry has changed, so
|
|
|
|
* we need to cascade the update throughout the ziplist */
|
|
|
|
if (nextdiff != 0)
|
|
|
|
zl = __ziplistCascadeUpdate(zl,p);
|
2010-05-29 11:38:23 -04:00
|
|
|
}
|
|
|
|
return zl;
|
2010-05-21 06:54:01 -04:00
|
|
|
}
|
|
|
|
|
2010-05-29 12:52:49 -04:00
|
|
|
/* Insert item at "p". */
|
2010-05-31 15:44:12 -04:00
|
|
|
static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
|
2012-02-08 16:59:35 -05:00
|
|
|
size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen, prevlen = 0;
|
2011-03-13 13:15:57 -04:00
|
|
|
size_t offset;
|
|
|
|
int nextdiff = 0;
|
2010-08-13 13:28:49 -04:00
|
|
|
unsigned char encoding = 0;
|
2011-10-23 04:57:01 -04:00
|
|
|
long long value = 123456789; /* initialized to avoid warning. Using a value
|
|
|
|
that is easy to see if for some reason
|
|
|
|
we use it uninitialized. */
|
2010-09-06 17:12:41 -04:00
|
|
|
zlentry entry, tail;
|
2010-05-21 06:54:01 -04:00
|
|
|
|
2010-05-29 12:52:49 -04:00
|
|
|
/* Find out prevlen for the entry that is inserted. */
|
|
|
|
if (p[0] != ZIP_END) {
|
|
|
|
entry = zipEntry(p);
|
|
|
|
prevlen = entry.prevrawlen;
|
2010-05-29 09:40:40 -04:00
|
|
|
} else {
|
2010-09-06 17:12:41 -04:00
|
|
|
unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
|
|
|
|
if (ptail[0] != ZIP_END) {
|
|
|
|
prevlen = zipRawEntryLength(ptail);
|
2010-05-29 12:52:49 -04:00
|
|
|
}
|
2010-05-29 09:40:40 -04:00
|
|
|
}
|
|
|
|
|
2010-05-22 09:07:12 -04:00
|
|
|
/* See if the entry can be encoded */
|
2010-07-27 09:26:08 -04:00
|
|
|
if (zipTryEncoding(s,slen,&value,&encoding)) {
|
2010-08-13 13:28:49 -04:00
|
|
|
/* 'encoding' is set to the appropriate integer encoding */
|
|
|
|
reqlen = zipIntSize(encoding);
|
2010-05-22 09:07:12 -04:00
|
|
|
} else {
|
2010-08-13 13:28:49 -04:00
|
|
|
/* 'encoding' is untouched, however zipEncodeLength will use the
|
|
|
|
* string length to figure out how to encode it. */
|
2010-05-29 12:52:49 -04:00
|
|
|
reqlen = slen;
|
2010-05-22 09:07:12 -04:00
|
|
|
}
|
2010-05-29 09:40:40 -04:00
|
|
|
/* We need space for both the length of the previous entry and
|
|
|
|
* the length of the payload. */
|
2010-05-29 13:26:35 -04:00
|
|
|
reqlen += zipPrevEncodeLength(NULL,prevlen);
|
2010-05-29 12:52:49 -04:00
|
|
|
reqlen += zipEncodeLength(NULL,encoding,slen);
|
|
|
|
|
|
|
|
/* When the insert position is not equal to the tail, we need to
|
|
|
|
* make sure that the next entry can hold this entry's length in
|
|
|
|
* its prevlen field. */
|
2010-05-29 19:37:03 -04:00
|
|
|
nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
|
2010-05-29 12:52:49 -04:00
|
|
|
|
|
|
|
/* Store offset because a realloc may change the address of zl. */
|
|
|
|
offset = p-zl;
|
|
|
|
zl = ziplistResize(zl,curlen+reqlen+nextdiff);
|
|
|
|
p = zl+offset;
|
|
|
|
|
|
|
|
/* Apply memory move when necessary and update tail offset. */
|
|
|
|
if (p[0] != ZIP_END) {
|
|
|
|
/* Subtract one because of the ZIP_END bytes */
|
|
|
|
memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
|
2010-09-06 17:12:41 -04:00
|
|
|
|
2010-05-29 12:52:49 -04:00
|
|
|
/* Encode this entry's raw length in the next entry. */
|
2010-05-29 13:26:35 -04:00
|
|
|
zipPrevEncodeLength(p+reqlen,reqlen);
|
2010-09-06 17:12:41 -04:00
|
|
|
|
2010-05-29 12:52:49 -04:00
|
|
|
/* Update offset for tail */
|
2012-02-09 10:28:35 -05:00
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
|
|
|
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);
|
2010-09-06 17:12:41 -04:00
|
|
|
|
|
|
|
/* When the tail contains more than one entry, we need to take
|
|
|
|
* "nextdiff" in account as well. Otherwise, a change in the
|
|
|
|
* size of prevlen doesn't have an effect on the *tail* offset. */
|
|
|
|
tail = zipEntry(p+reqlen);
|
2012-02-09 10:28:35 -05:00
|
|
|
if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
|
|
|
|
ZIPLIST_TAIL_OFFSET(zl) =
|
|
|
|
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
|
|
|
|
}
|
2010-05-21 06:54:01 -04:00
|
|
|
} else {
|
2010-05-29 12:52:49 -04:00
|
|
|
/* This element will be the new tail. */
|
2012-02-08 16:59:35 -05:00
|
|
|
ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
|
2010-05-29 09:40:40 -04:00
|
|
|
}
|
|
|
|
|
2010-09-06 17:12:41 -04:00
|
|
|
/* When nextdiff != 0, the raw length of the next entry has changed, so
|
|
|
|
* we need to cascade the update throughout the ziplist */
|
|
|
|
if (nextdiff != 0) {
|
|
|
|
offset = p-zl;
|
|
|
|
zl = __ziplistCascadeUpdate(zl,p+reqlen);
|
|
|
|
p = zl+offset;
|
|
|
|
}
|
|
|
|
|
2010-05-21 06:54:01 -04:00
|
|
|
/* Write the entry */
|
2010-05-29 13:26:35 -04:00
|
|
|
p += zipPrevEncodeLength(p,prevlen);
|
2010-05-29 12:52:49 -04:00
|
|
|
p += zipEncodeLength(p,encoding,slen);
|
2010-08-13 13:28:49 -04:00
|
|
|
if (ZIP_IS_STR(encoding)) {
|
2010-05-29 12:52:49 -04:00
|
|
|
memcpy(p,s,slen);
|
2010-08-13 13:28:49 -04:00
|
|
|
} else {
|
|
|
|
zipSaveInteger(p,value,encoding);
|
2010-05-22 09:07:12 -04:00
|
|
|
}
|
2010-05-22 08:58:58 -04:00
|
|
|
ZIPLIST_INCR_LENGTH(zl,1);
|
2010-05-21 06:54:01 -04:00
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-05-31 15:44:12 -04:00
|
|
|
unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
|
2010-05-29 12:52:49 -04:00
|
|
|
unsigned char *p;
|
2010-05-29 13:09:37 -04:00
|
|
|
p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
|
2010-05-29 12:52:49 -04:00
|
|
|
return __ziplistInsert(zl,p,s,slen);
|
|
|
|
}
|
|
|
|
|
2010-05-29 16:17:16 -04:00
|
|
|
/* Returns an offset to use for iterating with ziplistNext. When the given
|
|
|
|
* index is negative, the list is traversed back to front. When the list
|
|
|
|
* doesn't contain an element at the provided index, NULL is returned. */
|
|
|
|
unsigned char *ziplistIndex(unsigned char *zl, int index) {
|
|
|
|
unsigned char *p;
|
|
|
|
zlentry entry;
|
|
|
|
if (index < 0) {
|
|
|
|
index = (-index)-1;
|
|
|
|
p = ZIPLIST_ENTRY_TAIL(zl);
|
|
|
|
if (p[0] != ZIP_END) {
|
|
|
|
entry = zipEntry(p);
|
|
|
|
while (entry.prevrawlen > 0 && index--) {
|
|
|
|
p -= entry.prevrawlen;
|
|
|
|
entry = zipEntry(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = ZIPLIST_ENTRY_HEAD(zl);
|
|
|
|
while (p[0] != ZIP_END && index--) {
|
|
|
|
p += zipRawEntryLength(p);
|
|
|
|
}
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
2010-05-29 19:37:03 -04:00
|
|
|
return (p[0] == ZIP_END || index > 0) ? NULL : p;
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
|
2010-12-07 10:33:13 -05:00
|
|
|
/* Return pointer to next entry in ziplist.
|
|
|
|
*
|
|
|
|
* zl is the pointer to the ziplist
|
|
|
|
* p is the pointer to the current element
|
|
|
|
*
|
|
|
|
* The element after 'p' is returned, otherwise NULL if we are at the end. */
|
2010-05-31 14:17:21 -04:00
|
|
|
unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
|
|
|
|
((void) zl);
|
2010-05-31 17:35:21 -04:00
|
|
|
|
|
|
|
/* "p" could be equal to ZIP_END, caused by ziplistDelete,
|
|
|
|
* and we should return NULL. Otherwise, we should return NULL
|
|
|
|
* when the *next* element is ZIP_END (there is no next entry). */
|
|
|
|
if (p[0] == ZIP_END) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-03 18:48:55 -05:00
|
|
|
|
|
|
|
p += zipRawEntryLength(p);
|
|
|
|
if (p[0] == ZIP_END) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2010-05-22 09:48:27 -04:00
|
|
|
}
|
|
|
|
|
2010-05-29 19:38:56 -04:00
|
|
|
/* Return pointer to previous entry in ziplist. */
|
2010-05-31 14:17:21 -04:00
|
|
|
unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
|
|
|
|
zlentry entry;
|
|
|
|
|
|
|
|
/* Iterating backwards from ZIP_END should return the tail. When "p" is
|
|
|
|
* equal to the first element of the list, we're already at the head,
|
|
|
|
* and should return NULL. */
|
|
|
|
if (p[0] == ZIP_END) {
|
|
|
|
p = ZIPLIST_ENTRY_TAIL(zl);
|
|
|
|
return (p[0] == ZIP_END) ? NULL : p;
|
|
|
|
} else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
entry = zipEntry(p);
|
2010-09-06 17:12:41 -04:00
|
|
|
assert(entry.prevrawlen > 0);
|
2010-05-31 14:17:21 -04:00
|
|
|
return p-entry.prevrawlen;
|
|
|
|
}
|
2010-05-29 19:38:56 -04:00
|
|
|
}
|
|
|
|
|
2010-05-22 09:48:27 -04:00
|
|
|
/* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
|
|
|
|
* on the encoding of the entry. 'e' is always set to NULL to be able
|
|
|
|
* to find out whether the string pointer or the integer value was set.
|
|
|
|
* Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
|
2010-05-31 15:44:12 -04:00
|
|
|
unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
|
2010-05-29 10:20:01 -04:00
|
|
|
zlentry entry;
|
2010-05-29 16:17:16 -04:00
|
|
|
if (p == NULL || p[0] == ZIP_END) return 0;
|
2010-05-29 10:26:55 -04:00
|
|
|
if (sstr) *sstr = NULL;
|
2010-05-29 09:40:40 -04:00
|
|
|
|
2010-05-29 10:20:01 -04:00
|
|
|
entry = zipEntry(p);
|
2010-08-13 13:28:49 -04:00
|
|
|
if (ZIP_IS_STR(entry.encoding)) {
|
2010-05-29 10:26:55 -04:00
|
|
|
if (sstr) {
|
|
|
|
*slen = entry.len;
|
2010-05-31 15:44:12 -04:00
|
|
|
*sstr = p+entry.headersize;
|
2010-05-22 09:48:27 -04:00
|
|
|
}
|
|
|
|
} else {
|
2010-05-29 10:26:55 -04:00
|
|
|
if (sval) {
|
|
|
|
*sval = zipLoadInteger(p+entry.headersize,entry.encoding);
|
2010-05-22 09:48:27 -04:00
|
|
|
}
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
2010-05-22 09:48:27 -04:00
|
|
|
return 1;
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
|
2010-05-29 19:38:56 -04:00
|
|
|
/* Insert an entry at "p". */
|
2010-05-31 15:44:12 -04:00
|
|
|
unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
|
2010-05-29 19:38:56 -04:00
|
|
|
return __ziplistInsert(zl,p,s,slen);
|
2010-05-21 08:11:29 -04:00
|
|
|
}
|
|
|
|
|
2010-05-21 09:46:00 -04:00
|
|
|
/* Delete a single entry from the ziplist, pointed to by *p.
|
|
|
|
* Also update *p in place, to be able to iterate over the
|
|
|
|
* ziplist, while deleting entries. */
|
2010-05-31 14:18:23 -04:00
|
|
|
unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
|
2011-03-13 13:15:57 -04:00
|
|
|
size_t offset = *p-zl;
|
2010-05-29 11:38:23 -04:00
|
|
|
zl = __ziplistDelete(zl,*p,1);
|
2010-05-21 09:46:00 -04:00
|
|
|
|
2010-05-29 11:38:23 -04:00
|
|
|
/* Store pointer to current element in p, because ziplistDelete will
|
2010-05-29 19:39:41 -04:00
|
|
|
* do a realloc which might result in a different "zl"-pointer.
|
|
|
|
* When the delete direction is back to front, we might delete the last
|
|
|
|
* entry and end up with "p" pointing to ZIP_END, so check this. */
|
2010-05-31 14:18:23 -04:00
|
|
|
*p = zl+offset;
|
2010-05-21 09:46:00 -04:00
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-05-29 19:38:56 -04:00
|
|
|
/* Delete a range of entries from the ziplist. */
|
|
|
|
unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
|
|
|
|
unsigned char *p = ziplistIndex(zl,index);
|
|
|
|
return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
|
|
|
|
}
|
|
|
|
|
2010-05-22 10:25:06 -04:00
|
|
|
/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
|
2010-05-31 15:44:12 -04:00
|
|
|
unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
|
2010-05-29 10:20:01 -04:00
|
|
|
zlentry entry;
|
2010-05-31 15:44:12 -04:00
|
|
|
unsigned char sencoding;
|
|
|
|
long long zval, sval;
|
2010-05-29 19:37:03 -04:00
|
|
|
if (p[0] == ZIP_END) return 0;
|
2010-05-22 10:25:06 -04:00
|
|
|
|
2010-05-29 10:20:01 -04:00
|
|
|
entry = zipEntry(p);
|
2010-08-13 13:28:49 -04:00
|
|
|
if (ZIP_IS_STR(entry.encoding)) {
|
2010-05-22 10:25:06 -04:00
|
|
|
/* Raw compare */
|
2010-05-29 10:20:01 -04:00
|
|
|
if (entry.len == slen) {
|
2010-05-29 10:26:55 -04:00
|
|
|
return memcmp(p+entry.headersize,sstr,slen) == 0;
|
2010-05-22 10:25:06 -04:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-22 15:49:36 -04:00
|
|
|
} else {
|
2010-05-29 10:22:24 -04:00
|
|
|
/* Try to compare encoded values */
|
2010-07-27 09:26:08 -04:00
|
|
|
if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
|
2010-05-29 10:22:24 -04:00
|
|
|
if (entry.encoding == sencoding) {
|
2010-05-31 15:44:12 -04:00
|
|
|
zval = zipLoadInteger(p+entry.headersize,entry.encoding);
|
|
|
|
return zval == sval;
|
2010-05-29 10:22:24 -04:00
|
|
|
}
|
2010-05-22 15:49:36 -04:00
|
|
|
}
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
2010-05-22 15:49:36 -04:00
|
|
|
return 0;
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:48:55 -05:00
|
|
|
/* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
|
|
|
|
* between every comparison. Returns NULL when the field could not be found. */
|
|
|
|
unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
|
|
|
|
int skipcnt = 0;
|
|
|
|
unsigned char vencoding = 0;
|
|
|
|
long long vll = 0;
|
|
|
|
|
|
|
|
while (p[0] != ZIP_END) {
|
|
|
|
unsigned int prevlensize, encoding, lensize, len;
|
|
|
|
unsigned char *q;
|
|
|
|
|
|
|
|
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
|
|
|
|
ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
|
|
|
|
q = p + prevlensize + lensize;
|
|
|
|
|
|
|
|
if (skipcnt == 0) {
|
|
|
|
/* Compare current entry with specified entry */
|
|
|
|
if (ZIP_IS_STR(encoding)) {
|
|
|
|
if (len == vlen && memcmp(q, vstr, vlen) == 0) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Find out if the specified entry can be encoded */
|
|
|
|
if (vencoding == 0) {
|
|
|
|
/* UINT_MAX when the entry CANNOT be encoded */
|
|
|
|
if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
|
|
|
|
vencoding = UCHAR_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be non-zero by now */
|
|
|
|
assert(vencoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare current entry with specified entry */
|
|
|
|
if (encoding == vencoding) {
|
|
|
|
long long ll = zipLoadInteger(q, encoding);
|
|
|
|
if (ll == vll) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset skip count */
|
|
|
|
skipcnt = skip;
|
|
|
|
} else {
|
|
|
|
/* Skip entry */
|
|
|
|
skipcnt--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to next entry */
|
|
|
|
p = q + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-22 13:35:40 -04:00
|
|
|
/* Return length of ziplist. */
|
|
|
|
unsigned int ziplistLen(unsigned char *zl) {
|
|
|
|
unsigned int len = 0;
|
2012-02-08 16:59:35 -05:00
|
|
|
if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {
|
|
|
|
len = intrev16ifbe(ZIPLIST_LENGTH(zl));
|
2010-05-22 13:35:40 -04:00
|
|
|
} else {
|
|
|
|
unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
|
|
|
|
while (*p != ZIP_END) {
|
|
|
|
p += zipRawEntryLength(p);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-store length if small enough */
|
2012-02-08 16:59:35 -05:00
|
|
|
if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);
|
2010-05-22 13:35:40 -04:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-02-28 08:48:49 -05:00
|
|
|
/* Return ziplist blob size in bytes. */
|
|
|
|
size_t ziplistBlobLen(unsigned char *zl) {
|
2012-02-08 16:59:35 -05:00
|
|
|
return intrev32ifbe(ZIPLIST_BYTES(zl));
|
2010-05-29 05:33:38 -04:00
|
|
|
}
|
|
|
|
|
2010-05-21 06:54:01 -04:00
|
|
|
void ziplistRepr(unsigned char *zl) {
|
2010-05-29 10:25:05 -04:00
|
|
|
unsigned char *p;
|
2010-09-06 17:12:41 -04:00
|
|
|
int index = 0;
|
2010-05-29 10:25:05 -04:00
|
|
|
zlentry entry;
|
2010-05-21 06:54:01 -04:00
|
|
|
|
2010-09-06 17:12:41 -04:00
|
|
|
printf(
|
|
|
|
"{total bytes %d} "
|
|
|
|
"{length %u}\n"
|
|
|
|
"{tail offset %u}\n",
|
2012-02-08 16:59:35 -05:00
|
|
|
intrev32ifbe(ZIPLIST_BYTES(zl)),
|
|
|
|
intrev16ifbe(ZIPLIST_LENGTH(zl)),
|
|
|
|
intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));
|
2010-05-29 13:09:37 -04:00
|
|
|
p = ZIPLIST_ENTRY_HEAD(zl);
|
2010-05-21 06:54:01 -04:00
|
|
|
while(*p != ZIP_END) {
|
2010-05-29 10:25:05 -04:00
|
|
|
entry = zipEntry(p);
|
2010-09-06 17:12:41 -04:00
|
|
|
printf(
|
|
|
|
"{"
|
|
|
|
"addr 0x%08lx, "
|
|
|
|
"index %2d, "
|
|
|
|
"offset %5ld, "
|
|
|
|
"rl: %5u, "
|
|
|
|
"hs %2u, "
|
|
|
|
"pl: %5u, "
|
|
|
|
"pls: %2u, "
|
|
|
|
"payload %5u"
|
|
|
|
"} ",
|
2010-11-02 06:15:09 -04:00
|
|
|
(long unsigned)p,
|
2010-09-06 17:12:41 -04:00
|
|
|
index,
|
2010-11-02 06:15:09 -04:00
|
|
|
(unsigned long) (p-zl),
|
2010-09-06 17:12:41 -04:00
|
|
|
entry.headersize+entry.len,
|
|
|
|
entry.headersize,
|
|
|
|
entry.prevrawlen,
|
|
|
|
entry.prevrawlensize,
|
|
|
|
entry.len);
|
2010-05-29 10:25:05 -04:00
|
|
|
p += entry.headersize;
|
2010-08-13 13:28:49 -04:00
|
|
|
if (ZIP_IS_STR(entry.encoding)) {
|
2010-09-06 17:12:41 -04:00
|
|
|
if (entry.len > 40) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
|
2010-09-06 17:12:41 -04:00
|
|
|
printf("...");
|
|
|
|
} else {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (entry.len &&
|
|
|
|
fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
|
2010-09-06 17:12:41 -04:00
|
|
|
}
|
2010-05-22 09:07:12 -04:00
|
|
|
} else {
|
2010-07-01 15:13:38 -04:00
|
|
|
printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
|
2010-05-22 09:07:12 -04:00
|
|
|
}
|
2010-05-21 06:54:01 -04:00
|
|
|
printf("\n");
|
2010-05-29 10:25:05 -04:00
|
|
|
p += entry.len;
|
2010-09-06 17:12:41 -04:00
|
|
|
index++;
|
2010-05-21 06:54:01 -04:00
|
|
|
}
|
|
|
|
printf("{end}\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ZIPLIST_TEST_MAIN
|
2010-06-01 11:57:51 -04:00
|
|
|
#include <sys/time.h>
|
2010-10-14 15:11:42 -04:00
|
|
|
#include "adlist.h"
|
|
|
|
#include "sds.h"
|
|
|
|
|
|
|
|
#define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
|
2010-05-21 06:54:01 -04:00
|
|
|
|
2010-05-21 07:16:08 -04:00
|
|
|
unsigned char *createList() {
|
|
|
|
unsigned char *zl = ziplistNew();
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
|
|
|
|
zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
|
|
|
|
zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
|
|
|
|
zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
|
2010-05-21 07:16:08 -04:00
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-05-22 09:07:12 -04:00
|
|
|
unsigned char *createIntList() {
|
|
|
|
unsigned char *zl = ziplistNew();
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
sprintf(buf, "100");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
|
2010-05-22 09:07:12 -04:00
|
|
|
sprintf(buf, "128000");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
|
2010-05-22 09:07:12 -04:00
|
|
|
sprintf(buf, "-100");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
|
2010-05-22 09:07:12 -04:00
|
|
|
sprintf(buf, "4294967296");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
|
2010-05-22 09:07:12 -04:00
|
|
|
sprintf(buf, "non integer");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
|
2010-05-22 09:07:12 -04:00
|
|
|
sprintf(buf, "much much longer non integer");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
|
2010-05-22 09:07:12 -04:00
|
|
|
return zl;
|
|
|
|
}
|
|
|
|
|
2010-06-01 11:57:51 -04:00
|
|
|
long long usec(void) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stress(int pos, int num, int maxsize, int dnum) {
|
|
|
|
int i,j,k;
|
|
|
|
unsigned char *zl;
|
|
|
|
char posstr[2][5] = { "HEAD", "TAIL" };
|
|
|
|
long long start;
|
|
|
|
for (i = 0; i < maxsize; i+=dnum) {
|
|
|
|
zl = ziplistNew();
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do num times a push+pop from pos */
|
|
|
|
start = usec();
|
|
|
|
for (k = 0; k < num; k++) {
|
|
|
|
zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
|
|
|
|
zl = ziplistDeleteRange(zl,0,1);
|
|
|
|
}
|
|
|
|
printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
|
2012-02-08 16:59:35 -05:00
|
|
|
i,intrev32ifbe(ZIPLIST_BYTES(zl)),num,posstr[pos],usec()-start);
|
2010-06-01 11:57:51 -04:00
|
|
|
zfree(zl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 05:36:58 -04:00
|
|
|
void pop(unsigned char *zl, int where) {
|
|
|
|
unsigned char *p, *vstr;
|
|
|
|
unsigned int vlen;
|
|
|
|
long long vlong;
|
|
|
|
|
|
|
|
p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
|
|
|
|
if (ziplistGet(p,&vstr,&vlen,&vlong)) {
|
|
|
|
if (where == ZIPLIST_HEAD)
|
|
|
|
printf("Pop head: ");
|
|
|
|
else
|
|
|
|
printf("Pop tail: ");
|
|
|
|
|
|
|
|
if (vstr)
|
2010-11-02 06:15:09 -04:00
|
|
|
if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
|
2010-06-09 05:36:58 -04:00
|
|
|
else
|
|
|
|
printf("%lld", vlong);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
ziplistDeleteRange(zl,-1,1);
|
|
|
|
} else {
|
|
|
|
printf("ERROR: Could not pop\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:55:51 -04:00
|
|
|
int randstring(char *target, unsigned int min, unsigned int max) {
|
2010-10-14 15:11:42 -04:00
|
|
|
int p, len = min+rand()%(max-min+1);
|
|
|
|
int minval, maxval;
|
|
|
|
switch(rand() % 3) {
|
|
|
|
case 0:
|
|
|
|
minval = 0;
|
|
|
|
maxval = 255;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
minval = 48;
|
|
|
|
maxval = 122;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
minval = 48;
|
|
|
|
maxval = 52;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(p < len)
|
|
|
|
target[p++] = minval+rand()%(maxval-minval+1);
|
2011-05-05 08:55:51 -04:00
|
|
|
return len;
|
2010-10-14 15:11:42 -04:00
|
|
|
}
|
|
|
|
|
2010-05-21 07:16:08 -04:00
|
|
|
int main(int argc, char **argv) {
|
2010-05-29 13:34:14 -04:00
|
|
|
unsigned char *zl, *p;
|
2010-06-01 11:57:09 -04:00
|
|
|
unsigned char *entry;
|
2010-05-21 07:29:14 -04:00
|
|
|
unsigned int elen;
|
2010-05-22 09:48:27 -04:00
|
|
|
long long value;
|
2010-05-21 07:16:08 -04:00
|
|
|
|
2010-09-06 18:08:42 -04:00
|
|
|
/* If an argument is given, use it as the random seed. */
|
|
|
|
if (argc == 2)
|
|
|
|
srand(atoi(argv[1]));
|
|
|
|
|
2010-05-22 09:07:12 -04:00
|
|
|
zl = createIntList();
|
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-05-21 07:16:08 -04:00
|
|
|
zl = createList();
|
2010-05-21 06:54:01 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-06-09 05:36:58 -04:00
|
|
|
pop(zl,ZIPLIST_TAIL);
|
2010-05-21 06:54:01 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-06-09 05:36:58 -04:00
|
|
|
pop(zl,ZIPLIST_HEAD);
|
2010-05-21 06:54:01 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-06-09 05:36:58 -04:00
|
|
|
pop(zl,ZIPLIST_TAIL);
|
2010-05-29 09:40:40 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-06-09 05:36:58 -04:00
|
|
|
pop(zl,ZIPLIST_TAIL);
|
2010-05-29 09:40:40 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
|
2010-05-29 16:17:16 -04:00
|
|
|
printf("Get element at index 3:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, 3);
|
|
|
|
if (!ziplistGet(p, &entry, &elen, &value)) {
|
|
|
|
printf("ERROR: Could not access index 3\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-29 16:17:16 -04:00
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("%lld\n", value);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Get element at index 4 (out of range):\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, 4);
|
|
|
|
if (p == NULL) {
|
|
|
|
printf("No entry\n");
|
|
|
|
} else {
|
|
|
|
printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Get element at index -1 (last element):\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, -1);
|
|
|
|
if (!ziplistGet(p, &entry, &elen, &value)) {
|
|
|
|
printf("ERROR: Could not access index -1\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-29 16:17:16 -04:00
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("%lld\n", value);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Get element at index -4 (first element):\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, -4);
|
|
|
|
if (!ziplistGet(p, &entry, &elen, &value)) {
|
|
|
|
printf("ERROR: Could not access index -4\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-29 16:17:16 -04:00
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("%lld\n", value);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Get element at index -5 (reverse out of range):\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, -5);
|
|
|
|
if (p == NULL) {
|
|
|
|
printf("No entry\n");
|
|
|
|
} else {
|
|
|
|
printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2010-05-21 07:16:08 -04:00
|
|
|
printf("Iterate list from 0 to end:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, 0);
|
2010-05-22 09:48:27 -04:00
|
|
|
while (ziplistGet(p, &entry, &elen, &value)) {
|
2010-05-21 07:29:14 -04:00
|
|
|
printf("Entry: ");
|
2010-05-22 09:48:27 -04:00
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-22 09:48:27 -04:00
|
|
|
} else {
|
|
|
|
printf("%lld", value);
|
|
|
|
}
|
2010-05-31 14:17:21 -04:00
|
|
|
p = ziplistNext(zl,p);
|
2010-05-22 09:48:27 -04:00
|
|
|
printf("\n");
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Iterate list from 1 to end:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, 1);
|
2010-05-22 09:48:27 -04:00
|
|
|
while (ziplistGet(p, &entry, &elen, &value)) {
|
2010-05-21 07:29:14 -04:00
|
|
|
printf("Entry: ");
|
2010-05-22 09:48:27 -04:00
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-22 09:48:27 -04:00
|
|
|
} else {
|
|
|
|
printf("%lld", value);
|
|
|
|
}
|
2010-05-31 14:17:21 -04:00
|
|
|
p = ziplistNext(zl,p);
|
2010-05-22 09:48:27 -04:00
|
|
|
printf("\n");
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Iterate list from 2 to end:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, 2);
|
2010-05-22 09:48:27 -04:00
|
|
|
while (ziplistGet(p, &entry, &elen, &value)) {
|
2010-05-21 07:29:14 -04:00
|
|
|
printf("Entry: ");
|
2010-05-22 09:48:27 -04:00
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-22 09:48:27 -04:00
|
|
|
} else {
|
|
|
|
printf("%lld", value);
|
|
|
|
}
|
2010-05-31 14:17:21 -04:00
|
|
|
p = ziplistNext(zl,p);
|
2010-05-22 09:48:27 -04:00
|
|
|
printf("\n");
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Iterate starting out of range:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-22 09:48:27 -04:00
|
|
|
p = ziplistIndex(zl, 4);
|
|
|
|
if (!ziplistGet(p, &entry, &elen, &value)) {
|
2010-05-21 07:16:08 -04:00
|
|
|
printf("No entry\n");
|
|
|
|
} else {
|
|
|
|
printf("ERROR\n");
|
|
|
|
}
|
2010-05-21 08:11:29 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2010-05-29 19:39:41 -04:00
|
|
|
printf("Iterate from back to front:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, -1);
|
|
|
|
while (ziplistGet(p, &entry, &elen, &value)) {
|
|
|
|
printf("Entry: ");
|
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-29 19:39:41 -04:00
|
|
|
} else {
|
|
|
|
printf("%lld", value);
|
|
|
|
}
|
2010-05-31 14:17:21 -04:00
|
|
|
p = ziplistPrev(zl,p);
|
2010-05-29 19:39:41 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Iterate from back to front, deleting all items:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
|
|
|
p = ziplistIndex(zl, -1);
|
|
|
|
while (ziplistGet(p, &entry, &elen, &value)) {
|
|
|
|
printf("Entry: ");
|
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
|
2010-05-29 19:39:41 -04:00
|
|
|
} else {
|
|
|
|
printf("%lld", value);
|
|
|
|
}
|
2010-05-31 14:17:21 -04:00
|
|
|
zl = ziplistDelete(zl,&p);
|
|
|
|
p = ziplistPrev(zl,p);
|
2010-05-29 19:39:41 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2010-05-21 08:11:29 -04:00
|
|
|
printf("Delete inclusive range 0,0:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-21 09:44:47 -04:00
|
|
|
zl = ziplistDeleteRange(zl, 0, 1);
|
2010-05-21 08:11:29 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Delete inclusive range 0,1:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-21 09:44:47 -04:00
|
|
|
zl = ziplistDeleteRange(zl, 0, 2);
|
2010-05-21 08:11:29 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Delete inclusive range 1,2:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-21 09:44:47 -04:00
|
|
|
zl = ziplistDeleteRange(zl, 1, 2);
|
2010-05-21 08:11:29 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Delete with start index out of range:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-21 09:44:47 -04:00
|
|
|
zl = ziplistDeleteRange(zl, 5, 1);
|
2010-05-21 08:11:29 -04:00
|
|
|
ziplistRepr(zl);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Delete with num overflow:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-05-21 09:44:47 -04:00
|
|
|
zl = ziplistDeleteRange(zl, 1, 5);
|
2010-05-21 08:11:29 -04:00
|
|
|
ziplistRepr(zl);
|
2010-05-21 07:16:08 -04:00
|
|
|
}
|
|
|
|
|
2010-05-21 09:46:00 -04:00
|
|
|
printf("Delete foo while iterating:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-06-01 11:57:09 -04:00
|
|
|
p = ziplistIndex(zl,0);
|
|
|
|
while (ziplistGet(p,&entry,&elen,&value)) {
|
|
|
|
if (entry && strncmp("foo",(char*)entry,elen) == 0) {
|
2010-05-21 09:46:00 -04:00
|
|
|
printf("Delete foo\n");
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistDelete(zl,&p);
|
2010-05-21 09:46:00 -04:00
|
|
|
} else {
|
|
|
|
printf("Entry: ");
|
2010-05-22 09:48:27 -04:00
|
|
|
if (entry) {
|
2010-11-02 06:15:09 -04:00
|
|
|
if (elen && fwrite(entry,elen,1,stdout) == 0)
|
|
|
|
perror("fwrite");
|
2010-05-22 09:48:27 -04:00
|
|
|
} else {
|
2010-06-01 11:57:09 -04:00
|
|
|
printf("%lld",value);
|
2010-05-22 09:48:27 -04:00
|
|
|
}
|
2010-06-01 11:57:09 -04:00
|
|
|
p = ziplistNext(zl,p);
|
2010-05-22 09:48:27 -04:00
|
|
|
printf("\n");
|
2010-05-21 09:46:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
ziplistRepr(zl);
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
|
|
|
|
2010-09-23 16:04:19 -04:00
|
|
|
printf("Regression test for >255 byte strings:\n");
|
|
|
|
{
|
|
|
|
char v1[257],v2[257];
|
|
|
|
memset(v1,'x',256);
|
|
|
|
memset(v2,'y',256);
|
|
|
|
zl = ziplistNew();
|
|
|
|
zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
|
|
|
|
zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
|
|
|
|
|
|
|
|
/* Pop values again and compare their value. */
|
|
|
|
p = ziplistIndex(zl,0);
|
|
|
|
assert(ziplistGet(p,&entry,&elen,&value));
|
2010-10-14 15:11:42 -04:00
|
|
|
assert(strncmp(v1,(char*)entry,elen) == 0);
|
2010-09-23 16:04:19 -04:00
|
|
|
p = ziplistIndex(zl,1);
|
|
|
|
assert(ziplistGet(p,&entry,&elen,&value));
|
2010-10-14 15:11:42 -04:00
|
|
|
assert(strncmp(v2,(char*)entry,elen) == 0);
|
2010-09-23 16:04:19 -04:00
|
|
|
printf("SUCCESS\n\n");
|
|
|
|
}
|
|
|
|
|
2010-05-29 21:06:12 -04:00
|
|
|
printf("Create long list and check indices:\n");
|
|
|
|
{
|
|
|
|
zl = ziplistNew();
|
|
|
|
char buf[32];
|
|
|
|
int i,len;
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
len = sprintf(buf,"%d",i);
|
2010-06-01 11:57:09 -04:00
|
|
|
zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
|
2010-05-29 21:06:12 -04:00
|
|
|
}
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
p = ziplistIndex(zl,i);
|
|
|
|
assert(ziplistGet(p,NULL,NULL,&value));
|
|
|
|
assert(i == value);
|
|
|
|
|
|
|
|
p = ziplistIndex(zl,-i-1);
|
|
|
|
assert(ziplistGet(p,NULL,NULL,&value));
|
|
|
|
assert(999-i == value);
|
|
|
|
}
|
|
|
|
printf("SUCCESS\n\n");
|
|
|
|
}
|
|
|
|
|
2010-05-22 10:25:06 -04:00
|
|
|
printf("Compare strings with ziplist entries:\n");
|
|
|
|
{
|
|
|
|
zl = createList();
|
2010-06-01 11:57:09 -04:00
|
|
|
p = ziplistIndex(zl,0);
|
|
|
|
if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
|
2010-05-29 09:40:40 -04:00
|
|
|
printf("ERROR: not \"hello\"\n");
|
2010-05-29 13:34:14 -04:00
|
|
|
return 1;
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
2010-06-01 11:57:09 -04:00
|
|
|
if (ziplistCompare(p,(unsigned char*)"hella",5)) {
|
2010-05-29 09:40:40 -04:00
|
|
|
printf("ERROR: \"hella\"\n");
|
2010-05-29 13:34:14 -04:00
|
|
|
return 1;
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
|
|
|
|
2010-06-01 11:57:09 -04:00
|
|
|
p = ziplistIndex(zl,3);
|
|
|
|
if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
|
2010-05-29 09:40:40 -04:00
|
|
|
printf("ERROR: not \"1024\"\n");
|
2010-05-29 13:34:14 -04:00
|
|
|
return 1;
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
2010-06-01 11:57:09 -04:00
|
|
|
if (ziplistCompare(p,(unsigned char*)"1025",4)) {
|
2010-05-29 09:40:40 -04:00
|
|
|
printf("ERROR: \"1025\"\n");
|
2010-05-29 13:34:14 -04:00
|
|
|
return 1;
|
2010-05-22 10:25:06 -04:00
|
|
|
}
|
2010-09-06 17:12:41 -04:00
|
|
|
printf("SUCCESS\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Stress with random payloads of different encoding:\n");
|
|
|
|
{
|
2010-10-14 15:11:42 -04:00
|
|
|
int i,j,len,where;
|
2010-09-06 17:12:41 -04:00
|
|
|
unsigned char *p;
|
2010-10-14 15:11:42 -04:00
|
|
|
char buf[1024];
|
2011-05-05 08:55:51 -04:00
|
|
|
int buflen;
|
2010-10-14 15:11:42 -04:00
|
|
|
list *ref;
|
|
|
|
listNode *refnode;
|
|
|
|
|
|
|
|
/* Hold temp vars from ziplist */
|
|
|
|
unsigned char *sstr;
|
|
|
|
unsigned int slen;
|
|
|
|
long long sval;
|
|
|
|
|
|
|
|
for (i = 0; i < 20000; i++) {
|
|
|
|
zl = ziplistNew();
|
|
|
|
ref = listCreate();
|
|
|
|
listSetFreeMethod(ref,sdsfree);
|
|
|
|
len = rand() % 256;
|
|
|
|
|
|
|
|
/* Create lists */
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
|
2011-05-05 08:55:51 -04:00
|
|
|
if (rand() % 2) {
|
|
|
|
buflen = randstring(buf,1,sizeof(buf)-1);
|
|
|
|
} else {
|
|
|
|
switch(rand() % 3) {
|
|
|
|
case 0:
|
|
|
|
buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
buflen = sprintf(buf,"%lld",(0LL + rand()));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(NULL);
|
|
|
|
}
|
2010-10-14 15:11:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add to ziplist */
|
2011-05-05 08:55:51 -04:00
|
|
|
zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
|
2010-09-06 17:12:41 -04:00
|
|
|
|
2010-10-14 15:11:42 -04:00
|
|
|
/* Add to reference list */
|
|
|
|
if (where == ZIPLIST_HEAD) {
|
2011-05-05 08:55:51 -04:00
|
|
|
listAddNodeHead(ref,sdsnewlen(buf, buflen));
|
2010-10-14 15:11:42 -04:00
|
|
|
} else if (where == ZIPLIST_TAIL) {
|
2011-05-05 08:55:51 -04:00
|
|
|
listAddNodeTail(ref,sdsnewlen(buf, buflen));
|
2010-10-14 15:11:42 -04:00
|
|
|
} else {
|
|
|
|
assert(NULL);
|
|
|
|
}
|
2010-09-06 17:12:41 -04:00
|
|
|
}
|
|
|
|
|
2010-10-14 15:11:42 -04:00
|
|
|
assert(listLength(ref) == ziplistLen(zl));
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
/* Naive way to get elements, but similar to the stresser
|
|
|
|
* executed from the Tcl test suite. */
|
|
|
|
p = ziplistIndex(zl,j);
|
|
|
|
refnode = listIndex(ref,j);
|
|
|
|
|
|
|
|
assert(ziplistGet(p,&sstr,&slen,&sval));
|
|
|
|
if (sstr == NULL) {
|
2011-05-05 08:55:51 -04:00
|
|
|
buflen = sprintf(buf,"%lld",sval);
|
2010-10-14 15:11:42 -04:00
|
|
|
} else {
|
2011-05-05 08:55:51 -04:00
|
|
|
buflen = slen;
|
|
|
|
memcpy(buf,sstr,buflen);
|
|
|
|
buf[buflen] = '\0';
|
2010-10-14 15:11:42 -04:00
|
|
|
}
|
2011-05-05 08:55:51 -04:00
|
|
|
assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
|
2010-10-14 15:11:42 -04:00
|
|
|
}
|
|
|
|
zfree(zl);
|
|
|
|
listRelease(ref);
|
2010-09-06 17:12:41 -04:00
|
|
|
}
|
|
|
|
printf("SUCCESS\n\n");
|
2010-05-21 09:46:00 -04:00
|
|
|
}
|
|
|
|
|
2010-06-01 11:57:51 -04:00
|
|
|
printf("Stress with variable ziplist size:\n");
|
|
|
|
{
|
|
|
|
stress(ZIPLIST_HEAD,100000,16384,256);
|
|
|
|
stress(ZIPLIST_TAIL,100000,16384,256);
|
|
|
|
}
|
|
|
|
|
2010-05-21 06:54:01 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-01 11:57:51 -04:00
|
|
|
|
2010-05-21 06:54:01 -04:00
|
|
|
#endif
|