sbdb 0.0.8
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.
- data/LICENSE +165 -0
- data/README.md +56 -0
- data/VERSION +1 -0
- data/lib/sbdb.rb +19 -0
- data/lib/sbdb/cursor.rb +58 -0
- data/lib/sbdb/db.rb +156 -0
- data/lib/sbdb/environment.rb +122 -0
- data/lib/sbdb/transaction.rb +42 -0
- data/lib/sbdb/weakhash.rb +61 -0
- metadata +82 -0
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Dependencies
|
2
|
+
============
|
3
|
+
|
4
|
+
You need first the [Bdb](http://github.com/DenisKnauf/bdb) and of course [ruby](http://ruby-lang.org).
|
5
|
+
|
6
|
+
Download
|
7
|
+
========
|
8
|
+
|
9
|
+
via git:
|
10
|
+
|
11
|
+
git clone git://github.com/DenisKnauf/sbdb.git
|
12
|
+
|
13
|
+
Install
|
14
|
+
=======
|
15
|
+
|
16
|
+
gem build sbdb.gemspec
|
17
|
+
gem install sbdb-*.gem
|
18
|
+
|
19
|
+
Usage
|
20
|
+
=====
|
21
|
+
|
22
|
+
First, open environment and database
|
23
|
+
|
24
|
+
require 'sbdb'
|
25
|
+
Dir.mkdir 'newenv' rescue Errno::EEXIST
|
26
|
+
env = SBDB::Env.new 'newenv', SBDB::CREATE
|
27
|
+
db = env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE
|
28
|
+
|
29
|
+
It works nearly like a Ruby-Hash:
|
30
|
+
|
31
|
+
db['key'] = 'value'
|
32
|
+
db['key'] # => 'value'
|
33
|
+
db.to_hash # => {'key'=>'value'}
|
34
|
+
db.map {|k, v| [k, v].join ' => '} # => ["key => value"]
|
35
|
+
db.count # => 1
|
36
|
+
|
37
|
+
SBDB::DB#each uses a SBDB::Cursor:
|
38
|
+
|
39
|
+
cursor = db.cursor
|
40
|
+
cursor.each {|k,v| puts "#{k}: ${v}" }
|
41
|
+
|
42
|
+
**Don't forget to close everything, you've opened!**
|
43
|
+
|
44
|
+
cursor.close
|
45
|
+
db.close
|
46
|
+
env.close
|
47
|
+
|
48
|
+
But you can use a *lambda* to ensure to close everything:
|
49
|
+
|
50
|
+
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
|
51
|
+
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
|
52
|
+
db.to_hash
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
SBDB::DB#to_hash creates a cursor and close it later.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.8
|
data/lib/sbdb.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bdb'
|
2
|
+
require 'sbdb/environment'
|
3
|
+
require 'sbdb/db'
|
4
|
+
require 'sbdb/cursor'
|
5
|
+
require 'sbdb/transaction'
|
6
|
+
|
7
|
+
module SBDB
|
8
|
+
CREATE = Bdb::DB_CREATE
|
9
|
+
AUTO_COMMIT = Bdb::DB_AUTO_COMMIT
|
10
|
+
RDONLY = Bdb::DB_RDONLY
|
11
|
+
READONLY = RDONLY
|
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
|
18
|
+
alias open_db unknown
|
19
|
+
end
|
data/lib/sbdb/cursor.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
module SBDB
|
3
|
+
class Cursor
|
4
|
+
NEXT = Bdb::DB_NEXT
|
5
|
+
FIRST = Bdb::DB_FIRST
|
6
|
+
LAST = Bdb::DB_LAST
|
7
|
+
PREV = Bdb::DB_PREV
|
8
|
+
SET = Bdb::DB_SET
|
9
|
+
|
10
|
+
attr_reader :db
|
11
|
+
|
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
|
21
|
+
|
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
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize ref
|
31
|
+
@cursor, @db = *case ref
|
32
|
+
when Cursor then [ref.bdb_object.dup, ref.db]
|
33
|
+
when Bdb::Db::Cursor then [ref]
|
34
|
+
else [ref.bdb_object.cursor( nil, 0), ref]
|
35
|
+
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]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def reverse k = nil, v = nil, &e
|
45
|
+
each k, v, LAST, PREV, &e
|
46
|
+
end
|
47
|
+
|
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)
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/sbdb/db.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'bdb'
|
2
|
+
require 'sbdb/cursor'
|
3
|
+
|
4
|
+
module SBDB
|
5
|
+
TYPES = []
|
6
|
+
class DB
|
7
|
+
UNKNOWN = Bdb::Db::UNKNOWN
|
8
|
+
BTREE = Bdb::Db::BTREE
|
9
|
+
HASH = Bdb::Db::HASH
|
10
|
+
QUEUE = Bdb::Db::QUEUE
|
11
|
+
ARRAY = RECNO = Bdb::Db::RECNO
|
12
|
+
RDONLY = READLONY = Bdb::DB_RDONLY
|
13
|
+
CONSUME = Bdb::DB_CONSUME
|
14
|
+
CONSUME_WAIT = Bdb::DB_CONSUME_WAIT
|
15
|
+
|
16
|
+
attr_reader :home
|
17
|
+
attr_accessor :txn
|
18
|
+
include Enumerable
|
19
|
+
def bdb_object() @db end
|
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
|
23
|
+
|
24
|
+
def at k, txn = nil
|
25
|
+
@db.get _txn(txn), k.nil? ? nil : k.to_s, nil, 0
|
26
|
+
rescue Bdb::KeyEmpty
|
27
|
+
return nil
|
28
|
+
end
|
29
|
+
alias [] at
|
30
|
+
|
31
|
+
def put k, v, txn = nil
|
32
|
+
if v.nil?
|
33
|
+
@db.del _txn(txn), k.to_s, 0
|
34
|
+
else
|
35
|
+
@db.put _txn(txn), k.nil? ? nil : k.to_s, v.to_s, 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def []= k, v
|
40
|
+
put k, v
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete k, txn = nil
|
44
|
+
@db.del _txn(txn), k.to_s
|
45
|
+
end
|
46
|
+
alias del delete
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def new *p, &e
|
50
|
+
x = super *p
|
51
|
+
return x unless e
|
52
|
+
begin e.call x
|
53
|
+
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
|
63
|
+
end
|
64
|
+
alias open new
|
65
|
+
end
|
66
|
+
|
67
|
+
def _txn t
|
68
|
+
t ||= @txn
|
69
|
+
t && t.bdb_object
|
70
|
+
end
|
71
|
+
|
72
|
+
def initialize file, *args
|
73
|
+
opts = ::Hash === args.last ? args.pop : {}
|
74
|
+
opts = {:name => args[0], :type => args[1], :flags => args[2], :mode => args[3], :env => args[4]}.update opts
|
75
|
+
#type = BTREE if type == UNKNOWN and (flags & CREATE) == CREATE
|
76
|
+
@home, @db = opts[:env], opts[:env] ? opts[:env].bdb_object.db : Bdb::Db.new
|
77
|
+
opts[:type] = TYPES.index(self.class) || UNKNOWN
|
78
|
+
@db.re_len = opts[:re_len] if opts[:re_len]
|
79
|
+
txn = opts[:txn]
|
80
|
+
begin
|
81
|
+
@db.open txn && txn.bdb_object, file, opts[:name], opts[:type], opts[:flags] || 0, opts[:mode] || 0
|
82
|
+
rescue Object
|
83
|
+
close
|
84
|
+
raise $!
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def each k = nil, v = nil, &e
|
89
|
+
cursor{|c|c.each k, v, &e}
|
90
|
+
end
|
91
|
+
|
92
|
+
def reverse k = nil, v = nil, &e
|
93
|
+
cursor{|c|c.reverse k, v, &e}
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_hash k = nil, v = nil
|
97
|
+
h = {}
|
98
|
+
each( k, v) {|k, v| h[ k] = v }
|
99
|
+
h
|
100
|
+
end
|
101
|
+
|
102
|
+
def truncate txn = nil
|
103
|
+
@db.truncate _txn(txn)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
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)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Btree < DB
|
115
|
+
end
|
116
|
+
TYPES[DB::BTREE] = Btree
|
117
|
+
|
118
|
+
class Hash < DB
|
119
|
+
end
|
120
|
+
TYPES[DB::HASH] = Hash
|
121
|
+
|
122
|
+
class Recno < DB
|
123
|
+
def [] k
|
124
|
+
super [k].pack('I')
|
125
|
+
end
|
126
|
+
|
127
|
+
def []= k, v
|
128
|
+
super [k].pack('I'), v
|
129
|
+
end
|
130
|
+
|
131
|
+
def push v, txn = nil
|
132
|
+
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
|
133
|
+
end
|
134
|
+
end
|
135
|
+
Array = Recno
|
136
|
+
TYPES[DB::RECNO] = Recno
|
137
|
+
|
138
|
+
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
|
+
|
147
|
+
def unshift txn = nil
|
148
|
+
@db.get _txn(txn), "\0\0\0\0", nil, Bdb::DB_CONSUME
|
149
|
+
end
|
150
|
+
|
151
|
+
def push v, txn = nil
|
152
|
+
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
|
153
|
+
end
|
154
|
+
end
|
155
|
+
TYPES[DB::QUEUE] = Queue
|
156
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'bdb'
|
2
|
+
require 'sbdb/weakhash'
|
3
|
+
require 'sbdb/db'
|
4
|
+
require 'sbdb/transaction'
|
5
|
+
|
6
|
+
module SBDB
|
7
|
+
# Environments are for storing one or more databases and are important
|
8
|
+
# if you want to work with more than one process on one database.
|
9
|
+
# You needn't use Environment, but it's usefull.
|
10
|
+
class Environment
|
11
|
+
INIT_TXN = Bdb::DB_INIT_TXN
|
12
|
+
INIT_LOCK = Bdb::DB_INIT_LOCK
|
13
|
+
INIT_LOG = Bdb::DB_INIT_LOG
|
14
|
+
INIT_MPOOL = Bdb::DB_INIT_MPOOL
|
15
|
+
INIT_TRANSACTION = INIT_TXN | INIT_LOCK | INIT_LOG | INIT_MPOOL
|
16
|
+
LOCKDOWN = Bdb::DB_LOCKDOWN
|
17
|
+
NOMMAP = Bdb::DB_NOMMAP
|
18
|
+
PRIVATE = Bdb::DB_PRIVATE
|
19
|
+
SYSTEM_MEM = Bdb::DB_SYSTEM_MEM
|
20
|
+
TXN_NOSYNC = Bdb::DB_TXN_NOSYNC
|
21
|
+
LOG_DIRECT = Bdb::DB_LOG_DIRECT
|
22
|
+
LOG_DSYNC = Bdb::DB_LOG_DSYNC
|
23
|
+
LOG_AUTO_REMOVE = Bdb::DB_LOG_AUTO_REMOVE
|
24
|
+
LOG_IN_MEMORY = Bdb::DB_LOG_IN_MEMORY
|
25
|
+
LOG_ZERO = Bdb::DB_LOG_ZERO
|
26
|
+
|
27
|
+
# returns the Bdb-object.
|
28
|
+
def bdb_object() @env end
|
29
|
+
# Opens a Btree in this Environment
|
30
|
+
def btree file, *p, &e
|
31
|
+
open Btree, file, *p, &e
|
32
|
+
end
|
33
|
+
# Opens a Hash in this Environment
|
34
|
+
def hash file, *p, &e
|
35
|
+
open Hash, file, *p, &e
|
36
|
+
end
|
37
|
+
# Opens a Recno in this Environment
|
38
|
+
def recno file, *p, &e
|
39
|
+
open Recno, file, *p, &e
|
40
|
+
end
|
41
|
+
# Opens a Queue in this Environment
|
42
|
+
def queue file, *p, &e
|
43
|
+
open Queue, file, *p, &e
|
44
|
+
end
|
45
|
+
# Opens a DB of unknown type in this Environment
|
46
|
+
def unknown file, *p, &e
|
47
|
+
open Unknown, file, *p, &e
|
48
|
+
end
|
49
|
+
|
50
|
+
def transaction flags = nil, &e
|
51
|
+
SBDB::Transaction.new self, flags, &e
|
52
|
+
end
|
53
|
+
alias txn transaction
|
54
|
+
|
55
|
+
# args:
|
56
|
+
# args[0] => dir
|
57
|
+
# args[1] => flags
|
58
|
+
# args[3] => mode
|
59
|
+
# possible options (via Hash):
|
60
|
+
# :dir, :flags, :mode, :log_config
|
61
|
+
def initialize *args
|
62
|
+
opts = ::Hash === args.last ? args.pop : {}
|
63
|
+
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
64
|
+
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
|
65
|
+
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
66
|
+
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
|
67
|
+
@env.lg_max = opts[:lg_max] if opts[:lg_max]
|
68
|
+
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
|
69
|
+
rescue Object
|
70
|
+
close
|
71
|
+
raise
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
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
|
83
|
+
end if block_given?
|
84
|
+
r
|
85
|
+
end
|
86
|
+
|
87
|
+
# Close the Environment.
|
88
|
+
# First you should close all databases!
|
89
|
+
def close
|
90
|
+
@dbs.each{|k, db|db.close}
|
91
|
+
@env.close
|
92
|
+
end
|
93
|
+
|
94
|
+
class << self
|
95
|
+
alias open new
|
96
|
+
end
|
97
|
+
|
98
|
+
# Opens a Database.
|
99
|
+
# 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
|
104
|
+
end
|
105
|
+
alias db open
|
106
|
+
alias open_db open
|
107
|
+
|
108
|
+
# Returns the DB like open, but if it's already opened,
|
109
|
+
# it returns the old instance.
|
110
|
+
# If you use this, never use close. It's possible somebody else use it too.
|
111
|
+
# 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
|
119
|
+
end
|
120
|
+
end
|
121
|
+
Env = Environment
|
122
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'sbdb'
|
2
|
+
require 'bdb'
|
3
|
+
|
4
|
+
module SBDB
|
5
|
+
class Transaction
|
6
|
+
NOSYNC = Bdb::DB_TXN_NOSYNC
|
7
|
+
SYNC = Bdb::DB_TXN_SYNC
|
8
|
+
NOWAIT = Bdb::DB_TXN_NOWAIT
|
9
|
+
WRITE_NOSYNC = Bdb::DB_TXN_WRITE_NOSYNC
|
10
|
+
SNAPSHOT = Bdb::DB_TXN_SNAPSHOT
|
11
|
+
READ_COMMITED = Bdb::DB_READ_COMMITTED
|
12
|
+
READ_UNCOMMITED = Bdb::DB_READ_UNCOMMITTED
|
13
|
+
|
14
|
+
def bdb_object() @txn end
|
15
|
+
|
16
|
+
def self.new *p, &e
|
17
|
+
r = obj = super( *p)
|
18
|
+
begin
|
19
|
+
r = e.call obj
|
20
|
+
rescue Object
|
21
|
+
obj.abort
|
22
|
+
raise $!
|
23
|
+
ensure
|
24
|
+
obj.commit
|
25
|
+
end if e
|
26
|
+
r
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize env, flags = nil, parent = nil
|
30
|
+
@txn = env.bdb_object.txn_begin parent, flags || 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def commit flags = nil
|
34
|
+
@txn.commit flags || 0
|
35
|
+
end
|
36
|
+
|
37
|
+
def abort
|
38
|
+
@txn.abort
|
39
|
+
end
|
40
|
+
end
|
41
|
+
TXN = Transaction
|
42
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module SBDB
|
2
|
+
|
3
|
+
# See http://eigenclass.org/hiki/deferred-finalizers-in-Ruby
|
4
|
+
# Not threadsafe.
|
5
|
+
|
6
|
+
class WeakHash
|
7
|
+
attr_reader :cache
|
8
|
+
def initialize cache = ::Hash.new
|
9
|
+
@cache = cache
|
10
|
+
@key_map = {}
|
11
|
+
@rev_cache = ::Hash.new{|h,k| h[k] = {}}
|
12
|
+
@reclaim_value = lambda do |value_id|
|
13
|
+
if @rev_cache.has_key? value_id
|
14
|
+
@rev_cache[value_id].each_key{|key| @cache.delete key}
|
15
|
+
@rev_cache.delete value_id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
@reclaim_key = lambda do |key_id|
|
19
|
+
@cache.delete @key_map.delete(key_id) if @key_map.has_key? key_id
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def []= key, value
|
24
|
+
case key
|
25
|
+
when Fixnum, Symbol, true, false
|
26
|
+
key2 = key
|
27
|
+
else
|
28
|
+
key2 = key.dup
|
29
|
+
end
|
30
|
+
@rev_cache[value.object_id][key2] = true
|
31
|
+
@cache[key2] = value.object_id
|
32
|
+
@key_map[key.object_id] = key2
|
33
|
+
|
34
|
+
ObjectSpace.define_finalizer(value, @reclaim_value)
|
35
|
+
ObjectSpace.define_finalizer(key, @reclaim_key)
|
36
|
+
value
|
37
|
+
end
|
38
|
+
|
39
|
+
def [] key
|
40
|
+
value_id = @cache[key]
|
41
|
+
return ObjectSpace._id2ref( value_id) unless value_id.nil?
|
42
|
+
nil
|
43
|
+
rescue RangeError
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def each &e
|
48
|
+
@cache.each do |k, vid|
|
49
|
+
unless vid.nil?
|
50
|
+
obj = begin
|
51
|
+
ObjectSpace._id2ref vid
|
52
|
+
rescue RangeError
|
53
|
+
next
|
54
|
+
end
|
55
|
+
yield k, obj
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sbdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 8
|
9
|
+
version: 0.0.8
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Denis Knauf
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-11 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: dk-bdb
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description: Simple Ruby Berkeley DB wrapper library for bdb.
|
33
|
+
email: Denis.Knauf@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
files:
|
42
|
+
- README.md
|
43
|
+
- VERSION
|
44
|
+
- lib/sbdb.rb
|
45
|
+
- lib/sbdb/cursor.rb
|
46
|
+
- lib/sbdb/db.rb
|
47
|
+
- lib/sbdb/environment.rb
|
48
|
+
- lib/sbdb/transaction.rb
|
49
|
+
- lib/sbdb/weakhash.rb
|
50
|
+
- LICENSE
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/DenisKnauf/bdb
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Simple Ruby Berkeley DB
|
81
|
+
test_files: []
|
82
|
+
|