File: multi_alltermslist.cc

package info (click to toggle)
xapian-core 1.4.3-2%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,412 kB
  • sloc: cpp: 113,868; ansic: 8,723; sh: 4,433; perl: 836; makefile: 566; tcl: 317; python: 40
file content (182 lines) | stat: -rw-r--r-- 4,944 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/** @file multi_alltermslist.cc
 * @brief Class for merging AllTermsList objects from subdatabases.
 */
/* Copyright (C) 2007,2008,2009,2011 Olly Betts
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>

#include "multi_alltermslist.h"

#include <xapian/error.h>

#include "omassert.h"

#include <algorithm>

using namespace std;
using Xapian::Internal::intrusive_ptr;

/// Comparison functor which orders TermList* by ascending term name.
struct CompareTermListsByTerm {
    /// Order by ascending term name.
    bool operator()(const TermList *a, const TermList *b) const {
	return a->get_termname() > b->get_termname();
    }
};

template<class CLASS> struct delete_ptr {
    void operator()(CLASS *p) const { delete p; }
};

MultiAllTermsList::MultiAllTermsList(const vector<intrusive_ptr<Xapian::Database::Internal> > & dbs,
				     const string & prefix)
{
    // The 0 and 1 cases should be handled by our caller.
    AssertRel(dbs.size(), >=, 2);
    termlists.reserve(dbs.size());
    try {
	vector<intrusive_ptr<Xapian::Database::Internal> >::const_iterator i;
	for (i = dbs.begin(); i != dbs.end(); ++i) {
	    termlists.push_back((*i)->open_allterms(prefix));
	}
    } catch (...) {
	for_each(termlists.begin(), termlists.end(), delete_ptr<TermList>());
	throw;
    }
}

MultiAllTermsList::~MultiAllTermsList()
{
    for_each(termlists.begin(), termlists.end(), delete_ptr<TermList>());
}

string
MultiAllTermsList::get_termname() const
{
    return current_term;
}

Xapian::doccount
MultiAllTermsList::get_termfreq() const
{
    if (termlists.empty()) return 0;
    vector<TermList *>::const_iterator i = termlists.begin();
    Xapian::doccount total_tf = (*i)->get_termfreq();
    while (++i != termlists.end()) {
	if ((*i)->get_termname() == current_term)
	    total_tf += (*i)->get_termfreq();
    }
    return total_tf;
}

Xapian::termcount
MultiAllTermsList::get_collection_freq() const
{
    if (termlists.empty()) return 0;
    vector<TermList *>::const_iterator i = termlists.begin();
    Xapian::termcount total_cf = (*i)->get_collection_freq();
    while (++i != termlists.end()) {
	if ((*i)->get_termname() == current_term)
	    total_cf += (*i)->get_collection_freq();
    }
    return total_cf;
}

TermList *
MultiAllTermsList::next()
{
    if (current_term.empty()) {
	// Make termlists into a heap so that the one (or one of the ones) with
	// earliest sorting term is at the top of the heap.
	vector<TermList*>::iterator i = termlists.begin();
	while (i != termlists.end()) {
	    (*i)->next();
	    if ((*i)->at_end()) {
		delete *i;
		i = termlists.erase(i);
	    } else {
		++i;
	    }
	}
	make_heap(termlists.begin(), termlists.end(),
		  CompareTermListsByTerm());
    } else {
	// Advance to the next termname.
	do {
	    TermList * tl = termlists.front();
	    pop_heap(termlists.begin(), termlists.end(),
		     CompareTermListsByTerm());
	    tl->next();
	    if (tl->at_end()) {
		delete tl;
		termlists.pop_back();
	    } else {
		termlists.back() = tl;
		push_heap(termlists.begin(), termlists.end(),
			  CompareTermListsByTerm());
	    }
	} while (!termlists.empty() &&
		 termlists.front()->get_termname() == current_term);
    }

    if (termlists.size() <= 1) {
	if (termlists.empty()) return NULL;
	TermList * tl = termlists[0];
	termlists.clear();
	return tl;
    }

    current_term = termlists.front()->get_termname();
    return NULL;
}

TermList *
MultiAllTermsList::skip_to(const std::string &term)
{
    // Assume the skip is likely to be a long distance, and rebuild the heap
    // from scratch.  FIXME: It would be useful to profile this against an
    // approach more like that next() uses if this ever gets heavy use.
    vector<TermList*>::iterator i = termlists.begin();
    while (i != termlists.end()) {
	(*i)->skip_to(term);
	if ((*i)->at_end()) {
	    delete *i;
	    i = termlists.erase(i);
	} else {
	    ++i;
	}
    }

    if (termlists.size() <= 1) {
	if (termlists.empty()) return NULL;
	TermList * tl = termlists[0];
	termlists.clear();
	return tl;
    }

    make_heap(termlists.begin(), termlists.end(), CompareTermListsByTerm());

    current_term = termlists.front()->get_termname();
    return NULL;
}

bool
MultiAllTermsList::at_end() const
{
    return termlists.empty();
}