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