github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/leveldb-go/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 #include <iostream> 8 9 #include "leveldb/db.h" 10 11 static const char* dbname = "/tmp/db"; 12 13 // The program consists of up to 4 stages. If stage is in the range [1, 4], 14 // the program will exit after the stage'th stage. 15 // 1. create an empty DB. 16 // 2. add some key/value pairs. 17 // 3. close and re-open the DB, which forces a compaction. 18 // 4. add some more key/value pairs. 19 static const int stage = 4; 20 21 int main(int argc, char** argv) { 22 leveldb::Status status; 23 leveldb::Options o; 24 leveldb::WriteOptions wo; 25 leveldb::DB* db; 26 27 o.create_if_missing = true; 28 o.error_if_exists = true; 29 30 if (stage < 1) { 31 return 0; 32 } 33 cout << "Stage 1" << endl; 34 35 status = leveldb::DB::Open(o, dbname, &db); 36 if (!status.ok()) { 37 cerr << "DB::Open " << status.ToString() << endl; 38 return 1; 39 } 40 41 if (stage < 2) { 42 return 0; 43 } 44 cout << "Stage 2" << endl; 45 46 status = db->Put(wo, "foo", "one"); 47 if (!status.ok()) { 48 cerr << "DB::Put " << status.ToString() << endl; 49 return 1; 50 } 51 52 status = db->Put(wo, "bar", "two"); 53 if (!status.ok()) { 54 cerr << "DB::Put " << status.ToString() << endl; 55 return 1; 56 } 57 58 status = db->Put(wo, "baz", "three"); 59 if (!status.ok()) { 60 cerr << "DB::Put " << status.ToString() << endl; 61 return 1; 62 } 63 64 status = db->Put(wo, "foo", "four"); 65 if (!status.ok()) { 66 cerr << "DB::Put " << status.ToString() << endl; 67 return 1; 68 } 69 70 status = db->Delete(wo, "bar"); 71 if (!status.ok()) { 72 cerr << "DB::Delete " << status.ToString() << endl; 73 return 1; 74 } 75 76 if (stage < 3) { 77 return 0; 78 } 79 cout << "Stage 3" << endl; 80 81 delete db; 82 db = NULL; 83 o.create_if_missing = false; 84 o.error_if_exists = false; 85 86 status = leveldb::DB::Open(o, dbname, &db); 87 if (!status.ok()) { 88 cerr << "DB::Open " << status.ToString() << endl; 89 return 1; 90 } 91 92 if (stage < 4) { 93 return 0; 94 } 95 cout << "Stage 4" << endl; 96 97 status = db->Put(wo, "foo", "five"); 98 if (!status.ok()) { 99 cerr << "DB::Put " << status.ToString() << endl; 100 return 1; 101 } 102 103 status = db->Put(wo, "quux", "six"); 104 if (!status.ok()) { 105 cerr << "DB::Put " << status.ToString() << endl; 106 return 1; 107 } 108 109 status = db->Delete(wo, "baz"); 110 if (!status.ok()) { 111 cerr << "DB::Delete " << status.ToString() << endl; 112 return 1; 113 } 114 115 return 0; 116 }