X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=blobdiff_plain;f=lib%2FID3FS%2FPath.pm;h=629c02adc94fdf8857cec1a7e7df123967669a83;hp=e3aac80d8112cf0a9adab7a2ce8e5a92dbcb4724;hb=c8571ddb3cc1f9bce16c91838de17f27242d2f32;hpb=b3f06e92d5f9414dff01989e8b38253d43507f48 diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index e3aac80..629c02a 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -1,19 +1,37 @@ +# id3fs - a FUSE-based filesystem for browsing audio metadata +# Copyright (C) 2010 Ian Beckwith +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + package ID3FS::Path; use strict; use warnings; use ID3FS::Path::Node; -our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL, - $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST, - $STATE_FILE, $STATE_ALL)=(0..8); +our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_BOOLEAN, + $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7); +# operator precedence our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 ); our $PATH_ALLTRACKS= "TRACKS"; our $PATH_NOARTIST = "NOARTIST"; our $PATH_NOALBUM = "NOALBUM"; +our $ENABLE_FILTER = 1; + sub new { my $proto=shift; @@ -77,12 +95,12 @@ sub dirents # print "DIRENTS: FILE: $self->{path}\n"; if($state==$STATE_ALL) { - @dents=($PATH_ALLTRACKS, $PATH_NOARTIST, $self->artists()); + @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists()); } - elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) + elsif($state==$STATE_TAG) { - if($state==$STATE_TAG && $self->at("tag") && - $self->{db}->tag_has_values($self->tail()->id())) + my $tail=$self->tail(); + if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id())) { @dents=$self->tags(); } @@ -92,15 +110,13 @@ sub dirents { @dents=qw(AND OR); } - push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST)); - push(@dents, $self->artists()); + push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists()); } } elsif($state==$STATE_BOOLEAN) { my $parent=$self->tail(); - unless($self->is("boolean", $parent) && - $parent->{name} eq "NOT") + unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT") { @dents=("NOT"); } @@ -112,7 +128,7 @@ sub dirents } elsif($state==$STATE_ALBUMS) { - @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOALBUM), $self->albums()); + @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums()); } elsif($state==$STATE_TRACKLIST) { @@ -120,19 +136,20 @@ sub dirents } else { - print "DIRENTS: UNHANDLED STATE: $state\n"; + print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n"; } return(\@dents, \@fents); } +# State Machine Of Doom sub parse { my($self)=@_; + $self->state($STATE_ROOT); + return if($self->{path} eq "/"); @{$self->{components}}=split(/\//, $self->{path}); shift @{$self->{components}}; # drop empty field before leading / # print "PATH: $self->{path}\n"; - $self->state($STATE_ROOT); - return if($self->{path} eq "/"); my @parts=@{$self->{components}}; my($tag, $tagval); $self->{elements}=[]; @@ -159,12 +176,12 @@ sub parse elsif($name eq "NOT") { $root_not=1; - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); $self->state($STATE_BOOLEAN); } else { - $tag=ID3FS::Path::Node->new($self->{db},"tag", $name); + $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -177,18 +194,16 @@ sub parse } } } - elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) + elsif($state==$STATE_TAG) { my $tag=$self->tail(); # print "SM: TAG/TAGVAL($state): $name\n"; - if($state==$STATE_TAG && $self->is("tag", $tag) && - $self->{db}->tag_has_values($tag->id())) + if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id())) { # print "Parsing: parent: $tag->id()\n"; - my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id()); + my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id()); if(defined($tagval)) { - $self->state($STATE_TAGVAL); # stay in tag state push(@{$self->{elements}}, $tagval); } @@ -208,16 +223,16 @@ sub parse elsif($name eq "AND") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } elsif($name eq "OR") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -234,7 +249,7 @@ sub parse # print "SM: BOOLEAN: $name\n"; my $parent=$self->tail(); my $allownot=1; - if($self->is("boolean", $parent) && + if($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT") { $allownot=0; @@ -242,11 +257,11 @@ sub parse if($allownot && $name eq "NOT") { $self->state($STATE_BOOLEAN); - push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name)); + push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name)); } else { - my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name); + my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name); if($tag) { push(@{$self->{elements}}, $tag); @@ -272,7 +287,7 @@ sub parse } else { - my $album=ID3FS::Path::Node->new($self->{db}, "album", $name); + my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name); if($album) { push(@{$self->{elements}}, $album); @@ -287,7 +302,7 @@ sub parse elsif($state==$STATE_TRACKLIST) { # print "SM: TRACKLIST: $name\n"; - my $track=ID3FS::Path::Node->new($self->{db}, "file", $name); + my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name); if($track) { push(@{$self->{elements}}, $track); @@ -316,7 +331,7 @@ sub parse } else { - my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name); + my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name); if($artist) { push(@{$self->{elements}}, $artist); @@ -335,20 +350,15 @@ sub parse } } - # remove trailing boolean my @elements=@{$self->{elements}}; - while(@elements && $self->is("boolean", $elements[$#elements])) + # remove trailing boolean + while(@elements && $self->is($TYPE_BOOL, $elements[$#elements])) { pop @elements; } # sort elements by precedence @elements=$self->sort_elements(@elements); $self->{tagtree}=$self->elements_to_tree(\@elements); - if($self->{tagtree}) - { -# my ($conditions, @joins)=$self->{tagtree}->to_sql(); -# print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n"; - } } sub state @@ -362,13 +372,14 @@ sub state return $self->{state}; } +# link up precedence-sorted list into a binary tree sub elements_to_tree { my($self, $elements)=@_; return undef unless(@$elements); my ($left, $right, $op)=(undef, undef, undef); my $thing=pop @$elements; - if($self->is("boolean", $thing)) + if($self->is($TYPE_BOOL, $thing)) { $right=$self->elements_to_tree($elements); if($thing->{name} ne "NOT") @@ -390,18 +401,17 @@ sub sort_elements # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n"; while(my $thing = shift @input) { - if($self->is("tag", $thing)) + if($self->is($TYPE_TAG, $thing)) { # Handle tag values by dropping parent - if(@input && $self->is("tag", $input[0])) + if(@input && $self->is($TYPE_TAG, $input[0])) { $thing=shift @input; } push(@output, $thing); } - elsif($self->is("boolean", $thing)) + elsif($self->is($TYPE_BOOL, $thing)) { - # bool while(@opstack && ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}})) { @@ -429,7 +439,7 @@ sub expecting_values { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($self->{db}->tag_has_values($tail->id())); } @@ -439,7 +449,7 @@ sub trailing_tag_id { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($tail->id()); } @@ -450,7 +460,7 @@ sub trailing_tag_parent { my($self)=@_; my $tail=$self->tail(); - if($self->is("tag", $tail)) + if($self->is($TYPE_TAG, $tail)) { return($tail->{parents_id}); } @@ -463,18 +473,12 @@ sub tail return($self->{elements}->[$#{$self->{elements}}]); } -sub at -{ - my($self, $type)=@_; - return($self->is($type, $self->tail())); -} - sub is { my($self, $type, $thing)=@_; return 0 unless($thing); return 0 unless($thing->type()); - return 1 if($type eq $thing->type()); + return 1 if($type == $thing->type()); return 0; } @@ -500,11 +504,19 @@ sub tags # print "THASVALS: $hasvals\n"; # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n"; my @ids=(); - my $sql=("SELECT tags.name FROM (\n" . - $self->tags_subselect() . - ") AS subselect\n" . - "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" . - "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n"); + my $sql="SELECT tags.name FROM "; + if($self->in_or()) + { + $sql .= "files_x_tags\n"; + } + else + { + $sql .= ("(\n" . + $self->tags_subselect() . + ") AS subselect\n" . + "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n"); + } + $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n"; my @andclauses=(); my $id=$self->trailing_tag_id(); @@ -561,7 +573,7 @@ sub albums my($self)=@_; my @ids=(); my $tail=$self->tail(); - if($self->is("artist", $tail)) + if($self->is($TYPE_ARTIST, $tail)) { return $self->artist_albums($tail->id()); } @@ -621,15 +633,15 @@ sub tracks { my($self)=@_; my $tail=$self->tail(); - if($self->is("artist", $tail)) + if($self->is($TYPE_ARTIST, $tail)) { return $self->artist_tracks($tail->id()); } - elsif($self->is("album", $tail)) + elsif($self->is($TYPE_ALBUM, $tail)) { my $artist_id=0; my $artist=$self->tail_parent(); - if($self->is("artist", $artist)) + if($self->is($TYPE_ARTIST, $artist)) { # should always happen $artist_id=$artist->id(); @@ -653,7 +665,7 @@ sub filename { my($self, $mountpoint)=@_; my $tail=$self->tail(); - if($self->is("file", $tail)) + if($self->is($TYPE_FILE, $tail)) { my $id=$tail->id(); my $sql=("SELECT paths.name, files.name FROM files\n" . @@ -662,8 +674,7 @@ sub filename "GROUP BY paths.name, files.name"); print "FILENAME SQL: $sql\n" if($self->{verbose}); my ($path, $name)=$self->{db}->cmd_onerow($sql, $id); - my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}}); - return($self->{db}->relativise($path, $name, $mountpoint)); + return($self->{db}->relativise($path, $name, $mountpoint, $self->{path})); } # should never happen return "ERROR"; @@ -673,16 +684,9 @@ sub tags_subselect { my($self)=@_; my $hasvals=$self->expecting_values(); - if($self->{in_all}) - { - return "\tSELECT id FROM files AS files_id\n"; - } my $tree=$self->{tagtree}; - print "UNDEF!!\n" unless($self->{tagtree}); my $parent=$self->trailing_tag_parent(); -# print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n"; -# print "TREE: ", $tree->print(), "\n"; my $tag=undef; if($hasvals) { @@ -691,7 +695,9 @@ sub tags_subselect } my ($sqlclause, @joins)=(undef, ()); ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree); -# print "SQL(" . scalar(@joins) .": $sqlclause\n"; +# use Data::Dumper; +# print Dumper $tree if($tree); +# print "SQL(" . scalar(@joins) ."): $sqlclause\n"; my $sql="\tSELECT fxt1.files_id FROM tags t1"; my @crosses=(); my @inners=(); @@ -738,64 +744,18 @@ sub sql_start return $sql; } - -sub constraints_tag_list -{ - my($self, @constraints)=@_; - my $lasttag=undef; - my @tags=(); - my @tags_vals=(); - for my $constraint (@constraints) - { -# print ref($constraint), ": ", $constraint->{name}, "\n"; - if($self->is("tag", $constraint)) - { - if(defined($lasttag)) - { -# print "TAGVAL\n"; - push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id()); - $lasttag=undef; - } - elsif($self->tag_has_values($constraint->id())) - { -# print "HASVALUES\n"; - $lasttag=$constraint->id() if defined($constraint->id()); - } - else - { -# print "NOVALUES\n"; - push(@tags, $constraint->id()) if(defined($constraint->id())); - } - } - } - @tags=map{ "\"$_\""; } @tags; - @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); - $lasttag="\"$lasttag\"" if defined($lasttag); - return(\@tags, \@tags_vals, $lasttag); -} - # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM # filtering tags properly requires up to four levels of recursion # (tag/tagval/AND/NOT) and is too slow sub filter { my($self, @dirs)=@_; - # FIXME: disabled - too slow - return @dirs; + return(@dirs) unless($ENABLE_FILTER); my $base=$self->{path}; my @outdirs=(); for my $dir (@dirs) { -# print "\nFILTER (",$self->state(), "): $base / $dir\n"; - if($self->empty("$base/$dir")) - { -# print "empty: $base / $dir\n"; - } - else - { -# print "non-empty, accepting: $base / $dir\n"; - push(@outdirs, $dir); - } + push(@outdirs, $dir) unless($self->empty("$base/$dir")); } return(@outdirs); } @@ -811,4 +771,20 @@ sub empty return 1; } +# if path is .../OR/ or .../OR/NOT +sub in_or +{ + my($self)=@_; + my $tail=$self->tail(); + return 0 unless($tail); + return 0 unless($tail->type() == $TYPE_BOOL); + return 1 if($tail->name() eq "OR"); + return 0 unless($tail->name() eq "NOT"); + my $parent=$self->tail_parent(); + return 0 unless($parent); + return 0 unless($parent->type() == $TYPE_BOOL); + return 1 if($parent->name() eq "OR"); + return 0; +} + 1;