xapian-core  2.0.0
glass_synonym.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2007,2008,2009,2011,2014,2016,2017,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_GLASS_SYNONYM_H
22 #define XAPIAN_INCLUDED_GLASS_SYNONYM_H
23 
24 #include <xapian/types.h>
25 
26 #include "backends/alltermslist.h"
27 #include "glass_lazytable.h"
28 #include "api/termlist.h"
29 
30 #include <functional>
31 #include <set>
32 #include <string>
33 #include <string_view>
34 
35 class GlassDatabase;
36 
37 namespace Glass {
38  class RootInfo;
39 }
40 using Glass::RootInfo;
41 
44  mutable std::string last_term;
45 
47  mutable std::set<std::string, std::less<>> last_synonyms;
48 
49  public:
58  GlassSynonymTable(const std::string & dbdir, bool readonly)
59  : GlassLazyTable("synonym", dbdir + "/synonym.", readonly) { }
60 
61  GlassSynonymTable(int fd, off_t offset_, bool readonly)
62  : GlassLazyTable("synonym", fd, offset_, readonly) { }
63 
64  // Merge in batched-up changes.
65  void merge_changes();
66 
67  // Discard batched-up changes.
68  void discard_changes() {
69  last_term.resize(0);
70  last_synonyms.clear();
71  }
72 
77  void add_synonym(std::string_view term, std::string_view synonym);
78 
83  void remove_synonym(std::string_view term, std::string_view synonym);
84 
89  void clear_synonyms(std::string_view term);
90 
95  TermList* open_termlist(std::string_view term);
96 
104  bool is_modified() const {
105  return !last_term.empty() || GlassTable::is_modified();
106  }
107 
108  void flush_db() {
109  merge_changes();
111  }
112 
113  void cancel(const RootInfo & root_info, glass_revision_number_t rev) {
114  discard_changes();
115  GlassTable::cancel(root_info, rev);
116  }
117 
118  // @}
119 };
120 
121 class GlassCursor;
122 
126 
129 
132 
137 
139  string prefix;
140 
141  public:
143  GlassCursor * cursor_,
144  std::string_view prefix_)
145  : database(database_), cursor(cursor_), prefix(prefix_)
146  {
147  // Position the cursor on the highest key before the first key we want,
148  // so that the first call to next() will put us on the first key we
149  // want.
150  if (prefix.empty()) {
151  cursor->rewind();
152  } else {
153  // Seek to the first key before one with the desired prefix.
155  }
156  }
157 
160 
162 
165 
167  TermList * next();
168 
170  TermList* skip_to(std::string_view tname);
171 };
172 
173 #endif // XAPIAN_INCLUDED_GLASS_SYNONYM_H
Abstract base class for iterating all terms in a database.
Abstract base class for iterating all terms in a database.
Definition: alltermslist.h:27
A cursor pointing to a position in a Btree table, for reading several entries in order,...
Definition: glass_cursor.h:148
void rewind()
Position cursor on the dummy empty key.
Definition: glass_cursor.h:250
void find_entry_lt(const string &key)
Position the cursor on the highest entry with key < key.
A backend designed for efficient indexing and retrieval, using compressed posting lists and a btree s...
GlassSynonymTable(const std::string &dbdir, bool readonly)
Create a new GlassSynonymTable object.
Definition: glass_synonym.h:58
void clear_synonyms(std::string_view term)
Remove all synonyms for term.
std::set< std::string, std::less<> > last_synonyms
The synonyms for the last term which was updated.
Definition: glass_synonym.h:47
std::string last_term
The last term which was updated.
Definition: glass_synonym.h:44
GlassSynonymTable(int fd, off_t offset_, bool readonly)
Definition: glass_synonym.h:61
void flush_db()
Override methods of GlassTable.
void add_synonym(std::string_view term, std::string_view synonym)
Add a synonym for term.
TermList * open_termlist(std::string_view term)
Open synonym termlist for a term.
void remove_synonym(std::string_view term, std::string_view synonym)
Remove a synonym for term.
bool is_modified() const
Override methods of GlassTable.
void cancel(const RootInfo &root_info, glass_revision_number_t rev)
Override methods of GlassTable.
GlassSynonymTermList(const GlassSynonymTermList &)
Copying is not allowed.
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
TermList * skip_to(std::string_view tname)
Advance to the first term which is >= tname.
string prefix
The prefix to restrict the terms to.
~GlassSynonymTermList()
Destructor.
GlassCursor * cursor
A cursor which runs through the synonym table reading termnames from the keys.
void operator=(const GlassSynonymTermList &)
Assignment is not allowed.
Xapian::Internal::intrusive_ptr< const GlassDatabase > database
Keep a reference to our database to stop it being deleted.
GlassSynonymTermList(Xapian::Internal::intrusive_ptr< const GlassDatabase > database_, GlassCursor *cursor_, std::string_view prefix_)
TermList * next()
Advance to the next term in the list.
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:706
void cancel(const RootInfo &root_info, glass_revision_number_t rev)
Cancel any outstanding changes.
Abstract base class for termlists.
Definition: termlist.h:42
string term
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.
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:108
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
Abstract base class for termlists.
typedefs for Xapian