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