Rate This Document
Findability
Accuracy
Completeness
Readability

Merge

  1. Go to the /home directory.
    cd /home/
  2. Create the following source code:
    vim rocksdb_test_merge.cpp
  3. Add code content.
    #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;
    }
  4. Create a database directory.
    mkdir -p /home/rocksdb/rkdb2
  5. Compile the code.
    g++ -o rocksdb_test_merge --std=c++11 rocksdb_test_merge.cpp -lrocksdb -ldl
  6. Run the executable file.
    ./rocksdb_test_merge

  7. Query data in the database.
    ldb --db=/home/rocksdb/rkdb2 scan