static inline int si_dropof(siconf *conf, sr *r) { DIR *dir = opendir(conf->path); if (dir == NULL) { sr_malfunction(r->e, "directory '%s' open error: %s", conf->path, strerror(errno)); return -1; } char path[1024]; int rc; struct dirent *de; while ((de = readdir(dir))) { if (de->d_name[0] == '.') continue; /* skip drop file */ if (srunlikely(strcmp(de->d_name, "drop") == 0)) continue; snprintf(path, sizeof(path), "%s/%s", conf->path, de->d_name); rc = sr_fileunlink(path); if (srunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' unlink error: %s", path, strerror(errno)); closedir(dir); return -1; } } closedir(dir); snprintf(path, sizeof(path), "%s/drop", conf->path); rc = sr_fileunlink(path); if (srunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' unlink error: %s", path, strerror(errno)); return -1; } rc = rmdir(conf->path); if (srunlikely(rc == -1)) { sr_malfunction(r->e, "directory '%s' unlink error: %s", conf->path, strerror(errno)); return -1; } return 0; }
int si_nodefree(sinode *n, sr *r, int gc) { int rcret = 0; int rc; if (gc && n->file.file) { rc = sr_fileunlink(n->file.file); if (srunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' unlink error: %s", n->file.file, strerror(errno)); rcret = -1; } } si_nodefree_branches(n, r); rc = si_nodeclose(n, r); if (srunlikely(rc == -1)) rcret = -1; sr_free(r->a, n); return rcret; }
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); sd_buildfree(&b, &r); sr_keyfree(&cmp, &a); } static void sditer_gt1(stc *cx srunused) { 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 );
static inline int si_trackdir(sitrack *track, sr *r, si *i) { DIR *dir = opendir(i->conf->path); if (srunlikely(dir == NULL)) { sr_malfunction(r->e, "directory '%s' open error: %s", i->conf->path, strerror(errno)); return -1; } struct dirent *de; while ((de = readdir(dir))) { if (srunlikely(de->d_name[0] == '.')) continue; uint32_t id_parent = 0; uint32_t id = 0; int rc = si_process(de->d_name, &id, &id_parent); if (srunlikely(rc == -1)) continue; /* skip unknown file */ si_tracknsn(track, id_parent); si_tracknsn(track, id); sinode *head, *node; srpath path; switch (rc) { case SI_RDB_DBI: case SI_RDB_DBSEAL: { /* find parent node and mark it as having * incomplete compaction process */ head = si_trackget(track, id_parent); if (srlikely(head == NULL)) { head = si_nodenew(r); if (srunlikely(head == NULL)) goto error; head->self.id.id = id_parent; head->recover = SI_RDB_UNDEF; si_trackset(track, head); } head->recover |= rc; /* remove any incomplete file made during compaction */ if (rc == SI_RDB_DBI) { sr_pathAB(&path, i->conf->path, id_parent, id, ".db.incomplete"); rc = sr_fileunlink(path.path); if (srunlikely(rc == -1)) { sr_malfunction(r->e, "db file '%s' unlink error: %s", path.path, strerror(errno)); goto error; } continue; } assert(rc == SI_RDB_DBSEAL); /* recover 'sealed' node */ node = si_nodenew(r); if (srunlikely(node == NULL)) goto error; node->recover = SI_RDB_DBSEAL; sr_pathAB(&path, i->conf->path, id_parent, id, ".db.seal"); rc = si_nodeopen(node, r, &path); if (srunlikely(rc == -1)) { si_nodefree(node, r, 0); goto error; } si_trackset(track, node); si_trackmetrics(track, node); continue; } } assert(rc == SI_RDB); /* recover node */ node = si_nodenew(r); if (srunlikely(node == NULL)) goto error; node->recover = SI_RDB; sr_pathA(&path, i->conf->path, id, ".db"); rc = si_nodeopen(node, r, &path); if (srunlikely(rc == -1)) { si_nodefree(node, r, 0); goto error; } si_trackmetrics(track, node); /* track node */ head = si_trackget(track, id); if (srlikely(head == NULL)) { si_trackset(track, node); } else { /* replace a node previously created by a * incomplete compaction. */ if (! (head->recover & SI_RDB_UNDEF)) { sr_malfunction(r->e, "corrupted database repository: %s", i->conf->path); goto error; } si_trackreplace(track, head, node); head->recover &= ~SI_RDB_UNDEF; node->recover |= head->recover; si_nodefree(head, r, 0); } } closedir(dir); return 0; error: closedir(dir); return -1; }