Compare commits

...

79 Commits

Author SHA1 Message Date
Ash Moran 50e2fbe843 Bump version to 0.0.12.2 2011-08-27 05:55:24 +08:00
Ash Moran 4745db500c Remove the (erroneously re-added) weakref.rb file and the require line that was still including it (project now uses the ref gem) 2011-08-27 05:55:24 +08:00
Ash Moran 93cd84cd59 Bump version to 0.0.12.1 to bump bdb dependency to 0.2.6.5 2011-08-27 05:55:23 +08:00
Ash Moran 31021f703f Specify minimum bdb gem dependency version 2011-08-27 05:55:23 +08:00
Ash Moran 50f610da4a Version bump to 0.0.12 2011-08-27 05:55:23 +08:00
Andy Shipman 037544bdb5 Add weakhash back in 2011-08-27 05:55:23 +08:00
Denis Knauf 4700562b3e gemspec removed 2011-08-27 05:55:23 +08:00
Ash Moran 676718c40f Regenerate gemspec for version 0.0.10.1 2011-08-27 05:55:22 +08:00
Denis Knauf 6c686eb2d3 rubinius-compatible; ref.gem. README: INIT_TRANSACTION 2011-08-25 13:48:26 +02:00
Denis Knauf bfe13fcce8 weakhash entfernt. ref-gem 2011-08-25 13:23:38 +02:00
Denis Knauf f6a10c9131 updates gemspec 2011-08-25 00:48:41 +02:00
Denis Knauf d11eb016e5 Edited README.md via GitHub 2011-08-25 06:37:54 +08:00
Ash Moran 28275b71a8 Regenerate gemspec for version 0.0.10.1 2011-08-25 06:37:52 +08:00
Ash Moran c66c369df0 Update the Jeweler setup to use the new ruby-bdb homepage 2011-08-25 06:37:51 +08:00
Ash Moran 28dcffcb35 Bump version to 0.0.10.1 (0.0.0.1 increment just to reflect the change in gem dependency) 2011-08-25 06:37:49 +08:00
Ash Moran 7a7a8a7db5 Add .gitignore to ignore pkg/ 2011-08-25 06:37:48 +08:00
Ash Moran 6fc4c97bf3 Change dependency to be on the bdb gem rather than dk-bdb, now we have a GitHub organisation for it 2011-08-25 06:37:46 +08:00
Denis Knauf b65ff608b5 v0.0.10 gemspec 2011-08-01 10:07:07 +02:00
Denis Knauf 0551a8417e 0.0.10 2011-08-01 10:01:16 +02:00
Denis Knauf aeaccab933 reverted 2010-04-14 11:34:27 +02:00
Denis Knauf 55c7c8637b reverted 2010-04-14 11:33:49 +02:00
Denis Knauf 9ff255efcd More infos on exception (which database was trying to open) 2010-04-14 11:31:15 +02:00
Denis Knauf 26cf9ec0f8 More infos on exception (which database was trying to open) 2010-04-14 11:30:45 +02:00
Denis Knauf 9734d78410 DB#to_hash fixed 2010-04-03 13:18:50 +02:00
Denis Knauf a143782a9f DB#to_hash fixed 2010-04-03 13:12:57 +02:00
Denis Knauf 41c8d7005e DB#to_hash fixed 2010-04-03 13:09:53 +02:00
Denis Knauf ba288a0f4b Lesser Ruby-warnings: warning: instance variable @txn not initialized 2010-04-01 13:38:35 +02:00
Denis Knauf 0fca4a27e0 DB#initialize: @txn initialized (nil). 2010-03-31 00:41:27 +02:00
Denis Knauf a9d9aa3664 Env#[]: args should be the same like DB::initialize, now 2010-03-29 15:02:00 +02:00
Denis Knauf da7476fe70 woggle 2010-03-29 14:24:31 +02:00
Denis Knauf f441da9f18 Regenerated gemspec for version 0.0.9 2010-03-29 14:17:32 +02:00
Denis Knauf b0f1930e4c VERSION 0.0.9 2010-03-29 14:17:24 +02:00
Denis Knauf b01559ef13 typos 2010-03-29 11:45:57 +02:00
Denis Knauf 51f1627093 i think, i should write some tests. 2010-03-29 11:44:04 +02:00
Denis Knauf f35c674923 Exceptions, which are raised after ensure will not be raised 2010-03-21 00:39:45 +01:00
Denis Knauf 9cae19fee4 little changes: metafiles 2010-03-20 23:20:35 +01:00
Denis Knauf e7113ce586 reek detects common code smells 2010-03-12 21:14:38 +01:00
Denis Knauf b9d52726ae Flay Report said, i have duplication in my code 2010-03-12 20:45:06 +01:00
Denis Knauf f04fcac66d BDB::DB supports transactions now. Version 0.0.8 2010-03-11 00:18:38 +01:00
Denis Knauf febd0ea1bb transaction support added 2010-03-09 22:55:07 +01:00
Denis Knauf 586f239f00 transactions added, not tested 2010-03-09 19:09:22 +01:00
Denis Knauf a4d1031d93 Weakhash: self cleaning cache. SBDB::Env#new: Hash-options added 2010-03-07 22:01:05 +01:00
Denis Knauf bddc461ca4 README.md: deps 2010-02-23 21:49:08 +01:00
Denis Knauf 9182884749 SBDB::Queue::unshift ready 2010-02-23 21:48:02 +01:00
Denis Knauf 5a8329161b README uptodate 2010-02-23 20:43:05 +01:00
Denis Knauf e81f59834d README uptodate 2010-02-23 20:42:54 +01:00
Denis Knauf 4e2b89c419 0.0.7 2010-02-23 20:33:50 +01:00
Denis Knauf 5796cebdc8 re_len-support (never try to get re_len if it is not a Queue) 2010-02-23 20:28:38 +01:00
Denis Knauf 3c2ee96d63 re_len-support 2010-02-23 20:20:26 +01:00
Denis Knauf 1ce9a8ab4c Lesser parameters, more options 2010-02-23 19:39:27 +01:00
Denis Knauf 5f6a7d0038 unshift/push bugfix 2010-02-22 16:36:36 +01:00
Denis Knauf 7b36d00f29 gemspec 2010-02-22 16:26:53 +01:00
Denis Knauf e3a18fa0f7 Recno/Queue#push 2010-02-22 16:17:25 +01:00
Denis Knauf 954ca1751b SBDB::Unknown: Blocks are nice 2010-02-06 11:57:14 +01:00
Denis Knauf 72aa9c8599 SBDB::Unknown: Blocks are nice 2010-02-06 11:56:09 +01:00
Denis Knauf 02791744a6 SBDB::Unknown: Blocks are nice 2010-02-06 11:54:57 +01:00
Denis Knauf d723a228a0 SBDB::TYPES 2010-02-06 11:50:52 +01:00
Denis Knauf f3153fba9f SBDB::Unknown: Better support. Auto detection and type. 2010-02-06 11:47:41 +01:00
Denis Knauf 37a9161470 Wenn kein Element existiert, wird ein Exception geworfen. Dies ist etwas unschoen, also wird diese abgefangen und nil zurueckgegeben. 2010-02-06 00:41:43 +01:00
Denis Knauf 926e53ae3e 0.0.6 2010-02-05 19:16:40 +01:00
Denis Knauf 631e112d89 0.0.6 2010-02-05 19:10:22 +01:00
Denis Knauf ff2e444047 SBDB::DB#[]=: If set to nil, it will delete item. SBDB::Cursor#each: Integer-keys for Recno and Queue. 2010-02-05 19:08:30 +01:00
Denis Knauf 8385dd2868 Env#[] 2010-02-03 13:34:26 +01:00
Denis Knauf c6fe157334 Env#close 2010-02-03 13:21:40 +01:00
Denis Knauf 3b169d4838 RDONLY 2010-02-02 18:12:11 +01:00
Denis Knauf 8bb9d18313 SBDB::Env#[] 2010-02-02 18:01:00 +01:00
Denis Knauf b9d22ea98c DB: Recno/Queue-support 2010-02-02 15:15:10 +01:00
Denis Knauf e9e06b1b99 Cursor: Typo 2010-02-02 15:12:58 +01:00
Denis Knauf 4cf0388c5b So langsam sind es zuviele Parameter fuer DB in Env. 2010-02-02 15:01:50 +01:00
Denis Knauf 0078170a23 Env: Typo 2010-02-02 14:56:47 +01:00
Denis Knauf ff902f3283 So langsam sind es zuviele Parameter fuer DB in Env. 2010-02-02 14:55:23 +01:00
Denis Knauf 54ec11fd27 SBDB::Env: Env wird bei SBDB::* als Parameter 6 erwartet, nicht 5. 2010-02-02 14:36:55 +01:00
Denis Knauf 950d77bd95 SBDB::WeakHash: ::Hash statt Hash, da es sonst SBDB::Hash nimmt 2010-02-02 14:31:57 +01:00
Denis Knauf 503eea32fd Env#initialize: typo 2010-02-02 14:23:40 +01:00
Denis Knauf 299978a9ed added: Weakhash#each 2010-02-02 10:50:04 +01:00
Denis Knauf 0540eabcb4 Env#[]: first try 2010-02-02 01:08:14 +01:00
Denis Knauf 42a5609b07 new files 2010-02-02 00:52:25 +01:00
Denis Knauf 691b5dacf3 Recno/Queue#[] and #[]= fixed. value.to_s 2010-02-01 17:31:33 +01:00
Denis Knauf b6254035a6 A little bit docu 2010-01-30 15:29:46 +01:00
12 changed files with 480 additions and 350 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
pkg
*.gem
*.gemspec
rdoc
*.rbc

View File

66
README
View File

@ -1,66 +0,0 @@
Bdb
===
Your need first this library:
http://github.com/DenisKnauf/bdb
Download
========
via git:
git clone git://github.com/DenisKnauf/sbdb.git
Installation
============
gem install sbdb-*.gem
or:
gem install sbdb --source http://github.com/DenisKnauf/sbdb/raw/master/sbdb-0.0.5.gem
Documentation
=============
Doesn't exist yet.
Usage
=====
First, open environment and database
require 'sbdb'
Dir.mkdir 'newenv' rescue Errno::EEXIST
env = SBDB::Env.new 'newenv', SBDB::CREATE
db = env.open SBDB::Btree, 'newdb.db', 'mynewdb', SBDB::CREATE
It works nearly like a Ruby-Hash:
db['key'] = 'value'
db['key'] # => 'value'
db.to_hash # => {'key'=>'value'}
db.map {|k, v| [k, v].join ' => '} # => ["key => value"]
db.count # => 1
*SBDB::DB#each* uses a *SBDB::Cursor*:
cursor = db.cursor
cursor.each {|k,v| puts "#{k}: ${v}" }
Don't forget to close everything, you opened!
cursor.close
db.close
env.close
But you can use a *lambda* to ensure to close everything:
SBDB::Env.new( 'newenv', SBDB::CREATE) do |env|
env.open SBDB::Btree, 'newdb.db', 'mynewdb', SBDB::CREATE do |db|
db.to_hash
end
end
*SBDB::DB#to_hash* creates a cursor and close it later.

61
README.md Normal file
View File

@ -0,0 +1,61 @@
Dependencies
============
You need first the [Bdb](http://github.com/ruby-bdb/bdb) and of course [ruby](http://ruby-lang.org).
Download
========
via git:
git clone git://github.com/ruby-bdb/sbdb
Install
=======
gem build sbdb.gemspec
gem install sbdb-*.gem
Usage
=====
First, open environment and database
require 'sbdb'
Dir.mkdir 'newenv' rescue Errno::EEXIST
env = SBDB::Env.new 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION
db = env.btree 'newdb.db', :flags => SBDB::CREATE
It works nearly like a Ruby-Hash:
db['key'] = 'value'
db['key'] # => 'value'
db.to_hash # => {'key'=>'value'}
db.map {|k, v| "k => v" } # => ["key => value"]
db.count # => 1
db.each {|k,v| puts "#{k}: #{v}" }
`SBDB::DB#each` uses a `SBDB::Cursor`:
cursor = db.cursor
cursor.each {|k,v| puts "#{k}: #{v}" }
**Don't forget to close everything, you've opened!**
cursor.close
db.close
env.close
But you can use a *lambda* to ensure to close everything:
SBDB::Env.new( 'newenv', SBDB::CREATE | SBDB::Env::INIT_TRANSACTION) do |env|
env.open SBDB::Btree, 'newdb.db', :flags => SBDB::CREATE do |db|
db.to_hash
end
end
`SBDB::DB#to_hash` creates a cursor and close it later.
Tip:
Signal.trap 'EXIT', env.method( :close)

View File

@ -1,4 +1,6 @@
require 'rubygems'
require 'rdoc/task'
require 'rake/testtask'
require 'rake'
begin
@ -8,18 +10,18 @@ begin
gem.summary = %Q{Simple Ruby Berkeley DB}
gem.description = %Q{Simple Ruby Berkeley DB wrapper library for bdb.}
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.files = ["README", "VERSION", "lib/**/*.rb", "test/**/*.rb"]
gem.require_paths = ["lib"]
gem.add_dependency 'bdb'
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
gem.require_paths = %w[lib]
gem.add_dependency 'bdb', '>= 0.2.6.5'
gem.add_dependency 'ref'
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test' << 'ext'
test.pattern = 'test/**/*_test.rb'
@ -43,7 +45,6 @@ task :test => :check_dependencies
task :default => :test
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION')
version = File.read('VERSION')

View File

@ -1 +1 @@
0.0.5
0.0.12.2

View File

@ -1,246 +1,26 @@
require 'bdb'
require 'ref'
require 'sbdb/environment'
require 'sbdb/db'
require 'sbdb/cursor'
require 'sbdb/transaction'
module SBDB
CREATE = Bdb::DB_CREATE
CREATE = Bdb::DB_CREATE
AUTO_COMMIT = Bdb::DB_AUTO_COMMIT
INIT_TXN = Bdb::DB_INIT_TXN
INIT_LOCK = Bdb::DB_INIT_LOCK
INIT_LOG = Bdb::DB_INIT_LOG
INIT_MPOOL = Bdb::DB_INIT_MPOOL
RDONLY = Bdb::DB_RDONLY
READONLY = RDONLY
class Environment
def bdb_object
@env
end
def btree( *ps) Btree.new *ps end
def hash( *ps) Hash.new *ps end
def recno( *ps) Recno.new *ps end
def queue( *ps) Queue.new *ps end
def unknown( *ps) Unknown.new *ps end
alias open_db unknown
def initialize dir, flags = nil, mode = nil
flags ||= INIT_TXN | INIT_LOCK | INIT_LOG | INIT_MPOOL | CREATE
mode ||= 0
@env = Bdb::Env.new 0
begin @env.open dir, flags, mode
rescue Object
close
raise
end
return self unless block_given?
begin yield self
ensure close
end
nil
end
def close
@env.close
end
class << self
alias open new
end
def open type, *p, &e
p[5] = self
type.new *p, &e
end
alias db open
alias open_db open
end
Env = Environment
class DB
UNKNOWN = Bdb::Db::UNKNOWN
BTREE = Bdb::Db::BTREE
HASH = Bdb::Db::HASH
QUEUE = Bdb::Db::QUEUE
ARRAY = RECNO = Bdb::Db::RECNO
attr_reader :home
include Enumerable
def bdb_object
@db
end
class << self
def new *p, &e
x = super *p
return x unless e
begin e.call x
ensure
x.sync
x.close
end
end
alias open new
end
def initialize file, name = nil, type = nil, flags = nil, mode = nil, txn = nil, env = nil
flags ||= 0
type ||= UNKNOWN
type = BTREE if type == UNKNOWN and (flags & CREATE) == CREATE
mode ||= 0
@home = env
@db = env ? env.bdb_object.db : Bdb::Db.new
begin @db.open txn, file, name, type, flags, mode
rescue Object
close
raise $!
end
end
def sync
@db.sync
end
def close f = nil
@db.close f || 0
end
def [] k
@db.get nil, k, nil, 0
end
def []= k, v
@db.put nil, k, v, 0
end
def cursor &e
Cursor.new self, &e
end
def each k = nil, v = nil, &e
cursor{|c|c.each k, v, &e}
end
def reverse k = nil, v = nil, &e
cursor{|c|c.reverse k, v, &e}
end
def to_hash k = nil, v = nil
h = {}
each( k, v) {|k, v| h[ k] = v }
h
end
end
class Cursor
NEXT = Bdb::DB_NEXT
FIRST = Bdb::DB_FIRST
LAST = Bdb::DB_LAST
PREV = Bdb::DB_PREV
SET = Bdb::DB_SET
attr_reader :db
include Enumerable
def bdb_object
@cursor
end
def self.new *p
x = super *p
return x unless block_given?
begin yield x
ensure x.close
end
end
def initialize ref
obj = ref.bdb_object
@cursor, @db = *if Cursor === ref
[obj.dup, ref.db]
else [obj.cursor( nil, 0), ref]
end
end
def close
@cursor.close
end
def get k, v, f
@cursor.get k, v, f
end
def count
@cursor.count
end
def reverse k = nil, v = nil, &e
each k, v, LAST, PREV, &e
end
def each k = nil, v = nil, f = nil, n = nil
return Enumerator.new( self, :each, k, v, f, n) unless block_given?
n ||= NEXT
e = @cursor.get k, v, f || FIRST
return unless e
yield *e
yield *e while e = @cursor.get( k, v, n)
nil
end
def first k = nil, v = nil
@cursor.get k, v, FIRST
end
def last k = nil, v = nil
@cursor.get k, v, LAST
end
def next k = nil, v = nil
@cursor.get k, v, NEXT
end
def prev k = nil, v = nil
@cursor.get k, v, PREV
end
end
class Unknown < DB
def self.new *p, &e
super *p[0...2], UNKNOWN, *p[2..-1], &e
end
end
class Btree < DB
def self.new *p, &e
super *p[0...2], BTREE, *p[2..-1], &e
end
end
class Hash < DB
def self.new *p, &e
super *p[0...2], HASH, *p[2..-1], &e
end
end
class Recno < DB
def self.new *p, &e
super *p[0...2], RECNO, *p[2..-1], &e
end
def [] k
super [k].pack('N')
end
def []= k, v
super [k].pack('N'), v
end
end
Array = Recno
class Queue < DB
def self.new *p, &e
super *p[0...2], QUEUE, *p[2..-1], &e
end
def [] k
super [k].pack('N')
end
def []= k, v
super [k].pack('N'), v
end
def self.raise_barrier *ps, &e
e.call *ps
rescue Object
$stderr.puts [$!.class,$!,$!.backtrace].inspect
end
end

74
lib/sbdb/cursor.rb Normal file
View File

@ -0,0 +1,74 @@
module SBDB
class Cursor
NEXT = Bdb::DB_NEXT
FIRST = Bdb::DB_FIRST
LAST = Bdb::DB_LAST
PREV = Bdb::DB_PREV
SET = Bdb::DB_SET
attr_reader :db
include Enumerable
def bdb_object
@cursor
end
def close
@cursor.close
end
def get key, val, flg
@cursor.get key, val, flg
end
def count
@cursor.count
end
def first key = nil, val = nil
get key, val, FIRST
end
def last key = nil, val = nil
get key, val, LAST
end
def next key = nil, val = nil
get key, val, NEXT
end
def prev key = nil, val = nil
get key, val, PREV
end
def self.new *ps
ret = obj = super( *ps)
begin ret = yield obj
ensure obj.close
end if block_given?
ret
end
def initialize ref
@cursor, @db = *case ref
when Cursor then [ref.bdb_object.dup, ref.db]
when Bdb::Db::Cursor then [ref]
else [ref.bdb_object.cursor( nil, 0), ref]
end
if [Recno, Queue].any? {|dbt| dbt === @db }
def get *ps
key, val = @cursor.get( *ps)
key.nil? ? nil : [key.unpack('I')[0], val]
end
end
end
def reverse key = nil, val = nil, &exe
each key, val, LAST, PREV, &exe
end
def each key = nil, val = nil, flg = nil, nxt = nil
return Enumerator.new( self, :each, key, val, flg, nxt) unless block_given?
nxt ||= NEXT
ent = get key, val, flg || FIRST
return unless ent
yield *ent
yield *ent while ent = get( key, val, nxt)
nil
end
end
end

156
lib/sbdb/db.rb Normal file
View File

@ -0,0 +1,156 @@
require 'bdb'
require 'sbdb/cursor'
module SBDB
TYPES = []
class DB
UNKNOWN = Bdb::Db::UNKNOWN
BTREE = Bdb::Db::BTREE
HASH = Bdb::Db::HASH
QUEUE = Bdb::Db::QUEUE
ARRAY = RECNO = Bdb::Db::RECNO
RDONLY = READLONY = Bdb::DB_RDONLY
CONSUME = Bdb::DB_CONSUME
CONSUME_WAIT = Bdb::DB_CONSUME_WAIT
attr_reader :home
attr_accessor :txn
include Enumerable
def bdb_object() @db end
def sync() @db.sync end
def close( flg = nil) @db.close flg || 0 end
def cursor( &exe) Cursor.new self, &exe end
def at key, txn = nil
@db.get _txn(txn), key.nil? ? nil : key.to_s, nil, 0
rescue Bdb::KeyEmpty
return nil
end
alias [] at
def put key, val, txn = nil
if val.nil?
@db.del _txn(txn), key.to_s, 0
else
@db.put _txn(txn), key.nil? ? nil : key.to_s, val.to_s, 0
end
end
def []= key, val
put key, val
end
def delete key, txn = nil
@db.del _txn(txn), key.to_s
end
alias del delete
class << self
def new *ps, &exe
ret = obj = super( *ps)
begin ret = exe.call obj
ensure
SBDB::raise_barrier &obj.method(:sync)
SBDB::raise_barrier &obj.method(:close)
end if exe
ret
end
alias open new
end
def _txn txn
txn ||= @txn
txn && txn.bdb_object
end
def transaction flg = nil, &exe
block_given? ? home.transaction( flg, &exe) : home.transaction( flg)
end
# Arguments:
# * file
# * name
# * type
# * flags
# * mode
# * env
# or: **file**, **opts**. *opts* must be a *::Hash* with keys like above, excluded *file*.
def initialize file, *args
opts = ::Hash === args.last ? args.pop : {}
opts = {:name => args[0], :type => args[1], :flags => args[2], :mode => args[3], :env => args[4]}.update opts
#type = BTREE if type == UNKNOWN and (flags & CREATE) == CREATE
@home, @db = opts[:env], opts[:env] ? opts[:env].bdb_object.db : Bdb::Db.new
opts[:type] = TYPES.index(self.class) || UNKNOWN
@db.re_len = opts[:re_len] if opts[:re_len]
@txn = nil
txn = opts[:txn] # First is the global txn, second only for open.
begin
@db.open txn && txn.bdb_object, file, opts[:name], opts[:type], opts[:flags] || 0, opts[:mode] || 0
rescue Object => exc
close
raise exc
end
end
def each key = nil, val = nil, &exe
cursor {|c| c.each key, val, &exe }
end
def reverse key = nil, val = nil, &exe
cursor {|c| c.reverse key, val, &exe }
end
def to_hash key = nil, val = nil
ht = {}
each key, val, &ht.method( :[]=)
ht
end
def truncate txn = nil
@db.truncate _txn(txn)
end
end
class Unknown < DB
def self.new file, *ps, &exe
dbt = super( file, *ps) {|db| db.bdb_object.get_type }
TYPES[dbt] ? TYPES[dbt].new( file, *ps, &exe) : super( file, *ps, &exe)
end
end
class Btree < DB
end
TYPES[DB::BTREE] = Btree
class Hash < DB
end
TYPES[DB::HASH] = Hash
module Arrayisch
def [] key
super [key].pack('I')
end
def []= key, val
super [key].pack('I'), val
end
def push val, txn = nil
@db.put _txn(txn), "\0\0\0\0", val, Bdb::DB_APPEND
end
end
class Recno < DB
include Arrayisch
end
Array = Recno
TYPES[DB::RECNO] = Recno
class Queue < DB
include Arrayisch
def unshift txn = nil
@db.get _txn(txn), "\0\0\0\0", nil, Bdb::DB_CONSUME
end
end
TYPES[DB::QUEUE] = Queue
end

116
lib/sbdb/environment.rb Normal file
View File

@ -0,0 +1,116 @@
require 'bdb'
require 'sbdb/db'
require 'sbdb/transaction'
module SBDB
# Environments are for storing one or more databases and are important
# if you want to work with more than one process on one database.
# You needn't use Environment, but it's usefull.
class Environment
INIT_TXN = Bdb::DB_INIT_TXN
INIT_LOCK = Bdb::DB_INIT_LOCK
INIT_LOG = Bdb::DB_INIT_LOG
INIT_MPOOL = Bdb::DB_INIT_MPOOL
INIT_TRANSACTION = INIT_TXN | INIT_LOCK | INIT_LOG | INIT_MPOOL
LOCKDOWN = Bdb::DB_LOCKDOWN
NOMMAP = Bdb::DB_NOMMAP
PRIVATE = Bdb::DB_PRIVATE
SYSTEM_MEM = Bdb::DB_SYSTEM_MEM
TXN_NOSYNC = Bdb::DB_TXN_NOSYNC
LOG_DIRECT = Bdb::DB_LOG_DIRECT
LOG_DSYNC = Bdb::DB_LOG_DSYNC
LOG_AUTO_REMOVE = Bdb::DB_LOG_AUTO_REMOVE
LOG_IN_MEMORY = Bdb::DB_LOG_IN_MEMORY
LOG_ZERO = Bdb::DB_LOG_ZERO
# returns the Bdb-object.
def bdb_object() @env end
# Opens a Btree in this Environment
def btree file, *ps, &exe
open Btree, file, *ps, &exe
end
# Opens a Hash in this Environment
def hash file, *ps, &exe
open Hash, file, *ps, &exe
end
# Opens a Recno in this Environment
def recno file, *ps, &exe
open Recno, file, *ps, &exe
end
# Opens a Queue in this Environment
def queue file, *ps, &exe
open Queue, file, *ps, &exe
end
# Opens a DB of unknown type in this Environment
def unknown file, *ps, &exe
open Unknown, file, *ps, &exe
end
def transaction flg = nil, &exe
SBDB::Transaction.new self, flg, &exe
end
alias txn transaction
# args:
# args[0] => dir
# args[1] => flags
# args[3] => mode
# possible options (via Hash):
# :dir, :flags, :mode, :log_config
def initialize *args
opts = ::Hash === args.last ? args.pop : {}
opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts
@dbs, @env = Ref::WeakValueMap.new, Bdb::Env.new( 0)
@env.log_config opts[:log_config], 1 if opts[:log_config]
@env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize]
@env.lg_max = opts[:lg_max] if opts[:lg_max]
begin @env.open opts[:dir]||'.', opts[:flags]|| INIT_TRANSACTION|CREATE, opts[:mode]||0
rescue Object
close
raise
end
end
def self.new *args
obj = ret = super( *args)
begin ret = yield obj
ensure SBDB::raise_barrier &obj.method(:close)
end if block_given?
ret
end
# Close the Environment.
# First you should close all databases!
def close
@dbs.each{|key, db|db.close}
@env.close
end
class << self
alias open new
end
# Opens a Database.
# see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue
def open type, file, *ps, &exe
ps.push ::Hash.new unless ::Hash === ps.last
ps.last[:env] = self
(type || SBDB::Unkown).new file, *ps, &exe
end
alias db open
alias open_db open
# Returns the DB like open, but if it's already opened,
# it returns the old instance.
# If you use this, never use close. It's possible somebody else use it too.
# The Databases, which are opened, will close, if the Environment will close.
def [] file, *ps, &exe
opts = ::Hash === ps.last ? ps.pop : {}
opts[:env] = self
name, type, flg = ps[0] || opts[:name], ps[1] || opts[:type], ps[2] || opts[:flags]
ps.push opts
@dbs[ [file, name, flg | CREATE]] ||= (type || SBDB::Unknown).new file, *ps, &exe
end
end
Env = Environment
end

42
lib/sbdb/transaction.rb Normal file
View File

@ -0,0 +1,42 @@
require 'sbdb'
require 'bdb'
module SBDB
class Transaction
NOSYNC = Bdb::DB_TXN_NOSYNC
SYNC = Bdb::DB_TXN_SYNC
NOWAIT = Bdb::DB_TXN_NOWAIT
WRITE_NOSYNC = Bdb::DB_TXN_WRITE_NOSYNC
SNAPSHOT = Bdb::DB_TXN_SNAPSHOT
READ_COMMITED = Bdb::DB_READ_COMMITTED
READ_UNCOMMITED = Bdb::DB_READ_UNCOMMITTED
def bdb_object() @txn end
def self.new *p, &e
r = obj = super( *p)
begin
r = e.call obj
rescue Object
obj.abort
raise $!
ensure
obj.commit
end if e
r
end
def initialize env, flags = nil, parent = nil
@txn = env.bdb_object.txn_begin parent, flags || 0
end
def commit flags = nil
@txn.commit flags || 0
end
def abort
@txn.abort
end
end
TXN = Transaction
end

View File

@ -1,39 +0,0 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{sbdb}
s.version = "0.0.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Denis Knauf"]
s.date = %q{2010-01-29}
s.description = %q{Simple Ruby Berkeley DB wrapper library for bdb.}
s.email = %q{Denis.Knauf@gmail.com}
s.extra_rdoc_files = [
"README"
]
s.files = [
"README",
"VERSION",
"lib/sbdb.rb"
]
s.homepage = %q{http://github.com/DenisKnauf/bdb}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
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::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
end