ninjudd-bdb 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bdb.gemspec +1 -1
  2. data/lib/bdb/simple.rb +67 -38
  3. metadata +1 -1
data/bdb.gemspec CHANGED
@@ -2,7 +2,7 @@ BDB_SPEC = Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.required_ruby_version = '>=1.8.4'
4
4
  s.name = "bdb"
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
  s.authors = ["Matt Bauer", "Dan Janowski"]
7
7
  s.email = "bauer@pedalbrain.com"
8
8
  s.summary = "A Ruby interface to BerkeleyDB"
data/lib/bdb/simple.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'bdb'
1
+ require 'bdb' if not defined?(Bdb)
2
2
 
3
3
  class Bdb::Simple
4
4
  include Enumerable
@@ -19,7 +19,7 @@ class Bdb::Simple
19
19
  @db = Bdb::Db.new
20
20
  @db.flags = Bdb::DB_DUPSORT if dup?
21
21
  @db.btree_compare = lambda do |db, key1, key2|
22
- compare_absolute(Marshal.load(key1), Marshal.load(key2))
22
+ self.class.compare_absolute(Marshal.load(key1), Marshal.load(key2))
23
23
  end
24
24
  @db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
25
25
  end
@@ -30,33 +30,16 @@ class Bdb::Simple
30
30
  db[Marshal.dump(key)] = Marshal.dump(value)
31
31
  end
32
32
 
33
+ def delete(key)
34
+ db.del(nil, Marshal.dump(key), 0)
35
+ end
36
+
33
37
  def [](key)
34
- if key.kind_of?(Range)
35
- values = []
36
- cursor = db.cursor(nil, 0)
37
- k,v = cursor.get(Marshal.dump(key.first), nil, Bdb::DB_SET_RANGE)
38
- while k and key.include?(Marshal.load(k))
39
- values << Marshal.load(v)
40
- k, v = cursor.get(nil, nil, Bdb::DB_NEXT)
41
- end
42
- cursor.close
43
- values
38
+ if key.kind_of?(Range) or dup?
39
+ Bdb::SimpleSet.new(db, key)
44
40
  else
45
- key = Marshal.dump(key)
46
- if dup?
47
- values = []
48
- cursor = db.cursor(nil, 0)
49
- k,v = cursor.get(key, nil, Bdb::DB_SET)
50
- while data
51
- values << Marshal.load(v)
52
- k,v = cursor.get(nil, nil, Bdb::DB_NEXT_DUP)
53
- end
54
- cursor.close
55
- values
56
- else
57
- v = db.get(nil, key, nil, 0)
58
- Marshal.load(v) if v
59
- end
41
+ v = db.get(nil, Marshal.dump(key), nil, 0)
42
+ Marshal.load(v) if v
60
43
  end
61
44
  end
62
45
 
@@ -86,20 +69,66 @@ class Bdb::Simple
86
69
  end
87
70
 
88
71
  def self.compare_absolute(left, right)
89
- if left.is_a?(Array) and right.is_a?(Array)
90
- left.zip(right) do |l,r|
91
- comp = compare_absolute(l, r)
92
- return comp unless comp == 0
72
+ if left.class == right.class
73
+ if left.is_a?(Array) and right.is_a?(Array)
74
+ # Arrays: compare one element at a time.
75
+ left.zip(right) do |l,r|
76
+ comp = compare_absolute(l, r)
77
+ return comp unless comp == 0
78
+ end
79
+ left.size == right.size ? 0 : -1
80
+ elsif left.is_a?(Hash) and right.is_a?(Hash)
81
+ # Hashes: sort the keys and compare as an array of arrays.
82
+ left = left.to_a.sort {|a,b| compare_absolute(a[0],b[0])}
83
+ right = right.to_a.sort {|a,b| compare_absolute(a[0],b[0])}
84
+ compare_absolute(left, right)
85
+ else
86
+ begin
87
+ # Try to use the spaceship operator.
88
+ left <=> right
89
+ rescue NoMethodError => e
90
+ left.hash <=> right.hash
91
+ end
93
92
  end
94
- left.size == right.size ? 0 : -1
95
- elsif left.kind_of?(right.class) or right.kind_of?(left.class)
96
- left <=> right rescue 0
97
- elsif left.is_a?(NilClass)
98
- -1
99
- elsif right.is_a?(NilClass)
100
- 1
101
93
  else
94
+ # Nil is the smallest. Hash is the largest. All other objects are sorted by class name.
95
+ return -1 if left.is_a?(NilClass)
96
+ return 1 if right.is_a?(NilClass)
97
+ return 1 if left.is_a?(Hash)
98
+ return -1 if right.is_a?(Hash)
99
+
100
+ # Compare class names and hashes as a last resort if that fails.
102
101
  right.class.name <=> left.class.name
103
102
  end
104
103
  end
105
104
  end
105
+
106
+ class Bdb::SimpleSet
107
+ include Enumerable
108
+
109
+ def initialize(db, key)
110
+ @db = db
111
+ @key = key
112
+ end
113
+ attr_reader :db, :key
114
+
115
+ def each
116
+ if key.kind_of?(Range)
117
+ cursor = db.cursor(nil, 0)
118
+ k,v = cursor.get(Marshal.dump(key.first), nil, Bdb::DB_SET_RANGE)
119
+ while k and key.include?(Marshal.load(k))
120
+ yield Marshal.load(v)
121
+ k, v = cursor.get(nil, nil, Bdb::DB_NEXT)
122
+ end
123
+ cursor.close
124
+ else
125
+ cursor = db.cursor(nil, 0)
126
+ k,v = cursor.get(Marshal.dump(key), nil, Bdb::DB_SET)
127
+ while k
128
+ yield Marshal.load(v)
129
+ k,v = cursor.get(nil, nil, Bdb::DB_NEXT_DUP)
130
+ end
131
+ cursor.close
132
+ end
133
+ end
134
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjudd-bdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Bauer