Bitcask* bc_open(const char* path, int depth, int pos, time_t before) { if (path == NULL || depth > 4) return NULL; if (0 != access(path, F_OK) && 0 != mkdir(path, 0750)){ fprintf(stderr, "mkdir %s failed\n", path); return NULL; } const char* t[] = {path}; Mgr *mgr = mgr_create(t, 1); Bitcask* bc = bc_open2(mgr, depth, pos, before); bc_scan(bc); return bc; }
HStore* hs_open(char *path, int height, time_t before, int scan_threads) { if (NULL == path) return NULL; if (height < 0 || height > 3) { fprintf(stderr, "invalid db height: %d\n", height); return NULL; } if (before != 0){ if (before<0) { fprintf(stderr, "invalid time:%ld\n", before); return NULL; }else{ fprintf(stderr, "serve data modified before %s\n", ctime(&before)); } } char *paths[20], *rpath = path; int npath = 0; while ((paths[npath] = strsep(&rpath, ",:;")) != NULL) { if (npath >= MAX_PATHS) return NULL; path = paths[npath]; if (0 != access(path, F_OK) && 0 != mkdir(path, 0755)){ fprintf(stderr, "mkdir %s failed\n", path); return NULL; } if (height > 1){ // try to mkdir HStore *s = hs_open(path, height - 1, 0, 0); if (s == NULL){ return NULL; } hs_close(s); } npath ++; } int i, j, count = 1 << (height * 4); HStore *store = (HStore*) malloc(sizeof(HStore) + sizeof(Bitcask*) * count); if (!store) return NULL; memset(store, 0, sizeof(HStore) + sizeof(Bitcask*) * count); store->height = height; store->count = count; store->before = before; store->scan_threads = scan_threads; store->op_start = 0; store->op_end = 0; store->op_limit = 0; store->mgr = mgr_create((const char**)paths, npath); if (store->mgr == NULL) { free(store); return NULL; } for (i=0; i<NUM_OF_MUTEX; i++) { pthread_mutex_init(&store->locks[i], NULL); } char *buf[20] = {0}; for (i=0;i<npath;i++) { buf[i] = malloc(255); } for (i=0; i<count; i++){ for (j=0; j<npath; j++) { path = paths[j]; switch(height){ case 0: sprintf(buf[j], "%s", path); break; case 1: sprintf(buf[j], "%s/%x", path, i); break; case 2: sprintf(buf[j], "%s/%x/%x", path, i>>4, i & 0xf); break; case 3: sprintf(buf[j], "%s/%x/%x/%x", path, i>>8, (i>>4)&0xf, i&0xf); break; } } Mgr *mgr = mgr_create((const char**)buf, npath); if (mgr == NULL) return NULL; store->bitcasks[i] = bc_open2(mgr, height, i, before); } for (i=0;i<npath;i++) { free(buf[i]); } if (store->scan_threads > 1 && count > 1) { parallelize(store, bc_scan); }else{ for (i=0; i<count; i++) { bc_scan(store->bitcasks[i]); } } return store; }