sbdb 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Denis Knauf <denis dot knauf at gmail dot com>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
data/lib/sbdb.rb CHANGED
@@ -10,10 +10,16 @@ module SBDB
10
10
  RDONLY = Bdb::DB_RDONLY
11
11
  READONLY = RDONLY
12
12
 
13
- def btree( *p) Btree.new *p end
14
- def hash( *p) Hash.new *p end
15
- def recno( *p) Recno.new *p end
16
- def queue( *p) Queue.new *p end
17
- def unknown( *p) Unknown.new *p end
13
+ def btree( *ps) Btree.new *ps end
14
+ def hash( *ps) Hash.new *ps end
15
+ def recno( *ps) Recno.new *ps end
16
+ def queue( *ps) Queue.new *ps end
17
+ def unknown( *ps) Unknown.new *ps end
18
18
  alias open_db unknown
19
+
20
+ def self.raise_barrier *ps, &e
21
+ e.call *ps
22
+ rescue Object
23
+ $stderr.puts [$!.class,$!,$!.backtrace].inspect
24
+ end
19
25
  end
data/lib/sbdb/cursor.rb CHANGED
@@ -10,21 +10,37 @@ module SBDB
10
10
  attr_reader :db
11
11
 
12
12
  include Enumerable
13
- def bdb_object() @cursor end
14
- def close() @cursor.close end
15
- def get( k, v, f) @cursor.get( k, v, f) end
16
- def count() @cursor.count end
17
- def first( k = nil, v = nil) get k, v, FIRST end
18
- def last( k = nil, v = nil) get k, v, LAST end
19
- def next( k = nil, v = nil) get k, v, NEXT end
20
- def prev( k = nil, v = nil) get k, v, PREV end
13
+ def bdb_object
14
+ @cursor
15
+ end
16
+ def close
17
+ @cursor.close
18
+ end
19
+ def get key, val, flg
20
+ @cursor.get key, val, flg
21
+ end
22
+ def count
23
+ @cursor.count
24
+ end
25
+ def first key = nil, val = nil
26
+ get key, val, FIRST
27
+ end
28
+ def last key = nil, val = nil
29
+ get key, val, LAST
30
+ end
31
+ def next key = nil, val = nil
32
+ get key, val, NEXT
33
+ end
34
+ def prev key = nil, val = nil
35
+ get key, val, PREV
36
+ end
21
37
 
22
- def self.new *p
23
- x = super *p
24
- return x unless block_given?
25
- begin yield x
26
- ensure x.close
27
- end
38
+ def self.new *ps
39
+ ret = obj = super( *ps)
40
+ begin ret = yield obj
41
+ ensure obj.close
42
+ end if block_given?
43
+ ret
28
44
  end
29
45
 
30
46
  def initialize ref
@@ -33,25 +49,25 @@ module SBDB
33
49
  when Bdb::Db::Cursor then [ref]
34
50
  else [ref.bdb_object.cursor( nil, 0), ref]
35
51
  end
36
- if [Recno, Queue].any? {|t| t === @db }
37
- def get k, v, f
38
- l, w = @cursor.get( k, v, f)
39
- l.nil? ? nil : [l.unpack('I')[0], w]
52
+ if [Recno, Queue].any? {|dbt| dbt === @db }
53
+ def get *ps
54
+ key, val = @cursor.get( *ps)
55
+ key.nil? ? nil : [key.unpack('I')[0], val]
40
56
  end
41
57
  end
42
58
  end
43
59
 
44
- def reverse k = nil, v = nil, &e
45
- each k, v, LAST, PREV, &e
60
+ def reverse key = nil, val = nil, &exe
61
+ each key, val, LAST, PREV, &exe
46
62
  end
47
63
 
48
- def each k = nil, v = nil, f = nil, n = nil
49
- return Enumerator.new( self, :each, k, v, f, n) unless block_given?
50
- n ||= NEXT
51
- e = get k, v, f || FIRST
52
- return unless e
53
- yield *e
54
- yield *e while e = get( k, v, n)
64
+ def each key = nil, val = nil, flg = nil, nxt = nil
65
+ return Enumerator.new( self, :each, key, val, flg, nxt) unless block_given?
66
+ nxt ||= NEXT
67
+ ent = get key, val, flg || FIRST
68
+ return unless ent
69
+ yield *ent
70
+ yield *ent while ent = get( key, val, nxt)
55
71
  nil
56
72
  end
57
73
  end
data/lib/sbdb/db.rb CHANGED
@@ -18,55 +18,49 @@ module SBDB
18
18
  include Enumerable
19
19
  def bdb_object() @db end
20
20
  def sync() @db.sync end
21
- def close( f = nil) @db.close f || 0 end
22
- def cursor( &e) Cursor.new self, &e end
21
+ def close( flg = nil) @db.close flg || 0 end
22
+ def cursor( &exe) Cursor.new self, &exe end
23
23
 
24
- def at k, txn = nil
25
- @db.get _txn(txn), k.nil? ? nil : k.to_s, nil, 0
24
+ def at key, txn = nil
25
+ @db.get _txn(txn), key.nil? ? nil : key.to_s, nil, 0
26
26
  rescue Bdb::KeyEmpty
27
27
  return nil
28
28
  end
29
29
  alias [] at
30
30
 
31
- def put k, v, txn = nil
32
- if v.nil?
33
- @db.del _txn(txn), k.to_s, 0
31
+ def put key, val, txn = nil
32
+ if val.nil?
33
+ @db.del _txn(txn), key.to_s, 0
34
34
  else
35
- @db.put _txn(txn), k.nil? ? nil : k.to_s, v.to_s, 0
35
+ @db.put _txn(txn), key.nil? ? nil : key.to_s, val.to_s, 0
36
36
  end
37
37
  end
38
-
39
- def []= k, v
40
- put k, v
38
+
39
+ def []= key, val
40
+ put key, val
41
41
  end
42
42
 
43
- def delete k, txn = nil
44
- @db.del _txn(txn), k.to_s
43
+ def delete key, txn = nil
44
+ @db.del _txn(txn), key.to_s
45
45
  end
46
46
  alias del delete
47
47
 
48
48
  class << self
49
- def new *p, &e
50
- x = super *p
51
- return x unless e
52
- begin e.call x
49
+ def new *ps, &exe
50
+ ret = obj = super( *ps)
51
+ begin ret = exe.call obj
53
52
  ensure
54
- begin x.sync
55
- rescue Object
56
- $stderr.puts [$!.class,$!,$!.backtrace].inspect
57
- end
58
- begin x.close
59
- rescue Object
60
- $stderr.puts [$!.class,$!,$!.backtrace].inspect
61
- end
62
- end
53
+ SBDB::raise_barrier &obj.method(:sync)
54
+ SBDB::raise_barrier &obj.method(:close)
55
+ end if exe
56
+ ret
63
57
  end
64
58
  alias open new
65
59
  end
66
60
 
67
- def _txn t
68
- t ||= @txn
69
- t && t.bdb_object
61
+ def _txn txn
62
+ txn ||= @txn
63
+ txn && t.bdb_object
70
64
  end
71
65
 
72
66
  def initialize file, *args
@@ -85,17 +79,17 @@ module SBDB
85
79
  end
86
80
  end
87
81
 
88
- def each k = nil, v = nil, &e
89
- cursor{|c|c.each k, v, &e}
82
+ def each key = nil, val = nil, &exe
83
+ cursor{|c|c.each key, val, &exe}
90
84
  end
91
85
 
92
- def reverse k = nil, v = nil, &e
93
- cursor{|c|c.reverse k, v, &e}
86
+ def reverse key = nil, val = nil, &exe
87
+ cursor{|c|c.reverse key, val, &exe}
94
88
  end
95
89
 
96
90
  def to_hash k = nil, v = nil
97
91
  h = {}
98
- each( k, v) {|k, v| h[ k] = v }
92
+ each( key, val, h.method(:[]=))
99
93
  h
100
94
  end
101
95
 
@@ -105,9 +99,9 @@ module SBDB
105
99
  end
106
100
 
107
101
  class Unknown < DB
108
- def self.new file, *p, &e
109
- dbt = super( file, *p) {|db| db.bdb_object.get_type }
110
- TYPES[dbt] ? TYPES[dbt].new( file, *p, &e) : super( file, *p, &e)
102
+ def self.new file, *ps, &exe
103
+ dbt = super( file, *ps) {|db| db.bdb_object.get_type }
104
+ TYPES[dbt] ? TYPES[dbt].new( file, *ps, &exe) : super( file, *ps, &exe)
111
105
  end
112
106
  end
113
107
 
@@ -119,38 +113,31 @@ module SBDB
119
113
  end
120
114
  TYPES[DB::HASH] = Hash
121
115
 
122
- class Recno < DB
123
- def [] k
124
- super [k].pack('I')
116
+ module Arrayisch
117
+ def [] key
118
+ super [key].pack('I')
125
119
  end
126
120
 
127
- def []= k, v
128
- super [k].pack('I'), v
121
+ def []= key, val
122
+ super [key].pack('I'), val
129
123
  end
130
124
 
131
- def push v, txn = nil
132
- @db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
125
+ def push val, txn = nil
126
+ @db.put _txn(txn), "\0\0\0\0", val, Bdb::DB_APPEND
133
127
  end
134
128
  end
129
+
130
+ class Recno < DB
131
+ include Arrayisch
132
+ end
135
133
  Array = Recno
136
134
  TYPES[DB::RECNO] = Recno
137
135
 
138
136
  class Queue < DB
139
- def [] k
140
- super [k].pack('I')
141
- end
142
-
143
- def []= k, v
144
- super [k].pack('I'), v
145
- end
146
-
137
+ include Arrayisch
147
138
  def unshift txn = nil
148
139
  @db.get _txn(txn), "\0\0\0\0", nil, Bdb::DB_CONSUME
149
140
  end
150
-
151
- def push v, txn = nil
152
- @db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
153
- end
154
141
  end
155
142
  TYPES[DB::QUEUE] = Queue
156
143
  end
@@ -27,28 +27,28 @@ module SBDB
27
27
  # returns the Bdb-object.
28
28
  def bdb_object() @env end
29
29
  # Opens a Btree in this Environment
30
- def btree file, *p, &e
31
- open Btree, file, *p, &e
30
+ def btree file, *ps, &exe
31
+ open Btree, file, *ps, &exe
32
32
  end
33
33
  # Opens a Hash in this Environment
34
- def hash file, *p, &e
35
- open Hash, file, *p, &e
34
+ def hash file, *ps, &exe
35
+ open Hash, file, *ps, &exe
36
36
  end
37
37
  # Opens a Recno in this Environment
38
- def recno file, *p, &e
39
- open Recno, file, *p, &e
38
+ def recno file, *ps, &exe
39
+ open Recno, file, *ps, &exe
40
40
  end
41
41
  # Opens a Queue in this Environment
42
- def queue file, *p, &e
43
- open Queue, file, *p, &e
42
+ def queue file, *ps, &exe
43
+ open Queue, file, *ps, &exe
44
44
  end
45
45
  # Opens a DB of unknown type in this Environment
46
- def unknown file, *p, &e
47
- open Unknown, file, *p, &e
46
+ def unknown file, *ps, &exe
47
+ open Unknown, file, *ps, &exe
48
48
  end
49
49
 
50
- def transaction flags = nil, &e
51
- SBDB::Transaction.new self, flags, &e
50
+ def transaction flg = nil, &exe
51
+ SBDB::Transaction.new self, flg, &exe
52
52
  end
53
53
  alias txn transaction
54
54
 
@@ -73,21 +73,17 @@ module SBDB
73
73
  end
74
74
 
75
75
  def self.new *args
76
- obj = r = super( *args)
77
- begin r = yield obj
78
- ensure
79
- begin obj.close
80
- rescue Object
81
- $stderr.puts [$!.class,$!,$!.backtrace].inspect
82
- end
76
+ obj = ret = super( *args)
77
+ begin ret = yield obj
78
+ ensure SBDB::raise_barrier &obj.method(:close)
83
79
  end if block_given?
84
- r
80
+ ret
85
81
  end
86
82
 
87
83
  # Close the Environment.
88
84
  # First you should close all databases!
89
85
  def close
90
- @dbs.each{|k, db|db.close}
86
+ @dbs.each{|key, db|db.close}
91
87
  @env.close
92
88
  end
93
89
 
@@ -97,10 +93,10 @@ module SBDB
97
93
 
98
94
  # Opens a Database.
99
95
  # see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue
100
- def open type, file, *p, &e
101
- p.push ::Hash.new unless ::Hash === p.last
102
- p.last[:env] = self
103
- (type || SBDB::Unkown).new file, *p, &e
96
+ def open type, file, *ps, &exe
97
+ ps.push ::Hash.new unless ::Hash === ps.last
98
+ ps.last[:env] = self
99
+ (type || SBDB::Unkown).new file, *ps, &exe
104
100
  end
105
101
  alias db open
106
102
  alias open_db open
@@ -109,13 +105,14 @@ module SBDB
109
105
  # it returns the old instance.
110
106
  # If you use this, never use close. It's possible somebody else use it too.
111
107
  # The Databases, which are opened, will close, if the Environment will close.
112
- def [] file, *p, &e
113
- p.push ::Hash.new unless ::Hash === p.last
114
- p.last[:env] = self
115
- name = String === p[0] ? p[0] : p.last[:name]
116
- flags = Fixnum === p[1] ? p[1] : p.last[:flags]
117
- type = Fixnum === p[2] ? p[2] : p.last[:type]
118
- @dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unknown).new file, *p, &e
108
+ def [] file, *ps, &exe
109
+ ps.push ::Hash.new unless ::Hash === ps.last
110
+ ps.last[:env] = self
111
+ name, flg, type =
112
+ String === ps[0] ? ps[0] : ps.last[:name],
113
+ Fixnum === ps[1] ? ps[1] : ps.last[:flags],
114
+ Fixnum === ps[2] ? ps[2] : ps.last[:type]
115
+ @dbs[ [file, name, flg | CREATE]] ||= (type || SBDB::Unknown).new file, *ps, &exe
119
116
  end
120
117
  end
121
118
  Env = Environment
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 8
9
- version: 0.0.8
8
+ - 9
9
+ version: 0.0.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Denis Knauf
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +01:00
17
+ date: 2010-03-29 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -39,6 +39,7 @@ extra_rdoc_files:
39
39
  - LICENSE
40
40
  - README.md
41
41
  files:
42
+ - AUTHORS
42
43
  - README.md
43
44
  - VERSION
44
45
  - lib/sbdb.rb