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