reek detects common code smells

This commit is contained in:
Denis Knauf 2010-03-12 21:14:38 +01:00
parent b9d52726ae
commit e7113ce586
4 changed files with 106 additions and 90 deletions

View file

@ -10,11 +10,11 @@ module SBDB
RDONLY = Bdb::DB_RDONLY RDONLY = Bdb::DB_RDONLY
READONLY = RDONLY READONLY = RDONLY
def btree( *p) Btree.new *p end def btree( *ps) Btree.new *ps end
def hash( *p) Hash.new *p end def hash( *ps) Hash.new *ps end
def recno( *p) Recno.new *p end def recno( *ps) Recno.new *ps end
def queue( *p) Queue.new *p end def queue( *ps) Queue.new *ps end
def unknown( *p) Unknown.new *p end def unknown( *ps) Unknown.new *ps end
alias open_db unknown alias open_db unknown
def raise_barrier *ps, &e def raise_barrier *ps, &e

View file

@ -10,21 +10,37 @@ module SBDB
attr_reader :db attr_reader :db
include Enumerable include Enumerable
def bdb_object() @cursor end def bdb_object
def close() @cursor.close end @cursor
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 self.new *p
x = super *p
return x unless block_given?
begin yield x
ensure x.close
end 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 end
def initialize ref def initialize ref
@ -33,25 +49,25 @@ module SBDB
when Bdb::Db::Cursor then [ref] when Bdb::Db::Cursor then [ref]
else [ref.bdb_object.cursor( nil, 0), ref] else [ref.bdb_object.cursor( nil, 0), ref]
end end
if [Recno, Queue].any? {|t| t === @db } if [Recno, Queue].any? {|dbt| dbt === @db }
def get k, v, f def get *ps
l, w = @cursor.get( k, v, f) key, val = @cursor.get( *ps)
l.nil? ? nil : [l.unpack('I')[0], w] key.nil? ? nil : [key.unpack('I')[0], val]
end end
end end
end end
def reverse k = nil, v = nil, &e def reverse key = nil, val = nil, &exe
each k, v, LAST, PREV, &e each key, val, LAST, PREV, &exe
end end
def each k = nil, v = nil, f = nil, n = nil def each key = nil, val = nil, flg = nil, nxt = nil
return Enumerator.new( self, :each, k, v, f, n) unless block_given? return Enumerator.new( self, :each, key, val, flg, nxt) unless block_given?
n ||= NEXT nxt ||= NEXT
e = get k, v, f || FIRST ent = get key, val, flg || FIRST
return unless e return unless ent
yield *e yield *ent
yield *e while e = get( k, v, n) yield *ent while ent = get( key, val, nxt)
nil nil
end end
end end

View file

@ -18,37 +18,37 @@ module SBDB
include Enumerable include Enumerable
def bdb_object() @db end def bdb_object() @db end
def sync() @db.sync end def sync() @db.sync end
def close( f = nil) @db.close f || 0 end def close( flg = nil) @db.close flg || 0 end
def cursor( &e) Cursor.new self, &e end def cursor( &exe) Cursor.new self, &exe end
def at k, txn = nil def at key, txn = nil
@db.get _txn(txn), k.nil? ? nil : k.to_s, nil, 0 @db.get _txn(txn), key.nil? ? nil : key.to_s, nil, 0
rescue Bdb::KeyEmpty rescue Bdb::KeyEmpty
return nil return nil
end end
alias [] at alias [] at
def put k, v, txn = nil def put key, val, txn = nil
if v.nil? if val.nil?
@db.del _txn(txn), k.to_s, 0 @db.del _txn(txn), key.to_s, 0
else 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
end end
def []= k, v def []= key, val
put k, v put key, val
end end
def delete k, txn = nil def delete key, txn = nil
@db.del _txn(txn), k.to_s @db.del _txn(txn), key.to_s
end end
alias del delete alias del delete
class << self class << self
def new *ps, &exe def new *ps, &exe
ret = obj = super( *ps) ret = obj = super( *ps)
begin ret = e.call obj begin ret = exe.call obj
ensure ensure
SBDB::raise_barrier obj.method(:sync) SBDB::raise_barrier obj.method(:sync)
SBDB::raise_barrier obj.method(:close) SBDB::raise_barrier obj.method(:close)
@ -58,9 +58,9 @@ module SBDB
alias open new alias open new
end end
def _txn t def _txn txn
t ||= @txn txn ||= @txn
t && t.bdb_object txn && t.bdb_object
end end
def initialize file, *args def initialize file, *args
@ -79,17 +79,17 @@ module SBDB
end end
end end
def each k = nil, v = nil, &e def each key = nil, val = nil, &exe
cursor{|c|c.each k, v, &e} cursor{|c|c.each key, val, &exe}
end end
def reverse k = nil, v = nil, &e def reverse key = nil, val = nil, &exe
cursor{|c|c.reverse k, v, &e} cursor{|c|c.reverse key, val, &exe}
end end
def to_hash k = nil, v = nil def to_hash k = nil, v = nil
h = {} h = {}
each( k, v) {|k, v| h[ k] = v } each( key, val, h.method(:[]=))
h h
end end
@ -99,9 +99,9 @@ module SBDB
end end
class Unknown < DB class Unknown < DB
def self.new file, *p, &e def self.new file, *ps, &exe
dbt = super( file, *p) {|db| db.bdb_object.get_type } dbt = super( file, *ps) {|db| db.bdb_object.get_type }
TYPES[dbt] ? TYPES[dbt].new( file, *p, &e) : super( file, *p, &e) TYPES[dbt] ? TYPES[dbt].new( file, *ps, &exe) : super( file, *ps, &exe)
end end
end end
@ -114,15 +114,15 @@ module SBDB
TYPES[DB::HASH] = Hash TYPES[DB::HASH] = Hash
module Arrayisch module Arrayisch
def [] k def [] key
super [k].pack('I') super [key].pack('I')
end end
def []= k, v def []= key, v
super [k].pack('I'), v super [key].pack('I'), val
end end
def push v, txn = nil def push val, txn = nil
@db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND @db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND
end end
end end

View file

@ -27,28 +27,28 @@ module SBDB
# returns the Bdb-object. # returns the Bdb-object.
def bdb_object() @env end def bdb_object() @env end
# Opens a Btree in this Environment # Opens a Btree in this Environment
def btree file, *p, &e def btree file, *ps, &exe
open Btree, file, *p, &e open Btree, file, *ps, &exe
end end
# Opens a Hash in this Environment # Opens a Hash in this Environment
def hash file, *p, &e def hash file, *ps, &exe
open Hash, file, *p, &e open Hash, file, *ps, &exe
end end
# Opens a Recno in this Environment # Opens a Recno in this Environment
def recno file, *p, &e def recno file, *ps, &exe
open Recno, file, *p, &e open Recno, file, *ps, &exe
end end
# Opens a Queue in this Environment # Opens a Queue in this Environment
def queue file, *p, &e def queue file, *ps, &exe
open Queue, file, *p, &e open Queue, file, *ps, &exe
end end
# Opens a DB of unknown type in this Environment # Opens a DB of unknown type in this Environment
def unknown file, *p, &e def unknown file, *ps, &exe
open Unknown, file, *p, &e open Unknown, file, *ps, &exe
end end
def transaction flags = nil, &e def transaction flg = nil, &exe
SBDB::Transaction.new self, flags, &e SBDB::Transaction.new self, flg, &exe
end end
alias txn transaction alias txn transaction
@ -73,17 +73,17 @@ module SBDB
end end
def self.new *args def self.new *args
obj = r = super( *args) obj = ret = super( *args)
begin r = yield obj begin ret = yield obj
ensure SBDB::raise_barrier obj.method(:close) ensure SBDB::raise_barrier obj.method(:close)
end if block_given? end if block_given?
r ret
end end
# Close the Environment. # Close the Environment.
# First you should close all databases! # First you should close all databases!
def close def close
@dbs.each{|k, db|db.close} @dbs.each{|key, db|db.close}
@env.close @env.close
end end
@ -93,10 +93,10 @@ module SBDB
# Opens a Database. # Opens a Database.
# see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue # see SBDB::DB, SBDB::Btree, SBDB::Hash, SBDB::Recno, SBDB::Queue
def open type, file, *p, &e def open type, file, *ps, &exe
p.push ::Hash.new unless ::Hash === p.last ps.push ::Hash.new unless ::Hash === ps.last
p.last[:env] = self ps.last[:env] = self
(type || SBDB::Unkown).new file, *p, &e (type || SBDB::Unkown).new file, *ps, &exe
end end
alias db open alias db open
alias open_db open alias open_db open
@ -105,14 +105,14 @@ module SBDB
# it returns the old instance. # it returns the old instance.
# If you use this, never use close. It's possible somebody else use it too. # 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. # The Databases, which are opened, will close, if the Environment will close.
def [] file, *p, &e def [] file, *ps, &exe
p.push ::Hash.new unless ::Hash === p.last ps.push ::Hash.new unless ::Hash === ps.last
p.last[:env] = self ps.last[:env] = self
name, flags, type = name, flg, type =
String === p[0] ? p[0] : p.last[:name], String === ps[0] ? ps[0] : ps.last[:name],
Fixnum === p[1] ? p[1] : p.last[:flags], Fixnum === ps[1] ? ps[1] : ps.last[:flags],
Fixnum === p[2] ? p[2] : p.last[:type] Fixnum === ps[2] ? ps[2] : ps.last[:type]
@dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unknown).new file, *p, &e @dbs[ [file, name, flg | CREATE]] ||= (type || SBDB::Unknown).new file, *ps, &exe
end end
end end
Env = Environment Env = Environment