backends/flint/flint_synonym.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2005,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #ifndef XAPIAN_INCLUDED_FLINT_SYNONYM_H
00022 #define XAPIAN_INCLUDED_FLINT_SYNONYM_H
00023 
00024 #include <xapian/types.h>
00025 
00026 #include "alltermslist.h"
00027 #include "database.h"
00028 #include "flint_table.h"
00029 #include "omdebug.h"
00030 #include "termlist.h"
00031 
00032 #include <set>
00033 #include <string>
00034 
00035 class FlintSynonymTable : public FlintTable {
00037     mutable std::string last_term;
00038 
00040     mutable std::set<std::string> last_synonyms;
00041 
00042   public:
00051     FlintSynonymTable(std::string dbdir, bool readonly)
00052         : FlintTable(dbdir + "/synonym.", readonly, Z_DEFAULT_STRATEGY, true) { }
00053 
00054     // Merge in batched-up changes.
00055     void merge_changes();
00056 
00057     // Discard batched-up changes.
00058     void discard_changes() {
00059         last_term.resize(0);
00060         last_synonyms.clear();
00061     }
00062 
00067     void add_synonym(const std::string & term, const std::string & synonym);
00068 
00073     void remove_synonym(const std::string & term, const std::string & synonym);
00074 
00079     void clear_synonyms(const std::string & term);
00080 
00085     TermList * open_termlist(const std::string & term);
00086 
00094     bool is_modified() const {
00095         return !last_term.empty() || FlintTable::is_modified();
00096     }
00097 
00098     void create_and_open(unsigned int blocksize) {
00099         // The synonym table is created lazily, but erase it in case we're
00100         // overwriting an existing database and it already exists.
00101         FlintTable::erase();
00102         FlintTable::set_block_size(blocksize);
00103     }
00104 
00105     void commit(flint_revision_number_t revision) {
00106         merge_changes();
00107         FlintTable::commit(revision);
00108     }
00109 
00110     void cancel() {
00111         discard_changes();
00112         FlintTable::cancel();
00113     }
00114 
00115     // @}
00116 };
00117 
00118 class FlintCursor;
00119 
00120 class FlintSynonymTermList : public AllTermsList {
00122     FlintSynonymTermList(const FlintSynonymTermList &);
00123 
00125     void operator=(const FlintSynonymTermList &);
00126 
00128     Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database;
00129 
00133     FlintCursor * cursor;
00134 
00136     Xapian::termcount size;
00137 
00139     string prefix;
00140 
00141   public:
00142     FlintSynonymTermList(Xapian::Internal::RefCntPtr<const Xapian::Database::Internal> database_,
00143                       FlintCursor * cursor_,
00144                       Xapian::termcount size_,
00145                       const string & prefix_)
00146             : database(database_), cursor(cursor_), size(size_), prefix(prefix_)
00147     {
00148         // Position the on the highest key before the first key we want, so
00149         // that the first call to next() will put us on the first key we want.
00150         if (prefix.empty()) {
00151             cursor->find_entry(string());
00152         } else {
00153             // Seek to the first key before one with the desired prefix.
00154             cursor->find_entry_lt(prefix);
00155         }
00156     }
00157 
00159     ~FlintSynonymTermList();
00160 
00166     string get_termname() const;
00167 
00169     Xapian::doccount get_termfreq() const;
00170 
00172     Xapian::termcount get_collection_freq() const;
00173 
00175     TermList * next();
00176 
00178     TermList * skip_to(const string &tname);
00179 
00181     bool at_end() const;
00182 };
00183 
00184 #endif // XAPIAN_INCLUDED_FLINT_SYNONYM_H

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.