| 1 | package OpenInteract2::ResultsManage; | ||
| 2 | |||
| 3 | # $Id: ResultsManage.pm,v 1.14 2005/03/18 04:09:48 lachoy Exp $ | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use base qw( Class::Accessor::Fast ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use File::Basename qw( basename ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use File::Spec::Functions qw( catfile ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Log::Log4perl qw( get_logger ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use OpenInteract2::Constants qw( :log ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use OpenInteract2::Context qw( CTX ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use OpenInteract2::Exception qw( oi_error ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use OpenInteract2::ResultsIterator; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Scalar::Util qw( blessed ); | ||
| 16 | use SPOPS::Utility; | ||
| 17 | |||
| 18 | $OpenInteract2::ResultsManage::VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/); | ||
| 19 | |||
| 20 | my ( $log ); | ||
| 21 | |||
| 22 | use constant FILENAME_WIDTH => 10; | ||
| 23 | |||
| 24 | my $LOCK_EXT = 'lock'; | ||
| 25 | my $RECORD_SEP = ' // '; | ||
| 26 | my $EXTRA_NAME_SEP = ','; | ||
| 27 | |||
| 28 | my @FIELDS = qw( | ||
| 29 | search_id filename keywords | ||
| 30 | min max per_page page_num | ||
| 31 | num_records time date extra_name result_list | ||
| 32 | ); | ||
| 33 | __PACKAGE__->mk_accessors( @FIELDS ); | ||
| 34 | |||
| 35 | |||
| 36 | # NOTE: All internal methods should die() instead of throwing an | ||
| 37 | # exception, since we wrap everything that could fail in an eval at | ||
| 38 | # the user entrance level | ||
| 39 | |||
| 40 | ######################################## | ||
| 41 | # CLASS METHODS | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my ( $pkg, $params ) = @_; | ||
| 45 | my $class = ref $pkg || $pkg; | ||
| 46 | my %data = map { $_ => $params->{ $_ } } @FIELDS; | ||
| 47 | return bless( \%data, $class ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub find_page_boundaries { | ||
| 51 | my ( $class, $page_num, $per_page ) = @_; | ||
| 52 | return ( 0, 0 ) unless ( $page_num and $per_page ); | ||
| 53 | my $max = $page_num * $per_page; | ||
| 54 | my $min = $max - $per_page; | ||
| 55 | return ( $min, $max ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub set_page_boundaries { | ||
| 59 | my ( $self, $page_num, $per_page ) = @_; | ||
| 60 | $self->page_num( $page_num ); | ||
| 61 | $self->per_page( $per_page ); | ||
| 62 | my ( $min, $max ) = $self->find_page_boundaries( $page_num, $per_page ); | ||
| 63 | $self->min( $min ); | ||
| 64 | $self->max( $max ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # Note that $item can be a class or an object -- if object, we'll take | ||
| 68 | # the number of records from it. | ||
| 69 | |||
| 70 | sub find_total_page_count { | ||
| 71 | my ( $item, $per_page, $num_records ) = @_; | ||
| 72 | if ( blessed( $item ) ) { | ||
| 73 | $per_page ||= $item->per_page; | ||
| 74 | $num_records ||= $item->num_records; | ||
| 75 | } | ||
| 76 | return 0 unless ( $per_page and $num_records ); | ||
| 77 | |||
| 78 | my $num_pages = $num_records / $per_page; | ||
| 79 | return ( int $num_pages != $num_pages ) | ||
| 80 | ? int( $num_pages ) + 1 : int $num_pages; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub get_metadata { | ||
| 84 | my ( $item, $search_id ) = @_; | ||
| 85 | $log ||= get_logger( LOG_APP ); | ||
| 86 | |||
| 87 | my $self = ( ref( $item ) ) | ||
| 88 | ? $item : $item->new(); | ||
| 89 | $self->search_id( $search_id ); | ||
| 90 | my $results_filename = $self->_build_results_filename; | ||
| 91 | |||
| 92 | return {} unless ( -f $results_filename ); | ||
| 93 | eval { | ||
| 94 | open( META, $results_filename ) || die "Cannot open '$results_filename': $!" | ||
| 95 | }; | ||
| 96 | if ( $@ ) { | ||
| 97 | $log->error( "Error opening file '$results_filename' to ", | ||
| 98 | "read metadata. $@" ); | ||
| 99 | return {}; | ||
| 100 | } | ||
| 101 | my %meta_info = (); | ||
| 102 | while ( <META> ) { | ||
| 103 | chomp; | ||
| 104 | s/^\s+//; | ||
| 105 | s/\s+$//; | ||
| 106 | last unless ( s/^\#\s*// ); | ||
| 107 | my ( $k, $v ) = split /\s*:\s*/, $_, 2; | ||
| 108 | if ( $k =~ /^extra/ ) { | ||
| 109 | push @{ $meta_info{extra_field} }, $v; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | $meta_info{ $k } = $v; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | close( META ); | ||
| 116 | $meta_info{date} = scalar localtime( $meta_info{time} ); | ||
| 117 | return \%meta_info; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | ######################################## | ||
| 122 | # OBJECT METHODS | ||
| 123 | |||
| 124 | # Clear out all information in an object | ||
| 125 | |||
| 126 | sub clear { | ||
| 127 | my ( $self ) = @_; | ||
| 128 | $self->{ $_ } = undef for ( keys %{ $self } ); | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub assign_metadata { | ||
| 133 | my ( $self, $metadata ) = @_; | ||
| 134 | for my $field ( @FIELDS ) { | ||
| 135 | $self->$field( $metadata->{ $field } ) if ( $metadata->{ $field } ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | ######################################## | ||
| 140 | # SAVE RESULTS | ||
| 141 | |||
| 142 | sub save { | ||
| 143 | my ( $self, $to_save, $p ) = @_; | ||
| 144 | $log ||= get_logger( LOG_APP ); | ||
| 145 | |||
| 146 | if ( ! $to_save | ||
| 147 | || ( ref $to_save eq 'ARRAY' && ! scalar @{ $to_save } ) | ||
| 148 | || ( UNIVERSAL::isa( $to_save, 'SPOPS::Iterator' ) && ! $to_save->has_next ) ) { | ||
| 149 | $log->error( "Bailing out of saving search results -- nothing to save!" ); | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | $log->is_info && | ||
| 154 | $log->info( "Trying to store search results" ); | ||
| 155 | |||
| 156 | my %params = ( extra_name => $p->{extra_name} ); | ||
| 157 | |||
| 158 | # First ensure we have a results directory in the object | ||
| 159 | |||
| 160 | unless ( -d $self->directory ) { | ||
| 161 | oi_error "No configured results directory exists. ", | ||
| 162 | "(Tried: ", $self->directory, "). Please ensure that ", | ||
| 163 | "the server configuration key 'dir.overflow' is defined."; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Generate a search ID and get the filename, then lock the | ||
| 167 | # filename from further use while we're working | ||
| 168 | |||
| 169 | $params{search_id} = $self->generate_search_id; | ||
| 170 | $log->is_debug && | ||
| 171 | $log->debug( "Generated search ID '$params{search_id}'" ); | ||
| 172 | $self->_lock_results( $params{search_id} ); | ||
| 173 | my ( $num_records ); | ||
| 174 | my $out = IO::File->new(); | ||
| 175 | |||
| 176 | # First write out the actual data, then write out the metadata | ||
| 177 | |||
| 178 | eval { | ||
| 179 | my $results_file = $self->_build_results_filename( $params{search_id} ); | ||
| 180 | $self->filename( basename( $results_file ) ); | ||
| 181 | $out->open( "> $results_file" ) | ||
| 182 | || die "Cannot open '$results_file' for writing: $!\n"; | ||
| 183 | $num_records = $self->_persist( $out, $to_save, \%params ); | ||
| 184 | $out->close(); | ||
| 185 | }; | ||
| 186 | |||
| 187 | # If we find an error anywhere along the way, be sure the files | ||
| 188 | # are closed (paranoid, since falling out of scope should do it), | ||
| 189 | # clear the lockfile and die. | ||
| 190 | |||
| 191 | if ( $@ ) { | ||
| 192 | $log->error( "Search result save failure. $@" ); | ||
| 193 | $out->close(); | ||
| 194 | $self->_clear_results( $params{search_id} ); | ||
| 195 | oi_error "Search result save failure: $@"; | ||
| 196 | } | ||
| 197 | |||
| 198 | # Clear out the lockfile | ||
| 199 | |||
| 200 | $self->_unlock_results( $params{search_id} ); | ||
| 201 | |||
| 202 | # Set various information into the object | ||
| 203 | |||
| 204 | $log->is_info && | ||
| 205 | $log->info( "Results ($num_records) saved ok under ID $params{search_id}" ); | ||
| 206 | $self->num_records( $num_records ); | ||
| 207 | $self->search_id( $params{search_id} ); | ||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | sub _persist { | ||
| 213 | my ( $self, $out, $to_save, $params ) = @_; | ||
| 214 | |||
| 215 | my ( $results ); | ||
| 216 | |||
| 217 | # First we have to generate the content to write out | ||
| 218 | |||
| 219 | if ( ref $to_save eq 'ARRAY' ) { | ||
| 220 | $results = $self->_generate_records_from_list( $to_save, $params ); | ||
| 221 | } | ||
| 222 | elsif ( UNIVERSAL::isa( $to_save, 'SPOPS::Iterator' ) ) { | ||
| 223 | $results = $self->_generate_records_from_iterator( $to_save, $params ); | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | die "Item to be saved must either be arrayref or iterator!\n"; | ||
| 227 | } | ||
| 228 | |||
| 229 | my $num_results = scalar( @{ $results } ); | ||
| 230 | # next, start writing the file with the metadata... | ||
| 231 | |||
| 232 | $out->print( | ||
| 233 | join( "\n", | ||
| 234 | "# time: " . time, | ||
| 235 | "# num_records: " . $num_results, | ||
| 236 | "# filename: " . $self->filename, | ||
| 237 | "# directory: " . $self->directory, | ||
| 238 | "# search_id: " . $params->{search_id} ), | ||
| 239 | "\n", | ||
| 240 | ); | ||
| 241 | |||
| 242 | my $extra_name = $params->{extra_name} || []; | ||
| 243 | my $num_extra = 0; | ||
| 244 | foreach my $name ( @{ $extra_name } ) { | ||
| 245 | $num_extra++; | ||
| 246 | $out->print( "# extra $num_extra: ", $name, "\n" ); | ||
| 247 | } | ||
| 248 | |||
| 249 | # ...and then the actual records | ||
| 250 | |||
| 251 | $out->print( join( "\n", @{ $results } ) ); | ||
| 252 | |||
| 253 | return $num_results; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | sub _generate_records_from_list { | ||
| 258 | my ( $self, $record_list ) = @_; | ||
| 259 | |||
| 260 | my @all_records = (); | ||
| 261 | foreach my $item ( @{ $record_list } ) { | ||
| 262 | next unless ( $item ); | ||
| 263 | my @result_info = (); | ||
| 264 | if ( blessed( $item ) ) { | ||
| 265 | push @result_info, ref( $item ), $item->id; | ||
| 266 | } | ||
| 267 | elsif ( ref( $item ) eq 'ARRAY' ) { | ||
| 268 | @result_info = @{ $item }; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | die "Record #", scalar( @all_records ), " is neither an object ", | ||
| 272 | "nor an arrayref. (Record: $item )\n"; | ||
| 273 | } | ||
| 274 | push @all_records, join( $RECORD_SEP, @result_info ); | ||
| 275 | } | ||
| 276 | $log->is_debug && | ||
| 277 | $log->debug( "Generated ", scalar( @all_records ), " records ", | ||
| 278 | "from list" ); | ||
| 279 | return \@all_records; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | sub _generate_records_from_iterator { | ||
| 284 | my ( $self, $iterator ) = @_; | ||
| 285 | my @all_records = (); | ||
| 286 | while ( my $item = $iterator->get_next ) { | ||
| 287 | push @all_records, join( $RECORD_SEP, ref( $item ), scalar( $item->id ) ); | ||
| 288 | } | ||
| 289 | return \@all_records; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | ######################################## | ||
| 295 | # RETRIEVE RESULTS | ||
| 296 | |||
| 297 | sub retrieve { | ||
| 298 | my ( $self, $p ) = @_; | ||
| 299 | $log ||= get_logger( LOG_APP ); | ||
| 300 | |||
| 301 | unless ( $self->search_id ) { | ||
| 302 | oi_error "Cannot retrieve results without a search_id! Please ", | ||
| 303 | "set at object initialization or as a property of the ", | ||
| 304 | "object before running retrieve()."; | ||
| 305 | } | ||
| 306 | |||
| 307 | my $return_type = lc $p->{return_type} || 'list'; | ||
| 308 | |||
| 309 | # 'min' and 'max' can be properties or passed in | ||
| 310 | |||
| 311 | my $min = $p->{min} || $self->min; | ||
| 312 | my $max = $p->{max} || $self->max; | ||
| 313 | $self->min( $min ); | ||
| 314 | $self->max( $max ); | ||
| 315 | |||
| 316 | # Clear out the number of records | ||
| 317 | |||
| 318 | $self->num_records(0); | ||
| 319 | |||
| 320 | $log->is_info && | ||
| 321 | $log->info( "Retrieving raw search results for ", | ||
| 322 | "ID '", $self->search_id, "'" ); | ||
| 323 | |||
| 324 | my $raw_results = $self->_retrieve_raw_results; | ||
| 325 | $log->is_info && | ||
| 326 | $log->info( "Found: ", scalar @{ $raw_results }, " results; asked ", | ||
| 327 | "to return '$return_type'" ); | ||
| 328 | |||
| 329 | return ( $return_type eq 'iterator' ) | ||
| 330 | ? $self->_retrieve_iterator( $raw_results, $p ) | ||
| 331 | : $raw_results; | ||
| 332 | } | ||
| 333 | |||
| 334 | # Retrieve the results and store them in the object, respecting the | ||
| 335 | # min/max set in the object. | ||
| 336 | |||
| 337 | sub _retrieve_raw_results { | ||
| 338 | my ( $self, $p ) = @_; | ||
| 339 | $log ||= get_logger( LOG_APP ); | ||
| 340 | |||
| 341 | unless ( $self->search_id ) { | ||
| 342 | oi_error "No search_id defined in object!"; | ||
| 343 | } | ||
| 344 | |||
| 345 | my $meta_info = $self->get_metadata( $self->search_id ); | ||
| 346 | $self->assign_metadata( $meta_info ); | ||
| 347 | |||
| 348 | $log->is_debug && | ||
| 349 | $log->debug( "[Run on: $meta_info->{date}] ", | ||
| 350 | "[Saved: $meta_info->{num_records}] ", | ||
| 351 | "[Type: $meta_info->{record_class}]" ); | ||
| 352 | if ( $self->num_records <= 0 ) { | ||
| 353 | return [] | ||
| 354 | } | ||
| 355 | |||
| 356 | my $filename = $self->_build_results_filename; | ||
| 357 | eval { | ||
| 358 | open( RESULTS, $filename ) | ||
| 359 | || die "Cannot open '$filename' for reading: $!" | ||
| 360 | }; | ||
| 361 | if ( $@ ) { | ||
| 362 | $log->error( "Search result retrieval failure. $@" ); | ||
| 363 | return []; | ||
| 364 | } | ||
| 365 | |||
| 366 | my @records = (); | ||
| 367 | |||
| 368 | my ( $min, $max ) = ( $self->min, $self->max ); | ||
| 369 | my $count = 1; | ||
| 370 | |||
| 371 | while ( <RESULTS> ) { | ||
| 372 | next if ( /^\#/ ); | ||
| 373 | if ( $min and $count < $min ) { $count++; next; } | ||
| 374 | if ( $max and $count > $max ) { last; } | ||
| 375 | chomp; | ||
| 376 | push @records, [ split /$RECORD_SEP/, $_ ]; | ||
| 377 | $count++; | ||
| 378 | } | ||
| 379 | close( RESULTS ); | ||
| 380 | |||
| 381 | # Reset these since we've already got the data... | ||
| 382 | |||
| 383 | $self->min(0); | ||
| 384 | $self->max(0); | ||
| 385 | |||
| 386 | $self->result_list( \@records ); | ||
| 387 | return $self->result_list; | ||
| 388 | } | ||
| 389 | |||
| 390 | |||
| 391 | # Note that this only works on saved SPOPS objects | ||
| 392 | |||
| 393 | sub _retrieve_iterator { | ||
| 394 | my ( $self, $raw_results, $p ) = @_; | ||
| 395 | return OpenInteract2::ResultsIterator->new({ | ||
| 396 | results => $raw_results, | ||
| 397 | extra_name => $self->extra_name, | ||
| 398 | skip_security => $p->{skip_security} | ||
| 399 | }); | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | sub assign_results_to_object { | ||
| 404 | my ( $self, $result_info ) = @_; | ||
| 405 | for ( @FIELDS ) { | ||
| 406 | $self->$_( $result_info->{ $_ } ); | ||
| 407 | } | ||
| 408 | return $self; | ||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | ####################################### | ||
| 413 | # FILENAME/DIRECTORY METHODS | ||
| 414 | |||
| 415 | sub directory { | ||
| 416 | my ( $self, $directory ) = @_; | ||
| 417 | if ( $directory ) { | ||
| 418 | } | ||
| 419 | unless ( $self->{directory} ) { | ||
| 420 | $self->{directory} = CTX->lookup_directory( 'overflow' ); | ||
| 421 | } | ||
| 422 | return $self->{directory}; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub _build_results_filename { | ||
| 426 | my ( $self, $p_search_id ) = @_; | ||
| 427 | my $search_id = $self->search_id || $p_search_id; | ||
| 428 | unless ( $search_id ) { | ||
| 429 | oi_error "Cannot build a results filename without a ", | ||
| 430 | "search_id as property or parameter!"; | ||
| 431 | } | ||
| 432 | return catfile( $self->directory, $search_id ); | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | sub _build_lock_filename { | ||
| 437 | my ( $self, $p_search_id ) = @_; | ||
| 438 | my $search_id = $self->search_id || $p_search_id; | ||
| 439 | unless ( $search_id ) { | ||
| 440 | oi_error "Cannot build a lock filename without a search_id ", | ||
| 441 | "as property or parameter!"; | ||
| 442 | } | ||
| 443 | return catfile( $self->directory, "$search_id.$LOCK_EXT" ); | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | sub get_all_result_filenames { | ||
| 448 | my ( $self ) = @_; | ||
| 449 | opendir( RESULTS, $self->directory ) | ||
| 450 | || oi_error "Cannot open results directory '", $self->directory, "': $!"; | ||
| 451 | my @results_files = grep ! /\./, grep { length $_ == FILENAME_WIDTH } readdir( RESULTS ); | ||
| 452 | closedir( RESULTS ); | ||
| 453 | return \@results_files; | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | # Clear out the results, including the lockfile. This is called if we | ||
| 458 | # encounter some sort of error in the middle of writing. Don't die | ||
| 459 | # during this method because if this is called we have bigger | ||
| 460 | # problems... | ||
| 461 | |||
| 462 | sub _clear_results { | ||
| 463 | my ( $self, $search_id ) = @_; | ||
| 464 | eval { $self->_unlock_results( $search_id ) }; | ||
| 465 | unlink( $self->_build_results_filename( $search_id ) ); | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | ######################################## | ||
| 470 | # SEARCH ID | ||
| 471 | ######################################## | ||
| 472 | |||
| 473 | # Don't save the 'search_id' parameter into the object yet, since | ||
| 474 | # this method only ensures that we *can* create a file with the | ||
| 475 | # search_id | ||
| 476 | |||
| 477 | sub generate_search_id { | ||
| 478 | my ( $self ) = @_; | ||
| 479 | $log ||= get_logger( LOG_APP ); | ||
| 480 | |||
| 481 | my $results_dir = $self->directory; | ||
| 482 | unless ( -d $results_dir ) { | ||
| 483 | $log->error( "Search results directory '$results_dir'", | ||
| 484 | "is not a directory" ); | ||
| 485 | oi_error "Configuration option for writing search results ", | ||
| 486 | "'$results_dir' is not a directory"; | ||
| 487 | } | ||
| 488 | unless ( -w $results_dir ) { | ||
| 489 | $log->error( "Search results directory '$results_dir' ", | ||
| 490 | "is not writeable" ); | ||
| 491 | oi_error "Configuration option for writing search results ", | ||
| 492 | "'$results_dir' exists but is not writeable"; | ||
| 493 | } | ||
| 494 | my $iterations = 0; | ||
| 495 | my ( $search_id ); | ||
| 496 | FIND_ID: | ||
| 497 | while ( 1 ) { | ||
| 498 | $search_id = SPOPS::Utility->generate_random_code( FILENAME_WIDTH ); | ||
| 499 | my $filename = $self->_build_results_filename( $search_id ); | ||
| 500 | my $lockfile = $self->_build_lock_filename( $search_id ); | ||
| 501 | if ( $iterations > 20 ) { | ||
| 502 | oi_error "Tried 20 times to get a search ID in '$results_dir'; ", | ||
| 503 | "this indicates that either the random number generator ", | ||
| 504 | "is malfunctioning or there are so many files in the ", | ||
| 505 | "directory that we cannot generate a unique ID."; | ||
| 506 | } | ||
| 507 | $iterations++; | ||
| 508 | next if ( -f $filename || -f $lockfile ); | ||
| 509 | $log->is_debug && $log->debug( "Found unused search filename ", | ||
| 510 | "in '$results_dir': '$search_id'" ); | ||
| 511 | last FIND_ID; | ||
| 512 | } | ||
| 513 | return $search_id; | ||
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | ######################################## | ||
| 518 | # LOCKING | ||
| 519 | ######################################## | ||
| 520 | |||
| 521 | # Lock the results file using another file | ||
| 522 | |||
| 523 | sub _lock_results { | ||
| 524 | my ( $self, $search_id ) = @_; | ||
| 525 | my $lock_file = $self->_build_lock_filename( $search_id ); | ||
| 526 | open( LOCK, "> $lock_file" ) | ||
| 527 | || oi_error "Cannot open lockfile '$lock_file' for writing: $!"; | ||
| 528 | print LOCK scalar localtime; | ||
| 529 | close( LOCK ); | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | # Unlock the results file by deleting the lockfile. | ||
| 534 | |||
| 535 | sub _unlock_results { | ||
| 536 | my ( $self, $search_id ) = @_; | ||
| 537 | my $lock_file = $self->_build_lock_filename( $search_id ); | ||
| 538 | return unless ( -f $lock_file ); | ||
| 539 | unlink( $lock_file ) | ||
| 540 | || oi_error "Cannot remove lockfile '$lock_file': $!"; | ||
| 541 | } | ||
| 542 | |||
| 543 | 1; | ||
| 544 | |||
| 545 | __END__ | ||