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 +1 -0
- data/VERSION +1 -1
- data/lib/sbdb.rb +11 -5
- data/lib/sbdb/cursor.rb +43 -27
- data/lib/sbdb/db.rb +43 -56
- data/lib/sbdb/environment.rb +29 -32
- metadata +4 -3
data/AUTHORS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Denis Knauf <denis dot knauf at gmail dot com>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
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( *
|
14
|
-
def hash( *
|
15
|
-
def recno( *
|
16
|
-
def queue( *
|
17
|
-
def unknown( *
|
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
|
14
|
-
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
def
|
20
|
-
|
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 *
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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? {|
|
37
|
-
def get
|
38
|
-
|
39
|
-
|
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
|
45
|
-
each
|
60
|
+
def reverse key = nil, val = nil, &exe
|
61
|
+
each key, val, LAST, PREV, &exe
|
46
62
|
end
|
47
63
|
|
48
|
-
def each
|
49
|
-
return Enumerator.new( self, :each,
|
50
|
-
|
51
|
-
|
52
|
-
return unless
|
53
|
-
yield *
|
54
|
-
yield *
|
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(
|
22
|
-
def cursor( &
|
21
|
+
def close( flg = nil) @db.close flg || 0 end
|
22
|
+
def cursor( &exe) Cursor.new self, &exe end
|
23
23
|
|
24
|
-
def at
|
25
|
-
@db.get _txn(txn),
|
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
|
32
|
-
if
|
33
|
-
@db.del _txn(txn),
|
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),
|
35
|
+
@db.put _txn(txn), key.nil? ? nil : key.to_s, val.to_s, 0
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
39
|
-
def []=
|
40
|
-
put
|
38
|
+
|
39
|
+
def []= key, val
|
40
|
+
put key, val
|
41
41
|
end
|
42
42
|
|
43
|
-
def delete
|
44
|
-
@db.del _txn(txn),
|
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 *
|
50
|
-
|
51
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
68
|
-
|
69
|
-
|
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
|
89
|
-
cursor{|c|c.each
|
82
|
+
def each key = nil, val = nil, &exe
|
83
|
+
cursor{|c|c.each key, val, &exe}
|
90
84
|
end
|
91
85
|
|
92
|
-
def reverse
|
93
|
-
cursor{|c|c.reverse
|
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(
|
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, *
|
109
|
-
dbt = super( file, *
|
110
|
-
TYPES[dbt] ? TYPES[dbt].new( file, *
|
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
|
-
|
123
|
-
def []
|
124
|
-
super [
|
116
|
+
module Arrayisch
|
117
|
+
def [] key
|
118
|
+
super [key].pack('I')
|
125
119
|
end
|
126
120
|
|
127
|
-
def []=
|
128
|
-
super [
|
121
|
+
def []= key, val
|
122
|
+
super [key].pack('I'), val
|
129
123
|
end
|
130
124
|
|
131
|
-
def push
|
132
|
-
@db.put _txn(txn), "\0\0\0\0",
|
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
|
-
|
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
|
data/lib/sbdb/environment.rb
CHANGED
@@ -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, *
|
31
|
-
open Btree, file, *
|
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, *
|
35
|
-
open Hash, file, *
|
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, *
|
39
|
-
open Recno, file, *
|
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, *
|
43
|
-
open Queue, file, *
|
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, *
|
47
|
-
open Unknown, file, *
|
46
|
+
def unknown file, *ps, &exe
|
47
|
+
open Unknown, file, *ps, &exe
|
48
48
|
end
|
49
49
|
|
50
|
-
def transaction
|
51
|
-
SBDB::Transaction.new self,
|
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 =
|
77
|
-
begin
|
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
|
-
|
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{|
|
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, *
|
101
|
-
|
102
|
-
|
103
|
-
(type || SBDB::Unkown).new file, *
|
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, *
|
113
|
-
|
114
|
-
|
115
|
-
name
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
-
-
|
9
|
-
version: 0.0.
|
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-
|
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
|