Split PostList and PostingIterator::Internal
[xapian.git] / xapian-core / api / postingiterator.cc
blob3df8c9cab7816cd50485ef3968298f65d18e1aa7
1 /** @file postingiterator.cc
2 * @brief Class for iterating over a list of document ids.
3 */
4 /* Copyright (C) 2008,2009,2010,2011,2013,2017 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 "api/postingiteratorinternal.h"
26 #include "debuglog.h"
27 #include "omassert.h"
28 #include "postlist.h"
30 using namespace std;
32 namespace Xapian {
34 void
35 PostingIterator::decref()
37 Assert(internal);
38 if (--internal->_refs == 0)
39 delete internal;
42 PostingIterator::PostingIterator(Internal *internal_) : internal(internal_)
44 LOGCALL_CTOR(API, "PostingIterator", internal_);
45 if (!internal) return;
46 try {
47 ++internal->_refs;
48 if (!internal->next()) {
49 decref();
50 internal = NULL;
52 } catch (...) {
53 // The destructor only runs if the constructor completes, so we have to
54 // take care of cleaning up for ourselves here.
55 decref();
56 throw;
60 PostingIterator::PostingIterator(const PostingIterator & o)
61 : internal(o.internal)
63 LOGCALL_CTOR(API, "PostingIterator", o);
64 if (internal)
65 ++internal->_refs;
68 PostingIterator &
69 PostingIterator::operator=(const PostingIterator & o)
71 LOGCALL(API, PostingIterator &, "PostingIterator::operator=", o);
72 if (o.internal)
73 ++o.internal->_refs;
74 if (internal)
75 decref();
76 internal = o.internal;
77 RETURN(*this);
80 Xapian::docid
81 PostingIterator::operator*() const
83 LOGCALL(API, Xapian::docid, "PostingIterator::operator*", NO_ARGS);
84 Assert(internal);
85 RETURN(internal->get_docid());
88 PostingIterator &
89 PostingIterator::operator++()
91 LOGCALL(API, PostingIterator &, "PostingIterator::operator++", NO_ARGS);
92 Assert(internal);
93 if (!internal->next()) {
94 decref();
95 internal = NULL;
97 RETURN(*this);
100 Xapian::termcount
101 PostingIterator::get_wdf() const
103 LOGCALL(API, Xapian::termcount, "PostingIterator::get_wdf", NO_ARGS);
104 Assert(internal);
105 RETURN(internal->get_wdf());
108 Xapian::termcount
109 PostingIterator::get_doclength() const
111 LOGCALL(API, Xapian::termcount, "PostingIterator::get_doclength", NO_ARGS);
112 Assert(internal);
113 RETURN(internal->get_doclength());
116 Xapian::termcount
117 PostingIterator::get_unique_terms() const
119 LOGCALL(API, Xapian::termcount, "PostingIterator::get_unique_terms", NO_ARGS);
120 Assert(internal);
121 RETURN(internal->get_unique_terms());
124 #if 0 // FIXME: TermIterator supports this, so PostingIterator really ought to.
125 Xapian::termcount
126 PostingIterator::positionlist_count() const
128 LOGCALL(API, Xapian::termcount, "PostingIterator::positionlist_count", NO_ARGS);
129 Assert(internal);
130 RETURN(internal->positionlist_count());
132 #endif
134 PositionIterator
135 PostingIterator::positionlist_begin() const
137 LOGCALL(API, PositionIterator, "PostingIterator::positionlist_begin", NO_ARGS);
138 Assert(internal);
139 RETURN(PositionIterator(internal->open_position_list()));
142 void
143 PostingIterator::skip_to(Xapian::docid did)
145 LOGCALL_VOID(API, "PostingIterator::skip_to", did);
146 if (internal) {
147 if (!internal->skip_to(did)) {
148 decref();
149 internal = NULL;
154 std::string
155 PostingIterator::get_description() const
157 string desc = "PostingIterator(";
158 if (internal)
159 desc += internal->get_description();
160 desc += ')';
161 return desc;