1 测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| int main() { rocksdb::Options options; options.create_if_missing = true;
std::string dbName = "/tmp/rocksdb_ctest_read"; std::string walDir = dbName + "/wal"; std::string sstDir = dbName + "/sst"; options.wal_dir = walDir; std::vector<rocksdb::DbPath> sstPaths = {{sstDir + "/flash_path", 512}, {sstDir + "/hard_drive", 1024}}; options.db_paths = sstPaths;
auto* env = rocksdb::Env::Default(); env->CreateDirIfMissing(dbName); env->CreateDirIfMissing(walDir); env->CreateDirIfMissing(sstDir); env->CreateDirIfMissing(sstDir + "/flash_path"); env->CreateDirIfMissing(sstDir + "/hard_drive");
std::unique_ptr<rocksdb::DB> db; auto s = rocksdb::DB::Open(options, dbName, &db); assert(s.ok()); s = db->Put(rocksdb::WriteOptions(), "hello", "world"); assert(s.ok()); std::string val; s = db->Get(rocksdb::ReadOptions(), "hello", &val); assert(s.ok()); std::cout<<"key=hello, value="<<val<<std::endl; return 0; }
|
2 关于字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| virtual inline Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family, const Slice& key, std::string* value) final { assert(value != nullptr); PinnableSlice pinnable_val(value); assert(!pinnable_val.IsPinned()); auto s = Get(options, column_family, key, &pinnable_val); if (s.ok() && pinnable_val.IsPinned()) { value->assign(pinnable_val.data(), pinnable_val.size()); } return s; }
|
虽然用的是std::string,名称叫string,但是不能只是狭隘地理解成字符串,而是字节序列。针对string,RocksDB有两个自己的场景优化
3