xapian-core  1.4.27
glass_spelling.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2010,2011,2014,2015,2016 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef XAPIAN_INCLUDED_GLASS_SPELLING_H
22 #define XAPIAN_INCLUDED_GLASS_SPELLING_H
23 
24 #include <xapian/types.h>
25 
26 #include "glass_lazytable.h"
27 #include "api/termlist.h"
28 
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <cstring> // For memcpy() and memcmp().
33 
34 namespace Glass {
35 
36 class RootInfo;
37 
38 struct fragment {
39  char data[4];
40 
41  // Default constructor.
42  fragment() { }
43 
44  // Allow implicit conversion.
45  explicit fragment(char data_[4]) { std::memcpy(data, data_, 4); }
46 
47  char & operator[] (unsigned i) { return data[i]; }
48  const char & operator[] (unsigned i) const { return data[i]; }
49 
50  operator std::string() const {
51  return std::string(data, data[0] == 'M' ? 4 : 3);
52  }
53 
54  bool operator<(const fragment &b) const {
55  return std::memcmp(data, b.data, 4) < 0;
56  }
57 };
58 
59 }
60 
61 using Glass::RootInfo;
62 
64  void toggle_word(const std::string & word);
65  void toggle_fragment(Glass::fragment frag, const std::string & word);
66 
67  std::map<std::string, Xapian::termcount> wordfreq_changes;
68 
77  std::map<Glass::fragment, std::set<std::string>> termlist_deltas;
78 
80  Xapian::termcount wordfreq_upper_bound = 0;
81 
82  public:
91  GlassSpellingTable(const std::string & dbdir, bool readonly)
92  : GlassLazyTable("spelling", dbdir + "/spelling.", readonly) { }
93 
94  GlassSpellingTable(int fd, off_t offset_, bool readonly)
95  : GlassLazyTable("spelling", fd, offset_, readonly) { }
96 
101  void merge_changes();
102 
103  void add_word(const std::string & word, Xapian::termcount freqinc);
104  void remove_word(const std::string & word, Xapian::termcount freqdec);
105 
106  TermList * open_termlist(const std::string & word);
107 
108  Xapian::doccount get_word_frequency(const std::string & word) const;
109 
111  wordfreq_upper_bound = ub;
112  }
113 
121  bool is_modified() const {
122  return !wordfreq_changes.empty() || GlassTable::is_modified();
123  }
124 
127  merge_changes();
129  return wordfreq_upper_bound;
130  }
131 
132  void cancel(const RootInfo & root_info, glass_revision_number_t rev) {
133  // Discard batched-up changes.
134  wordfreq_changes.clear();
135  termlist_deltas.clear();
136 
137  GlassTable::cancel(root_info, rev);
138  }
139 
140  // @}
141 };
142 
146  std::string data;
147 
149  unsigned p;
150 
152  std::string current_term;
153 
156 
158  void operator=(const GlassSpellingTermList &);
159 
160  public:
162  explicit GlassSpellingTermList(const std::string & data_)
163  : data(data_), p(0) { }
164 
165  Xapian::termcount get_approx_size() const;
166 
167  std::string get_termname() const;
168 
169  Xapian::termcount get_wdf() const;
170 
171  Xapian::doccount get_termfreq() const;
172 
173  TermList * next();
174 
175  TermList * skip_to(const std::string & term);
176 
177  bool at_end() const;
178 
179  Xapian::termcount positionlist_count() const;
180 
181  Xapian::PositionIterator positionlist_begin() const;
182 };
183 
184 #endif // XAPIAN_INCLUDED_GLASS_SPELLING_H
bool operator<(const fragment &b) const
fragment(char data_[4])
typedefs for Xapian
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:133
uint4 glass_revision_number_t
The revision number of a glass database.
Definition: glass_defs.h:68
Subclass of GlassTable for deriving lazy tables from.
Abstract base class for termlists.
Definition: termlist.h:39
std::string data
The encoded data.
unsigned p
Position in the data.
char & operator[](unsigned i)
std::map< std::string, Xapian::termcount > wordfreq_changes
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
void flush_db()
Flush any outstanding changes to the DB file of the table.
bool is_modified() const
Determine whether the object contains uncommitted modifications.
Definition: glass_table.h:697
std::map< Glass::fragment, std::set< std::string > > termlist_deltas
Changes to make to the termlists.
Xapian::termcount flush_db()
Returns updated wordfreq upper bound.
Class for iterating over term positions.
void set_wordfreq_upper_bound(Xapian::termcount ub)
GlassSpellingTermList(const std::string &data_)
Constructor.
bool is_modified() const
Override methods of GlassTable.
std::string current_term
The current term.
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
Abstract base class for termlists.
GlassSpellingTable(int fd, off_t offset_, bool readonly)
void cancel(const RootInfo &root_info, glass_revision_number_t rev)
Cancel any outstanding changes.
void cancel(const RootInfo &root_info, glass_revision_number_t rev)
Override methods of GlassTable.
The list of words containing a particular trigram.
GlassSpellingTable(const std::string &dbdir, bool readonly)
Create a new GlassSpellingTable object.