xapian-core  2.0.0
termiterator.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2009,2010,2011,2013,2024 Olly Betts
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "xapian/termiterator.h"
24 
25 #include "debuglog.h"
26 #include "omassert.h"
27 #include "termlist.h"
28 
29 using namespace std;
30 
31 namespace Xapian {
32 
33 void
34 TermIterator::decref()
35 {
36  Assert(internal);
37  if (--internal->_refs == 0)
38  delete internal;
39 }
40 
41 void
42 TermIterator::post_advance(Internal * res)
43 {
44  if (res) {
45  if (res == internal) {
46  // No more items.
47  res = NULL;
48  } else {
49  // Prune - this can happen with iterating allterms from multiple
50  // databases.
51  ++res->_refs;
52  }
53  decref();
54  internal = res;
55  }
56 }
57 
58 TermIterator::TermIterator(Internal *internal_) : internal(internal_)
59 {
60  LOGCALL_CTOR(API, "TermIterator", internal_);
61  if (!internal) return;
62  try {
63  ++internal->_refs;
65  } catch (...) {
66  // The destructor only runs if the constructor completes, so we have to
67  // take care of cleaning up for ourselves here.
68  decref();
69  throw;
70  }
71 }
72 
74  : internal(o.internal)
75 {
76  LOGCALL_CTOR(API, "TermIterator", o);
77  if (internal)
78  ++internal->_refs;
79 }
80 
83 {
84  LOGCALL(API, TermIterator &, "TermIterator::operator=", o);
85  if (o.internal)
86  ++o.internal->_refs;
87  if (internal)
88  decref();
89  internal = o.internal;
90  RETURN(*this);
91 }
92 
93 string
95 {
96  LOGCALL(API, string, "TermIterator::operator*", NO_ARGS);
97  Assert(internal);
99 }
100 
101 TermIterator &
103 {
104  LOGCALL(API, TermIterator &, "TermIterator::operator++", NO_ARGS);
105  Assert(internal);
107  RETURN(*this);
108 }
109 
112 {
113  LOGCALL(API, Xapian::termcount, "TermIterator::get_wdf", NO_ARGS);
114  Assert(internal);
115  RETURN(internal->get_wdf());
116 }
117 
120 {
121  LOGCALL(API, Xapian::doccount, "TermIterator::get_termfreq", NO_ARGS);
122  Assert(internal);
124 }
125 
128 {
129  LOGCALL(API, Xapian::termcount, "TermIterator::positionlist_count", NO_ARGS);
130  Assert(internal);
132 }
133 
136 {
137  LOGCALL(API, PositionIterator, "TermIterator::positionlist_begin", NO_ARGS);
138  Assert(internal);
140 }
141 
142 void
144 {
145  LOGCALL_VOID(API, "TermIterator::skip_to", term);
146  if (internal)
148 }
149 
150 std::string
152 {
153 #if 0 // FIXME: Add TermIterator::Internal::get_description() method.
154  string desc = "TermIterator(";
155  if (internal)
156  desc += internal->get_description();
157  desc += ')';
158  return desc;
159 #else
160  return "TermIterator()";
161 #endif
162 }
163 
164 }
unsigned _refs
Reference count.
Definition: intrusive_ptr.h:74
Class for iterating over term positions.
Abstract base class for termlists.
Definition: termlist.h:42
virtual Internal * skip_to(std::string_view term)=0
Skip forward to the specified term.
virtual PositionList * positionlist_begin() const =0
Return PositionList for the current position.
virtual Xapian::doccount get_termfreq() const =0
Return the term frequency for the term at the current position.
virtual Internal * next()=0
Advance the current position to the next term in the termlist.
const std::string & get_termname() const
Return the termname at the current position.
Definition: termlist.h:69
virtual Xapian::termcount positionlist_count() const =0
Return the length of the position list for the current position.
virtual Xapian::termcount get_wdf() const =0
Return the wdf for the term at the current position.
Class for iterating over a list of terms.
Definition: termiterator.h:41
void skip_to(std::string_view term)
Advance the iterator to term term.
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
TermIterator & operator=(const TermIterator &o)
Assignment.
Definition: termiterator.cc:82
Xapian::termcount positionlist_count() const
Return the length of the position list for the current position.
TermIterator & operator++()
Advance the iterator to the next position.
std::string get_description() const
Return a string describing this object.
void post_advance(Internal *res)
Definition: termiterator.cc:42
Xapian::termcount get_wdf() const
Return the wdf for the term at the current position.
std::string operator*() const
Return the term at the current position.
Definition: termiterator.cc:94
TermIterator() noexcept
Default constructor.
Definition: termiterator.h:79
PositionIterator positionlist_begin() const
Return a PositionIterator for the current term.
string term
Debug logging macros.
#define RETURN(...)
Definition: debuglog.h:484
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:478
#define LOGCALL_CTOR(CATEGORY, CLASS, PARAMS)
Definition: debuglog.h:480
#define LOGCALL_VOID(CATEGORY, FUNC, PARAMS)
Definition: debuglog.h:479
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
Various assertion macros.
#define Assert(COND)
Definition: omassert.h:122
Class for iterating over a list of terms.
Abstract base class for termlists.