| 1 | package Fuse::TagLayer::PurePerl; | ||
| 2 | |||
| 3 | use Data::Dumper; | ||
| 4 | |||
| 5 | use Exporter; | ||
| 6 | push @Fuse::TagLayer::PurePerl::ISA, 'Exporter'; | ||
| 7 | our @EXPORT = ('db_init','db_sync','db_disconnect','db_tags_add','db_tags_all','db_files_add','db_files_for_tags'); | ||
| 8 | |||
| 9 | our %hash; # files by tag, HoA | ||
| 10 | our %tags; # tags by file, HoH | ||
| 11 | our @all; | ||
| 12 | our $opts; | ||
| 13 | |||
| 14 | sub db_init { | ||
| 15 | $opts = { @_ }; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub db_sync { 1; } | ||
| 19 | |||
| 20 | sub db_disconnect { | ||
| 21 | %hash = (); | ||
| 22 | %tags = (); | ||
| 23 | @all = (); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub db_tags_add { 1; } | ||
| 27 | |||
| 28 | sub db_tags_all { | ||
| 29 | unless(@all){ @all = keys %hash }; | ||
| 30 | return \@all; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub db_files_add { | ||
| 34 | my ($dir,$basename,@tags) = @_; | ||
| 35 | |||
| 36 | print "## PurePerl::db_files_add: dir:$dir, basename:$basename, tags:@tags\n" if $opts->{debug} > 2; | ||
| 37 | |||
| 38 | for(@tags){ | ||
| 39 | # %{ ${ $hash{$_} }[0] } = map { $_ => 1 } @tags; | ||
| 40 | |||
| 41 | %{ $tags{ $dir.'/'.$basename } } = map { $_ => 1 } @tags; | ||
| 42 | |||
| 43 | push( @{ $hash{$_} }, $dir.'/'.$basename ); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | ## returns: | ||
| 48 | ## ref to a list of files tagged with input tags, and | ||
| 49 | ## ref to a hash of subtags (all tags associated with returned files, minus the input tags) | ||
| 50 | sub db_files_for_tags { | ||
| 51 | my (@tags) = @_; | ||
| 52 | |||
| 53 | my @oktags; | ||
| 54 | for(@tags){ | ||
| 55 | push(@oktags, $_) if defined $hash{ $_ }; | ||
| 56 | } | ||
| 57 | unless(@oktags){ | ||
| 58 | print "## PurePerl::db_files_for_tags: @tags ; no files\n" if $opts->{debug}; | ||
| 59 | return ([],{}) | ||
| 60 | } | ||
| 61 | @tags = @oktags; | ||
| 62 | print "## PurePerl::db_files_for_tags: @tags\n" if $opts->{debug}; | ||
| 63 | |||
| 64 | # gather files | ||
| 65 | my @files_for_tags; | ||
| 66 | my %file_hash; | ||
| 67 | for my $tag (@tags){ | ||
| 68 | for(@{ $hash{ $tag } }){ | ||
| 69 | $file_hash{ $_ }++; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | @files_for_tags = keys %file_hash; | ||
| 73 | |||
| 74 | # gather tags | ||
| 75 | my %subtags; | ||
| 76 | for my $file ( @files_for_tags ){ | ||
| 77 | for( keys %{ $tags{ $file } } ){ | ||
| 78 | $subtags{$_}++; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | print "## PurePerl::db_files_for_tags: ".scalar(@files_for_tags)." files ; ".scalar(keys(%subtags))." subtags (before minus)\n" if $opts->{debug} > 1; | ||
| 83 | |||
| 84 | for(@tags){ delete($subtags{$_}); } | ||
| 85 | |||
| 86 | return (\@files_for_tags, \%subtags); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; |