static void sd_v_test(void) { sdbuild b; sd_buildinit(&b); t( sd_buildbegin(&b, &st_r.r, 1, 0, 0) == 0); int i = 7; int j = 8; addv(&b, &st_r.r, 3, 0, &i); addv(&b, &st_r.r, 4, 0, &j); sd_buildend(&b, &st_r.r); ssbuf buf; ss_bufinit(&buf); ssbuf xfbuf; ss_bufinit(&xfbuf); t( ss_bufensure(&xfbuf, &st_r.a, 1024) == 0 ); t( sd_commitpage(&b, &st_r.r, &buf) == 0 ); sdpageheader *h = (sdpageheader*)buf.s; sdpage page; sd_pageinit(&page, h); ssiter it; ss_iterinit(sd_pageiter, &it); ss_iteropen(sd_pageiter, &it, &st_r.r, &xfbuf, &page, SS_GTE, NULL, 0); t( ss_iteratorhas(&it) != 0 ); sv *v = ss_iteratorof(&it); t( v != NULL ); t( *(int*)sv_key(v, &st_r.r, 0) == i ); t( sv_lsn(v) == 3 ); t( sv_flags(v) == 0 ); ss_iteratornext(&it); t( ss_iteratorhas(&it) != 0 ); v = ss_iteratorof(&it); t( v != NULL ); t( *(int*)sv_key(v, &st_r.r, 0) == j ); t( sv_lsn(v) == 4 ); t( sv_flags(v) == 0 ); ss_iteratornext(&it); v = ss_iteratorof(&it); t( v == NULL ); sd_buildfree(&b, &st_r.r); ss_buffree(&buf, &st_r.a); ss_buffree(&xfbuf, &st_r.a); }
static void sv_mergeiter_merge_dup_a_chain(void) { stlist vlista; stlist vlistb; st_listinit(&vlista, 0); st_listinit(&vlistb, 0); int key = 7; int i = 0; int lsn = 5; while (i < 5) { st_sv(&st_r.g, &vlista, lsn, 0 | ((i > 0) ? SVDUP: 0), key); i++; lsn--; } ssiter ita; ss_iterinit(ss_bufiterref, &ita); ss_iteropen(ss_bufiterref, &ita, &vlista.list, sizeof(sv*)); ssiter itb; ss_iterinit(ss_bufiterref, &itb); ss_iteropen(ss_bufiterref, &itb, &vlistb.list, sizeof(sv*)); svmerge m; sv_mergeinit(&m); sv_mergeprepare(&m, &st_r.r, 2); svmergesrc *s = sv_mergeadd(&m, NULL); t(s != NULL); s->src = ita; s = sv_mergeadd(&m, NULL); t(s != NULL); s->src = itb; ssiter merge; ss_iterinit(sv_mergeiter, &merge); ss_iteropen(sv_mergeiter, &merge, &st_r.r, &m, SS_GTE); i = 0; while (ss_iteratorhas(&merge)) { sv *v = (sv*)ss_iteratorof(&merge); t( *(int*)sv_key(v, &st_r.r, 0) == key ); if (i == 0) { t( sv_flags(v) == 0 ); } else { t( (sv_flags(v) | sv_mergeisdup(&merge)) == (0|SVDUP) ); } ss_iteratornext(&merge); i++; } t( i == 5 ); ss_iteratorclose(&merge); sv_mergefree(&m, &st_r.a); st_listfree(&vlista, &st_r.r); st_listfree(&vlistb, &st_r.r); }
static void sv_mergeiter_merge_ba(void) { stlist vlista; stlist vlistb; st_listinit(&vlista, 0); st_listinit(&vlistb, 0); int i = 0; while (i < 5) { st_sv(&st_r.g, &vlista, i, 0, i); i++; } while (i < 10) { st_sv(&st_r.g, &vlistb, i, 0, i); i++; } ssiter ita; ss_iterinit(ss_bufiterref, &ita); ss_iteropen(ss_bufiterref, &ita, &vlista.list, sizeof(sv*)); ssiter itb; ss_iterinit(ss_bufiterref, &itb); ss_iteropen(ss_bufiterref, &itb, &vlistb.list, sizeof(sv*)); svmerge m; sv_mergeinit(&m); sv_mergeprepare(&m, &st_r.r, 3); svmergesrc *s = sv_mergeadd(&m, NULL); t(s != NULL); s->src = ita; s = sv_mergeadd(&m, NULL); t(s != NULL); s->src = itb; ssiter merge; ss_iterinit(sv_mergeiter, &merge); ss_iteropen(sv_mergeiter, &merge, &st_r.r, &m, SS_GTE); i = 0; while (ss_iteratorhas(&merge)) { sv *v = (sv*)ss_iteratorof(&merge); t( *(int*)sv_key(v, &st_r.r, 0) == i ); t( sv_lsn(v) == i ); t( sv_flags(v) == 0 ); ss_iteratornext(&merge); i++; } t( i == 10 ); ss_iteratorclose(&merge); sv_mergefree(&m, &st_r.a); st_listfree(&vlista, &st_r.r); st_listfree(&vlistb, &st_r.r); }
static inline int sd_buildadd_keyvalue(sdbuild *b, sr *r, sv *v) { /* calculate key size */ uint32_t keysize = 0; int i = 0; while (i < r->scheme->count) { keysize += sv_keysize(v, r, i); i++; } uint32_t valuesize = sv_valuesize(v, r); uint32_t size = keysize + valuesize; /* prepare buffer */ uint64_t lsn = sv_lsn(v); uint32_t sizemeta = ss_leb128size(size) + ss_leb128size(lsn); int rc = ss_bufensure(&b->v, r->a, sizemeta); if (ssunlikely(rc == -1)) return sr_oom(r->e); /* write meta */ ss_bufadvance(&b->v, ss_leb128write(b->v.p, size)); ss_bufadvance(&b->v, ss_leb128write(b->v.p, lsn)); /* write key-parts */ i = 0; for (; i < r->scheme->count; i++) { uint32_t partsize = sv_keysize(v, r, i); char *part = sv_key(v, r, i); int offsetstart = ss_bufused(&b->k); int offset = (offsetstart - sd_buildref(b)->k); /* match a key copy */ int is_duplicate = 0; uint32_t hash = 0; int pos = 0; if (b->compress_dup) { hash = ss_fnv(part, partsize); pos = sd_buildsearch(&b->tracker, hash, part, partsize, b); if (b->tracker.i[pos]) { is_duplicate = 1; sdbuildkey *ref = sscast(b->tracker.i[pos], sdbuildkey, node); offset = ref->offset; } } /* offset */ rc = ss_bufensure(&b->v, r->a, ss_leb128size(offset)); if (ssunlikely(rc == -1)) return sr_oom(r->e); ss_bufadvance(&b->v, ss_leb128write(b->v.p, offset)); if (is_duplicate) continue; /* copy key */ int partsize_meta = ss_leb128size(partsize); rc = ss_bufensure(&b->k, r->a, partsize_meta + partsize); if (ssunlikely(rc == -1)) return sr_oom(r->e); ss_bufadvance(&b->k, ss_leb128write(b->k.p, partsize)); memcpy(b->k.p, part, partsize); ss_bufadvance(&b->k, partsize); /* add key reference */ if (b->compress_dup) { if (ssunlikely(ss_htisfull(&b->tracker))) { rc = ss_htresize(&b->tracker, r->a); if (ssunlikely(rc == -1)) return sr_oom(r->e); } sdbuildkey *ref = ss_malloc(r->a, sizeof(sdbuildkey)); if (ssunlikely(rc == -1)) return sr_oom(r->e); ref->node.hash = hash; ref->offset = offset; ref->offsetstart = offsetstart + partsize_meta; ref->size = partsize; ss_htset(&b->tracker, pos, &ref->node); } } /* write value */ rc = ss_bufensure(&b->v, r->a, valuesize); if (ssunlikely(rc == -1)) return sr_oom(r->e); memcpy(b->v.p, sv_value(v, r), valuesize); ss_bufadvance(&b->v, valuesize); return 0; }
t( sr_filenew(&f, "./0000.db") == 0 ); t( sd_commit(&b, &r, &index, &f) == 0 ); srmap map; t( sr_mapfile(&map, &f, 1) == 0 ); sdindex i; sd_indexinit(&i); i.h = (sdindexheader*)(map.p); sriter it; sr_iterinit(sd_iter, &it, &r); sr_iteropen(sd_iter, &it, &i, map.p, 1, 0, NULL); t( sr_iteratorhas(&it) == 1 ); sv *v = sr_iteratorof(&it); t( *(int*)sv_key(v, &r, 0) == 7); sr_iteratornext(&it); v = sr_iteratorof(&it); t( *(int*)sv_key(v, &r, 0) == 8); sr_iteratornext(&it); v = sr_iteratorof(&it); t( *(int*)sv_key(v, &r, 0) == 9); sr_iteratornext(&it); t( sr_iteratorhas(&it) == 0 ); sr_iteratorclose(&it); sr_fileclose(&f); t( sr_mapunmap(&map) == 0 ); t( sr_fileunlink("./0000.db") == 0 ); sd_indexfree(&index, &r);
t( sr_filenew(&f, "./0000.db") == 0 ); t( sd_buildwrite(&b, &r, &index, &f) == 0 ); srmap map; t( sr_mapfile(&map, &f, 1) == 0 ); sdindex i; sd_indexinit(&i); i.h = (sdindexheader*)(map.p); sriter it; sr_iterinit(sd_iter, &it, &r); sr_iteropen(sd_iter, &it, &i, map.p, 1, 0, NULL); t( sr_iteratorhas(&it) == 1 ); sv *v = sr_iteratorof(&it); t( *(int*)sv_key(v) == 7); sr_iteratornext(&it); v = sr_iteratorof(&it); t( *(int*)sv_key(v) == 8); sr_iteratornext(&it); v = sr_iteratorof(&it); t( *(int*)sv_key(v) == 9); sr_iteratornext(&it); t( sr_iteratorhas(&it) == 0 ); sr_iteratorclose(&it); sr_fileclose(&f); t( sr_mapunmap(&map) == 0 ); t( sr_fileunlink("./0000.db") == 0 ); sd_indexfree(&index, &r);