1 #!/usr/bin/env python 2 3 """ 4 Simple indexing of sorted files. 5 6 Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 20 Motivations 21 ----------- 22 23 When searching files, the cost to scan an entire file typically exceeds the 24 cost to navigate to the appropriate region and to scan a smaller portion of the 25 file, especially for large files. At the same time, exotic data structures 26 encouraging multiple seeks and reads are likely to waste time compared to just 27 performing a single read operation, even if that operation involves a larger 28 quantity of data, at least for storage with hard disk access characteristics. 29 """ 30 31 from simplex.readers import * 32 import bisect 33 34 def make_index(reader, accessor, interval): 35 36 """ 37 Index a resource whose 'reader' provides records and whose 'accessor' can 38 yield the key for such records, creating an index entry for a record after a 39 given number of records, defined by 'interval', have been read since the 40 last entry was produced. 41 """ 42 43 l = [] 44 pos = 0 45 46 current_key = None 47 start_pos = 0 48 49 for i, record in enumerate(reader): 50 key = accessor.get_key(record) 51 52 # Where duplicate keys are permitted, the first record employing the key 53 # must be available as an index entry. Otherwise, records preceding the 54 # one referenced by the entry may have the same key and be missed when 55 # seeking using the index. 56 57 if key != current_key: 58 current_key = key 59 start_pos = pos 60 61 if i % interval == 0: 62 l.append((current_key, start_pos)) 63 64 pos += len(record) 65 66 return l 67 68 def find_with_index(reader, accessor, l, term): 69 70 """ 71 Find in the resource whose 'reader' provides records and whose 'accessor' 72 can yield the key for such records, using the given index list 'l', the 73 given 'term', returning a record employing the term or None if no such 74 record was found. 75 """ 76 77 i = bisect.bisect_left(l, (term, None)) 78 79 try: 80 found, pos = l[i] 81 except IndexError: 82 return None 83 84 # Since the index is more coarse than the underlying file, the bisect left 85 # operation will most likely point to an index entry for later records than 86 # the desired one. 87 88 if found > term: 89 i = max(0, i - 1) 90 found, pos = l[i] 91 92 reader.seek(pos) 93 return find_in_file(reader, accessor, term) 94 95 def find_in_file(reader, accessor, term): 96 97 """ 98 Find in the resource whose 'reader' provides records and whose 'accessor' 99 can yield the key for such records, the given 'term', returning a record 100 employing the term or None if no such record was found. 101 """ 102 103 for record in reader: 104 key = accessor.get_key(record) 105 if term == key: 106 return record 107 108 # Short-circuit failed searches. 109 110 elif term < key: 111 return None 112 113 return None 114 115 def groups(l, length): 116 117 "Split 'l' into groups of the given 'length'." 118 119 if length <= 0: 120 raise ValueError, "Groups must be greater than zero." 121 122 i = 0 123 g = [] 124 125 while i < len(l): 126 g.append(l[i:i+length]) 127 i += length 128 129 return g 130 131 # vim: tabstop=4 expandtab shiftwidth=4