00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef XAPIAN_INCLUDED_CHERT_SPELLING_H
00022 #define XAPIAN_INCLUDED_CHERT_SPELLING_H
00023
00024 #include <xapian/types.h>
00025
00026 #include "chert_lazytable.h"
00027 #include "termlist.h"
00028
00029 #include <map>
00030 #include <set>
00031 #include <string>
00032 #include <cstring>
00033
00034 struct fragment {
00035 char data[4];
00036
00037
00038 fragment() { }
00039
00040
00041 fragment(char data_[4]) { std::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 std::memcmp(a.data, b.data, 4) < 0;
00053 }
00054
00055 class ChertSpellingTable : public ChertLazyTable {
00056 void toggle_word(const std::string & word);
00057 void toggle_fragment(fragment frag, const std::string & word);
00058
00059 std::map<std::string, Xapian::termcount> wordfreq_changes;
00060
00069 std::map<fragment, std::set<std::string> > termlist_deltas;
00070
00071 public:
00080 ChertSpellingTable(const std::string & dbdir, bool readonly)
00081 : ChertLazyTable("spelling", dbdir + "/spelling.", readonly,
00082 Z_DEFAULT_STRATEGY) { }
00083
00084
00085 void merge_changes();
00086
00087 void add_word(const std::string & word, Xapian::termcount freqinc);
00088 void remove_word(const std::string & word, Xapian::termcount freqdec);
00089
00090 TermList * open_termlist(const std::string & word);
00091
00092 Xapian::doccount get_word_frequency(const std::string & word) const;
00093
00101 bool is_modified() const {
00102 return !wordfreq_changes.empty() || ChertTable::is_modified();
00103 }
00104
00105 void flush_db() {
00106 merge_changes();
00107 ChertTable::flush_db();
00108 }
00109
00110 void cancel() {
00111
00112 wordfreq_changes.clear();
00113 termlist_deltas.clear();
00114
00115 ChertTable::cancel();
00116 }
00117
00118
00119 };
00120
00122 class ChertSpellingTermList : public TermList {
00124 std::string data;
00125
00127 unsigned p;
00128
00130 std::string current_term;
00131
00133 ChertSpellingTermList(const ChertSpellingTermList &);
00134
00136 void operator=(const ChertSpellingTermList &);
00137
00138 public:
00140 ChertSpellingTermList(const std::string & data_)
00141 : data(data_), p(0) { }
00142
00143 Xapian::termcount get_approx_size() const;
00144
00145 std::string get_termname() const;
00146
00147 Xapian::termcount get_wdf() const;
00148
00149 Xapian::doccount get_termfreq() const;
00150
00151 Xapian::termcount get_collection_freq() const;
00152
00153 TermList * next();
00154
00155 TermList * skip_to(const std::string & term);
00156
00157 bool at_end() const;
00158
00159 Xapian::termcount positionlist_count() const;
00160
00161 Xapian::PositionIterator positionlist_begin() const;
00162 };
00163
00164 #endif // XAPIAN_INCLUDED_CHERT_SPELLING_H