xapian-core  2.1.0
honey_inverter.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009,2010,2013,2014,2023,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 #ifndef XAPIAN_INCLUDED_HONEY_INVERTER_H
22 #define XAPIAN_INCLUDED_HONEY_INVERTER_H
23 
24 #include "xapian/types.h"
25 
26 #include "api/smallvector.h"
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 #include "negate_unsigned.h"
33 #include "omassert.h"
34 #include "str.h"
35 #include "xapian/error.h"
36 
37 class HoneyPostListTable;
38 class HoneyPositionTable;
39 
40 namespace Xapian {
41 class TermIterator;
42 }
43 
46  friend class HoneyPostListTable;
47 
50 
53  friend class HoneyPostListTable;
54 
60 
66 
68  std::map<Xapian::docid, Xapian::termcount> pl_changes;
69 
70  public:
73  : tf_delta(1), cf_delta(wdf)
74  {
75  pl_changes.insert(std::make_pair(did, wdf));
76  }
77 
82  {
83  pl_changes.insert(std::make_pair(did, DELETED_POSTING));
84  }
85 
88  Xapian::termcount new_wdf)
89  : tf_delta(0),
90  cf_delta(UNSIGNED_OVERFLOW_OK(new_wdf - old_wdf))
91  {
92  pl_changes.insert(std::make_pair(did, new_wdf));
93  }
94 
97  // May overflow past 0.
100  // Add did to term's postlist
101  pl_changes[did] = wdf;
102  }
103 
106  // May overflow past 0.
109  // Remove did from term's postlist.
111  }
112 
115  Xapian::termcount new_wdf) {
116  UNSIGNED_OVERFLOW_OK(cf_delta += new_wdf - old_wdf);
117  pl_changes[did] = new_wdf;
118  }
119 
122 
125  };
126 
128  std::map<std::string, PostingChanges, std::less<>> postlist_changes;
129 
136  mutable int has_positions_cache = -1;
137 
139  std::map<std::string,
140  std::map<Xapian::docid, std::string>,
141  std::less<>> pos_changes;
142 
143  void store_positions(const HoneyPositionTable& position_table,
144  Xapian::docid did,
145  std::string_view term,
146  const Xapian::VecCOW<Xapian::termpos>& posvec,
147  bool modifying);
148 
150  std::string_view term,
151  std::string_view s);
152 
153  public:
155  std::map<Xapian::docid, Xapian::termcount> doclen_changes;
156 
157  public:
158  void add_posting(Xapian::docid did, const std::string& term,
159  Xapian::doccount wdf) {
160  auto i = postlist_changes.find(term);
161  if (i == postlist_changes.end()) {
162  postlist_changes.insert(
163  std::make_pair(term, PostingChanges(did, wdf)));
164  } else {
165  i->second.add_posting(did, wdf);
166  }
167  }
168 
169  void remove_posting(Xapian::docid did, const std::string& term,
170  Xapian::doccount wdf) {
171  auto i = postlist_changes.find(term);
172  if (i == postlist_changes.end()) {
173  postlist_changes.insert(
174  std::make_pair(term, PostingChanges(did, wdf, false)));
175  } else {
176  i->second.remove_posting(did, wdf);
177  }
178  }
179 
180  void update_posting(Xapian::docid did, const std::string& term,
181  Xapian::termcount old_wdf,
182  Xapian::termcount new_wdf) {
183  auto i = postlist_changes.find(term);
184  if (i == postlist_changes.end()) {
185  postlist_changes.insert(
186  std::make_pair(term, PostingChanges(did, old_wdf, new_wdf)));
187  } else {
188  i->second.update_posting(did, old_wdf, new_wdf);
189  }
190  }
191 
192  void set_positionlist(const HoneyPositionTable& position_table,
193  Xapian::docid did,
194  std::string_view term,
195  const Xapian::TermIterator& t,
196  bool modifying = false);
197 
199  std::string_view term);
200 
202  std::string_view term,
203  std::string& s) const;
204 
205  bool has_positions(const HoneyPositionTable& position_table) const;
206 
207  void clear() {
208  doclen_changes.clear();
209  postlist_changes.clear();
210  pos_changes.clear();
211  has_positions_cache = -1;
212  }
213 
215  if (add) {
216  Assert(doclen_changes.find(did) == doclen_changes.end() ||
218  }
219  doclen_changes[did] = doclen;
220  }
221 
223  Assert(doclen_changes.find(did) == doclen_changes.end() ||
226  }
227 
228  bool get_doclength(Xapian::docid did, Xapian::termcount& doclen) const {
229  auto i = doclen_changes.find(did);
230  if (i == doclen_changes.end())
231  return false;
232  if (rare(i->second == DELETED_POSTING))
233  throw Xapian::DocNotFoundError("Document not found: " + str(did));
234  doclen = i->second;
235  return true;
236  }
237 
240 
242  void flush_post_list(HoneyPostListTable& table, std::string_view term);
243 
246 
248  void flush_post_lists(HoneyPostListTable& table, std::string_view pfx);
249 
251  void flush(HoneyPostListTable& table);
252 
254  void flush_pos_lists(HoneyPositionTable& table);
255 
256  bool get_deltas(std::string_view term,
257  Xapian::termcount& tf_delta,
258  Xapian::termcount& cf_delta) const {
259  auto i = postlist_changes.find(term);
260  if (i == postlist_changes.end()) {
261  return false;
262  }
263  tf_delta = i->second.get_tfdelta();
264  cf_delta = i->second.get_cfdelta();
265  return true;
266  }
267 };
268 
269 #endif // XAPIAN_INCLUDED_HONEY_INVERTER_H
Class for storing the changes in frequencies for a term.
void remove_posting(Xapian::docid did, Xapian::termcount wdf)
Remove a posting.
PostingChanges(Xapian::docid did, Xapian::termcount old_wdf, Xapian::termcount new_wdf)
Constructor for an updated posting.
std::map< Xapian::docid, Xapian::termcount > pl_changes
Changes to this term's postlist.
Xapian::termcount cf_delta
Change in collection frequency.
Xapian::termcount tf_delta
Change in term frequency.
PostingChanges(Xapian::docid did, Xapian::termcount wdf, bool)
Constructor for a removed posting.
Xapian::termcount get_cfdelta() const
Get the collection frequency delta.
void update_posting(Xapian::docid did, Xapian::termcount old_wdf, Xapian::termcount new_wdf)
Update a posting.
Xapian::termcount get_tfdelta() const
Get the term frequency delta.
PostingChanges(Xapian::docid did, Xapian::termcount wdf)
Constructor for an added posting.
void add_posting(Xapian::docid did, Xapian::termcount wdf)
Add a posting.
Class which "inverts the file".
void delete_positionlist(Xapian::docid did, std::string_view term)
bool get_doclength(Xapian::docid did, Xapian::termcount &doclen) const
bool get_positionlist(Xapian::docid did, std::string_view term, std::string &s) const
bool get_deltas(std::string_view term, Xapian::termcount &tf_delta, Xapian::termcount &cf_delta) const
void store_positions(const HoneyPositionTable &position_table, Xapian::docid did, std::string_view term, const Xapian::VecCOW< Xapian::termpos > &posvec, bool modifying)
void add_posting(Xapian::docid did, const std::string &term, Xapian::doccount wdf)
bool has_positions(const HoneyPositionTable &position_table) const
void flush_pos_lists(HoneyPositionTable &table)
Flush position changes.
void flush(HoneyPostListTable &table)
Flush all postlist table changes.
static const Xapian::termcount DELETED_POSTING
Magic wdf value used for a deleted posting.
void flush_post_list(HoneyPostListTable &table, std::string_view term)
Flush postlist changes for term.
std::map< std::string, PostingChanges, std::less<> > postlist_changes
Buffered changes to postlists.
void set_positionlist(Xapian::docid did, std::string_view term, std::string_view s)
void set_doclength(Xapian::docid did, Xapian::termcount doclen, bool add)
void flush_all_post_lists(HoneyPostListTable &table)
Flush postlist changes for all terms.
int has_positions_cache
Cached answer to Inverter::has_positions().
void flush_doclengths(HoneyPostListTable &table)
Flush document length changes.
void update_posting(Xapian::docid did, const std::string &term, Xapian::termcount old_wdf, Xapian::termcount new_wdf)
void flush_post_lists(HoneyPostListTable &table, std::string_view pfx)
Flush postlist changes for all terms which start with pfx.
void remove_posting(Xapian::docid did, const std::string &term, Xapian::doccount wdf)
std::map< std::string, std::map< Xapian::docid, std::string >, std::less<> > pos_changes
Buffered changes to positional data.
void delete_doclength(Xapian::docid did)
std::map< Xapian::docid, Xapian::termcount > doclen_changes
Buffered changes to document lengths.
void add(std::string_view key, const char *val, size_t val_size, bool compressed=false)
Definition: honey_table.cc:74
Indicates an attempt to access a document not present in the database.
Definition: error.h:662
Class for iterating over a list of terms.
Definition: termiterator.h:41
Suitable for "simple" type T.
Definition: smallvector.h:62
#define UNSIGNED_OVERFLOW_OK(X)
Definition: config.h:635
#define rare(COND)
Definition: config.h:616
string term
Hierarchy of classes which Xapian can throw as exceptions.
string str(int value)
Convert int to std::string.
Definition: str.cc:91
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
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
Negate unsigned integer, avoiding compiler warnings.
constexpr std::enable_if_t< std::is_unsigned_v< T >, T > negate_unsigned(T value)
Various assertion macros.
#define Assert(COND)
Definition: omassert.h:122
Custom vector implementations using small vector optimisation.
Convert types to std::string.
typedefs for Xapian