Merge接口
- 进入“/home”目录。
1
cd /home/
- 创建如下源代码。
1
vim rocksdb_test_merge.cpp
- 添加代码内容。
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <iostream> #include <vector> #include <rocksdb/db.h> #include <rocksdb/options.h> #include <rocksdb/table.h> #include <rocksdb/merge_operator.h> #include <rocksdb/filter_policy.h> #include <rocksdb/perf_context.h> #include <rocksdb/iostats_context.h> #include <rocksdb/trace_reader_writer.h> #include "rocksdb/utilities/merge_operators.h" using namespace rocksdb; using namespace std; int main() { DB* db; Options options; options.create_if_missing = true; //no compression options.compression = kNoCompression; BlockBasedTableOptions table_options; table_options.no_block_cache = true; table_options.cache_index_and_filter_blocks = false; options.table_factory.reset(NewBlockBasedTableFactory(table_options)); options.merge_operator = MergeOperators::CreateStringAppendOperator(); Status s=rocksdb::DB:: Open(options,"/home/rocksdb/rkdb2",&db); if(!s.ok()){ cout<<"open rocksdb failed:"<<s.ToString()<<endl; delete db; exit(-1); } cout<<"open rocksdb success!"<<endl; //Merge operator int j = 0; string key = to_string(j); string value; char buf[8]; //EncodeFixed64(buf,2); s = db->Merge(WriteOptions(),key,"1"); //need change if(!s.ok()){ cout << "Merge value failed:" << s.ToString()<<endl; delete db; exit(-1); } s = db->Merge(WriteOptions(),key,"2"); db->Flush(FlushOptions()); if(!s.ok()){ cout << "Merge value failed:" << s.ToString()<<endl; delete db; exit(-1); } s = db->Get(ReadOptions(),key,&value); if(!s.ok()){ cout << "Merge value failed:" << s.ToString()<<endl; delete db; exit(-1); } cout << "Get merge value: " << value << "\n value of size:" << value.size() << endl; }
- 创建数据库目录。
1
mkdir -p /home/rocksdb/rkdb2
- 编译代码。
1
g++ -o rocksdb_test_merge --std=c++11 rocksdb_test_merge.cpp -lrocksdb -ldl
- 执行可执行文件。
1
./rocksdb_test_merge
- 查询库中的数据。
1
ldb --db=/home/rocksdb/rkdb2 scan
父主题: 验证RocksDB接口