Permalink
Browse files

implemented #putdup and #get4 (#getdup) for Rufus::Tokyo::Cabinet

  • Loading branch information...
jmettraux committed Aug 28, 2009
1 parent 6025075 commit a27fc067db2957ae2341d0b09d5d568d5a3f8d93
Showing with 97 additions and 1 deletion.
  1. +41 −0 lib/rufus/tokyo/cabinet/abstract.rb
  2. +13 −1 lib/rufus/tokyo/cabinet/lib.rb
  3. +43 −0 spec/cabinet_btree_spec.rb
@@ -527,14 +527,55 @@ def tranabort
libcall(:tcadbtranabort)
end
+ #--
+ #
+ # BTREE methods
+ #
+ #++
+
+ # This is a B+ Tree method only, puts a value for a key who has
+ # [potentially] multiple values.
+ #
+ def putdup (k, v)
+
+ lib.tcbdbputdup(
+ as_btree, k, Rufus::Tokyo.blen(k), v, Rufus::Tokyo.blen(v))
+ end
+
+ # This is a B+ Tree method only, returns all the values for a given
+ # key.
+ #
+ def get4 (k)
+
+ l = lib.tcbdbget4(as_btree, k, Rufus::Tokyo.blen(k))
+
+ Rufus::Tokyo::List.new(l).release
+ end
+ alias :getdup :get4
+
protected
+ # Returns the pointer to the btree hiding behind the abstract structure.
+ #
+ # Will raise an argument error if the structure behind the abstract db
+ # is not a B+ Tree structure.
+ #
+ def as_btree
+
+ raise(ArgumentError.new("cannot call B+ Tree function on #{@path}")) \
+ if ! @path.match(/\.tcb$/)
+
+ lib.tcadbreveal(@db)
+ end
+
+ #--
#def check_transaction_support
# raise(TokyoError.new(
# "The version of Tokyo Cabinet you're using doesn't support " +
# "transactions for non-table structures. Upgrade to TC >= 1.4.13.")
# ) unless lib.respond_to?(:tcadbtranbegin)
#end
+ #++
# Wrapping tcadbmisc or tcrdbmisc
# (and taking care of freeing the list_pointer)
@@ -35,7 +35,9 @@ module CabinetLib #:nodoc#
#
# find Tokyo Cabinet lib
- paths = Array(ENV['TOKYO_CABINET_LIB'] || Dir['/{opt,usr}/{,local/}lib{,64}/libtokyocabinet.{dylib,so*}'])
+ paths =
+ Array(ENV['TOKYO_CABINET_LIB'] ||
+ Dir['/{opt,usr}/{,local/}lib{,64}/libtokyocabinet.{dylib,so*}'])
begin
@@ -102,6 +104,8 @@ class << self
attfunc :addint, :tcadbaddint, [ :pointer, :string, :int, :int ], :int
attfunc :adddouble, :tcadbadddouble, [ :pointer, :string, :int, :double ], :double
+ attfunc :tcadbreveal, [ :pointer ], :pointer
+
# since TC 1.4.13
attfunc :tcadbtranbegin, [ :pointer ], :int
attfunc :tcadbtrancommit, [ :pointer ], :int
@@ -183,6 +187,14 @@ class << self
# since TC 1.4.12
attfunc :qry_count, :tctdbqrycount, [ :pointer ], :int
+ #
+ # tcbdb functions
+ #
+ # http://tokyocabinet.sourceforge.net/spex-en.html#tcbdbapi
+
+ attfunc :tcbdbputdup, [ :pointer, :pointer, :int, :pointer, :int ], :int
+ attfunc :tcbdbget4, [ :pointer, :pointer, :int ], :pointer
+
#
# tcmap functions
#
View
@@ -0,0 +1,43 @@
+
+#
+# Specifying rufus-tokyo
+#
+# Fri Aug 28 08:58:37 JST 2009
+#
+
+require File.dirname(__FILE__) + '/spec_base'
+
+require 'rufus/tokyo'
+
+FileUtils.mkdir('tmp') rescue nil
+
+
+describe 'Rufus::Tokyo::Cabinet .tcb' do
+
+ before do
+ @db = Rufus::Tokyo::Cabinet.new('tmp/cabinet_btree_spec.tcb')
+ @db.clear
+ end
+ after do
+ @db.close
+ end
+
+ it 'should accept duplicate values' do
+
+ @db.putdup('a', 'a0')
+ @db.putdup('a', 'a1')
+
+ @db.getdup('a').should.equal([ 'a0', 'a1' ])
+ end
+end
+
+describe 'Rufus::Tokyo::Cabinet .tcb methods' do
+
+ it 'should fail on other structures' do
+
+ @db = Rufus::Tokyo::Cabinet.new('tmp/cabinet_btree_spec.tch')
+
+ lambda { @db.putdup('a', 'a0') }.should.raise(ArgumentError)
+ end
+end
+

0 comments on commit a27fc06

Please sign in to comment.