00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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>
00033
00034 struct fragment {
00035 char data[4];
00036
00037
00038 fragment() { }
00039
00040
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
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
00096
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
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