File: make-db.cc

package info (click to toggle)
golang-github-golang-leveldb 0.0~git20161231.0.3435554-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,004 kB
  • sloc: cpp: 166; makefile: 11
file content (119 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download | duplicates (2)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2012 The LevelDB-Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This program creates a leveldb db at /tmp/db.
//
// To build and run:
// g++ make-db.cc -lleveldb && ./a.out

#include <iostream>

#include "leveldb/db.h"

static const char* dbname = "/tmp/db";

// The program consists of up to 4 stages. If stage is in the range [1, 4],
// the program will exit after the stage'th stage.
// 1. create an empty DB.
// 2. add some key/value pairs.
// 3. close and re-open the DB, which forces a compaction.
// 4. add some more key/value pairs.
static const int stage = 4;

int main(int argc, char** argv) {
  leveldb::Status status;
  leveldb::Options o;
  leveldb::WriteOptions wo;
  leveldb::DB* db;

  o.create_if_missing = true;
  o.error_if_exists = true;

  if (stage < 1) {
    return 0;
  }
  std::cout << "Stage 1" << std::endl;

  status = leveldb::DB::Open(o, dbname, &db);
  if (!status.ok()) {
    std::cerr << "DB::Open " << status.ToString() << std::endl;
    return 1;
  }

  if (stage < 2) {
    return 0;
  }
  std::cout << "Stage 2" << std::endl;

  status = db->Put(wo, "foo", "one");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Put(wo, "bar", "two");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Put(wo, "baz", "three");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Put(wo, "foo", "four");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Delete(wo, "bar");
  if (!status.ok()) {
    std::cerr << "DB::Delete " << status.ToString() << std::endl;
    return 1;
  }

  if (stage < 3) {
    return 0;
  }
  std::cout << "Stage 3" << std::endl;

  delete db;
  db = NULL;
  o.create_if_missing = false;
  o.error_if_exists = false;

  status = leveldb::DB::Open(o, dbname, &db);
  if (!status.ok()) {
    std::cerr << "DB::Open " << status.ToString() << std::endl;
    return 1;
  }

  if (stage < 4) {
    return 0;
  }
  std::cout << "Stage 4" << std::endl;

  status = db->Put(wo, "foo", "five");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Put(wo, "quux", "six");
  if (!status.ok()) {
    std::cerr << "DB::Put " << status.ToString() << std::endl;
    return 1;
  }

  status = db->Delete(wo, "baz");
  if (!status.ok()) {
    std::cerr << "DB::Delete " << status.ToString() << std::endl;
    return 1;
  }

  return 0;
}