backends/flint/flint_spelling.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2007,2009 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_SPELLING_H
00022 #define XAPIAN_INCLUDED_FLINT_SPELLING_H
00023 
00024 #include <xapian/types.h>
00025 
00026 #include "flint_table.h"
00027 #include "termlist.h"
00028 
00029 #include <map>
00030 #include <set>
00031 #include <string>
00032 #include <string.h> // For memcpy() and memcmp().
00033 
00034 struct fragment {
00035     char data[4];
00036 
00037     // Default constructor.
00038     fragment() { }
00039 
00040     // Allow implicit conversion.
00041     fragment(char data_[4]) { memcpy(data, data_, 4); }
00042 
00043     char & operator[] (unsigned i) { return data[i]; }
00044     const char & operator[] (unsigned i) const { return data[i]; }
00045 
00046     operator std::string () const {
00047         return std::string(data, data[0] == 'M' ? 4 : 3);
00048     }
00049 };
00050 
00051 inline bool operator<(const fragment &a, const fragment &b) {
00052     return memcmp(a.data, b.data, 4) < 0;
00053 }
00054 
00055 class FlintSpellingTable : public FlintTable {
00056     void toggle_fragment(fragment frag, const std::string & word);
00057 
00058     std::map<std::string, Xapian::termcount> wordfreq_changes;
00059     std::map<fragment, std::set<std::string> > termlist_deltas;
00060 
00061   public:
00070     FlintSpellingTable(const std::string & dbdir, bool readonly)
00071         : FlintTable(dbdir + "/spelling.", readonly, Z_DEFAULT_STRATEGY, true) { }
00072 
00073     // Merge in batched-up changes.
00074     void merge_changes();
00075 
00076     void add_word(const std::string & word, Xapian::termcount freqinc);
00077     void remove_word(const std::string & word, Xapian::termcount freqdec);
00078 
00079     TermList * open_termlist(const std::string & word);
00080 
00081     Xapian::doccount get_word_frequency(const std::string & word) const;
00082 
00090     bool is_modified() const {
00091         return !wordfreq_changes.empty() || FlintTable::is_modified();
00092     }
00093 
00094     void create_and_open(unsigned int blocksize) {
00095         // The spelling table is created lazily, but erase it in case we're
00096         // overwriting an existing database and it already exists.
00097         FlintTable::erase();
00098         FlintTable::set_block_size(blocksize);
00099     }
00100 
00101     void commit(flint_revision_number_t revision) {
00102         merge_changes();
00103         FlintTable::commit(revision);
00104     }
00105 
00106     void cancel() {
00107         // Discard batched-up changes.
00108         wordfreq_changes.clear();
00109         termlist_deltas.clear();
00110 
00111         FlintTable::cancel();
00112     }
00113 
00114     // @}
00115 };
00116 
00118 class FlintSpellingTermList : public TermList {
00120     std::string data;
00121 
00123     unsigned p;
00124 
00126     std::string current_term;
00127 
00129     FlintSpellingTermList(const FlintSpellingTermList &);
00130 
00132     void operator=(const FlintSpellingTermList &);
00133 
00134   public:
00136     FlintSpellingTermList(const std::string & data_)
00137         : data(data_), p(0) { }
00138 
00139     Xapian::termcount get_approx_size() const;
00140 
00141     std::string get_termname() const;
00142 
00143     Xapian::termcount get_wdf() const;
00144 
00145     Xapian::doccount get_termfreq() const;
00146 
00147     Xapian::termcount get_collection_freq() const;
00148 
00149     TermList *next();
00150 
00151     bool at_end() const;
00152 
00153     Xapian::termcount positionlist_count() const;
00154 
00155     Xapian::PositionIterator positionlist_begin() const;
00156 };
00157 
00158 #endif // XAPIAN_INCLUDED_FLINT_SPELLING_H

Documentation for Xapian (version 1.0.20).
Generated on 28 Apr 2010 by Doxygen 1.5.2.