File: tcbdbex.rb

package info (click to toggle)
ruby-tokyocabinet 1.31-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,760 kB
  • sloc: ruby: 3,594; ansic: 3,387; sh: 29; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 984 bytes parent folder | download | duplicates (6)
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
require 'tokyocabinet'
include TokyoCabinet

# create the object
bdb = BDB::new

# open the database
if !bdb.open("casket.tcb", BDB::OWRITER | BDB::OCREAT)
  ecode = bdb.ecode
  STDERR.printf("open error: %s\n", bdb.errmsg(ecode))
end

# store records
if !bdb.put("foo", "hop") ||
    !bdb.put("bar", "step") ||
    !bdb.put("baz", "jump")
  ecode = bdb.ecode
  STDERR.printf("put error: %s\n", bdb.errmsg(ecode))
end

# retrieve records
value = bdb.get("foo")
if value
  printf("%s\n", value)
else
  ecode = bdb.ecode
  STDERR.printf("get error: %s\n", bdb.errmsg(ecode))
end

# traverse records
cur = BDBCUR::new(bdb)
cur.first
while key = cur.key
  value = cur.val
  if value
    printf("%s:%s\n", key, value)
  end
  cur.next
end

# hash-like usage
bdb["quux"] = "touchdown"
printf("%s\n", bdb["quux"])
bdb.each do |key, value|
  printf("%s:%s\n", key, value)
end

# close the database
if !bdb.close
  ecode = bdb.ecode
  STDERR.printf("close error: %s\n", bdb.errmsg(ecode))
end