#include "CLucene/StdHeader.h" #ifndef NO_PREFIX_QUERY #include "PrefixQuery.h" using namespace lucene::index; namespace lucene{ namespace search{ /** Constructs a query for terms starting with prefix. */ PrefixQuery::PrefixQuery(Term* Prefix): prefix(Prefix->pointer()), query(NULL) /* DSR: So that we can safely 'delete query' in the destructor. */ { //this.reader = reader; //again, why? } PrefixQuery::~PrefixQuery(){ prefix->finalize(); /* DSR:CL_BUG_LEAK: In CLucene 0.8.11, this->query (which is created in ** the getQuery() method) was not deleted, causing a memory leak. */ delete query; } const char_t* PrefixQuery::getQueryName()const{ return _T("PrefixQuery"); } void PrefixQuery::prepare(IndexReader& reader) { query = NULL; this->reader = &reader; } float_t PrefixQuery::sumOfSquaredWeights(Searcher& searcher){ return getQuery()->sumOfSquaredWeights( searcher ); } void PrefixQuery::normalize(const float_t norm) { getQuery()->normalize(norm); } Scorer* PrefixQuery::scorer(IndexReader& reader) { return getQuery()->scorer(reader); } BooleanQuery* PrefixQuery::getQuery() { if (query == NULL) { BooleanQuery* q = new BooleanQuery(); TermEnum& _enum = reader->getTerms(prefix); _TRY { const char_t* prefixText = prefix->Text(); const char_t* prefixField = prefix->Field(); do { /* DSR:CL_BUG_LEAK: Ordered getTerm not to return reference ownership: */ Term* term = _enum.getTerm(false); if (term != NULL && stringFind(term->Text(),prefixText)==term->Text() && stringCompare(term->Field(), prefixField)==0 ) { TermQuery* tq = new TermQuery(*term); // found a match tq->setBoost(boost); // set the boost q->add(*tq,true, false, false); // add to q } else { break; } } while (_enum.next()); }_FINALLY ( _enum.close(); delete &_enum; /* DSR:CL_BUG_LEAK: Was closed, but not deleted. */ ); query = q; } return query; } /** Prints a user-readable version of this query. */ const char_t* PrefixQuery::toString(const char_t* field) { lucene::util::StringBuffer buffer; if ( stringCompare(prefix->Field(),field) != 0 ) { buffer.append(prefix->Field()); buffer.append(_T(":") ); } buffer.append(prefix->Text()); buffer.append(_T("*")); if (boost != 1.0f) { buffer.append(_T("^")); buffer.append( boost,10); } return buffer.ToString(); } }} #endif