Ticket #433: matchspy_api_changes_experiment.patch

File matchspy_api_changes_experiment.patch, 4.2 KB (added by Richard Boulton, 14 years ago)

Rough sketch of some of the API ideas I was playing with. Untested - may not compile.

  • matcher/multimatch.cc

     
    190190     *
    191191     *  This implementation calls all the spies in turn.
    192192     */
    193     void operator()(const Xapian::Document &doc, Xapian::weight wt);
     193    double operator()(const Xapian::Document &doc, Xapian::weight wt);
    194194};
    195195
    196 void
     196double
    197197MultipleMatchSpy::operator()(const Xapian::Document &doc, Xapian::weight wt) {
    198198    LOGCALL_VOID(MATCH, "MultipleMatchSpy::operator()", doc << ", " << wt);
    199     vector<Xapian::MatchSpy *>::const_iterator i;
    200     for (i = spies.begin(); i != spies.end(); ++i) {
    201         (**i)(doc, wt);
     199    double min_min_wt = DBL_MAX;
     200    vector<Xapian::MatchSpy *>::const_iterator i = spies.begin()
     201    while (i != spies.end()) {
     202        double min_wt = (**i)(doc, wt);
     203        if (rare(min_wt == DBL_MAX)) {
     204            i = spies.erase(i);
     205            continue;
     206        }
     207        min_min_wt = std::min(min_min_wt, min_wt);
     208        ++i;
    202209    }
     210    return min_min_wt;
    203211}
    204212
    205213////////////////////////////////////
     
    602610                    ++docs_matched;
    603611                    if (!calculated_weight) wt = pl->get_weight();
    604612                    if (matchspy) {
    605                         matchspy->operator()(doc, wt);
     613                        double matchspy_min_wt = matchspy->operator()(doc, wt);
     614                        if (matchspy_min_wt == DBL_MAX) {
     615                            matchspy = NULL;
     616                        }
    606617                    }
    607618                    if (wt > greatest_wt) goto new_greatest_weight;
    608619                    continue;
     
    645656                        new_item.wt = wt;
    646657                        calculated_weight = true;
    647658                    }
    648                     matchspy->operator()(doc, wt);
     659                    double matchspy_min_wt = matchspy->operator()(doc, wt);
     660                    if (matchspy_min_wt == DBL_MAX) {
     661                        matchspy = NULL;
     662                    }
    649663                }
    650664            }
    651665        }
  • include/xapian/matchspy.h

     
    6868     *
    6969     *  @param doc The document seen by the match spy.
    7070     *  @param wt The weight of the document.
     71     *
     72     *  @return The minimum weight for documents which the matchspy is
     73     *  interested in.
    7174     */
    72     virtual void operator()(const Xapian::Document &doc,
    73                             Xapian::weight wt) = 0;
     75    virtual double operator()(const Xapian::Document &doc,
     76                              Xapian::weight wt) = 0;
    7477
    7578    /** Clone the match spy.
    7679     *
     
    188191    /// Total number of documents seen by the match spy.
    189192    Xapian::doccount total;
    190193
     194    /// Maximum number of values to check.
     195    Xapian::doccount check_max;
     196
    191197    /// The values seen so far, together with their frequency.
    192198    std::map<std::string, Xapian::doccount> values;
    193199
    194200  public:
    195201    /// Construct an empty ValueCountMatchSpy.
    196     ValueCountMatchSpy() : slot(Xapian::BAD_VALUENO), total(0) {}
     202    ValueCountMatchSpy() : slot(Xapian::BAD_VALUENO), total(0), check_max(0) {}
    197203
    198     /// Construct a MatchSpy which counts the values in a particular slot.
    199     ValueCountMatchSpy(Xapian::valueno slot_)
    200             : slot(slot_), total(0) {
     204    /** Construct a MatchSpy which counts the values in a particular slot.
     205     *
     206     *  @param slot_ The slot number to check.
     207     *  @param check_max_ The maximum number of values to check in the slot.
     208     *
     209     *  If check_max_ is 0, no limit is places on the number of values to check.
     210     */
     211    ValueCountMatchSpy(Xapian::valueno slot_, check_max_=0)
     212            : slot(slot_), total(0), check_max(check_max_) {
    201213    }
    202214
    203215    /// Return the values seen in the slot.
     
    226238     *
    227239     *  This implementation tallies values for a matching document.
    228240     */
    229     void operator()(const Xapian::Document &doc, Xapian::weight wt);
     241    double operator()(const Xapian::Document &doc, Xapian::weight wt);
    230242
    231243    virtual MatchSpy * clone() const;
    232244    virtual std::string name() const;
  • api/matchspy.cc

     
    156156    }
    157157}
    158158
    159 void
     159double
    160160ValueCountMatchSpy::operator()(const Document &doc, weight) {
    161161    ++total;
    162162    string val(doc.get_value(slot));
    163163    if (!val.empty()) ++values[val];
     164    if (check_max != 0 && rare(total >= check_max)) {
     165        return DBL_MAX;
     166    }
     167    return 0.0;
    164168}
    165169
    166170void