Open egmkang opened 7 years ago
here is the storage
wrapper codes:
#include "storage.h"
#include <system.h>
#include <leveldb/db.h>
#include <leveldb/cache.h>
#include <logger.h>
const int32_t kDBCacheSize = 100 * 1048576; //100MB
leveldb::DB* db = NULL;
leveldb::Options options;
const char* db_name = "./db";
namespace storage {
void Init() {
options.create_if_missing = true;
//options.compression = leveldb::kNoCompression;
if (!options.block_cache)
options.block_cache = leveldb::NewLRUCache(kDBCacheSize);
leveldb::Status status = leveldb::DB::Open(options, db_name, &db);
if (!status.ok()) {
ERROR_LOG(logger)("Init LevelDB Fail");
} else {
db->CompactRange(NULL, NULL);
TRACE_LOG(logger)("Init LevelDB Success");
}
}
void Set(const std::string& key, const std::string& value) {
if (db) {
INFO_LOG(logger)("LevelDB Put:%s", key.c_str());
leveldb::Status s = db->Put(leveldb::WriteOptions(), key, value);
DEBUG_LOG(logger)("LevelDB Put:%s, Length:%lu, Success:%d", key.c_str(), value.size(), s.ok());
}
}
std::string Get(const std::string& key) {
std::string value;
INFO_LOG(logger)("LevelDB Get:%s", key.c_str());
leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value);
if (!s.ok()) value.clear();
DEBUG_LOG(logger)("LevelDB Get:%s, Success:%d", key.c_str(), s.ok());
return value;
}
void Delete(const std::string& key) {
if (db) {
INFO_LOG(logger)("LevelDB Delete:%s", key.c_str());
leveldb::Status s = db->Delete(leveldb::WriteOptions(), key);
DEBUG_LOG(logger)("LevelDB Delete:%s, Success:%d", key.c_str(), s.ok());
}
}
void Flush() {
if (db) {
leveldb::WriteOptions write_options;
write_options.sync = true;
db->Put(write_options, "FLUSH", "FLUSH");
DEBUG_LOG(logger)("LevelDB Flush");
}
}
}
thread 1:
thread 2:
the two threads are waiting for a long time. how can i solve this problem? leveldb's diectionary size is 4.6M.