| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::Cache::Static::CDB.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: DTA static cache | ||
| 6 | |||
| 7 | ##============================================================================== | ||
| 8 | ## Package | ||
| 9 | ##============================================================================== | ||
| 10 | package DTA::CAB::Analyzer::Cache::Static::CDB; | ||
| 11 | use DTA::CAB::Analyzer ':child'; | ||
| 12 | use DTA::CAB::Analyzer::Dict::JsonCDB; | ||
| 13 | use Carp; | ||
| 14 | use strict; | ||
| 15 | our @ISA = qw(DTA::CAB::Analyzer::Dict::JsonCDB); | ||
| 16 | |||
| 17 | ##-------------------------------------------------------------- | ||
| 18 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 19 | ## + object structure: see DTA::CAB::Analyzer::Dict::JsonCDB, DTA::CAB::Analyzer::Dict::Json | ||
| 20 | sub new { | ||
| 21 | my $that = shift; | ||
| 22 | my $dic = $that->SUPER::new( | ||
| 23 | ##-- overrides | ||
| 24 | label => 'static', | ||
| 25 | |||
| 26 | #typeKeys=>[qw(eqphox errid exlex f lts mlatin morph msafe pnd rw xlit)], | ||
| 27 | typeKeys => undef, ##-- auto-scanned on ensureLoaded() if not defined here | ||
| 28 | |||
| 29 | analyzeCode =>join("\n", | ||
| 30 | ( | ||
| 31 | 'return if (!defined($val=$dhash->{' | ||
| 32 | #._am_xlit('$_') | ||
| 33 | .'$_->{text}' | ||
| 34 | .'}));' | ||
| 35 | ), | ||
| 36 | '$val=$jxs->decode($val);', | ||
| 37 | '@$_{keys %$val}=values %$val;', | ||
| 38 | ), | ||
| 39 | |||
| 40 | ##-- user args | ||
| 41 | @_ | ||
| 42 | ); | ||
| 43 | |||
| 44 | ##-- set type keys from DTA::CAB::Chain::DTA if possible and not already set | ||
| 45 | #$dic->{typeKeys} = [DTA::CAB::Chain::DTA->new->typeKeys()] if (!$dic->{typeKeys} && DTA::CAB::Chain::DTA->can('new')); | ||
| 46 | return $dic; | ||
| 47 | } | ||
| 48 | |||
| 49 | ##-------------------------------------------------------------- | ||
| 50 | ## @keys = $anl->typeKeys(\%opts) | ||
| 51 | ## + get type keys | ||
| 52 | ## + default respects $opts->{"${key}_enabled"} | ||
| 53 | sub typeKeys { | ||
| 54 | return qw() if (!$_[0]{typeKeys}); | ||
| 55 | return grep {!defined($_[1]{"${_}_enabled"}) || $_[1]{"${_}enabled"}} @{$_[0]{typeKeys}}; | ||
| 56 | } | ||
| 57 | |||
| 58 | ##-------------------------------------------------------------- | ||
| 59 | ## $bool = $dic->ensureLoaded() | ||
| 60 | ## + ensures analyzer data is loaded from default files | ||
| 61 | sub ensureLoaded { | ||
| 62 | my $dic = shift; | ||
| 63 | my $rc = $dic->SUPER::ensureLoaded(@_) || return undef; | ||
| 64 | |||
| 65 | ##-- maybe scan for embedded keys | ||
| 66 | if (!defined($dic->{typeKeys}) && $dic->{dbf} && $dic->{dbf}{tied}) { | ||
| 67 | $dic->info("scanning CDB file for sub-keys..."); | ||
| 68 | my $tied = $dic->{dbf}{tied}; | ||
| 69 | my %jkeys = qw(); | ||
| 70 | my $jxs = $dic->jsonxs; | ||
| 71 | my ($t,$js,$j); | ||
| 72 | for ($t=$tied->FIRSTKEY; defined($t); $t=$tied->NEXTKEY($t)) { | ||
| 73 | $js = $tied->FETCH($t); | ||
| 74 | $j = $jxs->decode($js); | ||
| 75 | @jkeys{keys %$j} = qw() if ((ref($j)||'') eq 'HASH'); | ||
| 76 | } | ||
| 77 | $dic->{typeKeys} = [sort keys %jkeys]; | ||
| 78 | $dic->info("scanned sub-keys {", join(',', @{$dic->{typeKeys}}), "}"); | ||
| 79 | } | ||
| 80 | |||
| 81 | return $rc; | ||
| 82 | } | ||
| 83 | |||
| 84 | ##-------------------------------------------------------------- | ||
| 85 | ## $prefix = $dict->analyzePre() | ||
| 86 | sub analyzePre { | ||
| 87 | my $dic = shift; | ||
| 88 | return $dic->DTA::CAB::Analyzer::Dict::JsonCDB::analyzePre(@_);#.' my $tied=tied($dhash);'; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | 1; ##-- be happy | ||
| 93 | |||
| 94 | __END__ |