xapian-core  2.1.0
glass_alltermslist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2007,2008,2009,2010,2017,2024 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 "glass_alltermslist.h"
24 #include "glass_postlist.h"
25 
26 #include "clamp_cast.h"
27 #include "debuglog.h"
28 #include "pack.h"
29 #include "stringutils.h"
30 
31 #include <string_view>
32 
33 using namespace std;
34 
35 void
37 {
38  LOGCALL_VOID(DB, "GlassAllTermsList::read_termfreq", NO_ARGS);
39  Assert(!current_term.empty());
40  Assert(cursor);
41  Assert(!cursor->after_end());
42 
43  // Unpack the termfreq from the tag.
44  cursor->read_tag();
45  const char *p = cursor->current_tag.data();
46  const char *pend = p + cursor->current_tag.size();
47  GlassPostList::read_freqs(&p, pend, &termfreq, NULL);
48 }
49 
51 {
52  LOGCALL_DTOR(DB, "GlassAllTermsList");
53  delete cursor;
54 }
55 
58 {
59  // This is an over-estimate and not entirely proportional between shards,
60  // but we only use this value to build a balanced or-tree, and it'll at
61  // least tend to distinguish large databases from small ones.
62  auto entry_count = database->postlist_table.get_entry_count();
63  return clamp_cast<Xapian::termcount>(entry_count);
64 }
65 
68 {
69  LOGCALL(DB, Xapian::doccount, "GlassAllTermsList::get_termfreq", NO_ARGS);
70  Assert(!current_term.empty());
71  Assert(cursor);
72  Assert(!cursor->after_end());
73  if (termfreq == 0) read_termfreq();
74  RETURN(termfreq);
75 }
76 
77 TermList *
79 {
80  LOGCALL(DB, TermList *, "GlassAllTermsList::next", NO_ARGS);
81  // Set termfreq to 0 to indicate no termfreq has been read for the current
82  // term.
83  termfreq = 0;
84 
85  if (rare(!cursor)) {
86  cursor = database->postlist_table.cursor_get();
87  Assert(cursor); // The postlist table isn't optional.
88 
89  if (prefix.empty()) {
90  (void)cursor->find_entry_ge(string("\x00\xff", 2));
91  } else {
92  const string & key = pack_glass_postlist_key(prefix);
93  if (cursor->find_entry_ge(key)) {
94  // The exact term we asked for is there, so just copy it rather
95  // than wasting effort unpacking it from the key.
96  current_term = prefix;
97  RETURN(NULL);
98  }
99  }
100  if (cursor->after_end()) {
101  RETURN(this);
102  }
103  goto first_time;
104  }
105 
106  Assert(!cursor->after_end());
107  while (true) {
108  if (!cursor->next()) {
109  RETURN(this);
110  }
111 
112 first_time:
113  // Fast check for terms without any zero bytes. ~8.4% faster.
114  auto nul = cursor->current_key.find('\0');
115  if (nul == string::npos) {
116  current_term = cursor->current_key;
117  break;
118  }
119  if (cursor->current_key[nul + 1] != '\xff') {
120  continue;
121  }
122 
123  const char *p = cursor->current_key.data();
124  const char *pend = p + cursor->current_key.size();
125  if (!unpack_string_preserving_sort(&p, pend, current_term)) {
126  throw Xapian::DatabaseCorruptError("PostList table key has unexpected format");
127  }
128 
129  // If this key is for the first chunk of a postlist, we're done.
130  // Otherwise we need to skip past continuation chunks until we find the
131  // first chunk of the next postlist.
132  if (p == pend) break;
133  }
134 
135  if (!startswith(current_term, prefix)) {
136  // We've reached the end of the prefixed terms.
137  RETURN(this);
138  }
139 
140  RETURN(NULL);
141 }
142 
143 TermList*
145 {
146  LOGCALL(DB, TermList *, "GlassAllTermsList::skip_to", term);
147  // Set termfreq to 0 to indicate no termfreq has been read for the current
148  // term.
149  termfreq = 0;
150 
151  if (rare(!cursor)) {
152  cursor = database->postlist_table.cursor_get();
153  Assert(cursor); // The postlist table isn't optional.
154  }
155  Assert(!cursor->after_end());
156 
157  string key = pack_glass_postlist_key(term);
158  if (cursor->find_entry_ge(key)) {
159  // The exact term we asked for is there, so just copy it rather than
160  // wasting effort unpacking it from the key.
161  current_term = term;
162  } else {
163  if (cursor->after_end()) {
164  RETURN(this);
165  }
166 
167  const char *p = cursor->current_key.data();
168  const char *pend = p + cursor->current_key.size();
169  if (!unpack_string_preserving_sort(&p, pend, current_term)) {
170  throw Xapian::DatabaseCorruptError("PostList table key has unexpected format");
171  }
172  }
173 
174  if (!startswith(current_term, prefix)) {
175  // We've reached the end of the prefixed terms.
176  RETURN(this);
177  }
178 
179  RETURN(NULL);
180 }
Cast a value to a type, clamping out of range values.
Xapian::doccount get_termfreq() const
Returns the term frequency of the current term.
TermList * next()
Advance to the next term in the list.
void read_termfreq() const
Read and cache the term frequency.
~GlassAllTermsList()
Destructor.
TermList * skip_to(std::string_view term)
Advance to the first term which is >= term.
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
static void read_freqs(const char **posptr, const char *end, Xapian::doccount *number_of_entries_ptr, Xapian::termcount *collection_freq_ptr)
Read the term frequency and collection frequency.
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
A termlist containing all terms in a glass database.
Postlists in glass databases.
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.
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
std::string pack_glass_postlist_key(std::string_view term)
Definition: pack.h:574
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)
Definition: stringutils.h:56