X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FMojoFacets%2FData.pm;h=e69c16e5fbac008e77afa35f88b6fbe7a2a862c5;hb=2c8d9972340cb72f8eb5c0eb1385eb32ae657144;hp=a5ae43f7b795da05c3e170f3360373067a2cb58a;hpb=2e343e39c123fbcc6cb21c944c962d587a11c63f;p=MojoFacets.git diff --git a/lib/MojoFacets/Data.pm b/lib/MojoFacets/Data.pm index a5ae43f..e69c16e 100644 --- a/lib/MojoFacets/Data.pm +++ b/lib/MojoFacets/Data.pm @@ -47,11 +47,15 @@ sub index { sub _load_path { my ( $self, $path ) = @_; - return if defined $loaded->{$path}->{data}; - my $full_path = $self->app->home->rel_file( 'data/' . $path ); die "$full_path $!" unless -r $full_path; + if ( defined $loaded->{$path}->{data} ) { + my $mtime = (stat($full_path))[9]; + return if $loaded->{$path}->{mtime} == $mtime; + warn "reload $full_path, modified ", time() - $mtime, " seconds ago\n"; + } + # we could use Mojo::JSON here, but it's too slow # $data = from_json read_file $path; my $data = read_file $full_path; @@ -91,7 +95,12 @@ sub _load_path { } } my $item; - $item->{ $header[$_] || "f_$_" } = [ $v[$_] ] foreach ( 0 .. $#v ); + foreach my $i ( 0 .. $#v ) { + my $v = $v[$i]; + # unpack numeric values separated by commas + my $a = $v =~ m/\d+\s*,\s*\d+/ ? [ split(/\,\s*/,$v) ] : [ $v ]; + $item->{ $header[$i] || "f_$i" } = $a; + } push @{ $data->{items} }, $item; } } else { @@ -149,6 +158,7 @@ sub _load_path { stats => $stats, full_path => $full_path, size => -s $full_path, + mtime => (stat($full_path))[9], data => $data, }; @@ -598,19 +608,23 @@ sub facet { my $numeric = $self->_is_numeric($name); my $sort = $self->param('sort'); - $sort ||= $numeric ? 'a' : 'c'; + # sort numeric facets with more than 5 values ascending + $sort ||= $numeric && $#facet_names > 4 ? 'a' : 'c'; @facet_names = sort { - if ( $sort =~ m/a/i ) { - $numeric ? $a <=> $b : lc $a cmp lc $b; - } elsif ( $sort =~ m/d/i ) { - $numeric ? $b <=> $a : lc $b cmp lc $a; - } elsif ( $sort =~ m/c/i ) { - ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 ) + my $result; + if ( $sort eq 'a' ) { + $result = $numeric ? $a <=> $b : lc $a cmp lc $b; + } elsif ( $sort eq 'd' ) { + $result = $numeric ? $b <=> $a : lc $b cmp lc $a; + } elsif ( $sort eq 'c' ) { + $result = ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 ) } else { warn "unknown sort: $sort"; - $a cmp $b; + $result = $a cmp $b; } + $result = $a cmp $b unless defined $result; # FIXME cludge for numeric facets with invalid data + $result; } @facet_names; $self->render( name => $name, facet => $facet, checked => $checked,