int si_readopen(siread *q, sitx *x, sicache *c, ssorder o, uint64_t vlsn, void *prefix, uint32_t prefixsize, void *key, uint32_t keysize) { q->order = o; q->key = key; q->keysize = keysize; q->vlsn = vlsn; q->x = x; q->index = x->index; q->r = x->index->r; q->cache = c; q->prefix = prefix; q->prefixsize = prefixsize; q->has = 0; q->upsert_v = NULL; q->upsert_eq = 0; q->cache_only = 0; q->read_disk = 0; q->read_cache = 0; memset(&q->result, 0, sizeof(q->result)); sv_mergeinit(&q->merge); return 0; }
int si_queryopen(siquery *q, sicache *c, si *i, ssorder o, uint64_t vlsn, void *prefix, uint32_t prefixsize, void *key, uint32_t keysize) { q->order = o; q->key = key; q->keysize = keysize; q->vlsn = vlsn; q->index = i; q->r = i->r; q->cache = c; q->prefix = prefix; q->prefixsize = prefixsize; q->has = 0; q->update_v = NULL; q->update_eq = 0; q->cache_only = 0; q->read_disk = 0; q->read_cache = 0; memset(&q->result, 0, sizeof(q->result)); sv_mergeinit(&q->merge); si_lock(q->index); return 0; }
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); }
int si_compact(si *index, sr *r, sdc *c, siplan *plan, uint64_t vlsn) { sinode *node = plan->node; assert(node->flags & SI_LOCK); /* read node file */ sd_creset(c); int rc = si_noderead(r, &c->c, node); if (ssunlikely(rc == -1)) return -1; /* prepare for compaction */ rc = sd_censure(c, r, node->branch_count); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "%s", "memory allocation failed"); return -1; } svmerge merge; sv_mergeinit(&merge); rc = sv_mergeprepare(&merge, r, node->branch_count); if (ssunlikely(rc == -1)) return -1; uint32_t size_stream = 0; sdcbuf *cbuf = c->head; sibranch *b = node->branch; while (b) { svmergessc *s = sv_mergeadd(&merge, NULL); rc = ss_bufensure(&cbuf->b, r->a, b->index.h->sizevmax); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "%s", "memory allocation failed"); return -1; } size_stream += sd_indextotal(&b->index); ss_iterinit(sd_iter, &s->ssc); ss_iteropen(sd_iter, &s->ssc, r, &b->index, c->c.s, 0, index->scheme->compression, &cbuf->a, &cbuf->b); cbuf = cbuf->next; b = b->next; } ssiter i; ss_iterinit(sv_mergeiter, &i); ss_iteropen(sv_mergeiter, &i, r, &merge, SS_GTE); rc = si_compaction(index, r, c, vlsn, node, &i, size_stream); if (ssunlikely(rc == -1)) { sv_mergefree(&merge, r->a); return -1; } sv_mergefree(&merge, r->a); return 0; }
static inline sibranch* si_branchcreate(si *index, sdc *c, sinode *parent, svindex *vindex, uint64_t vlsn) { sr *r = index->r; svmerge vmerge; sv_mergeinit(&vmerge); int rc = sv_mergeprepare(&vmerge, r, 1); if (ssunlikely(rc == -1)) return NULL; svmergesrc *s = sv_mergeadd(&vmerge, NULL); ss_iterinit(sv_indexiterraw, &s->src); ss_iteropen(sv_indexiterraw, &s->src, vindex); ssiter i; ss_iterinit(sv_mergeiter, &i); ss_iteropen(sv_mergeiter, &i, r, &vmerge, SS_GTE, 1); /* merge iter is not used */ sdmergeconf mergeconf = { .size_stream = UINT32_MAX, .size_node = UINT64_MAX, .size_page = index->scheme->node_page_size, .checksum = index->scheme->node_page_checksum, .compression = index->scheme->compression, .compression_key = index->scheme->compression_key, .offset = parent->file.size, .vlsn = vlsn, .save_delete = 1 }; sdmerge merge; sd_mergeinit(&merge, r, &i, &c->build, &mergeconf); rc = sd_merge(&merge); if (ssunlikely(rc == -1)) { sv_mergefree(&vmerge, r->a); sr_oom_malfunction(r->e); goto error; } assert(rc == 1); sv_mergefree(&vmerge, r->a); sibranch *branch = si_branchnew(r); if (ssunlikely(branch == NULL)) goto error; sdid id = { .parent = parent->self.id.id, .flags = SD_IDBRANCH, .id = sr_seq(r->seq, SR_NSNNEXT) }; rc = sd_mergecommit(&merge, &id); if (ssunlikely(rc == -1)) goto error; si_branchset(branch, &merge.index); rc = sd_commit(&c->build, r, &branch->index, &parent->file); if (ssunlikely(rc == -1)) { si_branchfree(branch, r); return NULL; } SS_INJECTION(r->i, SS_INJECTION_SI_BRANCH_0, sr_malfunction(r->e, "%s", "error injection"); si_branchfree(branch, r); return NULL); if (index->scheme->sync) { rc = si_nodesync(parent, r); if (ssunlikely(rc == -1)) { si_branchfree(branch, r); return NULL; } } if (index->scheme->mmap) { ss_mmapinit(&parent->map_swap); rc = ss_mmap(&parent->map_swap, parent->file.fd, parent->file.size, 1); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' mmap error: %s", parent->file.file, strerror(errno)); return NULL; } } return branch; error: sd_mergefree(&merge); return NULL; } int si_branch(si *index, sdc *c, siplan *plan, uint64_t vlsn) { sr *r = index->r; sinode *n = plan->node; assert(n->flags & SI_LOCK); si_lock(index); if (ssunlikely(n->used == 0)) { si_nodeunlock(n); si_unlock(index); return 0; } svindex *i; i = si_noderotate(n); si_unlock(index); sd_creset(c); sibranch *branch = si_branchcreate(index, c, n, i, vlsn); if (ssunlikely(branch == NULL)) return -1; /* commit */ si_lock(index); branch->next = n->branch; n->branch = branch; n->branch_count++; uint32_t used = sv_indexused(i); n->used -= used; ss_quota(r->quota, SS_QREMOVE, used); svindex swap = *i; si_nodeunrotate(n); si_nodeunlock(n); si_plannerupdate(&index->p, SI_BRANCH|SI_COMPACT, n); ssmmap swap_map = n->map; n->map = n->map_swap; memset(&n->map_swap, 0, sizeof(n->map_swap)); si_unlock(index); /* gc */ if (index->scheme->mmap) { int rc = ss_munmap(&swap_map); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' munmap error: %s", n->file.file, strerror(errno)); return -1; } } si_nodegc_index(r, &swap); return 1; } static inline char* si_noderead(si *index, ssbuf *dest, sinode *node) { sr *r = index->r; if (index->scheme->mmap) { return node->map.p; } int rc = ss_bufensure(dest, r->a, node->file.size); if (ssunlikely(rc == -1)) { sr_oom_malfunction(r->e); return NULL; } rc = ss_filepread(&node->file, 0, dest->s, node->file.size); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' read error: %s", node->file.file, strerror(errno)); return NULL; } ss_bufadvance(dest, node->file.size); return dest->s; }
int si_compaction(si *index, sdc *c, siplan *plan, uint64_t vlsn) { sr *r = &index->r; sinode *node = plan->node; assert(node->flags & SI_LOCK); si_lock(index); svindex *vindex; vindex = si_noderotate(node); si_unlock(index); uint64_t size_stream = vindex->used; ssiter vindex_iter; ss_iterinit(sv_indexiter, &vindex_iter); ss_iteropen(sv_indexiter, &vindex_iter, &index->r, vindex, SS_GTE, NULL); /* prepare direct_io stream */ int rc; if (index->scheme.direct_io) { rc = sd_ioprepare(&c->io, r, index->scheme.direct_io, index->scheme.direct_io_page_size, index->scheme.direct_io_buffer_size); if (ssunlikely(rc == -1)) return sr_oom(r->e); } /* prepare for compaction */ svmerge merge; sv_mergeinit(&merge); rc = sv_mergeprepare(&merge, r, 1 + 1); if (ssunlikely(rc == -1)) return -1; svmergesrc *s; s = sv_mergeadd(&merge, &vindex_iter); sdcbuf *cbuf = &c->e; s = sv_mergeadd(&merge, NULL); sdreadarg arg = { .from_compaction = 1, .io = &c->io, .index = &node->index, .buf = &cbuf->a, .buf_read = &c->d, .index_iter = &cbuf->index_iter, .page_iter = &cbuf->page_iter, .use_mmap = index->scheme.mmap, .use_mmap_copy = 0, .use_compression = index->scheme.compression, .use_direct_io = index->scheme.direct_io, .direct_io_page_size = index->scheme.direct_io_page_size, .compression_if = index->scheme.compression_if, .has = 0, .has_vlsn = 0, .o = SS_GTE, .mmap = &node->map, .file = &node->file, .r = r }; ss_iterinit(sd_read, &s->src); rc = ss_iteropen(sd_read, &s->src, &arg, NULL); if (ssunlikely(rc == -1)) return -1; size_stream += sd_indextotal(&node->index); ssiter i; ss_iterinit(sv_mergeiter, &i); ss_iteropen(sv_mergeiter, &i, r, &merge, SS_GTE); rc = si_merge(index, c, node, vlsn, &i, size_stream, sd_indexkeys(&node->index)); sv_mergefree(&merge, r->a); return rc; }
static inline sibranch* si_branchcreate(si *index, sdc *c, sinode *parent, svindex *vindex, uint64_t vlsn) { sr *r = index->r; sibranch *branch = NULL; /* in-memory mode blob */ int rc; ssblob copy, *blob = NULL; if (parent->in_memory) { ss_blobinit(©, r->vfs); rc = ss_blobensure(©, 10ULL * 1024 * 1024); if (ssunlikely(rc == -1)) { sr_oom_malfunction(r->e); return NULL; } blob = © } svmerge vmerge; sv_mergeinit(&vmerge); rc = sv_mergeprepare(&vmerge, r, 1); if (ssunlikely(rc == -1)) return NULL; svmergesrc *s = sv_mergeadd(&vmerge, NULL); ss_iterinit(sv_indexiter, &s->src); ss_iteropen(sv_indexiter, &s->src, r, vindex, SS_GTE, NULL, 0); ssiter i; ss_iterinit(sv_mergeiter, &i); ss_iteropen(sv_mergeiter, &i, r, &vmerge, SS_GTE); /* merge iter is not used */ sdmergeconf mergeconf = { .size_stream = UINT32_MAX, .size_node = UINT64_MAX, .size_page = index->scheme->node_page_size, .checksum = index->scheme->node_page_checksum, .compression_key = index->scheme->compression_key, .compression = index->scheme->compression_branch, .compression_if = index->scheme->compression_branch_if, .vlsn = vlsn, .vlsn_lru = 0, .save_delete = 1, .save_upsert = 1 }; sdmerge merge; sd_mergeinit(&merge, r, &i, &c->build, &c->upsert, &mergeconf); while ((rc = sd_merge(&merge)) > 0) { assert(branch == NULL); /* write open seal */ uint64_t seal = parent->file.size; rc = sd_writeseal(r, &parent->file, blob); if (ssunlikely(rc == -1)) goto e0; /* write pages */ uint64_t offset = parent->file.size; while ((rc = sd_mergepage(&merge, offset)) == 1) { rc = sd_writepage(r, &parent->file, blob, merge.build); if (ssunlikely(rc == -1)) goto e0; offset = parent->file.size; } if (ssunlikely(rc == -1)) goto e0; sdid id = { .parent = parent->self.id.id, .flags = SD_IDBRANCH, .id = sr_seq(r->seq, SR_NSNNEXT) }; rc = sd_mergecommit(&merge, &id, parent->file.size); if (ssunlikely(rc == -1)) goto e0; /* write index */ rc = sd_writeindex(r, &parent->file, blob, &merge.index); if (ssunlikely(rc == -1)) goto e0; if (index->scheme->sync) { rc = ss_filesync(&parent->file); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "file '%s' sync error: %s", ss_pathof(&parent->file.path), strerror(errno)); goto e0; } } SS_INJECTION(r->i, SS_INJECTION_SI_BRANCH_0, sd_mergefree(&merge); sr_malfunction(r->e, "%s", "error injection"); return NULL); /* seal the branch */ rc = sd_seal(r, &parent->file, blob, &merge.index, seal); if (ssunlikely(rc == -1)) goto e0; if (index->scheme->sync == 2) { rc = ss_filesync(&parent->file); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "file '%s' sync error: %s", ss_pathof(&parent->file.path), strerror(errno)); goto e0; } } /* create new branch object */ branch = si_branchnew(r); if (ssunlikely(branch == NULL)) goto e0; si_branchset(branch, &merge.index); } sv_mergefree(&vmerge, r->a); if (ssunlikely(rc == -1)) { sr_oom_malfunction(r->e); goto e0; } assert(branch != NULL); /* in-memory mode support */ if (blob) { rc = ss_blobfit(blob); if (ssunlikely(rc == -1)) { ss_blobfree(blob); goto e1; } branch->copy = copy; } /* mmap support */ if (index->scheme->mmap) { ss_mmapinit(&parent->map_swap); rc = ss_vfsmmap(r->vfs, &parent->map_swap, parent->file.fd, parent->file.size, 1); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' mmap error: %s", ss_pathof(&parent->file.path), strerror(errno)); goto e1; } } return branch; e0: sd_mergefree(&merge); if (blob) ss_blobfree(blob); return NULL; e1: si_branchfree(branch, r); return NULL; } int si_branch(si *index, sdc *c, siplan *plan, uint64_t vlsn) { sr *r = index->r; sinode *n = plan->node; assert(n->flags & SI_LOCK); si_lock(index); if (ssunlikely(n->used == 0)) { si_nodeunlock(n); si_unlock(index); return 0; } svindex *i; i = si_noderotate(n); si_unlock(index); sibranch *branch = si_branchcreate(index, c, n, i, vlsn); if (ssunlikely(branch == NULL)) return -1; /* commit */ si_lock(index); branch->next = n->branch; n->branch->link = branch; n->branch = branch; n->branch_count++; uint32_t used = sv_indexused(i); n->used -= used; ss_quota(r->quota, SS_QREMOVE, used); index->size += sd_indexsize(branch->index.h) + sd_indextotal(&branch->index); svindex swap = *i; si_nodeunrotate(n); si_nodeunlock(n); si_plannerupdate(&index->p, SI_BRANCH|SI_COMPACT, n); ssmmap swap_map = n->map; n->map = n->map_swap; memset(&n->map_swap, 0, sizeof(n->map_swap)); si_unlock(index); /* gc */ if (index->scheme->mmap) { int rc = ss_vfsmunmap(r->vfs, &swap_map); if (ssunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' munmap error: %s", ss_pathof(&n->file.path), strerror(errno)); return -1; } } si_nodegc_index(r, &swap); return 1; } int si_compact(si *index, sdc *c, siplan *plan, uint64_t vlsn, uint64_t vlsn_lru, ssiter *vindex, uint64_t vindex_used) { sr *r = index->r; sinode *node = plan->node; assert(node->flags & SI_LOCK); /* prepare for compaction */ int rc; rc = sd_censure(c, r, node->branch_count); if (ssunlikely(rc == -1)) return sr_oom_malfunction(r->e); svmerge merge; sv_mergeinit(&merge); rc = sv_mergeprepare(&merge, r, node->branch_count + 1); if (ssunlikely(rc == -1)) return -1; /* read node file into memory */ int use_mmap = index->scheme->mmap; ssmmap *map = &node->map; ssmmap preload; if (index->scheme->node_compact_load) { rc = si_noderead(node, r, &c->c); if (ssunlikely(rc == -1)) return -1; preload.p = c->c.s; preload.size = ss_bufused(&c->c); map = &preload; use_mmap = 1; } /* include vindex into merge process */ svmergesrc *s; uint64_t size_stream = 0; if (vindex) { s = sv_mergeadd(&merge, vindex); size_stream = vindex_used; } sdcbuf *cbuf = c->head; sibranch *b = node->branch; while (b) { s = sv_mergeadd(&merge, NULL); /* choose compression type */ int compression; ssfilterif *compression_if; if (! si_branchis_root(b)) { compression = index->scheme->compression_branch; compression_if = index->scheme->compression_branch_if; } else { compression = index->scheme->compression; compression_if = index->scheme->compression_if; } sdreadarg arg = { .index = &b->index, .buf = &cbuf->a, .buf_xf = &cbuf->b, .buf_read = &c->d, .index_iter = &cbuf->index_iter, .page_iter = &cbuf->page_iter, .use_memory = node->in_memory, .use_mmap = use_mmap, .use_mmap_copy = 0, .use_compression = compression, .compression_if = compression_if, .has = 0, .has_vlsn = 0, .o = SS_GTE, .memory = &b->copy, .mmap = map, .file = &node->file, .r = r }; ss_iterinit(sd_read, &s->src); int rc = ss_iteropen(sd_read, &s->src, &arg, NULL, 0); if (ssunlikely(rc == -1)) return sr_oom_malfunction(r->e); size_stream += sd_indextotal(&b->index); cbuf = cbuf->next; b = b->next; } ssiter i; ss_iterinit(sv_mergeiter, &i); ss_iteropen(sv_mergeiter, &i, r, &merge, SS_GTE); rc = si_merge(index, c, node, vlsn, vlsn_lru, &i, size_stream); sv_mergefree(&merge, r->a); return rc; } int si_compact_index(si *index, sdc *c, siplan *plan, uint64_t vlsn, uint64_t vlsn_lru) { sinode *node = plan->node; si_lock(index); if (ssunlikely(node->used == 0)) { si_nodeunlock(node); si_unlock(index); return 0; } svindex *vindex; vindex = si_noderotate(node); si_unlock(index); uint64_t size_stream = sv_indexused(vindex); ssiter i; ss_iterinit(sv_indexiter, &i); ss_iteropen(sv_indexiter, &i, index->r, vindex, SS_GTE, NULL, 0); return si_compact(index, c, plan, vlsn, vlsn_lru, &i, size_stream); }