// Opens the tablet for reading and writing. This should be called directly. // Only the owning table should open a tablet. // // tablet - The tablet. // // Returns 0 if successful, otherwise returns -1. int sky_tablet_open(sky_tablet *tablet) { char* errptr = NULL; leveldb_options_t* options = NULL; assert(tablet != NULL); check(tablet->path != NULL, "Tablet path required"); // Close the tablet if it's already open. sky_tablet_close(tablet); // Initialize data file. options = leveldb_options_create(); leveldb_options_set_create_if_missing(options, true); tablet->leveldb_db = leveldb_open(options, bdata(tablet->path), &errptr); check(errptr == NULL, "LevelDB Error: %s", errptr); check(tablet->leveldb_db != NULL, "Unable to create LevelDB data file"); leveldb_options_destroy(options); return 0; error: if(errptr) leveldb_free(errptr); if(options) leveldb_options_destroy(options); sky_tablet_close(tablet); return -1; }
struct db_conn *xleveldb_open(const char *name, int flags) { struct db_conn *db; leveldb_options_t *options; leveldb_t *level_db; options = leveldb_options_create(); if (options == NULL) return NULL; leveldb_options_set_create_if_missing(options, 1); level_db = leveldb_open(options, name, NULL); leveldb_options_destroy(options); if (level_db == NULL) return NULL; db = malloc(sizeof(struct db_conn)); if (db == NULL) { leveldb_close(level_db); return NULL; } db->conn = level_db; db->set = xleveldb_set; db->get = xleveldb_get; db->del = xleveldb_del; db->firstkey = xleveldb_firstkey; db->nextkey = xleveldb_nextkey; db->close = xleveldb_close; return db; }
int leveldb_client_stop(leveldb *db) { int retval = 0; #if DELETE_DB_FROM_DISK char *err = NULL; #endif kp_debug("stopping db [%s]\n", db->name); leveldb_close(db->db); #if DELETE_DB_FROM_DISK leveldb_destroy_db(db->options, db->name, &err); //destroy db on disk if (err) { kp_error("leveldb_destroy_db() returned error: %s\n", err); retval = -1; } free_err(&err); #endif leveldb_options_destroy(db->options); leveldb_readoptions_destroy(db->roptions); leveldb_writeoptions_destroy(db->woptions); leveldb_cache_destroy(db->cache); leveldb_comparator_destroy(db->cmp); leveldb_env_destroy(db->env); free(db->name); free(db); kp_debug("freed the leveldb, returning %d\n", retval); return retval; }
int txdb_zap(struct config *config) { leveldb_options_t *options; char *err = NULL; char *path; int res; path = txdb_get_db_path(config); res = file_exists(path); if (res == 0) { goto exit; } Warning(LGPFX" destroying txdb @ '%s'\n", path); options = leveldb_options_create(); leveldb_options_set_info_log(options, NULL); leveldb_destroy_db(options, path, &err); leveldb_options_destroy(options); if (err) { Warning(LGPFX" failed to destroy DB %s: %s\n", path, err); free(err); res = 1; } exit: free(path); return res; }
/* * Close the ldb. */ void ldb_close(struct _leveldb_stuff *ldbs) { leveldb_close(ldbs->db); leveldb_options_destroy(ldbs->options); leveldb_readoptions_destroy(ldbs->roptions); leveldb_writeoptions_destroy(ldbs->woptions); leveldb_writebatch_destroy(ldbs->wbatch); }
void ds_close() { leveldb_close(server.ds_db); leveldb_readoptions_destroy(server.roptions); leveldb_writeoptions_destroy(server.woptions); leveldb_options_set_filter_policy(server.ds_options, NULL); leveldb_filterpolicy_destroy(server.policy); leveldb_options_destroy(server.ds_options); leveldb_cache_destroy(server.ds_cache); }
void db_leveldb_close() { leveldb_close(leveldb_db); leveldb_cache_destroy(leveldb_cache); leveldb_filterpolicy_destroy(leveldb_filterpolicy); leveldb_options_destroy(leveldb_options); leveldb_readoptions_destroy(leveldb_roptions); leveldb_writeoptions_destroy(leveldb_woptions); }
void kvdb_leveldb_close(kvdb_t *kvdb){ kvdb_leveldb_t *leveldb = (kvdb_leveldb_t*)kvdb; leveldb_close(leveldb->db); leveldb_writeoptions_destroy(leveldb->pWriteOpt); leveldb_readoptions_destroy(leveldb->pReadOpt); leveldb_options_destroy(leveldb->pOpt); zfree(kvdb); }
int stat_cache_close(stat_cache_t *cache, struct stat_cache_supplemental supplemental) { if (cache != NULL) leveldb_close(cache); if (supplemental.options != NULL) { leveldb_options_destroy(supplemental.options); log_print(LOG_DEBUG, "leveldb_options_destroy"); } if (supplemental.lru != NULL) leveldb_cache_destroy(supplemental.lru); return 0; }
/** * mdhim_leveldb_close * Closes the data store * * @param dbh in pointer to the leveldb db handle * @param dbs in pointer to the leveldb statistics db handle * @param mstore_opts in additional options for the data store layer * * @return MDHIM_SUCCESS on success or MDHIM_DB_ERROR on failure */ int mdhim_leveldb_close(void *dbh, void *dbs, struct mdhim_store_opts_t *mstore_opts) { leveldb_t *db = (leveldb_t *) dbh; leveldb_t *db_s = (leveldb_t *) dbs; //Close the databases leveldb_close(db); leveldb_close(db_s); //Destroy the options leveldb_comparator_destroy((leveldb_comparator_t *) mstore_opts->db_ptr1); leveldb_options_destroy((leveldb_options_t *) mstore_opts->db_ptr2); leveldb_readoptions_destroy((leveldb_readoptions_t *) mstore_opts->db_ptr3); leveldb_writeoptions_destroy((leveldb_writeoptions_t *) mstore_opts->db_ptr4); leveldb_options_destroy((leveldb_options_t *) mstore_opts->db_ptr5); leveldb_readoptions_destroy((leveldb_readoptions_t *) mstore_opts->db_ptr6); leveldb_writeoptions_destroy((leveldb_writeoptions_t *) mstore_opts->db_ptr7); leveldb_filterpolicy_destroy((leveldb_filterpolicy_t *) mstore_opts->db_ptr8); leveldb_filterpolicy_destroy((leveldb_filterpolicy_t *) mstore_opts->db_ptr9); // leveldb_cache_destroy((leveldb_cache_t *) mstore_opts->db_ptr10); //leveldb_cache_destroy((leveldb_cache_t *) mstore_opts->db_ptr11); return MDHIM_SUCCESS; }
/* * Destroy the ldb. */ void ldb_destroy(struct _leveldb_stuff *ldbs) { char* err = NULL; leveldb_close(ldbs->db); leveldb_destroy_db(ldbs->options, ldbs->dbname, &err); if (err) { fprintf(stderr, "%s\n", err); leveldb_free(err); err = NULL; } leveldb_options_destroy(ldbs->options); leveldb_readoptions_destroy(ldbs->roptions); leveldb_writeoptions_destroy(ldbs->woptions); leveldb_writebatch_destroy(ldbs->wbatch); free(ldbs); }
db_t * db_open(const char *path, char **errptr) { db_t *out; leveldb_options_t *opts; leveldb_t *db; opts = leveldb_options_create(); leveldb_options_set_create_if_missing(opts, 1); db = leveldb_open(opts, path, errptr); if (*errptr) { leveldb_options_destroy(opts); return NULL; } out = malloc(sizeof(db_t)); *out = (db_t){ .db=db, .opts=opts }; return out; }
void txdb_close(struct txdb *txdb) { if (txdb == NULL) { return; } if (txdb->db) { leveldb_close(txdb->db); } leveldb_options_destroy(txdb->db_opts); leveldb_readoptions_destroy(txdb->rd_opts); leveldb_writeoptions_destroy(txdb->wr_opts); hashtable_clear_with_callback(txdb->hash_txo, txdb_hashtable_free_txo_entry); hashtable_destroy(txdb->hash_txo); hashtable_clear_with_callback(txdb->hash_tx, txdb_hashtable_free_tx_entry); hashtable_destroy(txdb->hash_tx); free(txdb->path); memset(txdb, 0, sizeof *txdb); free(txdb); }
int main(int argc, char **argv) { int goopt; int listenport=8080; int numthreads=10; int tf; char *dbd=NULL; char *lpstr=NULL; char *ntstr=NULL; char *alfile=NULL; leveldb_options_t *dbopt; struct mg_context *ctx= NULL; char **mgoptions; signal(SIGINT,handlesig); signal(SIGTERM,handlesig); // command line parsing while ((goopt=getopt (argc, argv, "d:p:n:a:t:vh")) != -1) { switch (goopt) { case 'd': // database dbd=calloc(strlen((char*)optarg)+1,sizeof(char)); strncpy(dbd,(char*)optarg,strlen((char*)optarg)); break; case 'a': // access log, passed to mongoose alfile=calloc(strlen((char*)optarg)+1,sizeof(char)); strncpy(alfile,(char*)optarg,strlen((char*)optarg)); break; case 'p': // port listenport=atoi(optarg); break; case 'n': // number of threads numthreads=atoi(optarg); break; case 'v': // verbose vlevel++; break; case 'h': // help usage(NULL,EXIT_SUCCESS); break; default: // fallthrough usage(NULL,EXIT_FAILURE); } } LOG_TRACE(vlevel, _("Validation\n"),dbd); if(dbd==NULL) { usage("Must give a database dir\n",EXIT_FAILURE); } else { LOG_TRACE(vlevel, _("Using database dir: %s\n"),dbd); } if(alfile!=NULL) { if((tf=open(alfile,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))==-1) { LOG_FATAL(vlevel,_("Unable to open access log file: %s: %s\n"), alfile, strerror(errno)); exit(EXIT_FAILURE); } close(tf); } if(alfile!=NULL && access(alfile, W_OK)!=0) { LOG_FATAL(vlevel, _("Unable to write to access log file: %s\n"),alfile); exit(EXIT_FAILURE); } if(listenport<0 || listenport>65536) { LOG_FATAL(vlevel, _("Given port out of bounds: %i\n"),listenport); exit(EXIT_FAILURE); } if(numthreads<0 || numthreads>1024) { LOG_FATAL(vlevel, _("Given threads out of bounds: %i\n"),numthreads); exit(EXIT_FAILURE); } LOG_TRACE(vlevel, _("Setting up leveldb store in %s\n"),dbd); dbopt=leveldb_options_create(); leveldb_options_set_create_if_missing(dbopt, 1); leveldb_options_set_write_buffer_size(dbopt, 8388608); leveldb_options_set_compression(dbopt,leveldb_no_compression); dbh=leveldb_open(dbopt,dbd,&errptr); LOG_TRACE(vlevel, _("Setting leveldb read options\n")); ropt = leveldb_readoptions_create(); leveldb_readoptions_set_verify_checksums(ropt, 1); leveldb_readoptions_set_fill_cache(ropt, 0); LOG_TRACE(vlevel, _("Setting leveldb write options\n")); wopt = leveldb_writeoptions_create(); leveldb_writeoptions_set_sync(wopt, 0); // set mgoptions - XXX this needs to be handled better lpstr=calloc(7,sizeof(char)); snprintf(lpstr,6,"%i",listenport); ntstr=calloc(4,sizeof(char)); snprintf(ntstr,3,"%i",numthreads); if(alfile!=NULL) { mgoptions = calloc(9,sizeof(char*)); } else { mgoptions = calloc(7,sizeof(char*)); } mgoptions[0]="listening_ports"; mgoptions[1]=lpstr; mgoptions[2]="document_root"; mgoptions[3]="/dev/null"; mgoptions[4]="num_threads"; mgoptions[5]=ntstr; if(alfile!=NULL) { mgoptions[6]="access_log_file"; mgoptions[7]=alfile; mgoptions[8]=NULL; } else { mgoptions[6]=NULL; } // main loop LOG_INFO(vlevel, _("Starting Mongoose HTTP server loop\n")); ctx = mg_start(&mghandle, NULL, (const char**)mgoptions); if(ctx!=NULL) { while(!done) { // cleaner thread here? sleep(1); } LOG_INFO(vlevel, _("Ending Mongoose HTTP server loop\n")); mg_stop(ctx); } else { LOG_FATAL(vlevel,_("Error in creating Mongoose HTTP server\n")); } LOG_TRACE(vlevel, _("Closing database handle\n")); // close leveldb handle LOG_TRACE(vlevel, _("Cleaning up leveldb\n")); leveldb_compact_range(dbh, NULL, 0, NULL, 0); leveldb_options_destroy(dbopt); leveldb_readoptions_destroy(ropt); leveldb_writeoptions_destroy(wopt); leveldb_close(dbh); LOG_TRACE(vlevel, _("Cleaning up\n")); free(dbd); free(lpstr); free(ntstr); free(mgoptions); return EXIT_SUCCESS; }
int main(int argc, char** argv) { leveldb_t* db; leveldb_comparator_t* cmp; leveldb_cache_t* cache; leveldb_env_t* env; leveldb_options_t* options; leveldb_readoptions_t* roptions; leveldb_writeoptions_t* woptions; char* err = NULL; int run = -1; CheckCondition(leveldb_major_version() >= 1); CheckCondition(leveldb_minor_version() >= 1); snprintf(dbname, sizeof(dbname), "%s/leveldb_c_test-%di/meta/0", GetTempDir(), ((int) geteuid())); StartPhase("create_objects"); cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName); env = leveldb_create_default_env(); cache = leveldb_cache_create_lru(100000); options = leveldb_options_create(); leveldb_options_set_comparator(options, cmp); leveldb_options_set_error_if_exists(options, 1); leveldb_options_set_cache(options, cache); leveldb_options_set_env(options, env); leveldb_options_set_info_log(options, NULL); leveldb_options_set_write_buffer_size(options, 100000); leveldb_options_set_paranoid_checks(options, 1); leveldb_options_set_max_open_files(options, 10); leveldb_options_set_block_size(options, 1024); leveldb_options_set_block_restart_interval(options, 8); leveldb_options_set_compression(options, leveldb_no_compression); roptions = leveldb_readoptions_create(); leveldb_readoptions_set_verify_checksums(roptions, 1); leveldb_readoptions_set_fill_cache(roptions, 0); woptions = leveldb_writeoptions_create(); leveldb_writeoptions_set_sync(woptions, 1); StartPhase("destroy"); leveldb_destroy_db(options, dbname, &err); Free(&err); StartPhase("open"); db = leveldb_open(options, dbname, &err); CheckNoError(err); CheckGet(db, roptions, "foo", NULL); StartPhase("put"); leveldb_put(db, woptions, "foo", 3, "hello", 5, &err); CheckNoError(err); CheckGet(db, roptions, "foo", "hello"); StartPhase("compactall"); leveldb_compact_range(db, NULL, 0, NULL, 0); CheckGet(db, roptions, "foo", "hello"); StartPhase("compactrange"); leveldb_compact_range(db, "a", 1, "z", 1); CheckGet(db, roptions, "foo", "hello"); StartPhase("writebatch"); { leveldb_writebatch_t* wb = leveldb_writebatch_create(); leveldb_writebatch_put(wb, "foo", 3, "a", 1); leveldb_writebatch_clear(wb); leveldb_writebatch_put(wb, "bar", 3, "b", 1); leveldb_writebatch_put(wb, "box", 3, "c", 1); leveldb_writebatch_delete(wb, "bar", 3); leveldb_write(db, woptions, wb, &err); CheckNoError(err); CheckGet(db, roptions, "foo", "hello"); CheckGet(db, roptions, "bar", NULL); CheckGet(db, roptions, "box", "c"); int pos = 0; leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel); CheckCondition(pos == 3); leveldb_writebatch_destroy(wb); } StartPhase("iter"); { leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions); CheckCondition(!leveldb_iter_valid(iter)); leveldb_iter_seek_to_first(iter); CheckCondition(leveldb_iter_valid(iter)); CheckIter(iter, "box", "c"); leveldb_iter_next(iter); CheckIter(iter, "foo", "hello"); leveldb_iter_prev(iter); CheckIter(iter, "box", "c"); leveldb_iter_prev(iter); CheckCondition(!leveldb_iter_valid(iter)); leveldb_iter_seek_to_last(iter); CheckIter(iter, "foo", "hello"); leveldb_iter_seek(iter, "b", 1); CheckIter(iter, "box", "c"); leveldb_iter_get_error(iter, &err); CheckNoError(err); leveldb_iter_destroy(iter); } StartPhase("approximate_sizes"); { int i; int n = 20000; char keybuf[100]; char valbuf[100]; uint64_t sizes[2]; const char* start[2] = { "a", "k00000000000000010000" }; size_t start_len[2] = { 1, 21 }; const char* limit[2] = { "k00000000000000010000", "z" }; size_t limit_len[2] = { 21, 1 }; leveldb_writeoptions_set_sync(woptions, 0); for (i = 0; i < n; i++) { snprintf(keybuf, sizeof(keybuf), "k%020d", i); snprintf(valbuf, sizeof(valbuf), "v%020d", i); leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf), &err); CheckNoError(err); } leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes); CheckCondition(sizes[0] > 0); CheckCondition(sizes[1] > 0); } StartPhase("property"); { char* prop = leveldb_property_value(db, "nosuchprop"); /* CheckCondition(prop == NULL);*/ prop = leveldb_property_value(db, "leveldb.stats"); CheckCondition(prop != NULL); Free(&prop); } StartPhase("snapshot"); { const leveldb_snapshot_t* snap; snap = leveldb_create_snapshot(db); leveldb_delete(db, woptions, "foo", 3, &err); CheckNoError(err); leveldb_readoptions_set_snapshot(roptions, snap); CheckGet(db, roptions, "foo", "hello"); leveldb_readoptions_set_snapshot(roptions, NULL); CheckGet(db, roptions, "foo", NULL); leveldb_release_snapshot(db, snap); } StartPhase("repair"); { leveldb_close(db); leveldb_options_set_error_if_exists(options, 0); leveldb_repair_db(options, dbname, &err); CheckNoError(err); db = leveldb_open(options, dbname, &err); CheckNoError(err); CheckGet(db, roptions, "foo", NULL); CheckGet(db, roptions, "bar", NULL); CheckGet(db, roptions, "box", "c"); leveldb_options_set_error_if_exists(options, 1); } StartPhase("filter"); for (run = 0; run < 2; run++) { // First run uses custom filter, second run uses bloom filter CheckNoError(err); leveldb_filterpolicy_t* policy; if (run == 0) { policy = leveldb_filterpolicy_create( NULL, FilterDestroy, FilterCreate, FilterKeyMatch, FilterName); } else { policy = leveldb_filterpolicy_create_bloom(10); } // Create new database leveldb_close(db); leveldb_destroy_db(options, dbname, &err); leveldb_options_set_filter_policy(options, policy); db = leveldb_open(options, dbname, &err); CheckNoError(err); leveldb_put(db, woptions, "foo", 3, "foovalue", 8, &err); CheckNoError(err); leveldb_put(db, woptions, "bar", 3, "barvalue", 8, &err); CheckNoError(err); leveldb_compact_range(db, NULL, 0, NULL, 0); fake_filter_result = 1; CheckGet(db, roptions, "foo", "foovalue"); CheckGet(db, roptions, "bar", "barvalue"); if (phase == 0) { // Must not find value when custom filter returns false fake_filter_result = 0; CheckGet(db, roptions, "foo", NULL); CheckGet(db, roptions, "bar", NULL); fake_filter_result = 1; CheckGet(db, roptions, "foo", "foovalue"); CheckGet(db, roptions, "bar", "barvalue"); } leveldb_options_set_filter_policy(options, NULL); leveldb_filterpolicy_destroy(policy); } StartPhase("cleanup"); leveldb_close(db); leveldb_options_destroy(options); leveldb_readoptions_destroy(roptions); leveldb_writeoptions_destroy(woptions); leveldb_cache_destroy(cache); leveldb_comparator_destroy(cmp); leveldb_env_destroy(env); fprintf(stderr, "PASS\n"); return 0; }
int main(int argc, char** argv) { leveldb_t* db; leveldb_comparator_t* cmp; leveldb_cache_t* cache; leveldb_env_t* env; leveldb_options_t* options; leveldb_readoptions_t* roptions; leveldb_writeoptions_t* woptions; char* err = NULL; #if defined(LEVELDB_PLATFORM_WINDOWS) snprintf(dbname, sizeof(dbname), "tmp\\leveldb_c_test"); #else snprintf(dbname, sizeof(dbname), "/tmp/leveldb_c_test-%d", ((int) geteuid())); #endif StartPhase("create_objects"); cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName); env = leveldb_create_default_env(); cache = leveldb_cache_create_lru(100000); options = leveldb_options_create(); leveldb_options_set_comparator(options, cmp); leveldb_options_set_error_if_exists(options, 1); leveldb_options_set_cache(options, cache); leveldb_options_set_env(options, env); leveldb_options_set_info_log(options, NULL); leveldb_options_set_write_buffer_size(options, 100000); leveldb_options_set_paranoid_checks(options, 1); leveldb_options_set_max_open_files(options, 10); leveldb_options_set_block_size(options, 1024); leveldb_options_set_block_restart_interval(options, 8); leveldb_options_set_compression(options, leveldb_no_compression); roptions = leveldb_readoptions_create(); leveldb_readoptions_set_verify_checksums(roptions, 1); leveldb_readoptions_set_fill_cache(roptions, 0); woptions = leveldb_writeoptions_create(); leveldb_writeoptions_set_sync(woptions, 1); StartPhase("destroy"); leveldb_destroy_db(options, dbname, &err); leveldb_free(&err); StartPhase("open_error"); db = leveldb_open(options, dbname, &err); CheckCondition(err != NULL); leveldb_free(&err); StartPhase("open"); leveldb_options_set_create_if_missing(options, 1); db = leveldb_open(options, dbname, &err); CheckNoError(err); CheckGet(db, roptions, "foo", NULL); StartPhase("put"); leveldb_put(db, woptions, "foo", 3, "hello", 5, &err); CheckNoError(err); CheckGet(db, roptions, "foo", "hello"); StartPhase("writebatch"); { int pos = 0; leveldb_writebatch_t* wb = leveldb_writebatch_create(); leveldb_writebatch_put(wb, "foo", 3, "a", 1); leveldb_writebatch_clear(wb); leveldb_writebatch_put(wb, "bar", 3, "b", 1); leveldb_writebatch_put(wb, "box", 3, "c", 1); leveldb_writebatch_delete(wb, "bar", 3); leveldb_write(db, woptions, wb, &err); CheckNoError(err); CheckGet(db, roptions, "foo", "hello"); CheckGet(db, roptions, "bar", NULL); CheckGet(db, roptions, "box", "c"); leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel); CheckCondition(pos == 3); leveldb_writebatch_destroy(wb); } StartPhase("iter"); { leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions); CheckCondition(!leveldb_iter_valid(iter)); leveldb_iter_seek_to_first(iter); CheckCondition(leveldb_iter_valid(iter)); CheckIter(iter, "box", "c"); leveldb_iter_next(iter); CheckIter(iter, "foo", "hello"); leveldb_iter_prev(iter); CheckIter(iter, "box", "c"); leveldb_iter_prev(iter); CheckCondition(!leveldb_iter_valid(iter)); leveldb_iter_seek_to_last(iter); CheckIter(iter, "foo", "hello"); leveldb_iter_seek(iter, "b", 1); CheckIter(iter, "box", "c"); leveldb_iter_get_error(iter, &err); CheckNoError(err); leveldb_iter_destroy(iter); } StartPhase("approximate_sizes"); { int i; int n = 20000; char keybuf[100]; char valbuf[100]; uint64_t sizes[2]; const char* start[2] = { "a", "k00000000000000010000" }; size_t start_len[2] = { 1, 21 }; const char* limit[2] = { "k00000000000000010000", "z" }; size_t limit_len[2] = { 21, 1 }; leveldb_writeoptions_set_sync(woptions, 0); for (i = 0; i < n; i++) { snprintf(keybuf, sizeof(keybuf), "k%020d", i); snprintf(valbuf, sizeof(valbuf), "v%020d", i); leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf), &err); CheckNoError(err); } leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes); CheckCondition(sizes[0] > 0); CheckCondition(sizes[1] > 0); } StartPhase("property"); { char* prop = leveldb_property_value(db, "nosuchprop"); CheckCondition(prop == NULL); prop = leveldb_property_value(db, "leveldb.stats"); CheckCondition(prop != NULL); leveldb_free(&prop); } StartPhase("snapshot"); { const leveldb_snapshot_t* snap; snap = leveldb_create_snapshot(db); leveldb_delete(db, woptions, "foo", 3, &err); CheckNoError(err); leveldb_readoptions_set_snapshot(roptions, snap); CheckGet(db, roptions, "foo", "hello"); leveldb_readoptions_set_snapshot(roptions, NULL); CheckGet(db, roptions, "foo", NULL); leveldb_release_snapshot(db, snap); } StartPhase("repair"); { leveldb_close(db); leveldb_options_set_create_if_missing(options, 0); leveldb_options_set_error_if_exists(options, 0); leveldb_repair_db(options, dbname, &err); CheckNoError(err); db = leveldb_open(options, dbname, &err); CheckNoError(err); CheckGet(db, roptions, "foo", NULL); CheckGet(db, roptions, "bar", NULL); CheckGet(db, roptions, "box", "c"); } StartPhase("cleanup"); leveldb_close(db); leveldb_options_destroy(options); leveldb_readoptions_destroy(roptions); leveldb_writeoptions_destroy(woptions); leveldb_cache_destroy(cache); leveldb_comparator_destroy(cmp); leveldb_env_destroy(env); fprintf(stderr, "PASS\n"); return 0; }
void db_close() { leveldb_close(ldb); leveldb_options_destroy(ldb_options); leveldb_cache_destroy(ldb_cache); }
void db_close(db_t *db) { leveldb_options_destroy(db->opts); leveldb_close(db->db); free(db); }
void db_options_destroy(db_options_t **optr) { leveldb_options_destroy(*optr); *optr = NULL; }
int main(int argc, char *argv[]) { if (argc != 5) { fprintf(stderr, "Usage: ./leveldb_index [leveldb_directory] [sample_input] [index_output] [index_time_file]\n"); return -1; } if ((file = fopen(argv[2], "r")) == NULL) { fprintf(stderr, "Failed to open sample file \"%s\" for reading\n", argv[1]); return -1; } for (i = 0; fscanf(file, "%"PRId64"\t", &cnt) != EOF; ++i) { fgets(buffer, BUFFER_SIZE, file); for (len = strlen(buffer) - 1; len >= 0 && (buffer[len] == '\r' || buffer[len] == '\n'); buffer[len--] = 0); ++len; strcpy(request[i].key, buffer); } fclose(file); if ((file = fopen(argv[4], "w")) == NULL) { fprintf(stderr, "Failed to open time information file \"%s\" for reading\n", argv[4]); return -1; } fprintf(stdout, "Start indexing\n"); srand((unsigned)time(NULL)); options = leveldb_options_create(); leveldb_options_set_create_if_missing(options, 1); // default: write_buffer_size: 4MB, block_cache: 8MB, block_size ~ 4KB leveldb_options_set_write_buffer_size(options, BLK_SIZE * 1024); leveldb_options_set_cache(options, cache = leveldb_cache_create_lru(BLK_SIZE * 2048)); leveldb_options_set_block_size(options, BLK_SIZE); leveldb_options_set_compression(options, 0); db = leveldb_open(options, argv[1], &err); if (err != NULL) { fprintf(stderr, "Failed to open leveldb.\n"); return -1; } leveldb_free(err); roptions = leveldb_readoptions_create(); gettimeofday(&start, NULL); for (i = seekn = tot = 0; i < DISK_INDEX_SIZE; ++i) { val = leveldb_get(db, roptions, request[i].key, strlen(request[i].key), &read_len, &err); if (err != NULL) { fprintf(stderr, "Failed to read from leveldb.\n"); return -1; } leveldb_free(err); if (read_len == 0) request[i].value = 0; else { val[read_len] = 0; sscanf(val, "%"PRId64"", &request[i].value); } leveldb_free(val); if ((i + 1) % 1000 == 0) { gettimeofday(&end, NULL); tot += (int64_t)(end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec; fprintf(stdout, "\rIndexed %"PRId64" items in %"PRId64"us", i + 1, tot); fflush(stdout); fprintf(file, "Indexed %"PRId64" items in %"PRId64"us\n", i + 1, tot); fflush(file); gettimeofday(&start, NULL); } } fprintf(stdout, "\rIndexed %"PRId64" items in %"PRId64"us\n", i, tot); fprintf(file, "Indexed %"PRId64" items in %"PRId64"us\n", i, tot); fflush(file); fclose(file); leveldb_cache_destroy(cache); leveldb_readoptions_destroy(roptions); leveldb_options_destroy(options); leveldb_close(db); fprintf(stdout, "Used %"PRId64" us and seeked %"PRId64" times to index %"PRId64" keys\n", tot, seekn, i); if ((file = fopen(argv[3], "w")) == NULL) { fprintf(stdout, "Failed to open file \"%s\" for storing indexing results\n", argv[3]); return -1; } for (i = 0; i < DISK_INDEX_SIZE; ++i) fprintf(file, "%"PRId64"\t%s\n", request[i].value, request[i].key); fflush(file); fclose(file); return 0; }