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 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); }
int sd_buildadd(sdbuild *b, sr *r, sv *v, uint32_t flags) { /* prepare metadata reference */ int rc = sr_bufensure(&b->k, r->a, sizeof(sdv)); if (srunlikely(rc == -1)) return sr_error(r->e, "%s", "memory allocation failed"); sdpageheader *h = sd_buildheader(b); sdv *sv = (sdv*)b->k.p; sv->lsn = sv_lsn(v); sv->flags = sv_flags(v) | flags; sv->size = sv_size(v); sv->offset = sr_bufused(&b->v) - sd_buildref(b)->v; /* copy object */ rc = sr_bufensure(&b->v, r->a, sv->size); if (srunlikely(rc == -1)) return sr_error(r->e, "%s", "memory allocation failed"); memcpy(b->v.p, sv_pointer(v), sv->size); sr_bufadvance(&b->v, sv->size); sr_bufadvance(&b->k, sizeof(sdv)); /* update page header */ h->count++; h->size += sv->size + sizeof(sdv); if (sv->lsn > h->lsnmax) h->lsnmax = sv->lsn; if (sv->lsn < h->lsnmin) h->lsnmin = sv->lsn; if (sv->flags & SVDUP) { h->countdup++; if (sv->lsn < h->lsnmindup) h->lsnmindup = sv->lsn; } return 0; }
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 void svv_kv(stc *cx) { sra a; sr_aopen(&a, &sr_stda); srkey cmp; sr_keyinit(&cmp); srkeypart *part = sr_keyadd(&cmp, &a); t( sr_keypart_setname(part, &a, "key") == 0 ); t( sr_keypart_set(part, &a, "u32") == 0 ); sr r; sr_init(&r, NULL, &a, NULL, SR_FKV, &cmp, NULL, NULL, NULL); uint32_t key = 123; uint32_t value = 321; srformatv pv; pv.key = (char*)&key; pv.r.size = sizeof(key); pv.r.offset = 0; svv *vv = sv_vbuild(&r, &pv, 1, (char*)&value, sizeof(value)); t( vv != NULL ); vv->flags = SVSET; vv->lsn = 10; sv v; sv_init(&v, &sv_vif, vv, NULL); t( sv_flags(&v) == SVSET ); t( sv_lsn(&v) == 10 ); sv_lsnset(&v, 8); t( sv_lsn(&v) == 8 ); t( *(uint32_t*)sr_formatkey(sv_pointer(&v), 0) == key ); t( sr_formatkey_size(sv_pointer(&v), 0) == sizeof(key) ); t( *(uint32_t*)sr_formatvalue(SR_FKV, &cmp, sv_pointer(&v)) == value ); t( sr_formatvalue_size(SR_FKV, &cmp, sv_pointer(&v), sv_size(&v) ) == sizeof(value) ); sv_vfree(&a, vv); sr_keyfree(&cmp, &a); }
int sd_buildadd(sdbuild *b, sr *r, sv *v, uint32_t flags) { /* prepare object metadata */ int rc = ss_bufensure(&b->m, r->a, sizeof(sdv)); if (ssunlikely(rc == -1)) return sr_oom(r->e); sdpageheader *h = sd_buildheader(b); sdv *sv = (sdv*)b->m.p; sv->flags = sv_flags(v) | flags; sv->offset = ss_bufused(&b->v) - sd_buildref(b)->v; ss_bufadvance(&b->m, sizeof(sdv)); /* copy object */ switch (r->fmt_storage) { case SF_SKEYVALUE: rc = sd_buildadd_keyvalue(b, r, v); break; case SF_SRAW: rc = sd_buildadd_raw(b, r, v); break; } if (ssunlikely(rc == -1)) return -1; /* update page header */ h->count++; uint32_t size = sizeof(sdv) + sv_size(v) + sizeof(sfref) * r->scheme->count; if (size > b->vmax) b->vmax = size; uint64_t lsn = sv_lsn(v); if (lsn > h->lsnmax) h->lsnmax = lsn; if (lsn < h->lsnmin) h->lsnmin = lsn; if (sv->flags & SVDUP) { h->countdup++; if (lsn < h->lsnmindup) h->lsnmindup = lsn; } return 0; }