| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <config.h> |
|---|
| 24 | |
|---|
| 25 | #include <xapian/error.h> |
|---|
| 26 | |
|---|
| 27 | #include "expandweight.h" |
|---|
| 28 | #include "flint_positionlist.h" |
|---|
| 29 | #include "flint_termlist.h" |
|---|
| 30 | #include "flint_utils.h" |
|---|
| 31 | #include "omassert.h" |
|---|
| 32 | #include "utils.h" |
|---|
| 33 | |
|---|
| 34 | using namespace std; |
|---|
| 35 | |
|---|
| 36 | FlintTermList::FlintTermList(Xapian::Internal::RefCntPtr<const FlintDatabase> db_, |
|---|
| 37 | Xapian::docid did_) |
|---|
| 38 | : db(db_), did(did_), current_wdf(0), current_termfreq(0) |
|---|
| 39 | { |
|---|
| 40 | DEBUGCALL(DB, void, "FlintTermList", |
|---|
| 41 | "[RefCntPtr<const FlintDatabase>], " << did_); |
|---|
| 42 | |
|---|
| 43 | if (!db->termlist_table.get_exact_entry(flint_docid_to_key(did), data)) |
|---|
| 44 | throw Xapian::DocNotFoundError("No termlist for document " + om_tostring(did)); |
|---|
| 45 | |
|---|
| 46 | pos = data.data(); |
|---|
| 47 | end = pos + data.size(); |
|---|
| 48 | |
|---|
| 49 | if (pos == end) { |
|---|
| 50 | doclen = 0; |
|---|
| 51 | termlist_size = 0; |
|---|
| 52 | return; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | if (!unpack_uint(&pos, end, &doclen)) { |
|---|
| 57 | const char *msg; |
|---|
| 58 | if (pos == 0) { |
|---|
| 59 | msg = "Too little data for doclen in termlist"; |
|---|
| 60 | } else { |
|---|
| 61 | msg = "Overflowed value for doclen in termlist"; |
|---|
| 62 | } |
|---|
| 63 | throw Xapian::DatabaseCorruptError(msg); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | if (!unpack_uint(&pos, end, &termlist_size)) { |
|---|
| 68 | const char *msg; |
|---|
| 69 | if (pos == 0) { |
|---|
| 70 | msg = "Too little data for list size in termlist"; |
|---|
| 71 | } else { |
|---|
| 72 | msg = "Overflowed value for list size in termlist"; |
|---|
| 73 | } |
|---|
| 74 | throw Xapian::DatabaseCorruptError(msg); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if (pos != end && *pos == '0') ++pos; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | flint_doclen_t |
|---|
| 83 | FlintTermList::get_doclength() const |
|---|
| 84 | { |
|---|
| 85 | DEBUGCALL(DB, flint_doclen_t, "FlintTermList::get_doclength", ""); |
|---|
| 86 | RETURN(doclen); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | Xapian::termcount |
|---|
| 90 | FlintTermList::get_approx_size() const |
|---|
| 91 | { |
|---|
| 92 | DEBUGCALL(DB, Xapian::termcount, "FlintTermList::get_approx_size", ""); |
|---|
| 93 | RETURN(termlist_size); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void |
|---|
| 97 | FlintTermList::accumulate_stats(Xapian::Internal::ExpandStats & stats) const |
|---|
| 98 | { |
|---|
| 99 | DEBUGCALL(DB, void, "FlintTermList::accumulate_stats", "[stats&]"); |
|---|
| 100 | Assert(!at_end()); |
|---|
| 101 | stats.accumulate(current_wdf, doclen, get_termfreq(), db->get_doccount()); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | string |
|---|
| 105 | FlintTermList::get_termname() const |
|---|
| 106 | { |
|---|
| 107 | DEBUGCALL(DB, string, "FlintTermList::get_termname", ""); |
|---|
| 108 | RETURN(current_term); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | Xapian::termcount |
|---|
| 112 | FlintTermList::get_wdf() const |
|---|
| 113 | { |
|---|
| 114 | DEBUGCALL(DB, Xapian::termcount, "FlintTermList::get_wdf", ""); |
|---|
| 115 | RETURN(current_wdf); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | Xapian::doccount |
|---|
| 119 | FlintTermList::get_termfreq() const |
|---|
| 120 | { |
|---|
| 121 | DEBUGCALL(DB, Xapian::doccount, "FlintTermList::get_termfreq", ""); |
|---|
| 122 | if (current_termfreq == 0) |
|---|
| 123 | current_termfreq = db->get_termfreq(current_term); |
|---|
| 124 | RETURN(current_termfreq); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | TermList * |
|---|
| 128 | FlintTermList::next() |
|---|
| 129 | { |
|---|
| 130 | DEBUGCALL(DB, TermList *, "FlintTermList::next", ""); |
|---|
| 131 | Assert(!at_end()); |
|---|
| 132 | if (pos == end) { |
|---|
| 133 | pos = NULL; |
|---|
| 134 | RETURN(NULL); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | current_termfreq = 0; |
|---|
| 139 | |
|---|
| 140 | bool wdf_in_reuse = false; |
|---|
| 141 | if (!current_term.empty()) { |
|---|
| 142 | |
|---|
| 143 | size_t len = static_cast<unsigned char>(*pos++); |
|---|
| 144 | if (len > current_term.size()) { |
|---|
| 145 | |
|---|
| 146 | wdf_in_reuse = true; |
|---|
| 147 | size_t divisor = current_term.size() + 1; |
|---|
| 148 | current_wdf = len / divisor - 1; |
|---|
| 149 | len %= divisor; |
|---|
| 150 | } |
|---|
| 151 | current_term.resize(len); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | size_t append_len = static_cast<unsigned char>(*pos++); |
|---|
| 156 | current_term.append(pos, append_len); |
|---|
| 157 | pos += append_len; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | if (!wdf_in_reuse && !unpack_uint(&pos, end, ¤t_wdf)) { |
|---|
| 161 | const char *msg; |
|---|
| 162 | if (pos == 0) { |
|---|
| 163 | msg = "Too little data for wdf in termlist"; |
|---|
| 164 | } else { |
|---|
| 165 | msg = "Overflowed value for wdf in termlist"; |
|---|
| 166 | } |
|---|
| 167 | throw Xapian::DatabaseCorruptError(msg); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | RETURN(NULL); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | bool |
|---|
| 174 | FlintTermList::at_end() const |
|---|
| 175 | { |
|---|
| 176 | DEBUGCALL(DB, bool, "FlintTermList::at_end", ""); |
|---|
| 177 | RETURN(pos == NULL); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | Xapian::termcount |
|---|
| 181 | FlintTermList::positionlist_count() const |
|---|
| 182 | { |
|---|
| 183 | DEBUGCALL(DB, Xapian::termcount, "FlintTermList::positionlist_count", ""); |
|---|
| 184 | RETURN(db->position_table.positionlist_count(did, current_term)); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | Xapian::PositionIterator |
|---|
| 188 | FlintTermList::positionlist_begin() const |
|---|
| 189 | { |
|---|
| 190 | DEBUGCALL(DB, Xapian::PositionIterator, "FlintTermList::positionlist_begin", ""); |
|---|
| 191 | return Xapian::PositionIterator( |
|---|
| 192 | new FlintPositionList(&db->position_table, did, current_term)); |
|---|
| 193 | } |
|---|