Skip to content

Commit

Permalink
works with JRuby (1.1.6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Jan 23, 2009
1 parent 23b8ebc commit 8e65552
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/rufus/tokyo.rb
Expand Up @@ -71,10 +71,12 @@ module Func
attach_function :tcadbiterinit, [ :pointer ], :int
attach_function :tcadbiternext2, [ :pointer ], :string

def self.method_missing (m, *args)
mm = "tcadb#{m}"
self.respond_to?(mm) ? self.send(mm, *args) : super
end
#def self.method_missing (m, *args)
# mm = "tcadb#{m}"
# self.respond_to?(mm) ? self.send(mm, *args) : super
#end
#
# makes JRuby unhappy
end

#
Expand All @@ -96,21 +98,21 @@ class Cabinet
#
def initialize (name)

@db = Rufus::Tokyo::Func.new
@db = Rufus::Tokyo::Func.tcadbnew

name = '*' if name == :hash # in memory hash database
name = '+' if name == :tree # in memory B+ tree database

(Rufus::Tokyo::Func.open(@db, name) == 1) ||
(Rufus::Tokyo::Func.tcadbopen(@db, name) == 1) ||
raise("failed to open/create db '#{name}'")
end

def []= (k, v)
Rufus::Tokyo::Func.put2(@db, k, v)
Rufus::Tokyo::Func.tcadbput2(@db, k, v)
end

def [] (k)
Rufus::Tokyo::Func.get2(@db, k) rescue nil
Rufus::Tokyo::Func.tcadbget2(@db, k) rescue nil
end

#
Expand All @@ -119,38 +121,38 @@ def [] (k)
#
def delete (k)
v = self[k]
(Rufus::Tokyo::Func.out2(@db, k) == 1) ? v : nil
(Rufus::Tokyo::Func.tcadbout2(@db, k) == 1) ? v : nil
end

#
# Returns the number of records in the 'cabinet'
#
def size
Rufus::Tokyo::Func.rnum(@db)
Rufus::Tokyo::Func.tcadbrnum(@db)
end

#
# Returns the 'weight' of the db (in bytes)
#
def weight
Rufus::Tokyo::Func.size(@db)
Rufus::Tokyo::Func.tcadbsize(@db)
end

#
# Closes the cabinet, returns true in case of success.
#
def close
(Rufus::Tokyo::Func.close(@db) == 1)
(Rufus::Tokyo::Func.tcadbclose(@db) == 1)
end

#
# The classical Ruby each (unlocks the power of the Enumerable mixin)
#
def each

Rufus::Tokyo::Func.iterinit(@db) # concurrent access ??
Rufus::Tokyo::Func.tcadbiterinit(@db) # concurrent access ??

while (k = (Rufus::Tokyo::Func.iternext2(@db) rescue nil))
while (k = (Rufus::Tokyo::Func.tcadbiternext2(@db) rescue nil))
yield(k, self[k])
end
end
Expand Down

0 comments on commit 8e65552

Please sign in to comment.