Document xapian-compact --blocksize takes an argument
[xapian.git] / xapian-core / api / postingiterator.cc
blobe44fa94284501fb4b12a3874a822cf1d882b961d
1 /** @file postingiterator.cc
2 * @brief Class for iterating over a list of document ids.
3 */
4 /* Copyright (C) 2008,2009,2010,2011,2013 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include "xapian/postingiterator.h"
25 #include "debuglog.h"
26 #include "omassert.h"
27 #include "postlist.h"
29 using namespace std;
31 namespace Xapian {
33 void
34 PostingIterator::decref()
36 Assert(internal);
37 if (--internal->_refs == 0)
38 delete internal;
41 void
42 PostingIterator::post_advance(Internal * res)
44 if (res) {
45 // FIXME: It seems this can't happen for any PostList which we wrap
46 // with PostingIterator.
47 ++res->_refs;
48 decref();
49 internal = res;
51 if (internal->at_end()) {
52 decref();
53 internal = NULL;
57 PostingIterator::PostingIterator(Internal *internal_) : internal(internal_)
59 LOGCALL_CTOR(API, "PostingIterator", internal_);
60 if (!internal) return;
61 try {
62 ++internal->_refs;
63 post_advance(internal->next());
64 } catch (...) {
65 // The destructor only runs if the constructor completes, so we have to
66 // take care of cleaning up for ourselves here.
67 decref();
68 throw;
72 PostingIterator::PostingIterator(const PostingIterator & o)
73 : internal(o.internal)
75 LOGCALL_CTOR(API, "PostingIterator", o);
76 if (internal)
77 ++internal->_refs;
80 PostingIterator &
81 PostingIterator::operator=(const PostingIterator & o)
83 LOGCALL(API, PostingIterator &, "PostingIterator::operator=", o);
84 if (o.internal)
85 ++o.internal->_refs;
86 if (internal)
87 decref();
88 internal = o.internal;
89 RETURN(*this);
92 Xapian::docid
93 PostingIterator::operator*() const
95 LOGCALL(API, Xapian::docid, "PostingIterator::operator*", NO_ARGS);
96 Assert(internal);
97 RETURN(internal->get_docid());
100 PostingIterator &
101 PostingIterator::operator++()
103 LOGCALL(API, PostingIterator &, "PostingIterator::operator++", NO_ARGS);
104 Assert(internal);
105 post_advance(internal->next());
106 RETURN(*this);
109 Xapian::termcount
110 PostingIterator::get_wdf() const
112 LOGCALL(API, Xapian::termcount, "PostingIterator::get_wdf", NO_ARGS);
113 Assert(internal);
114 RETURN(internal->get_wdf());
117 Xapian::termcount
118 PostingIterator::get_doclength() const
120 LOGCALL(API, Xapian::termcount, "PostingIterator::get_doclength", NO_ARGS);
121 Assert(internal);
122 RETURN(internal->get_doclength());
125 Xapian::termcount
126 PostingIterator::get_unique_terms() const
128 LOGCALL(API, Xapian::termcount, "PostingIterator::get_unique_terms", NO_ARGS);
129 Assert(internal);
130 RETURN(internal->get_unique_terms());
133 #if 0 // FIXME: TermIterator supports this, so PostingIterator really ought to.
134 Xapian::termcount
135 PostingIterator::positionlist_count() const
137 LOGCALL(API, Xapian::termcount, "PostingIterator::positionlist_count", NO_ARGS);
138 Assert(internal);
139 RETURN(internal->positionlist_count());
141 #endif
143 PositionIterator
144 PostingIterator::positionlist_begin() const
146 LOGCALL(API, PositionIterator, "PostingIterator::positionlist_begin", NO_ARGS);
147 Assert(internal);
148 RETURN(PositionIterator(internal->open_position_list()));
151 void
152 PostingIterator::skip_to(Xapian::docid did)
154 LOGCALL_VOID(API, "PostingIterator::skip_to", did);
155 if (internal)
156 post_advance(internal->skip_to(did));
159 std::string
160 PostingIterator::get_description() const
162 string desc = "PostingIterator(";
163 if (internal)
164 desc += internal->get_description();
165 desc += ')';
166 return desc;