/* * 创建一个 INTSET 编码的集合对象 */ robj *createIntsetObject(void) { intset *is = intsetNew(); robj *o = createObject(REDIS_SET,is); o->encoding = REDIS_ENCODING_INTSET; return o; }
intset *createSet(int bits, int size) { uint64_t mask = (1<<bits)-1; uint64_t i, value; intset *is = intsetNew(); for (i = 0; i < size; i++) { if (bits > 32) { value = (rand()*rand()) & mask; } else { value = rand() & mask; } is = intsetAdd(is,value,NULL); } return is; }
int main(int argc, char **argv) { uint8_t success; int i; intset *is; sranddev(); printf("Value encodings: "); { assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16); assert(_intsetValueEncoding(+32767) == INTSET_ENC_INT16); assert(_intsetValueEncoding(-32769) == INTSET_ENC_INT32); assert(_intsetValueEncoding(+32768) == INTSET_ENC_INT32); assert(_intsetValueEncoding(-2147483648) == INTSET_ENC_INT32); assert(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32); assert(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64); assert(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64); assert(_intsetValueEncoding(-9223372036854775808ull) == INTSET_ENC_INT64); assert(_intsetValueEncoding(+9223372036854775807ull) == INTSET_ENC_INT64); ok(); } printf("Basic adding: "); { is = intsetNew(); is = intsetAdd(is,5,&success); assert(success); is = intsetAdd(is,6,&success); assert(success); is = intsetAdd(is,4,&success); assert(success); is = intsetAdd(is,4,&success); assert(!success); ok(); } printf("Large number of random adds: "); { int inserts = 0; is = intsetNew(); for (i = 0; i < 1024; i++) { is = intsetAdd(is,rand()%0x800,&success); if (success) inserts++; } assert(is->length == inserts); checkConsistency(is); ok(); } printf("Upgrade from int16 to int32: "); { is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); assert(intsetFind(is,32)); assert(intsetFind(is,65535)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,-65535,NULL); assert(is->encoding == INTSET_ENC_INT32); assert(intsetFind(is,32)); assert(intsetFind(is,-65535)); checkConsistency(is); ok(); } printf("Upgrade from int16 to int64: "); { is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,32)); assert(intsetFind(is,4294967295)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,-4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,32)); assert(intsetFind(is,-4294967295)); checkConsistency(is); ok(); } printf("Upgrade from int32 to int64: "); { is = intsetNew(); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); is = intsetAdd(is,4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,65535)); assert(intsetFind(is,4294967295)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); is = intsetAdd(is,-4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,65535)); assert(intsetFind(is,-4294967295)); checkConsistency(is); ok(); } printf("Stress lookups: "); { long num = 100000, size = 10000; int i, bits = 20; long long start; is = createSet(bits,size); checkConsistency(is); start = usec(); for (i = 0; i < num; i++) intsetSearch(is,rand() % ((1<<bits)-1),NULL); printf("%ld lookups, %ld element set, %lldusec\n",num,size,usec()-start); } printf("Stress add+delete: "); { int i, v1, v2; is = intsetNew(); for (i = 0; i < 0xffff; i++) { v1 = rand() % 0xfff; is = intsetAdd(is,v1,NULL); assert(intsetFind(is,v1)); v2 = rand() % 0xfff; is = intsetRemove(is,v2,NULL); assert(!intsetFind(is,v2)); } checkConsistency(is); ok(); } }
//创建一个intset编码的集合对象 robj *createIntsetObject(void) { intset *is = intsetNew(); //创建一个整数集合 robj *o = createObject(OBJ_SET,is); //创建一个对象,对象的数据类型为OBJ_SET o->encoding = OBJ_ENCODING_INTSET; //对象的编码类型OBJ_ENCODING_INTSET return o; }