File: tchdbex.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 (50 lines) | stat: -rw-r--r-- 963 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
require 'tokyocabinet'
include TokyoCabinet

# create the object
hdb = HDB::new

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

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

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

# traverse records
hdb.iterinit
while key = hdb.iternext
  value = hdb.get(key)
  if value
    printf("%s:%s\n", key, value)
  end
end

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

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