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