xapian-core  2.1.0
honey_alltermslist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2007,2008,2009,2010,2017,2018,2024,2026 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (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 "honey_alltermslist.h"
24 #include "honey_cursor.h"
25 #include "honey_postlist.h"
27 
28 #include "clamp_cast.h"
29 #include "debuglog.h"
30 #include "pack.h"
31 #include "stringutils.h"
32 
33 #include "xapian/error.h"
34 
35 using namespace std;
36 
37 void
39 {
40  LOGCALL_VOID(DB, "HoneyAllTermsList::read_termfreq", NO_ARGS);
41  Assert(cursor != NULL);
42 
43  // Unpack the termfreq from the tag.
44  Xapian::termcount collfreq;
45  cursor->read_tag();
46  const char* p = cursor->current_tag.data();
47  const char* pend = p + cursor->current_tag.size();
49  termfreq, collfreq)) {
50  throw Xapian::DatabaseCorruptError("Postlist initial chunk header not "
51  "as expected");
52  }
53  // Not used.
54  (void)collfreq;
55 }
56 
58 {
59  LOGCALL_DTOR(DB, "HoneyAllTermsList");
60  delete cursor;
61 }
62 
65 {
66  // This is an over-estimate and not entirely proportional between shards,
67  // but we only use this value to build a balanced or-tree, and it'll at
68  // least tend to distinguish large databases from small ones.
69  auto entry_count = database->postlist_table.get_approx_entry_count();
70  return clamp_cast<Xapian::termcount>(entry_count);
71 }
72 
75 {
76  LOGCALL(DB, Xapian::doccount, "HoneyAllTermsList::get_termfreq", NO_ARGS);
77  Assert(cursor != NULL);
78  if (termfreq == 0) read_termfreq();
79  RETURN(termfreq);
80 }
81 
82 TermList*
84 {
85  LOGCALL(DB, TermList*, "HoneyAllTermsList::next", NO_ARGS);
86  // Set termfreq to 0 to indicate no termfreq has been read for the current
87  // term.
88  termfreq = 0;
89 
90  if (rare(!cursor)) {
91  Assert(database);
92  cursor = database->postlist_table.cursor_get();
93  Assert(cursor); // The postlist table isn't optional.
94 
95  if (prefix.empty()) {
96  (void)cursor->find_entry_ge(string("\x00\xff", 2));
97  } else {
98  const string& key = pack_honey_postlist_key(prefix);
99  if (cursor->find_entry_ge(key)) {
100  // The exact term we asked for is there, so just copy it rather
101  // than wasting effort unpacking it from the key.
102  current_term = prefix;
103  RETURN(NULL);
104  }
105  }
106  if (cursor->after_end()) {
107  RETURN(this);
108  }
109  goto first_time;
110  }
111 
112  while (true) {
113  if (!cursor->next()) {
114  RETURN(this);
115  }
116 
117 first_time:
118  // Fast check for terms without any zero bytes. ~8.4% faster for
119  // glass.
120  auto nul = cursor->current_key.find('\0');
121  if (nul == string::npos) {
122  current_term = cursor->current_key;
123  break;
124  }
125  if (cursor->current_key[nul + 1] != '\xff') {
126  continue;
127  }
128 
129  const char* p = cursor->current_key.data();
130  const char* pend = p + cursor->current_key.size();
131  if (!unpack_string_preserving_sort(&p, pend, current_term)) {
132  throw Xapian::DatabaseCorruptError("PostList table key has "
133  "unexpected format");
134  }
135 
136  // If this key is for the first chunk of a postlist, we're done.
137  // Otherwise we need to skip past continuation chunks until we find the
138  // first chunk of the next postlist.
139  if (p == pend) break;
140  }
141 
142  if (!startswith(current_term, prefix)) {
143  // We've reached the end of the prefixed terms.
144  RETURN(this);
145  }
146 
147  RETURN(NULL);
148 }
149 
150 TermList*
152 {
153  LOGCALL(DB, TermList*, "HoneyAllTermsList::skip_to", term);
154  // Set termfreq to 0 to indicate no termfreq has been read for the current
155  // term.
156  termfreq = 0;
157 
158  if (rare(!cursor)) {
159  if (rare(term.empty())) {
160  RETURN(next());
161  }
162  cursor = database->postlist_table.cursor_get();
163  Assert(cursor); // The postlist table isn't optional.
164  }
165 
166  if (rare(term.empty())) {
167  RETURN(NULL);
168  }
169 
170  string key = pack_honey_postlist_key(term);
171  if (cursor->find_entry_ge(key)) {
172  // The exact term we asked for is there, so just copy it rather than
173  // wasting effort unpacking it from the key.
174  current_term = term;
175  } else {
176  if (cursor->after_end()) {
177  RETURN(this);
178  }
179 
180  const char* p = cursor->current_key.data();
181  const char* pend = p + cursor->current_key.size();
182  if (!unpack_string_preserving_sort(&p, pend, current_term) ||
183  p != pend) {
184  throw Xapian::DatabaseCorruptError("PostList table key has "
185  "unexpected format");
186  }
187  }
188 
189  if (!startswith(current_term, prefix)) {
190  // We've reached the end of the prefixed terms.
191  RETURN(this);
192  }
193 
194  RETURN(NULL);
195 }
Cast a value to a type, clamping out of range values.
Xapian::doccount get_termfreq() const
Returns the term frequency of the current term.
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
TermList * next()
Advance to the next term in the list.
void read_termfreq() const
Read and cache the term frequency.
TermList * skip_to(std::string_view term)
Advance to the first term which is >= term.
~HoneyAllTermsList()
Destructor.
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:397
Abstract base class for termlists.
Definition: termlist.h:42
#define rare(COND)
Definition: config.h:616
string term
PositionList * p
Debug logging macros.
#define RETURN(...)
Definition: debuglog.h:484
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:478
#define LOGCALL_VOID(CATEGORY, FUNC, PARAMS)
Definition: debuglog.h:479
#define LOGCALL_DTOR(CATEGORY, CLASS)
Definition: debuglog.h:481
Hierarchy of classes which Xapian can throw as exceptions.
A termlist containing all terms in a honey database.
HoneyCursor class.
PostList in a honey database.
Encoding and decoding functions for honey postlists.
bool decode_initial_chunk_header_freqs(const char **p, const char *end, Xapian::doccount &termfreq, Xapian::termcount &collfreq)
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
#define Assert(COND)
Definition: omassert.h:122
Pack types into strings and unpack them again.
std::string pack_honey_postlist_key(std::string_view term)
Definition: pack.h:602
bool unpack_string_preserving_sort(const char **p, const char *end, std::string &result)
Decode a "sort preserved" std::string from a string.
Definition: pack.h:551
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)
Definition: stringutils.h:56