Compare commits
17 commits
Author | SHA1 | Date | |
---|---|---|---|
|
50e2fbe843 | ||
|
4745db500c | ||
|
93cd84cd59 | ||
|
31021f703f | ||
|
50f610da4a | ||
|
037544bdb5 | ||
|
4700562b3e | ||
|
676718c40f | ||
|
6c686eb2d3 | ||
|
bfe13fcce8 | ||
|
f6a10c9131 | ||
|
d11eb016e5 | ||
|
28275b71a8 | ||
|
c66c369df0 | ||
|
28dcffcb35 | ||
|
7a7a8a7db5 | ||
|
6fc4c97bf3 |
9 changed files with 33 additions and 127 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pkg
|
||||||
|
*.gem
|
||||||
|
*.gemspec
|
||||||
|
rdoc
|
||||||
|
*.rbc
|
23
README.md
23
README.md
|
@ -1,14 +1,14 @@
|
||||||
Dependencies
|
Dependencies
|
||||||
============
|
============
|
||||||
|
|
||||||
You need first the [Bdb](http://github.com/DenisKnauf/bdb) and of course [ruby](http://ruby-lang.org).
|
You need first the [Bdb](http://github.com/ruby-bdb/bdb) and of course [ruby](http://ruby-lang.org).
|
||||||
|
|
||||||
Download
|
Download
|
||||||
========
|
========
|
||||||
|
|
||||||
via git:
|
via git:
|
||||||
|
|
||||||
git clone git://github.com/DenisKnauf/sbdb.git
|
git clone git://github.com/ruby-bdb/sbdb
|
||||||
|
|
||||||
Install
|
Install
|
||||||
=======
|
=======
|
||||||
|
@ -23,21 +23,22 @@ First, open environment and database
|
||||||
|
|
||||||
require 'sbdb'
|
require 'sbdb'
|
||||||
Dir.mkdir 'newenv' rescue Errno::EEXIST
|
Dir.mkdir 'newenv' rescue Errno::EEXIST
|
||||||
env = SBDB::Env.new 'newenv', SBDB::CREATE
|
env = SBDB::Env.new 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION
|
||||||
db = env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE
|
db = env.btree 'newdb.db', :flags => SBDB::CREATE
|
||||||
|
|
||||||
It works nearly like a Ruby-Hash:
|
It works nearly like a Ruby-Hash:
|
||||||
|
|
||||||
db['key'] = 'value'
|
db['key'] = 'value'
|
||||||
db['key'] # => 'value'
|
db['key'] # => 'value'
|
||||||
db.to_hash # => {'key'=>'value'}
|
db.to_hash # => {'key'=>'value'}
|
||||||
db.map {|k, v| [k, v].join ' => '} # => ["key => value"]
|
db.map {|k, v| "k => v" } # => ["key => value"]
|
||||||
db.count # => 1
|
db.count # => 1
|
||||||
|
db.each {|k,v| puts "#{k}: #{v}" }
|
||||||
|
|
||||||
SBDB::DB#each uses a SBDB::Cursor:
|
`SBDB::DB#each` uses a `SBDB::Cursor`:
|
||||||
|
|
||||||
cursor = db.cursor
|
cursor = db.cursor
|
||||||
cursor.each {|k,v| puts "#{k}: ${v}" }
|
cursor.each {|k,v| puts "#{k}: #{v}" }
|
||||||
|
|
||||||
**Don't forget to close everything, you've opened!**
|
**Don't forget to close everything, you've opened!**
|
||||||
|
|
||||||
|
@ -47,10 +48,14 @@ SBDB::DB#each uses a SBDB::Cursor:
|
||||||
|
|
||||||
But you can use a *lambda* to ensure to close everything:
|
But you can use a *lambda* to ensure to close everything:
|
||||||
|
|
||||||
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
|
SBDB::Env.new( 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION) do |env|
|
||||||
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
|
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
|
||||||
db.to_hash
|
db.to_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
SBDB::DB#to_hash creates a cursor and close it later.
|
`SBDB::DB#to_hash` creates a cursor and close it later.
|
||||||
|
|
||||||
|
Tip:
|
||||||
|
|
||||||
|
Signal.trap 'EXIT', env.method( :close)
|
||||||
|
|
5
Rakefile
5
Rakefile
|
@ -10,11 +10,12 @@ begin
|
||||||
gem.summary = %Q{Simple Ruby Berkeley DB}
|
gem.summary = %Q{Simple Ruby Berkeley DB}
|
||||||
gem.description = %Q{Simple Ruby Berkeley DB wrapper library for bdb.}
|
gem.description = %Q{Simple Ruby Berkeley DB wrapper library for bdb.}
|
||||||
gem.email = "Denis.Knauf@gmail.com"
|
gem.email = "Denis.Knauf@gmail.com"
|
||||||
gem.homepage = "http://github.com/DenisKnauf/bdb"
|
gem.homepage = "http://github.com/ruby-bdb/sbdb"
|
||||||
gem.authors = ["Denis Knauf"]
|
gem.authors = ["Denis Knauf"]
|
||||||
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
|
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
|
||||||
gem.require_paths = %w[lib]
|
gem.require_paths = %w[lib]
|
||||||
gem.add_dependency 'dk-bdb'
|
gem.add_dependency 'bdb', '>= 0.2.6.5'
|
||||||
|
gem.add_dependency 'ref'
|
||||||
end
|
end
|
||||||
Jeweler::GemcutterTasks.new
|
Jeweler::GemcutterTasks.new
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
|
|
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.0.10
|
0.0.12.2
|
|
@ -1,4 +1,5 @@
|
||||||
require 'bdb'
|
require 'bdb'
|
||||||
|
require 'ref'
|
||||||
require 'sbdb/environment'
|
require 'sbdb/environment'
|
||||||
require 'sbdb/db'
|
require 'sbdb/db'
|
||||||
require 'sbdb/cursor'
|
require 'sbdb/cursor'
|
||||||
|
|
|
@ -60,7 +60,11 @@ module SBDB
|
||||||
|
|
||||||
def _txn txn
|
def _txn txn
|
||||||
txn ||= @txn
|
txn ||= @txn
|
||||||
txn && t.bdb_object
|
txn && txn.bdb_object
|
||||||
|
end
|
||||||
|
|
||||||
|
def transaction flg = nil, &exe
|
||||||
|
block_given? ? home.transaction( flg, &exe) : home.transaction( flg)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Arguments:
|
# Arguments:
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
require 'bdb'
|
require 'bdb'
|
||||||
require 'sbdb/weakhash'
|
|
||||||
require 'sbdb/db'
|
require 'sbdb/db'
|
||||||
require 'sbdb/transaction'
|
require 'sbdb/transaction'
|
||||||
|
|
||||||
|
@ -61,7 +60,7 @@ module SBDB
|
||||||
def initialize *args
|
def initialize *args
|
||||||
opts = ::Hash === args.last ? args.pop : {}
|
opts = ::Hash === args.last ? args.pop : {}
|
||||||
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
|
||||||
@dbs, @env = WeakHash.new, Bdb::Env.new( 0)
|
@dbs, @env = Ref::WeakValueMap.new, Bdb::Env.new( 0)
|
||||||
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
@env.log_config opts[:log_config], 1 if opts[:log_config]
|
||||||
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
|
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
|
||||||
@env.lg_max = opts[:lg_max] if opts[:lg_max]
|
@env.lg_max = opts[:lg_max] if opts[:lg_max]
|
||||||
|
|
|
@ -1,61 +0,0 @@
|
||||||
module SBDB
|
|
||||||
|
|
||||||
# See http://eigenclass.org/hiki/deferred-finalizers-in-Ruby
|
|
||||||
# Not threadsafe.
|
|
||||||
|
|
||||||
class WeakHash
|
|
||||||
attr_reader :cache
|
|
||||||
def initialize cache = ::Hash.new
|
|
||||||
@cache = cache
|
|
||||||
@key_map = {}
|
|
||||||
@rev_cache = ::Hash.new{|h,k| h[k] = {}}
|
|
||||||
@reclaim_value = lambda do |value_id|
|
|
||||||
if @rev_cache.has_key? value_id
|
|
||||||
@rev_cache[value_id].each_key{|key| @cache.delete key}
|
|
||||||
@rev_cache.delete value_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@reclaim_key = lambda do |key_id|
|
|
||||||
@cache.delete @key_map.delete(key_id) if @key_map.has_key? key_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def []= key, value
|
|
||||||
case key
|
|
||||||
when Fixnum, Symbol, true, false
|
|
||||||
key2 = key
|
|
||||||
else
|
|
||||||
key2 = key.dup
|
|
||||||
end
|
|
||||||
@rev_cache[value.object_id][key2] = true
|
|
||||||
@cache[key2] = value.object_id
|
|
||||||
@key_map[key.object_id] = key2
|
|
||||||
|
|
||||||
ObjectSpace.define_finalizer(value, @reclaim_value)
|
|
||||||
ObjectSpace.define_finalizer(key, @reclaim_key)
|
|
||||||
value
|
|
||||||
end
|
|
||||||
|
|
||||||
def [] key
|
|
||||||
value_id = @cache[key]
|
|
||||||
return ObjectSpace._id2ref( value_id) unless value_id.nil?
|
|
||||||
nil
|
|
||||||
rescue RangeError
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def each &e
|
|
||||||
@cache.each do |k, vid|
|
|
||||||
unless vid.nil?
|
|
||||||
obj = begin
|
|
||||||
ObjectSpace._id2ref vid
|
|
||||||
rescue RangeError
|
|
||||||
next
|
|
||||||
end
|
|
||||||
yield k, obj
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
48
sbdb.gemspec
48
sbdb.gemspec
|
@ -1,48 +0,0 @@
|
||||||
# Generated by jeweler
|
|
||||||
# DO NOT EDIT THIS FILE DIRECTLY
|
|
||||||
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
||||||
# -*- encoding: utf-8 -*-
|
|
||||||
|
|
||||||
Gem::Specification.new do |s|
|
|
||||||
s.name = %q{sbdb}
|
|
||||||
s.version = "0.0.10"
|
|
||||||
|
|
||||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
||||||
s.authors = ["Denis Knauf"]
|
|
||||||
s.date = %q{2011-08-01}
|
|
||||||
s.description = %q{Simple Ruby Berkeley DB wrapper library for bdb.}
|
|
||||||
s.email = %q{Denis.Knauf@gmail.com}
|
|
||||||
s.extra_rdoc_files = [
|
|
||||||
"LICENSE",
|
|
||||||
"README.md"
|
|
||||||
]
|
|
||||||
s.files = [
|
|
||||||
"AUTHORS",
|
|
||||||
"README.md",
|
|
||||||
"VERSION",
|
|
||||||
"lib/sbdb.rb",
|
|
||||||
"lib/sbdb/cursor.rb",
|
|
||||||
"lib/sbdb/db.rb",
|
|
||||||
"lib/sbdb/environment.rb",
|
|
||||||
"lib/sbdb/transaction.rb",
|
|
||||||
"lib/sbdb/weakhash.rb"
|
|
||||||
]
|
|
||||||
s.homepage = %q{http://github.com/DenisKnauf/bdb}
|
|
||||||
s.require_paths = ["lib"]
|
|
||||||
s.rubygems_version = %q{1.3.7}
|
|
||||||
s.summary = %q{Simple Ruby Berkeley DB}
|
|
||||||
|
|
||||||
if s.respond_to? :specification_version then
|
|
||||||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
||||||
s.specification_version = 3
|
|
||||||
|
|
||||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
||||||
s.add_runtime_dependency(%q<dk-bdb>, [">= 0"])
|
|
||||||
else
|
|
||||||
s.add_dependency(%q<dk-bdb>, [">= 0"])
|
|
||||||
end
|
|
||||||
else
|
|
||||||
s.add_dependency(%q<dk-bdb>, [">= 0"])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue