paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Specific classes for storing position information. |
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.files import * |
paul@56 | 22 | from iixr.data import vint, vint_to_array |
paul@56 | 23 | from array import array |
paul@44 | 24 | |
paul@44 | 25 | class PositionWriter(FileWriter): |
paul@44 | 26 | |
paul@44 | 27 | "Writing position information to files." |
paul@44 | 28 | |
paul@44 | 29 | def reset(self): |
paul@44 | 30 | self.last_docnum = 0 |
paul@44 | 31 | |
paul@44 | 32 | def write_positions(self, docnum, positions): |
paul@44 | 33 | |
paul@44 | 34 | """ |
paul@44 | 35 | Write for the document 'docnum' the given 'positions'. |
paul@44 | 36 | """ |
paul@44 | 37 | |
paul@44 | 38 | if docnum < self.last_docnum: |
paul@44 | 39 | raise ValueError, "Document number %r is less than previous number %r." % (docnum, self.last_docnum) |
paul@44 | 40 | |
paul@44 | 41 | # Make sure that the positions are sorted. |
paul@44 | 42 | |
paul@44 | 43 | positions.sort() |
paul@44 | 44 | |
paul@52 | 45 | # Write the document number delta. |
paul@52 | 46 | # Write the number of positions. |
paul@52 | 47 | |
paul@56 | 48 | output = array('B') |
paul@56 | 49 | vint_to_array(docnum - self.last_docnum, output) |
paul@56 | 50 | vint_to_array(len(positions), output) |
paul@52 | 51 | |
paul@44 | 52 | # Write the position deltas. |
paul@44 | 53 | |
paul@44 | 54 | last = 0 |
paul@44 | 55 | |
paul@44 | 56 | for position in positions: |
paul@56 | 57 | vint_to_array(position - last, output) |
paul@44 | 58 | last = position |
paul@44 | 59 | |
paul@56 | 60 | output.tofile(self.f) |
paul@44 | 61 | |
paul@44 | 62 | self.last_docnum = docnum |
paul@44 | 63 | |
paul@44 | 64 | class PositionOpener(FileOpener): |
paul@44 | 65 | |
paul@44 | 66 | "Reading position information from files." |
paul@44 | 67 | |
paul@44 | 68 | def read_term_positions(self, offset, count): |
paul@44 | 69 | |
paul@44 | 70 | """ |
paul@44 | 71 | Read all positions from 'offset', seeking to that position in the file |
paul@44 | 72 | before reading. The number of documents available for reading is limited |
paul@44 | 73 | to 'count'. |
paul@44 | 74 | """ |
paul@44 | 75 | |
paul@44 | 76 | # Duplicate the file handle. |
paul@44 | 77 | |
paul@44 | 78 | f = self.open("rb") |
paul@44 | 79 | return PositionIterator(f, offset, count) |
paul@44 | 80 | |
paul@44 | 81 | class PositionIndexWriter(FileWriter): |
paul@44 | 82 | |
paul@44 | 83 | "Writing position index information to files." |
paul@44 | 84 | |
paul@44 | 85 | def reset(self): |
paul@44 | 86 | self.last_docnum = 0 |
paul@44 | 87 | self.last_pos_offset = 0 |
paul@44 | 88 | |
paul@44 | 89 | def write_positions(self, docnum, pos_offset, count): |
paul@44 | 90 | |
paul@44 | 91 | """ |
paul@44 | 92 | Write the given 'docnum, 'pos_offset' and document 'count' to the |
paul@44 | 93 | position index file. |
paul@44 | 94 | """ |
paul@44 | 95 | |
paul@44 | 96 | # Write the document number delta. |
paul@44 | 97 | # Write the position file offset delta. |
paul@44 | 98 | # Write the document count. |
paul@44 | 99 | |
paul@52 | 100 | output = [docnum - self.last_docnum, pos_offset - self.last_pos_offset, count] |
paul@44 | 101 | |
paul@44 | 102 | # Actually write the data. |
paul@44 | 103 | |
paul@52 | 104 | self.f.write("".join([vint(x) for x in output])) |
paul@44 | 105 | |
paul@52 | 106 | self.last_pos_offset = pos_offset |
paul@52 | 107 | self.last_docnum = docnum |
paul@44 | 108 | |
paul@44 | 109 | class PositionIndexOpener(FileOpener): |
paul@44 | 110 | |
paul@44 | 111 | "Reading position index information from files." |
paul@44 | 112 | |
paul@44 | 113 | def read_term_positions(self, offset, doc_frequency): |
paul@44 | 114 | |
paul@44 | 115 | """ |
paul@44 | 116 | Read all positions from 'offset', seeking to that position in the file |
paul@44 | 117 | before reading. The number of documents available for reading is limited |
paul@44 | 118 | to 'doc_frequency'. |
paul@44 | 119 | """ |
paul@44 | 120 | |
paul@44 | 121 | # Duplicate the file handle. |
paul@44 | 122 | |
paul@44 | 123 | f = self.open("rb") |
paul@44 | 124 | return PositionIndexIterator(f, offset, doc_frequency) |
paul@44 | 125 | |
paul@44 | 126 | # Iterators for position-related files. |
paul@44 | 127 | |
paul@44 | 128 | class IteratorBase: |
paul@44 | 129 | |
paul@44 | 130 | def __init__(self, count): |
paul@44 | 131 | self.replenish(count) |
paul@44 | 132 | |
paul@44 | 133 | def replenish(self, count): |
paul@44 | 134 | self.count = count |
paul@44 | 135 | self.read_documents = 0 |
paul@44 | 136 | |
paul@44 | 137 | def __len__(self): |
paul@44 | 138 | return self.count |
paul@44 | 139 | |
paul@44 | 140 | def sort(self): |
paul@44 | 141 | pass # Stored document positions are already sorted. |
paul@44 | 142 | |
paul@44 | 143 | def __iter__(self): |
paul@44 | 144 | return self |
paul@44 | 145 | |
paul@44 | 146 | class PositionIterator(FileReader, IteratorBase): |
paul@44 | 147 | |
paul@44 | 148 | "Iterating over document positions." |
paul@44 | 149 | |
paul@44 | 150 | def __init__(self, f, offset, count): |
paul@44 | 151 | FileReader.__init__(self, f) |
paul@44 | 152 | IteratorBase.__init__(self, count) |
paul@52 | 153 | self.f.seek(offset) |
paul@44 | 154 | |
paul@44 | 155 | def reset(self): |
paul@44 | 156 | self.last_docnum = 0 |
paul@44 | 157 | |
paul@44 | 158 | def read_positions(self): |
paul@44 | 159 | |
paul@44 | 160 | "Read positions, returning a document number and a list of positions." |
paul@44 | 161 | |
paul@44 | 162 | # Read the document number delta and add it to the last number. |
paul@44 | 163 | |
paul@44 | 164 | self.last_docnum += self.read_number() |
paul@44 | 165 | |
paul@44 | 166 | # Read the number of positions. |
paul@44 | 167 | |
paul@44 | 168 | npositions = self.read_number() |
paul@44 | 169 | |
paul@44 | 170 | # Read the position deltas, adding each previous position to get the |
paul@44 | 171 | # appropriate collection of absolute positions. |
paul@44 | 172 | |
paul@44 | 173 | i = 0 |
paul@44 | 174 | last = 0 |
paul@44 | 175 | positions = [] |
paul@44 | 176 | |
paul@44 | 177 | while i < npositions: |
paul@44 | 178 | last += self.read_number() |
paul@44 | 179 | positions.append(last) |
paul@44 | 180 | i += 1 |
paul@44 | 181 | |
paul@44 | 182 | return self.last_docnum, positions |
paul@44 | 183 | |
paul@44 | 184 | def next(self): |
paul@44 | 185 | |
paul@44 | 186 | "Read positions for a single document." |
paul@44 | 187 | |
paul@44 | 188 | if self.read_documents < self.count: |
paul@44 | 189 | self.read_documents += 1 |
paul@44 | 190 | return self.read_positions() |
paul@44 | 191 | else: |
paul@44 | 192 | raise StopIteration |
paul@44 | 193 | |
paul@44 | 194 | class PositionIndexIterator(FileReader, IteratorBase): |
paul@44 | 195 | |
paul@44 | 196 | "Iterating over document positions." |
paul@44 | 197 | |
paul@44 | 198 | def __init__(self, f, offset, count): |
paul@44 | 199 | FileReader.__init__(self, f) |
paul@44 | 200 | IteratorBase.__init__(self, count) |
paul@52 | 201 | self.f.seek(offset) |
paul@44 | 202 | |
paul@44 | 203 | def reset(self): |
paul@44 | 204 | self.last_docnum = 0 |
paul@44 | 205 | self.last_pos_offset = 0 |
paul@47 | 206 | self.section_count = 0 |
paul@44 | 207 | |
paul@44 | 208 | def read_positions(self): |
paul@44 | 209 | |
paul@44 | 210 | """ |
paul@44 | 211 | Read a document number, a position file offset for the position index |
paul@44 | 212 | file, and the number of documents in a section of that file. |
paul@44 | 213 | """ |
paul@44 | 214 | |
paul@44 | 215 | # Read the document number delta. |
paul@44 | 216 | |
paul@44 | 217 | self.last_docnum += self.read_number() |
paul@44 | 218 | |
paul@44 | 219 | # Read the offset delta. |
paul@44 | 220 | |
paul@44 | 221 | self.last_pos_offset += self.read_number() |
paul@44 | 222 | |
paul@44 | 223 | # Read the document count. |
paul@44 | 224 | |
paul@44 | 225 | count = self.read_number() |
paul@44 | 226 | |
paul@44 | 227 | return self.last_docnum, self.last_pos_offset, count |
paul@44 | 228 | |
paul@44 | 229 | def next(self): |
paul@44 | 230 | |
paul@44 | 231 | "Read positions for a single document." |
paul@44 | 232 | |
paul@44 | 233 | self.read_documents += self.section_count |
paul@44 | 234 | if self.read_documents < self.count: |
paul@44 | 235 | docnum, pos_offset, self.section_count = t = self.read_positions() |
paul@44 | 236 | return t |
paul@44 | 237 | else: |
paul@47 | 238 | assert self.read_documents == self.count |
paul@44 | 239 | raise StopIteration |
paul@44 | 240 | |
paul@44 | 241 | class PositionDictionaryWriter: |
paul@44 | 242 | |
paul@44 | 243 | "Writing position dictionaries." |
paul@44 | 244 | |
paul@44 | 245 | def __init__(self, position_writer, position_index_writer, interval): |
paul@44 | 246 | self.position_writer = position_writer |
paul@44 | 247 | self.position_index_writer = position_index_writer |
paul@44 | 248 | self.interval = interval |
paul@44 | 249 | |
paul@44 | 250 | def write_term_positions(self, doc_positions): |
paul@44 | 251 | |
paul@44 | 252 | """ |
paul@44 | 253 | Write all 'doc_positions' - a collection of tuples of the form (document |
paul@44 | 254 | number, position list) - to the file. |
paul@44 | 255 | |
paul@44 | 256 | Add some records to the index, making dictionary entries. |
paul@44 | 257 | |
paul@44 | 258 | Return a tuple containing the offset of the written data, the frequency |
paul@44 | 259 | (number of positions), and document frequency (number of documents) for |
paul@44 | 260 | the term involved. |
paul@44 | 261 | """ |
paul@44 | 262 | |
paul@44 | 263 | # Reset the writers. |
paul@44 | 264 | |
paul@44 | 265 | self.position_writer.reset() |
paul@44 | 266 | self.position_index_writer.reset() |
paul@44 | 267 | |
paul@55 | 268 | # Remember the first index entry offset. |
paul@55 | 269 | |
paul@55 | 270 | index_offset = self.position_index_writer.f.tell() |
paul@44 | 271 | |
paul@44 | 272 | # Write the positions. |
paul@44 | 273 | |
paul@44 | 274 | frequency = 0 |
paul@44 | 275 | count = 0 |
paul@44 | 276 | |
paul@55 | 277 | if doc_positions: |
paul@44 | 278 | |
paul@44 | 279 | # Retain the first record offset for a subsequent index entry. |
paul@44 | 280 | |
paul@55 | 281 | first_offset = self.position_writer.f.tell() |
paul@55 | 282 | first_docnum = None |
paul@44 | 283 | |
paul@55 | 284 | doc_positions.sort() |
paul@44 | 285 | |
paul@55 | 286 | for docnum, positions in doc_positions: |
paul@55 | 287 | if first_docnum is None: |
paul@55 | 288 | first_docnum = docnum |
paul@44 | 289 | |
paul@55 | 290 | self.position_writer.write_positions(docnum, positions) |
paul@44 | 291 | |
paul@55 | 292 | frequency += len(positions) |
paul@55 | 293 | count += 1 |
paul@55 | 294 | |
paul@55 | 295 | # Every {interval} entries, write an index entry. |
paul@44 | 296 | |
paul@55 | 297 | if count % self.interval == 0: |
paul@44 | 298 | |
paul@55 | 299 | self.position_index_writer.write_positions(first_docnum, first_offset, self.interval) |
paul@44 | 300 | |
paul@55 | 301 | first_offset = self.position_writer.f.tell() |
paul@55 | 302 | first_docnum = None |
paul@44 | 303 | |
paul@55 | 304 | # Reset the position writer so that position readers accessing |
paul@55 | 305 | # a section start with the correct document number. |
paul@55 | 306 | |
paul@55 | 307 | self.position_writer.reset() |
paul@44 | 308 | |
paul@55 | 309 | # Finish writing an index entry for the remaining documents. |
paul@44 | 310 | |
paul@55 | 311 | else: |
paul@55 | 312 | if first_docnum is not None: |
paul@55 | 313 | self.position_index_writer.write_positions(first_docnum, first_offset, count % self.interval) |
paul@44 | 314 | |
paul@44 | 315 | return index_offset, frequency, count |
paul@44 | 316 | |
paul@44 | 317 | def close(self): |
paul@44 | 318 | self.position_writer.close() |
paul@44 | 319 | self.position_index_writer.close() |
paul@44 | 320 | |
paul@44 | 321 | class PositionDictionaryReader: |
paul@44 | 322 | |
paul@44 | 323 | "Reading position dictionaries." |
paul@44 | 324 | |
paul@44 | 325 | def __init__(self, position_opener, position_index_opener): |
paul@44 | 326 | self.position_opener = position_opener |
paul@44 | 327 | self.position_index_opener = position_index_opener |
paul@44 | 328 | |
paul@44 | 329 | def read_term_positions(self, offset, doc_frequency): |
paul@44 | 330 | |
paul@44 | 331 | """ |
paul@44 | 332 | Return an iterator for dictionary entries starting at 'offset' with the |
paul@44 | 333 | given 'doc_frequency'. |
paul@44 | 334 | """ |
paul@44 | 335 | |
paul@44 | 336 | return PositionDictionaryIterator(self.position_opener, |
paul@44 | 337 | self.position_index_opener, offset, doc_frequency) |
paul@44 | 338 | |
paul@44 | 339 | def close(self): |
paul@44 | 340 | pass |
paul@44 | 341 | |
paul@44 | 342 | class PositionDictionaryIterator: |
paul@44 | 343 | |
paul@44 | 344 | "Iteration over position dictionary entries." |
paul@44 | 345 | |
paul@44 | 346 | def __init__(self, position_opener, position_index_opener, offset, doc_frequency): |
paul@44 | 347 | self.position_opener = position_opener |
paul@47 | 348 | self.position_index_opener = position_index_opener |
paul@44 | 349 | self.doc_frequency = doc_frequency |
paul@47 | 350 | |
paul@47 | 351 | self.index_iterator = None |
paul@44 | 352 | self.iterator = None |
paul@44 | 353 | |
paul@47 | 354 | # Initialise the iterators. |
paul@47 | 355 | |
paul@47 | 356 | self.reset(offset, doc_frequency) |
paul@47 | 357 | |
paul@47 | 358 | def reset(self, offset, doc_frequency): |
paul@47 | 359 | |
paul@44 | 360 | # Remember the last values. |
paul@44 | 361 | |
paul@44 | 362 | self.found_docnum, self.found_positions = None, None |
paul@44 | 363 | |
paul@47 | 364 | # Attempt to reuse the index iterator. |
paul@47 | 365 | |
paul@47 | 366 | if self.index_iterator is not None: |
paul@52 | 367 | ii = self.index_iterator |
paul@52 | 368 | ii.replenish(doc_frequency) |
paul@52 | 369 | ii.f.seek(offset) |
paul@52 | 370 | ii.reset() |
paul@47 | 371 | |
paul@47 | 372 | # Or make a new index iterator. |
paul@47 | 373 | |
paul@47 | 374 | else: |
paul@47 | 375 | self.index_iterator = self.position_index_opener.read_term_positions(offset, doc_frequency) |
paul@47 | 376 | |
paul@44 | 377 | # Maintain state for the next index entry, if read. |
paul@44 | 378 | |
paul@44 | 379 | self.next_docnum, self.next_pos_offset, self.next_section_count = None, None, None |
paul@44 | 380 | |
paul@44 | 381 | # Initialise the current index entry and current position file iterator. |
paul@44 | 382 | |
paul@44 | 383 | self._next_section() |
paul@44 | 384 | self._init_section() |
paul@44 | 385 | |
paul@44 | 386 | # Sequence methods. |
paul@44 | 387 | |
paul@44 | 388 | def __len__(self): |
paul@44 | 389 | return self.doc_frequency |
paul@44 | 390 | |
paul@44 | 391 | def sort(self): |
paul@44 | 392 | pass |
paul@44 | 393 | |
paul@44 | 394 | # Iterator methods. |
paul@44 | 395 | |
paul@44 | 396 | def __iter__(self): |
paul@44 | 397 | return self |
paul@44 | 398 | |
paul@44 | 399 | def next(self): |
paul@44 | 400 | |
paul@44 | 401 | """ |
paul@44 | 402 | Attempt to get the next document record from the section in the |
paul@44 | 403 | positions file. |
paul@44 | 404 | """ |
paul@44 | 405 | |
paul@44 | 406 | # Return any visited but unrequested record. |
paul@44 | 407 | |
paul@44 | 408 | if self.found_docnum is not None: |
paul@44 | 409 | t = self.found_docnum, self.found_positions |
paul@44 | 410 | self.found_docnum, self.found_positions = None, None |
paul@44 | 411 | return t |
paul@44 | 412 | |
paul@44 | 413 | # Or search for the next record. |
paul@44 | 414 | |
paul@44 | 415 | while 1: |
paul@44 | 416 | |
paul@44 | 417 | # Either return the next record. |
paul@44 | 418 | |
paul@44 | 419 | try: |
paul@44 | 420 | return self.iterator.next() |
paul@44 | 421 | |
paul@44 | 422 | # Or, where a section is finished, get the next section and try again. |
paul@44 | 423 | |
paul@44 | 424 | except StopIteration: |
paul@44 | 425 | |
paul@44 | 426 | # Where a section follows, update the index iterator, but keep |
paul@44 | 427 | # reading using the same file iterator (since the data should |
paul@44 | 428 | # just follow on from the last section). |
paul@44 | 429 | |
paul@44 | 430 | self._next_section() |
paul@44 | 431 | self.iterator.replenish(self.section_count) |
paul@44 | 432 | |
paul@44 | 433 | # Reset the state of the iterator to make sure that document |
paul@44 | 434 | # numbers are correct. |
paul@44 | 435 | |
paul@44 | 436 | self.iterator.reset() |
paul@44 | 437 | |
paul@44 | 438 | def from_document(self, docnum): |
paul@44 | 439 | |
paul@44 | 440 | """ |
paul@44 | 441 | Attempt to navigate to a positions entry for the given 'docnum', |
paul@44 | 442 | returning the positions for 'docnum', or None otherwise. |
paul@44 | 443 | """ |
paul@44 | 444 | |
paul@44 | 445 | # Return any unrequested document positions. |
paul@44 | 446 | |
paul@44 | 447 | if docnum == self.found_docnum: |
paul@44 | 448 | return self.found_positions |
paul@44 | 449 | |
paul@44 | 450 | # Read ahead in the index until the next entry refers to a document |
paul@44 | 451 | # later than the desired document. |
paul@44 | 452 | |
paul@44 | 453 | try: |
paul@44 | 454 | if self.next_docnum is None: |
paul@44 | 455 | self.next_docnum, self.next_pos_offset, self.next_section_count = self.index_iterator.next() |
paul@44 | 456 | |
paul@44 | 457 | # Read until the next entry is after the desired document number, |
paul@44 | 458 | # or until the end of the results. |
paul@44 | 459 | |
paul@44 | 460 | while self.next_docnum <= docnum: |
paul@44 | 461 | self._next_read_section() |
paul@44 | 462 | if self.docnum < docnum: |
paul@44 | 463 | self.next_docnum, self.next_pos_offset, self.next_section_count = self.index_iterator.next() |
paul@44 | 464 | else: |
paul@44 | 465 | break |
paul@44 | 466 | |
paul@44 | 467 | except StopIteration: |
paul@44 | 468 | pass |
paul@44 | 469 | |
paul@44 | 470 | # Navigate in the position file to the document. |
paul@44 | 471 | |
paul@44 | 472 | self._init_section() |
paul@44 | 473 | |
paul@44 | 474 | try: |
paul@44 | 475 | while 1: |
paul@44 | 476 | found_docnum, found_positions = self.iterator.next() |
paul@44 | 477 | |
paul@44 | 478 | # Return the desired document positions or None (retaining the |
paul@44 | 479 | # positions for the document immediately after). |
paul@44 | 480 | |
paul@44 | 481 | if docnum == found_docnum: |
paul@44 | 482 | return found_positions |
paul@44 | 483 | elif docnum < found_docnum: |
paul@44 | 484 | self.found_docnum, self.found_positions = found_docnum, found_positions |
paul@44 | 485 | return None |
paul@44 | 486 | |
paul@44 | 487 | except StopIteration: |
paul@44 | 488 | return None |
paul@44 | 489 | |
paul@44 | 490 | # Internal methods. |
paul@44 | 491 | |
paul@44 | 492 | def _next_section(self): |
paul@44 | 493 | |
paul@44 | 494 | "Attempt to get the next section in the index." |
paul@44 | 495 | |
paul@44 | 496 | if self.next_docnum is None: |
paul@44 | 497 | self.docnum, self.pos_offset, self.section_count = self.index_iterator.next() |
paul@44 | 498 | else: |
paul@44 | 499 | self._next_read_section() |
paul@44 | 500 | |
paul@44 | 501 | def _next_read_section(self): |
paul@44 | 502 | |
paul@44 | 503 | """ |
paul@44 | 504 | Make the next index entry the current one without reading from the |
paul@44 | 505 | index. |
paul@44 | 506 | """ |
paul@44 | 507 | |
paul@44 | 508 | self.docnum, self.pos_offset, self.section_count = self.next_docnum, self.next_pos_offset, self.next_section_count |
paul@44 | 509 | self.next_docnum, self.next_pos_offset, self.next_section_count = None, None, None |
paul@44 | 510 | |
paul@44 | 511 | def _init_section(self): |
paul@44 | 512 | |
paul@44 | 513 | "Initialise the iterator for the section in the position file." |
paul@44 | 514 | |
paul@47 | 515 | # Attempt to reuse any correctly positioned iterator. |
paul@47 | 516 | |
paul@44 | 517 | if self.iterator is not None: |
paul@52 | 518 | i = self.iterator |
paul@52 | 519 | i.replenish(self.section_count) |
paul@52 | 520 | i.f.seek(self.pos_offset) |
paul@52 | 521 | i.reset() |
paul@47 | 522 | |
paul@47 | 523 | # Otherwise, obtain a new iterator. |
paul@47 | 524 | |
paul@47 | 525 | else: |
paul@47 | 526 | self.iterator = self.position_opener.read_term_positions(self.pos_offset, self.section_count) |
paul@44 | 527 | |
paul@44 | 528 | def close(self): |
paul@44 | 529 | if self.iterator is not None: |
paul@44 | 530 | self.iterator.close() |
paul@44 | 531 | self.iterator = None |
paul@44 | 532 | if self.index_iterator is not None: |
paul@44 | 533 | self.index_iterator.close() |
paul@44 | 534 | self.index_iterator = None |
paul@44 | 535 | |
paul@47 | 536 | class ResetPositionDictionaryIterator: |
paul@47 | 537 | |
paul@47 | 538 | """ |
paul@47 | 539 | A helper class which permits the reuse of iterators without modifying their |
paul@47 | 540 | state. |
paul@47 | 541 | """ |
paul@47 | 542 | |
paul@47 | 543 | def __init__(self, iterator, offset, doc_frequency): |
paul@47 | 544 | self.iterator = iterator |
paul@47 | 545 | self.offset = offset |
paul@47 | 546 | self.doc_frequency = doc_frequency |
paul@47 | 547 | |
paul@47 | 548 | def __iter__(self): |
paul@47 | 549 | self.iterator.reset(self.offset, self.doc_frequency) |
paul@47 | 550 | return iter(self.iterator) |
paul@47 | 551 | |
paul@44 | 552 | # vim: tabstop=4 expandtab shiftwidth=4 |