~ubuntu-branches/ubuntu/quantal/kyotocabinet/quantal

« back to all changes in this revision

Viewing changes to example/kchashex.cc

  • Committer: Package Import Robot
  • Author(s): Shawn Landden
  • Date: 2012-06-07 16:12:07 UTC
  • Revision ID: package-import@ubuntu.com-20120607161207-prbj5blqgzzfl8of
Tags: upstream-1.2.76
ImportĀ upstreamĀ versionĀ 1.2.76

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <kchashdb.h>
 
2
 
 
3
using namespace std;
 
4
using namespace kyotocabinet;
 
5
 
 
6
// main routine
 
7
int main(int argc, char** argv) {
 
8
 
 
9
  // create the database object
 
10
  HashDB db;
 
11
 
 
12
  // open the database
 
13
  if (!db.open("casket.kch", HashDB::OWRITER | HashDB::OCREATE)) {
 
14
    cerr << "open error: " << db.error().name() << endl;
 
15
  }
 
16
 
 
17
  // store records
 
18
  if (!db.set("foo", "hop") ||
 
19
      !db.set("bar", "step") ||
 
20
      !db.set("baz", "jump")) {
 
21
    cerr << "set error: " << db.error().name() << endl;
 
22
  }
 
23
 
 
24
  // retrieve a record
 
25
  string value;
 
26
  if (db.get("foo", &value)) {
 
27
    cout << value << endl;
 
28
  } else {
 
29
    cerr << "get error: " << db.error().name() << endl;
 
30
  }
 
31
 
 
32
  // traverse records
 
33
  DB::Cursor* cur = db.cursor();
 
34
  cur->jump();
 
35
  string ckey, cvalue;
 
36
  while (cur->get(&ckey, &cvalue, true)) {
 
37
    cout << ckey << ":" << cvalue << endl;
 
38
  }
 
39
  delete cur;
 
40
 
 
41
  // close the database
 
42
  if (!db.close()) {
 
43
    cerr << "close error: " << db.error().name() << endl;
 
44
  }
 
45
 
 
46
  return 0;
 
47
}