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
|