| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #ifndef XAPIAN_INCLUDED_FLINT_SPELLING_H |
|---|
| 22 | #define XAPIAN_INCLUDED_FLINT_SPELLING_H |
|---|
| 23 | |
|---|
| 24 | #include <xapian/types.h> |
|---|
| 25 | |
|---|
| 26 | #include "flint_table.h" |
|---|
| 27 | #include "termlist.h" |
|---|
| 28 | |
|---|
| 29 | #include <map> |
|---|
| 30 | #include <set> |
|---|
| 31 | #include <string> |
|---|
| 32 | #include <string.h> // For memcpy() and memcmp(). |
|---|
| 33 | |
|---|
| 34 | struct fragment { |
|---|
| 35 | char data[4]; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | fragment() { } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | fragment(char data_[4]) { memcpy(data, data_, 4); } |
|---|
| 42 | |
|---|
| 43 | char & operator[] (unsigned i) { return data[i]; } |
|---|
| 44 | const char & operator[] (unsigned i) const { return data[i]; } |
|---|
| 45 | |
|---|
| 46 | operator std::string () const { |
|---|
| 47 | return string(data, data[0] == 'M' ? 4 : 3); |
|---|
| 48 | } |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | inline bool operator<(const fragment &a, const fragment &b) { |
|---|
| 52 | return memcmp(a.data, b.data, 4) < 0; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | class FlintSpellingTable : public FlintTable { |
|---|
| 56 | void add_fragment(fragment frag, const string & word); |
|---|
| 57 | void remove_fragment(fragment frag, const string & word); |
|---|
| 58 | |
|---|
| 59 | std::map<std::string, Xapian::termcount> wordfreq_changes; |
|---|
| 60 | std::map<fragment, std::set<std::string> > termlist_deltas; |
|---|
| 61 | |
|---|
| 62 | public: |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | FlintSpellingTable(std::string dbdir, bool readonly) |
|---|
| 72 | : FlintTable(dbdir + "/spelling.", readonly, Z_DEFAULT_STRATEGY, true) { } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | void merge_changes(); |
|---|
| 76 | |
|---|
| 77 | void add_word(const std::string & word, Xapian::termcount freqinc); |
|---|
| 78 | void remove_word(const std::string & word, Xapian::termcount freqdec); |
|---|
| 79 | |
|---|
| 80 | TermList * open_termlist(const std::string & word); |
|---|
| 81 | |
|---|
| 82 | Xapian::doccount get_word_frequency(const string & word) const; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | bool is_modified() const { |
|---|
| 92 | return !wordfreq_changes.empty() || FlintTable::is_modified(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void create_and_open(unsigned int blocksize) { |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | FlintTable::erase(); |
|---|
| 99 | FlintTable::set_block_size(blocksize); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void commit(flint_revision_number_t revision) { |
|---|
| 103 | merge_changes(); |
|---|
| 104 | FlintTable::commit(revision); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void cancel() { |
|---|
| 108 | |
|---|
| 109 | wordfreq_changes.clear(); |
|---|
| 110 | termlist_deltas.clear(); |
|---|
| 111 | |
|---|
| 112 | FlintTable::cancel(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class FlintSpellingTermList : public TermList { |
|---|
| 120 | |
|---|
| 121 | std::string data; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | unsigned p; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | std::string current_term; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | FlintSpellingTermList(const FlintSpellingTermList &); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | void operator=(const FlintSpellingTermList &); |
|---|
| 134 | |
|---|
| 135 | public: |
|---|
| 136 | |
|---|
| 137 | FlintSpellingTermList(const std::string & data_) |
|---|
| 138 | : data(data_), p(0) { } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | ~FlintSpellingTermList(); |
|---|
| 142 | |
|---|
| 143 | Xapian::termcount get_approx_size() const; |
|---|
| 144 | |
|---|
| 145 | std::string get_termname() const; |
|---|
| 146 | |
|---|
| 147 | Xapian::termcount get_wdf() const; |
|---|
| 148 | |
|---|
| 149 | Xapian::doccount get_termfreq() const; |
|---|
| 150 | |
|---|
| 151 | Xapian::termcount get_collection_freq() const; |
|---|
| 152 | |
|---|
| 153 | TermList *next(); |
|---|
| 154 | |
|---|
| 155 | bool at_end() const; |
|---|
| 156 | |
|---|
| 157 | Xapian::termcount positionlist_count() const; |
|---|
| 158 | |
|---|
| 159 | Xapian::PositionIterator positionlist_begin() const; |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | #endif // XAPIAN_INCLUDED_FLINT_SPELLING_H |
|---|