From: rkinyon Date: Tue, 30 Jan 2007 03:25:19 +0000 (+0000) Subject: Cleaned up validation code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d736cba07a484c8b295a477f0dae5d7ecc7a3ee;p=dbsrgits%2FDBM-Deep.git Cleaned up validation code --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 65aa508..c2e4a0e 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -81,52 +81,26 @@ sub new { $self->{$param} = $args->{$param}; } - ## - # Number of buckets per blist before another level of indexing is - # done. Increase this value for slightly greater speed, but larger database - # files. DO NOT decrease this value below 16, due to risk of recursive - # reindex overrun. - ## - if ( !defined $self->{max_buckets} - || !length $self->{max_buckets} - || $self->{max_buckets} =~ /\D/ - || $self->{max_buckets} < 16 - ) { - $self->{max_buckets} = '(undef)' if !defined $self->{max_buckets}; - warn "Floor of max_buckets is 16. Setting it to 16 from '$self->{max_buckets}'\n"; - $self->{max_buckets} = 16; - } - elsif ( $self->{max_buckets} > 256 ) { - warn "Ceiling of max_buckets is 256. Setting it to 256 from '$self->{max_buckets}'\n"; - $self->{max_buckets} = 256; - } - - if ( !defined $self->{num_txns} - || !length $self->{num_txns} - || $self->{num_txns} =~ /\D/ - || $self->{num_txns} < 1 - ) { - $self->{num_txns} = '(undef)' if !defined $self->{num_txns}; - warn "Floor of num_txns is 1. Setting it to 1 from '$self->{num_txns}'\n"; - $self->{num_txns} = 1; - } - elsif ( $self->{num_txns} > 255 ) { - warn "Ceiling of num_txns is 255. Setting it to 255 from '$self->{num_txns}'\n"; - $self->{num_txns} = 255; - } - - if ( !defined $self->{data_sector_size} - || !length $self->{data_sector_size} - || $self->{data_sector_size} =~ /\D/ - || $self->{data_sector_size} < 32 - ) { - $self->{data_sector_size} = '(undef)' if !defined $self->{data_sector_size}; - warn "Floor of data_sector_size is 32. Setting it to 32 from '$self->{data_sector_size}'\n"; - $self->{data_sector_size} = 32; - } - elsif ( $self->{data_sector_size} > 256 ) { - warn "Ceiling of data_sector_size is 256. Setting it to 256 from '$self->{data_sector_size}'\n"; - $self->{data_sector_size} = 256; + my %validations = ( + max_buckets => { floor => 16, ceil => 256 }, + num_txns => { floor => 1, ceil => 255 }, + data_sector_size => { floor => 32, ceil => 256 }, + ); + + while ( my ($attr, $c) = each %validations ) { + if ( !defined $self->{$attr} + || !length $self->{$attr} + || $self->{$attr} =~ /\D/ + || $self->{$attr} < $c->{floor} + ) { + $self->{$attr} = '(undef)' if !defined $self->{$attr}; + warn "Floor of $attr is $c->{floor}. Setting it to $c->{floor} from '$self->{$attr}'\n"; + $self->{$attr} = $c->{floor}; + } + elsif ( $self->{$attr} > $c->{ceil} ) { + warn "Ceiling of $attr is $c->{ceil}. Setting it to $c->{ceil} from '$self->{$attr}'\n"; + $self->{$attr} = $c->{ceil}; + } } if ( !$self->{digest} ) {