xapian-core  2.1.0
honey_spellingwordslist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2005,2006,2007,2008,2009,2017,2018,2024,2026 Olly Betts
5  * Copyright (C) 2007 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
25 
26 #include "xapian/error.h"
27 #include "xapian/types.h"
28 
29 #include "clamp_cast.h"
30 #include "debuglog.h"
31 #include "honey_database.h"
32 #include "pack.h"
33 #include "stringutils.h"
34 
35 using namespace std;
36 
38 {
39  LOGCALL_DTOR(DB, "HoneySpellingWordsList");
40  delete cursor;
41 }
42 
45 {
46  // This is an over-estimate, but we only use this value to build a balanced
47  // or-tree, and it'll do a decent enough job for that.
48  auto entry_count = database->spelling_table.get_approx_entry_count();
49  return clamp_cast<Xapian::termcount>(entry_count);
50 }
51 
54 {
55  LOGCALL(DB, Xapian::doccount, "HoneySpellingWordsList::get_termfreq", NO_ARGS);
56  Assert(cursor);
57  Assert(!cursor->after_end());
58  Assert(!cursor->current_key.empty());
59  AssertRel(static_cast<unsigned char>(cursor->current_key[0]), >=,
61  cursor->read_tag();
62 
63  Xapian::termcount freq;
64  const char* p = cursor->current_tag.data();
65  if (!unpack_uint_last(&p, p + cursor->current_tag.size(), &freq)) {
66  throw Xapian::DatabaseCorruptError("Bad spelling word freq");
67  }
68  RETURN(freq);
69 }
70 
71 TermList*
73 {
74  LOGCALL(DB, TermList*, "HoneySpellingWordsList::next", NO_ARGS);
75  Assert(cursor);
76 
77  if (cursor->after_end()) {
78  // This is the first action on a new HoneySpellingWordsList.
79  (void)cursor->find_entry_ge(string(1, char(Honey::KEY_PREFIX_WORD)));
80  } else {
81  cursor->next();
82  }
83  if (cursor->after_end()) {
84  // We've reached the end of the prefixed terms.
85  RETURN(this);
86  }
87  const string& key = cursor->current_key;
88  unsigned char first = key[0];
90  if (first > Honey::KEY_PREFIX_WORD) {
91  current_term = key;
92  } else {
93  current_term.assign(key, 1);
94  }
95  RETURN(NULL);
96 }
97 
98 TermList*
100 {
101  LOGCALL(DB, TermList*, "HoneySpellingWordsList::skip_to", term);
102  Assert(cursor);
103 
104  if (cursor->find_entry_ge(Honey::make_spelling_wordlist_key(term))) {
105  // Exact match.
106  current_term = term;
107  } else {
108  // The exact term we asked for isn't there, so check if the next term
109  // after it also has a W prefix.
110  if (cursor->after_end()) {
111  // We've reached the end of the prefixed terms.
112  RETURN(this);
113  }
114  const string& key = cursor->current_key;
115  unsigned char first = key[0];
116  AssertRel(first, >=, Honey::KEY_PREFIX_WORD);
117  if (first > Honey::KEY_PREFIX_WORD) {
118  current_term = key;
119  } else {
120  current_term.assign(key, 1);
121  }
122  }
123  RETURN(NULL);
124 }
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.
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.
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:397
Abstract base class for termlists.
Definition: termlist.h:42
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_DTOR(CATEGORY, CLASS)
Definition: debuglog.h:481
Hierarchy of classes which Xapian can throw as exceptions.
Database using honey backend.
A termlist containing all words which are spelling targets.
const unsigned KEY_PREFIX_WORD
std::string make_spelling_wordlist_key(std::string_view word)
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 AssertRel(A, REL, B)
Definition: omassert.h:123
#define Assert(COND)
Definition: omassert.h:122
Pack types into strings and unpack them again.
bool unpack_uint_last(const char **p, const char *end, U *result)
Decode an unsigned integer as the last item in a string.
Definition: pack.h:118
Various handy string-related helpers.
typedefs for Xapian