/* Measure getting _nonexistent_ values (lookup failure). */ static void get_nonexistent(void) { skiplist *sl = skiplist_new(intptr_cmp, NULL, NULL); for (intptr_t i=0; i < lim; i++) { skiplist_add(sl, (void *) i, (void *) i); } TIME(pre); for (intptr_t i=0; i < lim; i++) { intptr_t k = (i * largeish_prime) + lim; intptr_t v = 0; skiplist_get(sl, (void *) k, (void **)&v); assert(v == 0); if (0) { printf("%lu %lu\n", k, v); } } TIME(post); TDIFF(); skiplist_free(sl, NULL, NULL); }
static void set_and_get(void) { skiplist *sl = skiplist_new(intptr_cmp, NULL, NULL); TIME(pre); for (intptr_t i=0; i < lim; i++) { intptr_t k = i % (lim / 2); skiplist_set(sl, (void *) k, (void *) k, NULL); } for (intptr_t i=0; i < lim; i++) { intptr_t k = (i * largeish_prime) % (lim / 2); intptr_t v = (intptr_t)0; skiplist_get(sl, (void *) k, (void **)&v); if (0) { printf("%lu %lu\n", k, v); } assert(v == k); } TIME(post); TDIFF(); skiplist_free(sl, NULL, NULL); }
void case_skiplist_base() { struct skiplist *skiplist = skiplist(NULL); assert(skiplist != NULL); assert(skiplist->len == 0); assert(skiplist->head != NULL); assert(skiplist->tail == skiplist->head); assert(skiplist->level == 1); assert(skiplist_push(skiplist, 2, NULL) == SKIPLIST_OK); assert(skiplist_push(skiplist, 1, NULL) == SKIPLIST_OK); assert(skiplist_push(skiplist, 3, NULL) == SKIPLIST_OK); assert(skiplist_len(skiplist) == 3); assert(skiplist_level(skiplist) <= SKIPLIST_LEVEL_MAX); assert(skiplist->tail->score == 3); skiplist_clear(skiplist); assert(skiplist_level(skiplist) == 1); assert(skiplist_len(skiplist) == 0); assert(skiplist->head != NULL); assert(skiplist->tail == skiplist->head); skiplist_free(skiplist); }
void case_skiplist_get() { struct skiplist *skiplist = skiplist(NULL); int v1 = 1, v2 = 2, v3 = 3, v4 = 4, v5 = 5, v6 = 6, v7 = 7, v8 = 8, v9 = 9; assert(skiplist_push(skiplist, 3, (void *)(&v3)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 9, (void *)(&v9)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 7, (void *)(&v7)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 2, (void *)(&v2)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 6, (void *)(&v6)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 5, (void *)(&v5)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 8, (void *)(&v8)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 1, (void *)(&v1)) == SKIPLIST_OK); assert(skiplist_push(skiplist, 4, (void *)(&v4)) == SKIPLIST_OK); assert(skiplist_len(skiplist) == 9); assert(skiplist_get(skiplist, 8) == &v8); assert(skiplist_get(skiplist, 9) == &v9); assert(skiplist_get(skiplist, 1) == &v1); assert(skiplist_get(skiplist, 2) == &v2); assert(skiplist_get(skiplist, 10) == NULL); assert(skiplist_get(skiplist, 9999) == NULL); skiplist_free(skiplist); }
void _flush_list(struct sst *sst, struct skipnode *x, struct skipnode *hdr, int flush_count) { int pos = 0; int count = flush_count; struct skipnode *cur = x; struct skipnode *first = hdr; struct skiplist *merge = NULL; struct meta_node *meta_info = NULL; while(cur != first) { meta_info = meta_get(sst->meta, cur->key); /* If m is NULL, cur->key more larger than meta's largest area * need to create new index-file */ if(!meta_info) { /* If merge is NULL, it has no merge */ if(merge) { struct skipnode *h = merge->hdr->forward[0]; _flush_merge_list(sst, h, merge->count, NULL); skiplist_free(merge); merge = NULL; } /* Flush the last nodes to disk */ _flush_new_list(sst, x, count - pos); return ; } else { /* If m is not NULL, means found the index of the cur * We need: * 1) compare the sst->name with meta index name * a)If 0: add the cur to merge, and continue * b)others: * b1)Flush the merge list to disk * b2)Open the meta's mmap, and load all blocks to new merge, add cur to merge */ int cmp = strcmp(sst->name, meta_info->index_name); if(cmp == 0) { if(!merge) merge = _read_mmap(sst, count); skiplist_insert_node(merge, cur); } else { if(merge) { struct skipnode *h = merge->hdr->forward[0]; _flush_merge_list(sst, h, merge->count, meta_info); skiplist_free(merge); merge = NULL; } memset(sst->name, 0, FILE_NAME_SIZE); memcpy(sst->name, meta_info->index_name, FILE_NAME_SIZE); merge = _read_mmap(sst, count); /* Add to merge list */ skiplist_insert_node(merge, cur); } } pos++; cur = cur->forward[0]; } if(merge) { struct skipnode *h = merge->hdr->forward[0]; _flush_merge_list(sst, h, merge->count, meta_info); skiplist_free(merge); } }