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