Branch data Line data Source code
1 : : /** @file flint_synonym.h
2 : : * @brief Synonym data for a flint database.
3 : : */
4 : : /* Copyright (C) 2005,2007,2008,2009 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_FLINT_SYNONYM_H
22 : : #define XAPIAN_INCLUDED_FLINT_SYNONYM_H
23 : :
24 : : #include <xapian/types.h>
25 : :
26 : : #include "alltermslist.h"
27 : : #include "database.h"
28 : : #include "flint_table.h"
29 : : #include "termlist.h"
30 : :
31 : : #include <set>
32 : : #include <string>
33 : :
34 : 2193 : class FlintSynonymTable : public FlintTable {
35 : : /// The last term which was updated.
36 : : mutable std::string last_term;
37 : :
38 : : /// The synonyms for the last term which was updated.
39 : : mutable std::set<std::string> last_synonyms;
40 : :
41 : : public:
42 : : /** Create a new FlintSynonymTable object.
43 : : *
44 : : * This method does not create or open the table on disk - you
45 : : * must call the create() or open() methods respectively!
46 : : *
47 : : * @param dbdir The directory the flint database is stored in.
48 : : * @param readonly true if we're opening read-only, else false.
49 : : */
50 : 2193 : FlintSynonymTable(const std::string & dbdir, bool readonly)
51 : 2193 : : FlintTable("synonym", dbdir + "/synonym.", readonly, Z_DEFAULT_STRATEGY, true) { }
52 : :
53 : : // Merge in batched-up changes.
54 : : void merge_changes();
55 : :
56 : : // Discard batched-up changes.
57 : 149 : void discard_changes() {
58 : 149 : last_term.resize(0);
59 : 149 : last_synonyms.clear();
60 : 149 : }
61 : :
62 : : /** Add a synonym for @a term.
63 : : *
64 : : * If the synonym has already been added, no action is taken.
65 : : */
66 : : void add_synonym(const std::string & term, const std::string & synonym);
67 : :
68 : : /** Remove a synonym for @a term.
69 : : *
70 : : * If the synonym doesn't exist, no action is taken.
71 : : */
72 : : void remove_synonym(const std::string & term, const std::string & synonym);
73 : :
74 : : /** Remove all synonyms for @a term.
75 : : *
76 : : * If @a term has no synonyms, no action is taken.
77 : : */
78 : : void clear_synonyms(const std::string & term);
79 : :
80 : : /** Open synonym termlist for a term.
81 : : *
82 : : * If @a term has no synonyms, NULL is returned.
83 : : */
84 : : TermList * open_termlist(const std::string & term);
85 : :
86 : : /** Override methods of FlintTable.
87 : : *
88 : : * NB: these aren't virtual, but we always call them on the subclass in
89 : : * cases where it matters).
90 : : * @{
91 : : */
92 : :
93 : 683 : bool is_modified() const {
94 [ + + ][ + + ]: 683 : return !last_term.empty() || FlintTable::is_modified();
95 : : }
96 : :
97 : 305 : void create_and_open(unsigned int blocksize) {
98 : : // The synonym table is created lazily, but erase it in case we're
99 : : // overwriting an existing database and it already exists.
100 : 305 : FlintTable::erase();
101 : 305 : FlintTable::set_block_size(blocksize);
102 : 305 : }
103 : :
104 : 460 : void flush_db() {
105 : 460 : merge_changes();
106 : 460 : FlintTable::flush_db();
107 : 460 : }
108 : :
109 : 149 : void cancel() {
110 : 149 : discard_changes();
111 : 149 : FlintTable::cancel();
112 : 149 : }
113 : :
114 : : // @}
115 : : };
116 : :
117 : : class FlintCursor;
118 : :
119 : : class FlintSynonymTermList : public AllTermsList {
120 : : /// Copying is not allowed.
121 : : FlintSynonymTermList(const FlintSynonymTermList &);
122 : :
123 : : /// Assignment is not allowed.
124 : : void operator=(const FlintSynonymTermList &);
125 : :
126 : : /// Keep a reference to our database to stop it being deleted.
127 : : Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database;
128 : :
129 : : /** A cursor which runs through the synonym table reading termnames from
130 : : * the keys.
131 : : */
132 : : FlintCursor * cursor;
133 : :
134 : : /// The prefix to restrict the terms to.
135 : : string prefix;
136 : :
137 : : public:
138 : 11 : FlintSynonymTermList(Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database_,
139 : : FlintCursor * cursor_,
140 : : const string & prefix_)
141 : 11 : : database(database_), cursor(cursor_), prefix(prefix_)
142 : : {
143 : : // Position the cursor on the highest key before the first key we want,
144 : : // so that the first call to next() will put us on the first key we
145 : : // want.
146 [ + + ]: 11 : if (prefix.empty()) {
147 : 7 : cursor->find_entry(string());
148 : : } else {
149 : : // Seek to the first key before one with the desired prefix.
150 : 4 : cursor->find_entry_lt(prefix);
151 : : }
152 : 11 : }
153 : :
154 : : /// Destructor.
155 : : ~FlintSynonymTermList();
156 : :
157 : : /** Returns the current termname.
158 : : *
159 : : * Either next() or skip_to() must have been called before this
160 : : * method can be called.
161 : : */
162 : : string get_termname() const;
163 : :
164 : : /// Return the term frequency for the term at the current position.
165 : : Xapian::doccount get_termfreq() const;
166 : :
167 : : /// Return the collection frequency for the term at the current position.
168 : : Xapian::termcount get_collection_freq() const;
169 : :
170 : : /// Advance to the next term in the list.
171 : : TermList * next();
172 : :
173 : : /// Advance to the first term which is >= tname.
174 : : TermList * skip_to(const string &tname);
175 : :
176 : : /// True if we're off the end of the list
177 : : bool at_end() const;
178 : : };
179 : :
180 : : #endif // XAPIAN_INCLUDED_FLINT_SYNONYM_H
|