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