Changeset 4047


Ignore:
Timestamp:
06/06/09 15:44:38 (3 years ago)
Author:
CrawfordCurrie
Message:

Item1691: now works on trunk too

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/QueryAcceleratorPlugin/lib/Foswiki/Store/QueryAlgorithms/DBCache.pm

    r4042 r4047  
    1111 
    1212package Foswiki::Store::QueryAlgorithms::DBCache; 
    13  
    1413use strict; 
     14#@ISA = ( 'Foswiki::Query::QueryAlgorithms' ); # interface 
     15 
    1516use Assert; 
    1617use Error ( ':try' ); 
     
    1920 
    2021# 1 for debug 
    21 sub MONITOR_EVAL { 0 } 
    22  
     22sub MONITOR_EVAL { Foswiki::Query::Node::MONITOR_EVAL() } 
     23 
     24# See Foswiki::Query::QueryAlgorithms in Foswiki 1.1 or later for details 
    2325sub query { 
    24     my ( $query, $web, $topics, $store ) = @_; 
     26    my ( $query, $web, $topics, $store, $options ) = @_; 
    2527 
    2628    my $db = Foswiki::Plugins::QueryAcceleratorPlugin::getDB( $web ); 
    2729    ASSERT($db) if DEBUG; 
    2830 
    29     # Monkey-patch 
    30     my @saved = ( \&Foswiki::Query::Node::evaluate, 
    31                   \&Foswiki::Query::OP_ref::evaluate ); 
    32     no warnings 'redefine'; 
    33     *Foswiki::Query::Node::evaluate = \&_nodeEvaluate; 
    34     *Foswiki::Query::OP_ref::evaluate = \&_op_refEvaluate; 
    35     use warnings 'redefine'; 
     31    my $is10 = (ref($topics) eq 'ARRAY'); # 1.0.x 
     32    my @monkeys; 
     33 
     34    if ($is10) { 
     35        # Monkey-patch 
     36        @monkeys = ( \&Foswiki::Query::Node::evaluate, 
     37                     \&Foswiki::Query::OP_ref::evaluate ); 
     38        no warnings 'redefine'; 
     39        *Foswiki::Query::Node::evaluate = \&_nodeEvaluate; 
     40        *Foswiki::Query::OP_ref::evaluate = \&_op_refEvaluate; 
     41        use warnings 'redefine'; 
     42    } 
    3643 
    3744    my %matches; 
    38     local $/; 
    39     foreach my $topic (@$topics) { 
    40         my $meta = $db->fastget( $topic ); 
    41         next unless $meta; 
    42         print STDERR "TEST $topic\n" if MONITOR_EVAL; 
    43         my $match = $query->evaluate( tom => $meta, data => $meta ); 
    44         if ($match) { 
    45             $matches{$topic} = $match; 
    46         } 
    47     } 
    48  
    49     # Remove the monkey patches 
    50     no warnings 'redefine'; 
    51     *Foswiki::Query::Node::evaluate = $saved[0]; 
    52     *Foswiki::Query::OP_ref::evaluate = $saved[1]; 
    53     use warnings 'redefine'; 
    54  
    55     return \%matches; 
    56 } 
    57  
    58 sub _getField { 
    59     my ( $node, $data, $field ) = @_; 
     45    if ($is10) { 
     46        foreach my $topic (@$topics) { 
     47            my $meta = $db->fastget( $topic ); 
     48            next unless $meta; 
     49            print STDERR "Processing $topic\n" if MONITOR_EVAL; 
     50            my $match = $query->evaluate( tom => $meta, data => $meta ); 
     51            if ($match) { 
     52                $matches{$topic} = $match; 
     53            } 
     54        } 
     55    } else { 
     56        # 1.1 and later 
     57        while ($topics->hasNext()) { 
     58            my $topic = $topics->next(); 
     59            my $meta = $db->fastget( $topic ); 
     60            next unless $meta; 
     61            print STDERR "Processing $topic\n" if MONITOR_EVAL; 
     62            my $match = $query->evaluate( tom => $meta, data => $meta ); 
     63            if ($match) { 
     64                $matches{$topic} = $match; 
     65            } 
     66        } 
     67    } 
     68 
     69    if ($is10) { 
     70        # Remove the monkey patches 
     71        no warnings 'redefine'; 
     72        *Foswiki::Query::Node::evaluate = $monkeys[0]; 
     73        *Foswiki::Query::OP_ref::evaluate = $monkeys[1]; 
     74        use warnings 'redefine'; 
     75 
     76        # 1.0.x and earlier 
     77        return \%matches; 
     78    } else { 
     79        require Foswiki::Search::InfoCache; 
     80 
     81        my @topics = keys(%matches); 
     82        my $resultTopicSet = 
     83          new Foswiki::Search::InfoCache( $Foswiki::Plugins::SESSION, $web, 
     84                                          \@topics ); 
     85        return $resultTopicSet; 
     86    } 
     87} 
     88 
     89# See Foswiki::Query::QueryAlgorithms in Foswiki 1.1 or later for details 
     90sub getField { 
     91    my ( $class, $node, $data, $field ) = @_; 
    6092 
    6193    my $result = undef; 
    6294 
     95    # The query evaluation process can return either DBCacheContrib maps 
     96    # and arrays (when data in the store is matched) and also standard 
     97    # perl arrays and hashes (when data is filtered, for example). To simplify 
     98    # the following code, we map them all to perl objects, using the fact that 
     99    # DBCacheContrib objects are designed to be tied to. 
    63100    if ( UNIVERSAL::isa( 
    64101        $data, 'Foswiki::Contrib::DBCacheContrib::Map' )) { 
     
    88125            for( my $i = 0; $i < scalar(@$data); $i++) { 
    89126                my $f = $data->[$i]; 
    90                 my $val = _getField( $node, $f, $field ); 
     127                my $val = getField( undef, $node, $f, $field ); 
    91128                push( @res, $val ) if defined($val); 
    92129            } 
     
    160197} 
    161198 
     199# See Foswiki::Query::QueryAlgorithms in Foswiki 1.1 or later for details 
     200sub getRefTopic { 
     201    my ($class, $relativeTo, $w, $t) = @_; 
     202    return Foswiki::Plugins::QueryAcceleratorPlugin::getDB( 
     203        $w)->fastget( $t ); 
     204} 
     205 
     206########################################################################### 
     207# The following monkey-patching functions are only required for Foswiki 1.0 
     208# Later versions do not need them as the core functions have been generalised 
     209# to the versions given here 
     210########################################################################### 
     211 
    162212my $ind = 0; 
    163213 
    164 # Override of Foswiki::Query::Node 
     214# This should be identical to Foswiki::Query::Node::evaluate in 
     215# Foswiki 1.1. It is provided here for use in monkey-patching Foswiki 1.0. 
    165216sub _nodeEvaluate { 
    166217    my $node = shift; 
     
    175226            && defined $domain{data} ) 
    176227        { 
    177             # a name; look it up in clientData 
    178             $result = _getField( $node, $domain{data}, $node->{params}[0] ); 
     228            # a name; look it up in $domain{data} 
     229            $result = $Foswiki::cfg{RCS}{QueryAlgorithm}->getField( 
     230                $node, $domain{data}, $node->{params}[0] ); 
    179231        } 
    180232        else { 
     
    194246} 
    195247 
    196 # Override of Foswiki::Query::OP_ref 
     248# This should be identical to Foswiki::Query::OP_ref::evaluate in 
     249# Foswiki 1.1. It is provided here for use in monkey-patching Foswiki 1.0. 
    197250sub _op_refEvaluate { 
    198251    my $this   = shift; 
     
    218271        my $result = undef; 
    219272        try { 
    220             my $submeta = Foswiki::Plugins::QueryAcceleratorPlugin::getDB( 
    221                 $w)->fastget( $t ); 
     273            my $submeta = $Foswiki::cfg{RCS}{QueryAlgorithm}->getRefTopic( 
     274                $domain{tom}, $w, $t ); 
    222275            my $b       = $pnode->{params}[1]; 
    223276            my $res     = $b->evaluate( tom => $submeta, data => $submeta ); 
Note: See TracChangeset for help on using the changeset viewer.