| 1 | package KinoSearch1::Store::FSInvIndex; | ||||||||||||||||||
| 2 | 40 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 40 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 40 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 40 | ▶ | use base qw( KinoSearch1::Store::InvIndex ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 40 | our $LOCK_DIR; # used by FSLock | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 40 | ▶ | use File::Spec::Functions qw( canonpath catfile catdir tmpdir no_upwards ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 40 | ▶ | use Fcntl; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 40 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 13 | 40 | __PACKAGE__->init_instance_vars(); | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | # confirm or create a directory to put lockfiles in | ||||||||||||||||||
| 16 | 40 | $LOCK_DIR = catdir( tmpdir, 'kinosearch_lockdir' ); | |||||||||||||||||
| 17 | 40 | ▶ | if ( !-d $LOCK_DIR ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 18 | 1 | ▶ | mkdir $LOCK_DIR or die "couldn't mkdir '$LOCK_DIR': $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | 1 | chmod 0777, $LOCK_DIR; | |||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | } | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 40 | ▶ | use Digest::MD5 qw( md5_hex ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 24 | 40 | ▶ | use KinoSearch1::Store::InStream; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | 40 | ▶ | use KinoSearch1::Store::OutStream; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 26 | 40 | ▶ | use KinoSearch1::Store::FSLock; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 27 | 40 | ▶ | use KinoSearch1::Index::IndexFileNames; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | 24 | ▶ | sub init_instance { | ||||||||||||||||
|
Subroutine
init_instance: called CC=19 SCAR=32.8
Pod
documented
| |||||||||||||||||||
| 30 | 24 | my $self = shift; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | # clean up path. | ||||||||||||||||||
| 33 | 24 | my $path = $self->{path} = canonpath( $self->{path} ); | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 24 | ▶ | if ( $self->{create} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | # clear out lockfiles related to this path | ||||||||||||||||||
| 37 | 4 | my $lock_prefix = $self->get_lock_prefix; | |||||||||||||||||
| 38 | 4 | ▶ | opendir( my $lock_dh, $LOCK_DIR ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 39 | or confess("Couldn't opendir '$LOCK_DIR': $!"); | ||||||||||||||||||
| 40 | 4 | my @lockfiles = grep {/$lock_prefix/} readdir $lock_dh; | |||||||||||||||||
| 41 | 4 | ▶ | closedir $lock_dh | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | or confess("Couldn't closedir '$LOCK_DIR': $!"); | ||||||||||||||||||
| 43 | 4 | for (@lockfiles) { | |||||||||||||||||
| 44 | 0 | $_ = catfile( $LOCK_DIR, $_ ); | |||||||||||||||||
| 45 | 0 | ▶ | unlink $_ or confess("couldn't unlink '$_': $!"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | # blast any existing index files | ||||||||||||||||||
| 49 | 4 | ▶ | if ( -e $path ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | 2 | ▶ | opendir( my $invindex_dh, $path ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 51 | or confess("Couldn't opendir '$path': $!"); | ||||||||||||||||||
| 52 | 2 | my @to_remove; | |||||||||||||||||
| 53 | 2 | for ( readdir $invindex_dh ) { | |||||||||||||||||
| 54 | 6 | ▶ | if (/(^\w+\.(?:cfs|del)$)/) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 55 | 1 | push @to_remove, $1; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | elsif ( $_ eq 'segments' ) { | ||||||||||||||||||
| 58 | 0 | push @to_remove, 'segments'; | |||||||||||||||||
| 59 | } | ||||||||||||||||||
| 60 | elsif ( $_ eq 'delqueue' ) { | ||||||||||||||||||
| 61 | 0 | push @to_remove, 'delqueue'; | |||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | 2 | for my $removable (@to_remove) { | |||||||||||||||||
| 65 | 1 | $removable = catfile( $path, $removable ); | |||||||||||||||||
| 66 | 1 | ▶ | unlink $removable | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | or confess "Couldn't unlink file '$removable': $!"; | ||||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | 2 | ▶ | closedir $invindex_dh | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | or confess("Couldn't closedir '$path': $!"); | ||||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | 4 | ▶ | if ( !-d $path ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 73 | 2 | ▶ | mkdir $path or confess("Couldn't mkdir '$path': $!"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | # by now, we should have a directory, so throw an error if we don't | ||||||||||||||||||
| 78 | 24 | ▶ | if ( !-d $path ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 1 | ▶ | confess("Can't open invindex location '$path': $! ") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 80 | unless -e $path; | ||||||||||||||||||
| 81 | 0 | confess("invindex location '$path' isn't a directory"); | |||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 182 | ▶ | sub open_outstream { | ||||||||||||||||
|
Subroutine
open_outstream: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||
| 86 | 182 | my ( $self, $filename ) = @_; | |||||||||||||||||
| 87 | 182 | my $filepath = catfile( $self->{path}, $filename ); | |||||||||||||||||
| 88 | 182 | ▶ | sysopen( my $fh, $filepath, O_CREAT | O_RDWR | O_EXCL ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | or confess("Couldn't open file '$filepath': $!"); | ||||||||||||||||||
| 90 | 182 | binmode($fh); | |||||||||||||||||
| 91 | 182 | return KinoSearch1::Store::OutStream->new($fh); | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 215 | ▶ | sub open_instream { | ||||||||||||||||
|
Subroutine
open_instream: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||
| 95 | 215 | my ( $self, $filename, $offset, $len ) = @_; | |||||||||||||||||
| 96 | 215 | my $filepath = catfile( $self->{path}, $filename ); | |||||||||||||||||
| 97 | # must be unbuffered, or PerlIO messes up with the shared handles | ||||||||||||||||||
| 98 | 215 | ▶ | open( my $fh, "<:unix", $filepath ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 99 | or confess("Couldn't open file '$filepath': $!"); | ||||||||||||||||||
| 100 | 215 | binmode($fh); | |||||||||||||||||
| 101 | 215 | return KinoSearch1::Store::InStream->new( $fh, $offset, $len ); | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 2 | ▶ | sub list { | ||||||||||||||||
|
Subroutine
list: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||
| 105 | 2 | my $self = shift; | |||||||||||||||||
| 106 | opendir( my $dh, $self->{path} ) | ||||||||||||||||||
| 107 | 2 | ▶ | or confess("Couldn't opendir '$self->{path}'"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 2 | my @files = no_upwards( readdir $dh ); | |||||||||||||||||
| 109 | 2 | ▶ | closedir $dh or confess("Couldn't closedir '$self->{path}'"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 110 | 2 | return @files; | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 295 | ▶ | sub file_exists { | ||||||||||||||||
|
Subroutine
file_exists: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 114 | 295 | my ( $self, $filename ) = @_; | |||||||||||||||||
| 115 | 295 | return -e catfile( $self->{path}, $filename ); | |||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 35 | ▶ | sub rename_file { | ||||||||||||||||
|
Subroutine
rename_file: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||
| 119 | 35 | my ( $self, $from, $to ) = @_; | |||||||||||||||||
| 120 | 35 | $_ = catfile( $self->{path}, $_ ) for ( $from, $to ); | |||||||||||||||||
| 121 | 35 | ▶ | rename( $from, $to ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | or confess("couldn't rename file '$from' to '$to': $!"); | ||||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | 163 | ▶ | sub delete_file { | ||||||||||||||||
|
Subroutine
delete_file: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||
| 126 | 163 | my ( $self, $filename ) = @_; | |||||||||||||||||
| 127 | 163 | $filename = catfile( $self->{path}, $filename ); | |||||||||||||||||
| 128 | 163 | ▶ | unlink $filename or confess("couldn't unlink file '$filename': $!"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | 1 | ▶ | sub slurp_file { | ||||||||||||||||
|
Subroutine
slurp_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||
| 132 | 1 | my ( $self, $filename ) = @_; | |||||||||||||||||
| 133 | 1 | my $filepath = catfile( $self->{path}, $filename ); | |||||||||||||||||
| 134 | 1 | ▶ | open( my $fh, "<", $filepath ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 135 | or confess("Couldn't open file '$filepath': $!"); | ||||||||||||||||||
| 136 | 1 | binmode($fh); | |||||||||||||||||
| 137 | 1 | local $/; | |||||||||||||||||
| 138 | 1 | return <$fh>; | |||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 69 | ▶ | sub make_lock { | ||||||||||||||||
|
Subroutine
make_lock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 142 | 69 | my $self = shift; | |||||||||||||||||
| 143 | 69 | return KinoSearch1::Store::FSLock->new( @_, invindex => $self ); | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | # Create a hashed string derived from this invindex's path. | ||||||||||||||||||
| 147 | 73 | ▶ | sub get_lock_prefix { | ||||||||||||||||
|
Subroutine
get_lock_prefix: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 148 | 73 | my $self = shift; | |||||||||||||||||
| 149 | 73 | return "kinosearch-" . md5_hex( canonpath( $self->{path} ) ); | |||||||||||||||||
| 150 | } | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | 1 | ▶ | sub close { } | ||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 40 | 1; | |||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||