static int binlog_file_add(binlog *bl, void *buf, unsigned int len) { int ret; /* bail out early if there's no room */ if (bl->file_size + (off_t)len > bl->max_file_size) return BINLOG_ENOSPC; ret = binlog_open(bl); if (ret < 0) return ret; ret = safe_write(bl, &len, sizeof(len)); if (ret) return ret; ret = safe_write(bl, buf, len); bl->file_size += len + sizeof(len); bl->file_entries++; return ret; }
struct _leveldb_stuff *ldb_initialize(char *path) { struct _leveldb_stuff *ldbs = NULL; leveldb_cache_t *cache; leveldb_filterpolicy_t *policy; char* err = NULL; ldbs = malloc(sizeof(struct _leveldb_stuff)); memset(ldbs, 0, sizeof(struct _leveldb_stuff)); ldbs->options = leveldb_options_create(); snprintf(ldbs->dbname, sizeof(ldbs->dbname), "%s/%s", LDB_WORK_PATH, path); cache = leveldb_cache_create_lru(LDB_CACHE_LRU_SIZE); policy = leveldb_filterpolicy_create_bloom(LDB_BLOOM_KEY_SIZE); leveldb_options_set_filter_policy(ldbs->options, policy); leveldb_options_set_cache(ldbs->options, cache); leveldb_options_set_block_size(ldbs->options, LDB_BLOCK_SIZE); leveldb_options_set_write_buffer_size(ldbs->options, LDB_WRITE_BUFFER_SIZE); leveldb_options_set_compaction_speed(ldbs->options, g_cfg_pool.ldb_compaction_speed); #if defined(OPEN_COMPRESSION) leveldb_options_set_compression(ldbs->options, leveldb_snappy_compression); #else leveldb_options_set_compression(ldbs->options, leveldb_no_compression); #endif /* R */ ldbs->roptions = leveldb_readoptions_create(); leveldb_readoptions_set_verify_checksums(ldbs->roptions, 1); leveldb_readoptions_set_fill_cache(ldbs->roptions, 1);/* set 1 is faster */ /* W */ ldbs->woptions = leveldb_writeoptions_create(); #ifdef SYNC_PUT leveldb_writeoptions_set_sync(ldbs->woptions, 1); #else leveldb_writeoptions_set_sync(ldbs->woptions, 0); #endif /* B */ ldbs->wbatch = leveldb_writebatch_create(); leveldb_options_set_create_if_missing(ldbs->options, 1); ldbs->db = leveldb_open(ldbs->options, ldbs->dbname, &err); if (err) { fprintf(stderr, "%s", err); leveldb_free(err); err = NULL; free(ldbs); return NULL ; } /* initial binlog. */ binlog_open(ldbs, g_cfg_pool.has_slave); /* initial slave. */ if (slave_open(ldbs, g_cfg_pool.has_slave, g_cfg_pool.slave_ip, g_cfg_pool.slave_wport) != 0) { return NULL; } return ldbs; }