Skip to content

Instantly share code, notes, and snippets.

@rust
Created January 9, 2009 02:25
Show Gist options
  • Save rust/44996 to your computer and use it in GitHub Desktop.
Save rust/44996 to your computer and use it in GitHub Desktop.
require 'tokyocabinet'
include TokyoCabinet
# create the object
hdb = HDB::new
# open the database
if(!hdb.open("casket.hdb", 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment