reek detects common code smells
This commit is contained in:
parent
b9d52726ae
commit
e7113ce586
4 changed files with 106 additions and 90 deletions
10
lib/sbdb.rb
10
lib/sbdb.rb
|
@ -10,11 +10,11 @@ module SBDB
|
|||
RDONLY = Bdb::DB_RDONLY
|
||||
READONLY = RDONLY
|
||||
|
||||
def btree( *p) Btree.new *p end
|
||||
def hash( *p) Hash.new *p end
|
||||
def recno( *p) Recno.new *p end
|
||||
def queue( *p) Queue.new *p end
|
||||
def unknown( *p) Unknown.new *p 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 raise_barrier *ps, &e
|
||||
|
|
|
@ -10,21 +10,37 @@ module SBDB
|
|||
attr_reader :db
|
||||
|
||||
include Enumerable
|
||||
def bdb_object() @cursor end
|
||||
def close() @cursor.close end
|
||||
def get( k, v, f) @cursor.get( k, v, f) end
|
||||
def count() @cursor.count end
|
||||
def first( k = nil, v = nil) get k, v, FIRST end
|
||||
def last( k = nil, v = nil) get k, v, LAST end
|
||||
def next( k = nil, v = nil) get k, v, NEXT end
|
||||
def prev( k = nil, v = nil) get k, v, PREV end
|
||||
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 *p
|
||||
x = super *p
|
||||
return x unless block_given?
|
||||
begin yield x
|
||||
ensure x.close
|
||||
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
|
||||
|
@ -33,25 +49,25 @@ module SBDB
|
|||
when Bdb::Db::Cursor then [ref]
|
||||
else [ref.bdb_object.cursor( nil, 0), ref]
|
||||
end
|
||||
if [Recno, Queue].any? {|t| t === @db }
|
||||
def get k, v, f
|
||||
l, w = @cursor.get( k, v, f)
|
||||
l.nil? ? nil : [l.unpack('I')[0], w]
|
||||
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 k = nil, v = nil, &e
|
||||
each k, v, LAST, PREV, &e
|
||||
def reverse key = nil, val = nil, &exe
|
||||
each key, val, LAST, PREV, &exe
|
||||
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 = get k, v, f || FIRST
|
||||
return unless e
|
||||
yield *e
|
||||
yield *e while e = get( k, v, n)
|
||||
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
|
||||
|
|
|
@ -18,37 +18,37 @@ module SBDB
|
|||
include Enumerable
|
||||
def bdb_object() @db end
|
||||
def sync() @db.sync end
|
||||
def close( f = nil) @db.close f || 0 end
|
||||
def cursor( &e) Cursor.new self, &e end
|
||||
def close( flg = nil) @db.close flg || 0 end
|
||||
def cursor( &exe) Cursor.new self, &exe end
|
||||
|
||||
def at k, txn = nil
|
||||
@db.get _txn(txn), k.nil? ? nil : k.to_s, nil, 0
|
||||
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 k, v, txn = nil
|
||||
if v.nil?
|
||||
@db.del _txn(txn), k.to_s, 0
|
||||
def put key, val, txn = nil
|
||||
if val.nil?
|
||||
@db.del _txn(txn), key.to_s, 0
|
||||
else
|
||||
@db.put _txn(txn), k.nil? ? nil : k.to_s, v.to_s, 0
|
||||
@db.put _txn(txn), key.nil? ? nil : key.to_s, val.to_s, 0
|
||||
end
|
||||
end
|
||||
|
||||
def []= k, v
|
||||
put k, v
|
||||
def []= key, val
|
||||
put key, val
|
||||
end
|
||||
|
||||
def delete k, txn = nil
|
||||
@db.del _txn(txn), k.to_s
|
||||
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 = e.call obj
|
||||
begin ret = exe.call obj
|
||||
ensure
|
||||
SBDB::raise_barrier obj.method(:sync)
|
||||
SBDB::raise_barrier obj.method(:close)
|
||||
|
@ -58,9 +58,9 @@ module SBDB
|
|||
alias open new
|
||||
end
|
||||
|
||||
def _txn t
|
||||
t ||= @txn
|
||||
t && t.bdb_object
|
||||
def _txn txn
|
||||
txn ||= @txn
|
||||
txn && t.bdb_object
|
||||
end
|
||||
|
||||
def initialize file, *args
|
||||
|
@ -79,17 +79,17 @@ module SBDB
|
|||
end
|
||||
end
|
||||
|
||||
def each k = nil, v = nil, &e
|
||||
cursor{|c|c.each k, v, &e}
|
||||
def each key = nil, val = nil, &exe
|
||||
cursor{|c|c.each key, val, &exe}
|
||||
end
|
||||
|
||||
def reverse k = nil, v = nil, &e
|
||||
cursor{|c|c.reverse k, v, &e}
|
||||
def reverse key = nil, val = nil, &exe
|
||||
cursor{|c|c.reverse key, val, &exe}
|
||||
end
|
||||
|
||||
def to_hash k = nil, v = nil
|
||||
h = {}
|
||||
each( k, v) {|k, v| h[ k] = v }
|
||||
each( key, val, h.method(:[]=))
|
||||
h
|
||||
end
|
||||
|
||||
|
@ -99,9 +99,9 @@ module SBDB
|
|||
end
|
||||
|
||||
class Unknown < DB
|
||||
def self.new file, *p, &e
|
||||
dbt = super( file, *p) {|db| db.bdb_object.get_type }
|
||||
TYPES[dbt] ? TYPES[dbt].new( file, *p, &e) : super( file, *p, &e)
|
||||
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
|
||||
|
||||
|
@ -114,15 +114,15 @@ module SBDB
|
|||
TYPES[DB::HASH] = Hash
|
||||
|
||||
module Arrayisch
|
||||
def [] k
|
||||
super [k].pack('I')
|
||||
def [] key
|
||||
super [key].pack('I')
|
||||
end
|
||||
|
||||
def []= k, v
|
||||
super [k].pack('I'), v
|
||||
def []= key, v
|
||||
super [key].pack('I'), val
|
||||
end
|
||||
|
||||
def push v, txn = nil
|
||||
def push val, txn = nil
|
||||
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,28 +27,28 @@ module SBDB
|
|||
# returns the Bdb-object.
|
||||
def bdb_object() @env end
|
||||
# Opens a Btree in this Environment
|
||||
def btree file, *p, &e
|
||||
open Btree, file, *p, &e
|
||||
def btree file, *ps, &exe
|
||||
open Btree, file, *ps, &exe
|
||||
end
|
||||
# Opens a Hash in this Environment
|
||||
def hash file, *p, &e
|
||||
open Hash, file, *p, &e
|
||||
def hash file, *ps, &exe
|
||||
open Hash, file, *ps, &exe
|
||||
end
|
||||
# Opens a Recno in this Environment
|
||||
def recno file, *p, &e
|
||||
open Recno, file, *p, &e
|
||||
def recno file, *ps, &exe
|
||||
open Recno, file, *ps, &exe
|
||||
end
|
||||
# Opens a Queue in this Environment
|
||||
def queue file, *p, &e
|
||||
open Queue, file, *p, &e
|
||||
def queue file, *ps, &exe
|
||||
open Queue, file, *ps, &exe
|
||||
end
|
||||
# Opens a DB of unknown type in this Environment
|
||||
def unknown file, *p, &e
|
||||
open Unknown, file, *p, &e
|
||||
def unknown file, *ps, &exe
|
||||
open Unknown, file, *ps, &exe
|
||||
end
|
||||
|
||||
def transaction flags = nil, &e
|
||||
SBDB::Transaction.new self, flags, &e
|
||||
def transaction flg = nil, &exe
|
||||
SBDB::Transaction.new self, flg, &exe
|
||||
end
|
||||
alias txn transaction
|
||||
|
||||
|
@ -73,17 +73,17 @@ module SBDB
|
|||
end
|
||||
|
||||
def self.new *args
|
||||
obj = r = super( *args)
|
||||
begin r = yield obj
|
||||
obj = ret = super( *args)
|
||||
begin ret = yield obj
|
||||
ensure SBDB::raise_barrier obj.method(:close)
|
||||
end if block_given?
|
||||
r
|
||||
ret
|
||||
end
|
||||
|
||||
# Close the Environment.
|
||||
# First you should close all databases!
|
||||
def close
|
||||
@dbs.each{|k, db|db.close}
|
||||
@dbs.each{|key, db|db.close}
|
||||
@env.close
|
||||
end
|
||||
|
||||
|
@ -93,10 +93,10 @@ module SBDB
|
|||
|
||||
# Opens a Database.
|
||||
# see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue
|
||||
def open type, file, *p, &e
|
||||
p.push ::Hash.new unless ::Hash === p.last
|
||||
p.last[:env] = self
|
||||
(type || SBDB::Unkown).new file, *p, &e
|
||||
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
|
||||
|
@ -105,14 +105,14 @@ module SBDB
|
|||
# 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, *p, &e
|
||||
p.push ::Hash.new unless ::Hash === p.last
|
||||
p.last[:env] = self
|
||||
name, flags, type =
|
||||
String === p[0] ? p[0] : p.last[:name],
|
||||
Fixnum === p[1] ? p[1] : p.last[:flags],
|
||||
Fixnum === p[2] ? p[2] : p.last[:type]
|
||||
@dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unknown).new file, *p, &e
|
||||
def [] file, *ps, &exe
|
||||
ps.push ::Hash.new unless ::Hash === ps.last
|
||||
ps.last[:env] = self
|
||||
name, flg, type =
|
||||
String === ps[0] ? ps[0] : ps.last[:name],
|
||||
Fixnum === ps[1] ? ps[1] : ps.last[:flags],
|
||||
Fixnum === ps[2] ? ps[2] : ps.last[:type]
|
||||
@dbs[ [file, name, flg | CREATE]] ||= (type || SBDB::Unknown).new file, *ps, &exe
|
||||
end
|
||||
end
|
||||
Env = Environment
|
||||
|
|
Loading…
Reference in a new issue