root / tags / 1.0.8 / xapian-core / backends / flint / flint_synonym.h

Revision 9275, 5.5 kB (checked in by olly, 16 months ago)

backends/flint/flint_spellingwordslist.cc,
backends/flint/flint_synonym.cc: Use FlintCursor::find_entry_ge().
backends/flint/flint_synonym.h: Use FlintCursor::find_entry_lt().
backends/flint/flint_alltermslist.h: Use FlintCursor::find_entry_lt()
and skip any keys before "\x00\xff" to allow for extra metadata
keys.

Line 
1/** @file flint_synonym.h
2 * @brief Synonym data for a flint database.
3 */
4/* Copyright (C) 2005,2007 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 "omdebug.h"
30#include "termlist.h"
31
32#include <set>
33#include <string>
34
35class FlintSynonymTable : public FlintTable {
36    /// The last term which was updated.
37    mutable std::string last_term;
38
39    /// The synonyms for the last term which was updated.
40    mutable std::set<std::string> last_synonyms;
41
42  public:
43    /** Create a new FlintSynonymTable object.
44     *
45     *  This method does not create or open the table on disk - you
46     *  must call the create() or open() methods respectively!
47     *
48     *  @param dbdir            The directory the flint database is stored in.
49     *  @param readonly         true if we're opening read-only, else false.
50     */
51    FlintSynonymTable(std::string dbdir, bool readonly)
52        : FlintTable(dbdir + "/synonym.", readonly, Z_DEFAULT_STRATEGY, true) { }
53
54    // Merge in batched-up changes.
55    void merge_changes();
56
57    // Discard batched-up changes.
58    void discard_changes() {
59        last_term.resize(0);
60        last_synonyms.clear();
61    }
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 FlintTable.
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    bool is_modified() const {
95        return !last_term.empty() || FlintTable::is_modified();
96    }
97
98    void create_and_open(unsigned int blocksize) {
99        // The synonym table is created lazily, but erase it in case we're
100        // overwriting an existing database and it already exists.
101        FlintTable::erase();
102        FlintTable::set_block_size(blocksize);
103    }
104
105    void commit(flint_revision_number_t revision) {
106        merge_changes();
107        FlintTable::commit(revision);
108    }
109
110    void cancel() {
111        discard_changes();
112        FlintTable::cancel();
113    }
114
115    // @}
116};
117
118class FlintCursor;
119
120class FlintSynonymTermList : public AllTermsList {
121    /// Copying is not allowed.
122    FlintSynonymTermList(const FlintSynonymTermList &);
123
124    /// Assignment is not allowed.
125    void operator=(const FlintSynonymTermList &);
126
127    /// Keep a reference to our database to stop it being deleted.
128    Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database;
129
130    /** A cursor which runs through the synonym table reading termnames from
131     *  the keys.
132     */
133    FlintCursor * cursor;
134
135    /// The number of terms in this list.
136    Xapian::termcount size;
137
138    /// The prefix to restrict the terms to.
139    string prefix;
140
141  public:
142    FlintSynonymTermList(Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database_,
143                      FlintCursor * cursor_,
144                      Xapian::termcount size_,
145                      const string & prefix_)
146            : database(database_), cursor(cursor_), size(size_), prefix(prefix_)
147    {
148        // Position the on the highest key before the first key we want, so
149        // that the first call to next() will put us on the first key we want.
150        if (prefix.empty()) {
151            cursor->find_entry(string());
152        } else {
153            // Seek to the first key before one with the desired prefix.
154            cursor->find_entry_lt(prefix);
155        }
156    }
157
158    /// Destructor.
159    ~FlintSynonymTermList();
160
161    /** Returns the approximate size of the list.
162     *
163     *  This may be unused for this class.
164     */
165    Xapian::termcount get_approx_size() const;
166
167    /** Returns the current termname.
168     *
169     *  Either next() or skip_to() must have been called before this
170     *  method can be called.
171     */
172    string get_termname() const;
173
174    /// Return the term frequency for the term at the current position.
175    Xapian::doccount get_termfreq() const;
176
177    /// Return the collection frequency for the term at the current position.
178    Xapian::termcount get_collection_freq() const;
179
180    /// Advance to the next term in the list.
181    TermList * next();
182
183    /// Advance to the first term which is >= tname.
184    TermList * skip_to(const string &tname);
185
186    /// True if we're off the end of the list
187    bool at_end() const;
188};
189
190#endif // XAPIAN_INCLUDED_FLINT_SYNONYM_H
Note: See TracBrowser for help on using the browser.