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