| 1 | package OpenInteract2::FullTextIterator; | ||
| 2 | |||
| 3 | # $Id: FullTextIterator.pm,v 1.2 2005/03/18 04:09:48 lachoy Exp $ | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base qw( SPOPS::Iterator ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | use OpenInteract2::Constants qw( :log ); | ||
| 9 | use SPOPS::Iterator qw( ITER_IS_DONE ); | ||
| 10 | |||
| 11 | $OpenInteract2::FullTextIterator::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | my ( $log ); | ||
| 14 | |||
| 15 | sub initialize { | ||
| 16 | my ( $self, $p ) = @_; | ||
| 17 | $log ||= get_logger( LOG_APP ); | ||
| 18 | |||
| 19 | $self->{_FT_RESULTS} = $p->{results}; | ||
| 20 | $self->{_FT_COUNT} = 0; | ||
| 21 | $self->{_FT_RAW_COUNT} = 0; | ||
| 22 | $self->{_FT_OFFSET} = $p->{min} || $p->{offset}; | ||
| 23 | $self->{_FT_MAX} = $p->{max}; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sub fetch_object { | ||
| 28 | my ( $self ) = @_; | ||
| 29 | |||
| 30 | # Get the info for retrieving the object | ||
| 31 | |||
| 32 | my $current_count = $self->{_FT_RAW_COUNT}; | ||
| 33 | |||
| 34 | my ( $class, $id, $fulltext_score, $term_occur ); | ||
| 35 | |||
| 36 | if ( ref $self->{_FT_RESULTS} ) { | ||
| 37 | my $current_rec = $self->{_FT_RESULTS}->[ $current_count ]; | ||
| 38 | $class = $current_rec->[0]; | ||
| 39 | $id = $current_rec->[1]; | ||
| 40 | $fulltext_score = $current_rec->[2]; | ||
| 41 | $term_occur = $current_rec->[3]; | ||
| 42 | } | ||
| 43 | |||
| 44 | # If there's no class/oid, go on to the next | ||
| 45 | |||
| 46 | unless ( $class and $id ) { | ||
| 47 | $self->{_FT_RAW_COUNT}++; | ||
| 48 | return $self->fetch_object; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $obj = eval { | ||
| 52 | $class->fetch( $id, { skip_security => $self->{_SKIP_SECURITY} } ) | ||
| 53 | }; | ||
| 54 | if ( $@ ) { | ||
| 55 | $log->warn( "Caught error fetching [$class: $id]: $@" ); | ||
| 56 | $self->{_FT_RAW_COUNT}++; | ||
| 57 | return $self->fetch_object; | ||
| 58 | } | ||
| 59 | |||
| 60 | unless ( $obj ) { | ||
| 61 | $self->{_FT_RAW_COUNT}++; | ||
| 62 | return $self->fetch_object; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Using min/max and haven't reached it yet | ||
| 66 | |||
| 67 | if ( $self->{_FT_OFFSET} and | ||
| 68 | ( $self->{_FT_COUNT} < $self->{_FT_OFFSET} ) ) { | ||
| 69 | $self->{_FT_COUNT}++; | ||
| 70 | $self->{_FT_RAW_COUNT}++; | ||
| 71 | return $self->fetch_object; | ||
| 72 | } | ||
| 73 | |||
| 74 | if ( $self->{_FT_MAX} and | ||
| 75 | ( $self->{_FT_COUNT} > $self->{_FT_MAX} ) ) { | ||
| 76 | return ITER_IS_DONE; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Ok, we've gone through all the necessary contortions -- we can | ||
| 80 | # actually return the object. | ||
| 81 | |||
| 82 | if ( UNIVERSAL::isa( $obj, 'SPOPS' ) ) { | ||
| 83 | $obj->{tmp_fulltext_score} = $fulltext_score; | ||
| 84 | $obj->{tmp_fulltext_term_count} = $term_occur; | ||
| 85 | } | ||
| 86 | |||
| 87 | $self->{_FT_RAW_COUNT}++; | ||
| 88 | $self->{_FT_COUNT}++; | ||
| 89 | |||
| 90 | return wantarray ? ( $obj, $self->{_FT_COUNT}, $fulltext_score ) : $obj; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||
| 95 | __END__ | ||