sbdb 0.0.10.1 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -10
- data/VERSION +1 -1
- data/lib/sbdb.rb +1 -0
- data/lib/sbdb/db.rb +5 -1
- data/lib/sbdb/environment.rb +1 -1
- metadata +15 -4
- data/lib/sbdb/weakhash.rb +0 -61
data/README.md
CHANGED
@@ -23,21 +23,22 @@ First, open environment and database
|
|
23
23
|
|
24
24
|
require 'sbdb'
|
25
25
|
Dir.mkdir 'newenv' rescue Errno::EEXIST
|
26
|
-
env = SBDB::Env.new 'newenv', SBDB::CREATE
|
27
|
-
db = env.
|
26
|
+
env = SBDB::Env.new 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION
|
27
|
+
db = env.btree 'newdb.db', :flags => SBDB::CREATE
|
28
28
|
|
29
29
|
It works nearly like a Ruby-Hash:
|
30
30
|
|
31
31
|
db['key'] = 'value'
|
32
|
-
db['key']
|
33
|
-
db.to_hash
|
34
|
-
db.map {|k, v|
|
35
|
-
db.count
|
32
|
+
db['key'] # => 'value'
|
33
|
+
db.to_hash # => {'key'=>'value'}
|
34
|
+
db.map {|k, v| "k => v" } # => ["key => value"]
|
35
|
+
db.count # => 1
|
36
|
+
db.each {|k,v| puts "#{k}: #{v}" }
|
36
37
|
|
37
|
-
SBDB::DB#each uses a SBDB::Cursor
|
38
|
+
`SBDB::DB#each` uses a `SBDB::Cursor`:
|
38
39
|
|
39
40
|
cursor = db.cursor
|
40
|
-
cursor.each {|k,v| puts "#{k}:
|
41
|
+
cursor.each {|k,v| puts "#{k}: #{v}" }
|
41
42
|
|
42
43
|
**Don't forget to close everything, you've opened!**
|
43
44
|
|
@@ -47,10 +48,14 @@ SBDB::DB#each uses a SBDB::Cursor:
|
|
47
48
|
|
48
49
|
But you can use a *lambda* to ensure to close everything:
|
49
50
|
|
50
|
-
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
|
51
|
+
SBDB::Env.new( 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION) do |env|
|
51
52
|
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
|
52
53
|
db.to_hash
|
53
54
|
end
|
54
55
|
end
|
55
56
|
|
56
|
-
SBDB::DB#to_hash creates a cursor and close it later.
|
57
|
+
`SBDB::DB#to_hash` creates a cursor and close it later.
|
58
|
+
|
59
|
+
Tip:
|
60
|
+
|
61
|
+
Signal.trap 'EXIT', env.method( :close)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.11
|
data/lib/sbdb.rb
CHANGED
data/lib/sbdb/db.rb
CHANGED
data/lib/sbdb/environment.rb
CHANGED
@@ -61,7 +61,7 @@ module SBDB
|
|
61
61
|
def initialize *args
|
62
62
|
opts = ::Hash === args.last ? args.pop : {}
|
63
63
|
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
64
|
-
@dbs, @env =
|
64
|
+
@dbs, @env = Ref::WeakValueMap.new, Bdb::Env.new( 0)
|
65
65
|
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
66
66
|
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
|
67
67
|
@env.lg_max = opts[:lg_max] if opts[:lg_max]
|
metadata
CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.0.10.1
|
8
|
+
- 11
|
9
|
+
version: 0.0.11
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Denis Knauf
|
@@ -31,6 +30,19 @@ dependencies:
|
|
31
30
|
version: "0"
|
32
31
|
type: :runtime
|
33
32
|
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: ref
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
34
46
|
description: Simple Ruby Berkeley DB wrapper library for bdb.
|
35
47
|
email: Denis.Knauf@gmail.com
|
36
48
|
executables: []
|
@@ -49,7 +61,6 @@ files:
|
|
49
61
|
- lib/sbdb/db.rb
|
50
62
|
- lib/sbdb/environment.rb
|
51
63
|
- lib/sbdb/transaction.rb
|
52
|
-
- lib/sbdb/weakhash.rb
|
53
64
|
- LICENSE
|
54
65
|
has_rdoc: true
|
55
66
|
homepage: http://github.com/ruby-bdb/sbdb
|
data/lib/sbdb/weakhash.rb
DELETED
@@ -1,61 +0,0 @@
|
|
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
|