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
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;
}
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;
}
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;
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;
}
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;