From: imiyazaki Date: Tue, 27 Jan 2004 17:17:18 +0000 (+0000) Subject: add flag of $db_opened and $rdb_opened. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=f147f7a9b4996022a00ab9fbf885e47ac0e688d6;p=chise%2Fperl.git add flag of $db_opened and $rdb_opened. --- diff --git a/Chise_utils/Chise_utils.pm b/Chise_utils/Chise_utils.pm index b03cf0b..00d241a 100644 --- a/Chise_utils/Chise_utils.pm +++ b/Chise_utils/Chise_utils.pm @@ -8,8 +8,8 @@ require Exporter; use utf8; use BerkeleyDB; -use vars qw(%db %chardb - %reverse_db %reverse_chardb +use vars qw(%db %chardb %db_opened + %reverse_db %reverse_chardb %rdb_opened %er_alias $er_prefix_re $atr $idc $omegadb_path @@ -140,11 +140,15 @@ if(-d "$DB_HOME/character"){ sub get_db{ my($atr)=@_; - return 1 if(defined(%{$chardb{$atr}})); + return 1 if($db_opened{$atr}); if(defined($db{$atr}) and -f $db{$atr}){ - tie %{$chardb{$atr}}, "BerkeleyDB::Hash", - -Filename => $db{$atr}, - -Flags => DB_RDONLY; + if(tie %{$chardb{$atr}}, 'BerkeleyDB::Hash', + -Filename => $db{$atr}, + -Flags => DB_RDONLY){ + $db_opened{$atr}=1; + }else{ + return undef; + } }else{ return undef; } @@ -152,11 +156,15 @@ sub get_db{ sub get_reverse_db{ my($atr)=@_; - return 1 if(defined(%{$reverse_chardb{$atr}})); + return 1 if($rdb_opened{$atr}); if(defined($reverse_db{$atr}) and -f $reverse_db{$atr}){ - tie %{$reverse_chardb{$atr}}, "BerkeleyDB::Hash", - -Filename => $reverse_db{$atr}, - -Flags => DB_RDONLY; + if(tie %{$reverse_chardb{$atr}}, "BerkeleyDB::Hash", + -Filename => $reverse_db{$atr}, + -Flags => DB_RDONLY){ + $rdb_opened{$atr}=1; + }else{ + return undef; + } }else{ return undef; } @@ -166,7 +174,9 @@ sub get_reverse_db{ sub get_char_attribute{ my($char,$atr)=@_; my($res); - &get_db($atr) or return ""; + unless($db_opened{$atr}){ + &get_db($atr) or return ""; + } if($res=$chardb{$atr}->{"?$char"}){ utf8::decode($res); return $res; @@ -178,14 +188,15 @@ sub get_char_attribute{ sub get_chars_containing{ my($atr,$value)=@_; my($char,@res); - if(&get_db($atr)){ - foreach $char (keys %{$chardb{$atr}}){ - if($chardb{$atr}->{$char}=~/$value/){ - utf8::decode($char); - $char=~s/^\?//; - push @res,$char; - } - } + unless($db_opened{$atr}){ + &get_db($atr) or return (); + } + foreach $char (keys %{$chardb{$atr}}){ + if($chardb{$atr}->{$char}=~/$value/){ + utf8::decode($char); + $char=~s/^\?//; + push @res,$char; + } } return @res; } @@ -194,26 +205,28 @@ sub get_chars_matching{ my($atr,$value)=@_; my($char,@res); if(defined($reverse_db{$atr})){ - if(&get_reverse_db($atr)){ - if($char=$reverse_chardb{$atr}->{$value}){ - utf8::decode($char); - $char=~s/^\?//; - push @res,$char; - } + unless($rdb_opened{$atr}){ + &get_reverse_db($atr) or return (); } + if($char=$reverse_chardb{$atr}->{$value}){ + utf8::decode($char); + $char=~s/^\?//; + push @res,$char; + } } else{ # never fall back. # unless(@res){ # # fall back if DB inconsistency exists. - if(&get_db($atr)){ - foreach $char (keys %{$chardb{$atr}}){ - if($chardb{$atr}->{$char} eq $value){ - utf8::decode($char); - $char=~s/^\?//; - push @res,$char; - } - } + unless($db_opened{$atr}){ + &get_db($atr) or return (); + } + foreach $char (keys %{$chardb{$atr}}){ + if($chardb{$atr}->{$char} eq $value){ + utf8::decode($char); + $char=~s/^\?//; + push @res,$char; + } } } return @res;