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
|
require 'tokyocabinet'
include TokyoCabinet
# create the object
adb = ADB::new
# open the database
if !adb.open("casket.tch")
STDERR.printf("open error\n")
end
# store records
if !adb.put("foo", "hop") ||
!adb.put("bar", "step") ||
!adb.put("baz", "jump")
STDERR.printf("put error\n")
end
# retrieve records
value = adb.get("foo")
if value
printf("%s\n", value)
else
STDERR.printf("get error\n")
end
# traverse records
adb.iterinit
while key = adb.iternext
value = adb.get(key)
if value
printf("%s:%s\n", key, value)
end
end
# hash-like usage
adb["quux"] = "touchdown"
printf("%s\n", adb["quux"])
adb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the database
if !adb.close
STDERR.printf("close error\n")
end
|