paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | High-level classes. |
paul@44 | 5 | |
paul@72 | 6 | Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@44 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@44 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@44 | 16 | |
paul@44 | 17 | You should have received a copy of the GNU General Public License along |
paul@44 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 19 | """ |
paul@44 | 20 | |
paul@44 | 21 | from iixr.filesystem import * |
paul@47 | 22 | from iixr.merging import * |
paul@73 | 23 | from itertools import islice |
paul@76 | 24 | from os import mkdir # index discovery |
paul@44 | 25 | from os.path import exists |
paul@44 | 26 | |
paul@44 | 27 | # Constants. |
paul@44 | 28 | |
paul@44 | 29 | TERM_INTERVAL = 100 |
paul@44 | 30 | DOCUMENT_INTERVAL = 100 |
paul@44 | 31 | FIELD_INTERVAL = 100 |
paul@44 | 32 | FLUSH_INTERVAL = 10000 |
paul@73 | 33 | OPEN_PARTITIONS = 20 |
paul@44 | 34 | |
paul@44 | 35 | # High-level classes. |
paul@44 | 36 | |
paul@44 | 37 | class Document: |
paul@44 | 38 | |
paul@44 | 39 | "A container of document information." |
paul@44 | 40 | |
paul@63 | 41 | def __init__(self, docnum, fields=None): |
paul@44 | 42 | self.docnum = docnum |
paul@63 | 43 | self.fields = fields or [] |
paul@44 | 44 | self.terms = {} |
paul@63 | 45 | self.field_dict = None |
paul@44 | 46 | |
paul@44 | 47 | def add_position(self, term, position): |
paul@44 | 48 | |
paul@44 | 49 | """ |
paul@44 | 50 | Add a position entry for the given 'term', indicating the given |
paul@44 | 51 | 'position'. |
paul@44 | 52 | """ |
paul@44 | 53 | |
paul@44 | 54 | self.terms.setdefault(term, []).append(position) |
paul@44 | 55 | |
paul@44 | 56 | def add_field(self, identifier, value): |
paul@44 | 57 | |
paul@44 | 58 | "Add a field having the given 'identifier' and 'value'." |
paul@44 | 59 | |
paul@44 | 60 | self.fields.append((identifier, unicode(value))) # convert to string |
paul@44 | 61 | |
paul@44 | 62 | def set_fields(self, fields): |
paul@44 | 63 | |
paul@44 | 64 | """ |
paul@44 | 65 | Set the document's 'fields': a list of tuples each containing an integer |
paul@44 | 66 | identifier and a string value. |
paul@44 | 67 | """ |
paul@44 | 68 | |
paul@44 | 69 | self.fields = fields |
paul@44 | 70 | |
paul@63 | 71 | def _ensure_dict(self): |
paul@63 | 72 | if self.field_dict is None: |
paul@63 | 73 | self.field_dict = dict(self.fields) |
paul@63 | 74 | |
paul@63 | 75 | def keys(self): |
paul@63 | 76 | self._ensure_dict() |
paul@63 | 77 | return self.field_dict.keys() |
paul@63 | 78 | |
paul@63 | 79 | def __getitem__(self, key): |
paul@63 | 80 | self._ensure_dict() |
paul@63 | 81 | return self.field_dict[key] |
paul@63 | 82 | |
paul@44 | 83 | class IndexWriter: |
paul@44 | 84 | |
paul@44 | 85 | """ |
paul@44 | 86 | Building term information and writing it to the term and field dictionaries. |
paul@44 | 87 | """ |
paul@44 | 88 | |
paul@66 | 89 | def __init__(self, pathname, interval, doc_interval, field_interval, flush_interval): |
paul@44 | 90 | self.pathname = pathname |
paul@44 | 91 | self.interval = interval |
paul@44 | 92 | self.doc_interval = doc_interval |
paul@66 | 93 | self.field_interval = field_interval |
paul@44 | 94 | self.flush_interval = flush_interval |
paul@44 | 95 | |
paul@76 | 96 | self.dict_partition = get_next_partition(get_term_partitions(self.pathname)) |
paul@76 | 97 | self.field_dict_partition = get_next_partition(get_field_partitions(self.pathname)) |
paul@44 | 98 | |
paul@44 | 99 | self.terms = {} |
paul@63 | 100 | self.docs = [] |
paul@44 | 101 | |
paul@44 | 102 | self.doc_counter = 0 |
paul@44 | 103 | |
paul@44 | 104 | def add_document(self, doc): |
paul@44 | 105 | |
paul@44 | 106 | """ |
paul@44 | 107 | Add the given document 'doc', updating the document counter and flushing |
paul@44 | 108 | terms and fields if appropriate. |
paul@44 | 109 | """ |
paul@44 | 110 | |
paul@44 | 111 | for term, positions in doc.terms.items(): |
paul@44 | 112 | self.terms.setdefault(term, {})[doc.docnum] = positions |
paul@44 | 113 | |
paul@63 | 114 | self.docs.append((doc.docnum, doc.fields)) |
paul@44 | 115 | |
paul@44 | 116 | self.doc_counter += 1 |
paul@44 | 117 | if self.flush_interval and self.doc_counter >= self.flush_interval: |
paul@44 | 118 | self.flush_terms() |
paul@44 | 119 | self.flush_fields() |
paul@44 | 120 | self.doc_counter = 0 |
paul@44 | 121 | |
paul@44 | 122 | def get_term_writer(self): |
paul@44 | 123 | |
paul@44 | 124 | "Return a term dictionary writer for the current partition." |
paul@44 | 125 | |
paul@44 | 126 | return get_term_writer(self.pathname, self.dict_partition, self.interval, self.doc_interval) |
paul@44 | 127 | |
paul@44 | 128 | def get_field_writer(self): |
paul@44 | 129 | |
paul@44 | 130 | "Return a field dictionary writer for the current partition." |
paul@44 | 131 | |
paul@65 | 132 | return get_field_writer(self.pathname, self.field_dict_partition, self.field_interval) |
paul@44 | 133 | |
paul@44 | 134 | def flush_terms(self): |
paul@44 | 135 | |
paul@44 | 136 | "Flush terms into the current term dictionary partition." |
paul@44 | 137 | |
paul@44 | 138 | # Get the terms in order. |
paul@44 | 139 | |
paul@44 | 140 | all_terms = self.terms |
paul@44 | 141 | terms = all_terms.keys() |
paul@44 | 142 | terms.sort() |
paul@44 | 143 | |
paul@44 | 144 | dict_writer = self.get_term_writer() |
paul@44 | 145 | |
paul@44 | 146 | for term in terms: |
paul@44 | 147 | doc_positions = all_terms[term].items() |
paul@44 | 148 | dict_writer.write_term_positions(term, doc_positions) |
paul@44 | 149 | |
paul@44 | 150 | dict_writer.close() |
paul@44 | 151 | |
paul@44 | 152 | self.terms = {} |
paul@44 | 153 | self.dict_partition += 1 |
paul@44 | 154 | |
paul@44 | 155 | def flush_fields(self): |
paul@44 | 156 | |
paul@44 | 157 | "Flush fields into the current term dictionary partition." |
paul@44 | 158 | |
paul@44 | 159 | # Get the documents in order. |
paul@44 | 160 | |
paul@63 | 161 | self.docs.sort() |
paul@44 | 162 | |
paul@44 | 163 | field_dict_writer = self.get_field_writer() |
paul@44 | 164 | |
paul@63 | 165 | for docnum, fields in self.docs: |
paul@44 | 166 | field_dict_writer.write_fields(docnum, fields) |
paul@44 | 167 | |
paul@44 | 168 | field_dict_writer.close() |
paul@44 | 169 | |
paul@63 | 170 | self.docs = [] |
paul@44 | 171 | self.field_dict_partition += 1 |
paul@44 | 172 | |
paul@44 | 173 | def close(self): |
paul@76 | 174 | if self.terms or not get_term_partitions(self.pathname): |
paul@44 | 175 | self.flush_terms() |
paul@76 | 176 | if self.docs or not get_field_partitions(self.pathname): |
paul@44 | 177 | self.flush_fields() |
paul@44 | 178 | |
paul@44 | 179 | class IndexReader: |
paul@44 | 180 | |
paul@44 | 181 | "Accessing the term and field dictionaries." |
paul@44 | 182 | |
paul@44 | 183 | def __init__(self, pathname): |
paul@44 | 184 | self.dict_reader = get_term_reader(pathname, "merged") |
paul@44 | 185 | self.field_dict_reader = get_field_reader(pathname, "merged") |
paul@44 | 186 | |
paul@72 | 187 | def get_terms(self): |
paul@72 | 188 | return self.dict_reader.get_terms() |
paul@72 | 189 | |
paul@44 | 190 | def find_terms(self, term): |
paul@44 | 191 | return self.dict_reader.find_terms(term) |
paul@44 | 192 | |
paul@44 | 193 | def find_positions(self, term): |
paul@44 | 194 | return self.dict_reader.find_positions(term) |
paul@44 | 195 | |
paul@70 | 196 | def find_common_positions(self, terms): |
paul@70 | 197 | return self.dict_reader.find_common_positions(terms) |
paul@60 | 198 | |
paul@44 | 199 | def get_frequency(self, term): |
paul@44 | 200 | return self.dict_reader.get_frequency(term) |
paul@44 | 201 | |
paul@44 | 202 | def get_document_frequency(self, term): |
paul@44 | 203 | return self.dict_reader.get_document_frequency(term) |
paul@44 | 204 | |
paul@44 | 205 | def get_fields(self, docnum): |
paul@44 | 206 | return self.field_dict_reader.get_fields(docnum) |
paul@44 | 207 | |
paul@63 | 208 | def get_document(self, docnum): |
paul@63 | 209 | return Document(docnum, self.get_fields(docnum)) |
paul@63 | 210 | |
paul@44 | 211 | def close(self): |
paul@44 | 212 | self.dict_reader.close() |
paul@44 | 213 | self.field_dict_reader.close() |
paul@44 | 214 | |
paul@44 | 215 | class Index: |
paul@44 | 216 | |
paul@44 | 217 | "An inverted index solution encapsulating the various components." |
paul@44 | 218 | |
paul@64 | 219 | def __init__(self, pathname, interval=TERM_INTERVAL, doc_interval=DOCUMENT_INTERVAL, field_interval=FIELD_INTERVAL, |
paul@73 | 220 | flush_interval=FLUSH_INTERVAL, open_partitions=OPEN_PARTITIONS): |
paul@64 | 221 | |
paul@44 | 222 | self.pathname = pathname |
paul@64 | 223 | self.interval = interval |
paul@64 | 224 | self.doc_interval = doc_interval |
paul@64 | 225 | self.field_interval = field_interval |
paul@64 | 226 | self.flush_interval = flush_interval |
paul@73 | 227 | self.open_partitions = open_partitions |
paul@44 | 228 | self.reader = None |
paul@44 | 229 | self.writer = None |
paul@44 | 230 | |
paul@64 | 231 | def get_writer(self): |
paul@44 | 232 | |
paul@64 | 233 | "Return a writer." |
paul@44 | 234 | |
paul@59 | 235 | self._ensure_directory() |
paul@66 | 236 | self.writer = IndexWriter(self.pathname, self.interval, self.doc_interval, |
paul@66 | 237 | self.field_interval, self.flush_interval) |
paul@59 | 238 | return self.writer |
paul@59 | 239 | |
paul@59 | 240 | def _ensure_directory(self): |
paul@44 | 241 | if not exists(self.pathname): |
paul@44 | 242 | mkdir(self.pathname) |
paul@44 | 243 | |
paul@44 | 244 | def get_reader(self, partition=0): |
paul@44 | 245 | |
paul@44 | 246 | "Return a reader for the index." |
paul@44 | 247 | |
paul@44 | 248 | # Ensure that only one partition exists. |
paul@44 | 249 | |
paul@44 | 250 | self.merge() |
paul@44 | 251 | return self._get_reader(partition) |
paul@44 | 252 | |
paul@44 | 253 | def _get_reader(self, partition): |
paul@44 | 254 | |
paul@44 | 255 | "Return a reader for the index." |
paul@44 | 256 | |
paul@44 | 257 | if not exists(self.pathname): |
paul@44 | 258 | raise OSError, "Index path %r does not exist." % self.pathname |
paul@44 | 259 | |
paul@44 | 260 | self.reader = IndexReader(self.pathname) |
paul@44 | 261 | return self.reader |
paul@44 | 262 | |
paul@58 | 263 | def get_term_partitions(self): |
paul@58 | 264 | |
paul@58 | 265 | "Return a set of term partition identifiers." |
paul@58 | 266 | |
paul@76 | 267 | return get_term_partitions(self.pathname) |
paul@58 | 268 | |
paul@58 | 269 | def get_field_partitions(self): |
paul@58 | 270 | |
paul@58 | 271 | "Return a set of field partition identifiers." |
paul@58 | 272 | |
paul@76 | 273 | return get_field_partitions(self.pathname) |
paul@58 | 274 | |
paul@44 | 275 | def merge(self): |
paul@44 | 276 | |
paul@44 | 277 | "Merge/optimise index partitions." |
paul@44 | 278 | |
paul@58 | 279 | self._merge_terms() |
paul@58 | 280 | self._merge_fields() |
paul@44 | 281 | |
paul@73 | 282 | def _merge_dictionaries(self, get_partitions, rename_files, remove_files, get_reader, get_writer, get_merger, intervals): |
paul@73 | 283 | |
paul@73 | 284 | "Merge term or field dictionaries." |
paul@44 | 285 | |
paul@73 | 286 | partitions = get_partitions() |
paul@73 | 287 | |
paul@73 | 288 | # Ensure the correct labelling of a single partition. |
paul@44 | 289 | |
paul@73 | 290 | if len(partitions) == 1: |
paul@73 | 291 | partition = list(partitions)[0] |
paul@73 | 292 | if partition != "merged": |
paul@73 | 293 | rename_files(self.pathname, partition, "merged") |
paul@73 | 294 | return |
paul@44 | 295 | |
paul@73 | 296 | # Merge the partitions. |
paul@73 | 297 | |
paul@73 | 298 | old_merged_counter = 0 |
paul@44 | 299 | |
paul@73 | 300 | while len(partitions) > 1: |
paul@44 | 301 | |
paul@44 | 302 | if "merged" in partitions: |
paul@73 | 303 | rename_files(self.pathname, "merged", "old-merged-%d" % old_merged_counter) |
paul@44 | 304 | partitions.remove("merged") |
paul@73 | 305 | partitions.add("old-merged-%d" % old_merged_counter) |
paul@73 | 306 | old_merged_counter += 1 |
paul@73 | 307 | |
paul@73 | 308 | # Process only a certain number at once, avoiding resource limits. |
paul@73 | 309 | |
paul@73 | 310 | active_partitions = list(islice(partitions, self.open_partitions)) |
paul@44 | 311 | |
paul@73 | 312 | readers = [] |
paul@73 | 313 | for partition in active_partitions: |
paul@73 | 314 | readers.append(get_reader(self.pathname, partition)) |
paul@73 | 315 | |
paul@73 | 316 | # Write directly to a dictionary. |
paul@73 | 317 | |
paul@73 | 318 | writer = get_writer(self.pathname, "merged", *intervals) |
paul@73 | 319 | merger = get_merger(writer, readers) |
paul@44 | 320 | merger.merge() |
paul@44 | 321 | merger.close() |
paul@44 | 322 | |
paul@44 | 323 | # Remove old files. |
paul@44 | 324 | |
paul@73 | 325 | for partition in active_partitions: |
paul@73 | 326 | remove_files(self.pathname, partition) |
paul@73 | 327 | |
paul@73 | 328 | # Acquire the partitions to check their number again. |
paul@73 | 329 | |
paul@73 | 330 | partitions = get_partitions() |
paul@44 | 331 | |
paul@73 | 332 | def _merge_terms(self): |
paul@73 | 333 | |
paul@73 | 334 | "Merge term dictionaries." |
paul@73 | 335 | |
paul@73 | 336 | self._merge_dictionaries(self.get_term_partitions, rename_term_files, |
paul@73 | 337 | remove_term_files, get_term_reader, get_term_writer, |
paul@73 | 338 | TermDictionaryMerger, [self.interval, self.doc_interval]) |
paul@44 | 339 | |
paul@64 | 340 | def _merge_fields(self): |
paul@44 | 341 | |
paul@64 | 342 | "Merge field dictionaries." |
paul@44 | 343 | |
paul@73 | 344 | self._merge_dictionaries(self.get_field_partitions, rename_field_files, |
paul@73 | 345 | remove_field_files, get_field_reader, get_field_writer, |
paul@73 | 346 | FieldDictionaryMerger, [self.field_interval]) |
paul@44 | 347 | |
paul@58 | 348 | def update(self, other_indexes): |
paul@58 | 349 | |
paul@58 | 350 | "Copy the content of the 'other_indexes' into this index and merge." |
paul@58 | 351 | |
paul@59 | 352 | self._ensure_directory() |
paul@59 | 353 | |
paul@58 | 354 | for i, index in enumerate(other_indexes): |
paul@58 | 355 | for partition in index.get_term_partitions(): |
paul@58 | 356 | copy_term_files(index.pathname, partition, self.pathname, "-added-%d" % i) |
paul@58 | 357 | for partition in index.get_field_partitions(): |
paul@58 | 358 | copy_field_files(index.pathname, partition, self.pathname, "-added-%d" % i) |
paul@58 | 359 | |
paul@58 | 360 | self.merge() |
paul@58 | 361 | |
paul@44 | 362 | def close(self): |
paul@44 | 363 | if self.reader is not None: |
paul@44 | 364 | self.reader.close() |
paul@44 | 365 | self.reader = None |
paul@44 | 366 | if self.writer is not None: |
paul@44 | 367 | self.writer.close() |
paul@44 | 368 | self.writer = None |
paul@44 | 369 | |
paul@44 | 370 | # vim: tabstop=4 expandtab shiftwidth=4 |