From 401b8e8085fb630d11850f618a3b33efe84daf6c Mon Sep 17 00:00:00 2001 From: Justin Balthrop Date: Thu, 6 Aug 2009 18:16:38 -0700 Subject: [PATCH] add absolute comparison function --- lib/bdb/simple.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/bdb/simple.rb b/lib/bdb/simple.rb index 1b2ca9d..5da5b92 100644 --- a/lib/bdb/simple.rb +++ b/lib/bdb/simple.rb @@ -19,7 +19,7 @@ class Bdb::Simple @db = Bdb::Db.new @db.flags = Bdb::DB_DUPSORT if dup? @db.btree_compare = lambda do |db, key1, key2| - Marshal.load(key1) <=> Marshal.load(key2) + compare_absolute(Marshal.load(key1), Marshal.load(key2)) end @db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0) end @@ -84,4 +84,22 @@ class Bdb::Simple db.close(0) @db = nil end + + def self.compare_absolute(left, right) + if left.is_a?(Array) and right.is_a?(Array) + left.zip(right) do |l,r| + comp = compare_absolute(l, r) + return comp unless comp == 0 + end + left.size == right.size ? 0 : -1 + elsif left.kind_of?(right.class) or right.kind_of?(left.class) + left <=> right rescue 0 + elsif left.is_a?(NilClass) + -1 + elsif right.is_a?(NilClass) + 1 + else + right.class.name <=> left.class.name + end + end end