void bc_scan(Bitcask* bc) { const char* path = mgr_base(bc->mgr); char dname[20], hname[20], datapath[255], hintpath[255]; int i=0; struct stat st; for (i=0; i<MAX_BUCKET_COUNT; i++) { sprintf(dname, DATA_FILE, i); sprintf(datapath, "%s/%s", path, dname); if (stat(datapath, &st) != 0) { break; } bc->bytes += st.st_size; sprintf(hname, HINT_FILE, i); sprintf(hintpath, "%s/%s", path, hname); if (bc->before == 0){ if (0 == stat(hintpath, &st)){ scanHintFile(bc->tree, i, hintpath, NULL); }else{ sprintf(hintpath, "%s/%s", mgr_alloc(bc->mgr, hname), hname); scanDataFile(bc->tree, i, datapath, hintpath); } }else{ if (0 == stat(hintpath, &st) && (st.st_mtime < bc->before || 0 == stat(datapath, &st) && st.st_mtime < bc->before)){ scanHintFile(bc->tree, i, hintpath, NULL); }else{ scanDataFileBefore(bc->tree, i, datapath, bc->before); } } } bc->curr = i; }
void bc_scan(Bitcask* bc) { char datapath[255], hintpath[255]; int i=0; struct stat st, hst; skip_empty_file(bc); const char* base = mgr_base(bc->mgr); // load snapshot of htree for (i=MAX_BUCKET_COUNT-1; i>=0; i--) { if (stat(gen_path(datapath, base, HTREE_FILE, i), &st) == 0 && stat(gen_path(hintpath, base, HINT_FILE, i), &hst) == 0 && st.st_mtime >= hst.st_mtime && (bc->before == 0 || st.st_mtime < bc->before)) { bc->tree = ht_open(bc->depth, bc->pos, datapath); if (bc->tree != NULL) { bc->last_snapshot = i; break; } else { fprintf(stderr, "open HTree from %s failed\n", datapath); mgr_unlink(datapath); } } } if (bc->tree == NULL) { bc->tree = ht_new(bc->depth, bc->pos); } for (i=0; i<MAX_BUCKET_COUNT; i++) { if (stat(gen_path(datapath, base, DATA_FILE, i), &st) != 0) { break; } bc->bytes += st.st_size; if (i <= bc->last_snapshot) continue; gen_path(hintpath, base, HINT_FILE, i); if (bc->before == 0) { if (0 == stat(hintpath, &st)) { scanHintFile(bc->tree, i, hintpath, NULL); } else { scanDataFile(bc->tree, i, datapath, new_path(hintpath, bc->mgr, HINT_FILE, i)); } } else { if (0 == stat(hintpath, &st) && (st.st_mtime < bc->before || 0 == stat(datapath, &st) && st.st_mtime < bc->before)) { scanHintFile(bc->tree, i, hintpath, NULL); } else { scanDataFileBefore(bc->tree, i, datapath, bc->before); } } } if (i - bc->last_snapshot > SAVE_HTREE_LIMIT) { if (ht_save(bc->tree, new_path(datapath, bc->mgr, HTREE_FILE, i-1)) == 0) { mgr_unlink(gen_path(NULL, base, HTREE_FILE, bc->last_snapshot)); bc->last_snapshot = i-1; } else { fprintf(stderr, "save HTree to %s failed\n", datapath); } } bc->curr = i; }