Branch data Line data Source code
1 : : /** @file brass_synonym.h
2 : : * @brief Synonym data for a brass 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_BRASS_SYNONYM_H
22 : : #define XAPIAN_INCLUDED_BRASS_SYNONYM_H
23 : :
24 : : #include <xapian/types.h>
25 : :
26 : : #include "alltermslist.h"
27 : : #include "database.h"
28 : : #include "brass_lazytable.h"
29 : : #include "termlist.h"
30 : :
31 : : #include <set>
32 : : #include <string>
33 : :
34 : 2208 : class BrassSynonymTable : public BrassLazyTable {
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 BrassSynonymTable 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 brass database is stored in.
48 : : * @param readonly true if we're opening read-only, else false.
49 : : */
50 : 2208 : BrassSynonymTable(const std::string & dbdir, bool readonly)
51 : : : BrassLazyTable("synonym", dbdir + "/synonym.", readonly,
52 : 2208 : 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 BrassTable.
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 : 690 : bool is_modified() const {
95 [ + + ][ + + ]: 690 : return !last_term.empty() || BrassTable::is_modified();
96 : : }
97 : :
98 : 473 : void flush_db() {
99 : 473 : merge_changes();
100 : 473 : BrassTable::flush_db();
101 : 473 : }
102 : :
103 : 152 : void cancel() {
104 : 152 : discard_changes();
105 : 152 : BrassTable::cancel();
106 : 152 : }
107 : :
108 : : // @}
109 : : };
110 : :
111 : : class BrassCursor;
112 : :
113 : : class BrassSynonymTermList : public AllTermsList {
114 : : /// Copying is not allowed.
115 : : BrassSynonymTermList(const BrassSynonymTermList &);
116 : :
117 : : /// Assignment is not allowed.
118 : : void operator=(const BrassSynonymTermList &);
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 : : BrassCursor * cursor;
127 : :
128 : : /// The prefix to restrict the terms to.
129 : : string prefix;
130 : :
131 : : public:
132 : 11 : BrassSynonymTermList(Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database_,
133 : : BrassCursor * cursor_,
134 : : const string & prefix_)
135 : 11 : : 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 [ + + ]: 11 : if (prefix.empty()) {
141 : 7 : cursor->find_entry(string());
142 : : } else {
143 : : // Seek to the first key before one with the desired prefix.
144 : 4 : cursor->find_entry_lt(prefix);
145 : : }
146 : 11 : }
147 : :
148 : : /// Destructor.
149 : : ~BrassSynonymTermList();
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_BRASS_SYNONYM_H
|