github.com/cockroachdb/pebble@v1.1.2/testdata/make-db.cc (about)

     1  // Copyright 2012 The LevelDB-Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This program creates a leveldb db at /tmp/db.
     6  //
     7  // To build and run:
     8  // g++ make-db.cc -lleveldb && ./a.out
     9  
    10  #include <iostream>
    11  #include <sstream>
    12  
    13  #include "rocksdb/db.h"
    14  
    15  template <typename T>
    16  inline std::string ToString(const T& t) {
    17    std::ostringstream s;
    18    s << t;
    19    return s.str();
    20  }
    21  
    22  template <typename T>
    23  inline T FromString(const std::string &str, T val = T()) {
    24    std::istringstream s(str);
    25    s >> val;
    26    return val;
    27  }
    28  
    29  int main(int argc, char** argv) {
    30    rocksdb::Status status;
    31    rocksdb::Options o;
    32    rocksdb::WriteOptions wo;
    33    rocksdb::DB* db;
    34  
    35    o.create_if_missing = true;
    36    o.error_if_exists = true;
    37  
    38    // The program consists of up to 4 stages. If stage is in the range [1, 4],
    39    // the program will exit after the stage'th stage.
    40    // 1. create an empty DB.
    41    // 2. add some key/value pairs.
    42    // 3. close and re-open the DB, which forces a compaction.
    43    // 4. add some more key/value pairs.
    44    if (argc != 2) {
    45      std::cerr << "usage: " << argv[0] << " [1,2,3,4]\n";
    46      return 1;
    47    }
    48  
    49    const int stage = FromString<int>(argv[1]);
    50    if (stage < 1) {
    51      return 0;
    52    }
    53    std::cout << "Stage 1" << std::endl;
    54  
    55    const std::string dbname = "db-stage-" + ToString(stage);
    56    status = rocksdb::DB::Open(o, dbname, &db);
    57    if (!status.ok()) {
    58      std::cerr << "DB::Open " << status.ToString() << std::endl;
    59      return 1;
    60    }
    61  
    62    if (stage < 2) {
    63      return 0;
    64    }
    65    std::cout << "Stage 2" << std::endl;
    66  
    67    status = db->Put(wo, "foo", "one");
    68    if (!status.ok()) {
    69      std::cerr << "DB::Put " << status.ToString() << std::endl;
    70      return 1;
    71    }
    72  
    73    status = db->Put(wo, "bar", "two");
    74    if (!status.ok()) {
    75      std::cerr << "DB::Put " << status.ToString() << std::endl;
    76      return 1;
    77    }
    78  
    79    status = db->Put(wo, "baz", "three");
    80    if (!status.ok()) {
    81      std::cerr << "DB::Put " << status.ToString() << std::endl;
    82      return 1;
    83    }
    84  
    85    status = db->Put(wo, "foo", "four");
    86    if (!status.ok()) {
    87      std::cerr << "DB::Put " << status.ToString() << std::endl;
    88      return 1;
    89    }
    90  
    91    status = db->Delete(wo, "bar");
    92    if (!status.ok()) {
    93      std::cerr << "DB::Delete " << status.ToString() << std::endl;
    94      return 1;
    95    }
    96  
    97    if (stage < 3) {
    98      return 0;
    99    }
   100    std::cout << "Stage 3" << std::endl;
   101  
   102    delete db;
   103    db = NULL;
   104    o.create_if_missing = false;
   105    o.error_if_exists = false;
   106  
   107    status = rocksdb::DB::Open(o, dbname, &db);
   108    if (!status.ok()) {
   109      std::cerr << "DB::Open " << status.ToString() << std::endl;
   110      return 1;
   111    }
   112  
   113    if (stage < 4) {
   114      return 0;
   115    }
   116    std::cout << "Stage 4" << std::endl;
   117  
   118    status = db->Put(wo, "foo", "five");
   119    if (!status.ok()) {
   120      std::cerr << "DB::Put " << status.ToString() << std::endl;
   121      return 1;
   122    }
   123  
   124    status = db->Put(wo, "quux", "six");
   125    if (!status.ok()) {
   126      std::cerr << "DB::Put " << status.ToString() << std::endl;
   127      return 1;
   128    }
   129  
   130    status = db->Delete(wo, "baz");
   131    if (!status.ok()) {
   132      std::cerr << "DB::Delete " << status.ToString() << std::endl;
   133      return 1;
   134    }
   135  
   136    return 0;
   137  }