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 import bisect 32 33 class TextFile: 34 35 "A wrapper around text files." 36 37 def __init__(self, f, keys=None, delimiter=None): 38 self.f = f 39 self.keys = keys or [0] 40 self.delimiter = delimiter 41 42 def seek(self, pos): 43 self.f.seek(pos) 44 45 def get_records(self): 46 return self.f.xreadlines() 47 48 def get_key(self, record): 49 values = record.split(self.delimiter) 50 return [values[key] for key in self.keys] 51 52 def make_index(reader, accessor, interval): 53 54 """ 55 Index a resource whose 'reader' provides records and whose 'accessor' can 56 yield the key for such records, creating an index entry for a record after a 57 given number of records, defined by 'interval', have been read since the 58 last entry was produced. 59 """ 60 61 l = [] 62 pos = 0 63 64 current_key = None 65 start_pos = 0 66 67 for i, record in enumerate(reader.get_records()): 68 key = accessor.get_key(record) 69 70 # Where duplicate keys are permitted, the first record employing the key 71 # must be available as an index entry. Otherwise, records preceding the 72 # one referenced by the entry may have the same key and be missed when 73 # seeking using the index. 74 75 if key != current_key: 76 current_key = key 77 start_pos = pos 78 79 if i % interval == 0: 80 l.append((current_key, start_pos)) 81 82 pos += len(record) 83 84 return l 85 86 def find_with_index(reader, accessor, l, term): 87 88 """ 89 Find in the resource whose 'reader' provides records and whose 'accessor' 90 can yield the key for such records, using the given index list 'l', the 91 given 'term', returning a record employing the term or None if no such 92 record was found. 93 """ 94 95 i = bisect.bisect_left(l, (term, None)) 96 97 try: 98 found, pos = l[i] 99 except IndexError: 100 return None 101 102 # Since the index is more coarse than the underlying file, the bisect left 103 # operation will most likely point to an index entry for later records than 104 # the desired one. 105 106 if found > term: 107 i = max(0, i - 1) 108 found, pos = l[i] 109 110 reader.seek(pos) 111 return find_in_file(reader, accessor, term) 112 113 def find_in_file(reader, accessor, term): 114 115 """ 116 Find in the resource whose 'reader' provides records and whose 'accessor' 117 can yield the key for such records, the given 'term', returning a record 118 employing the term or None if no such record was found. 119 """ 120 121 for record in reader.get_records(): 122 if term == accessor.get_key(record): 123 return record 124 125 return None 126 127 def groups(l, length): 128 129 "Split 'l' into groups of the given 'length'." 130 131 if length <= 0: 132 raise ValueError, "Groups must be greater than zero." 133 134 i = 0 135 g = [] 136 137 while i < len(l): 138 g.append(l[i:i+length]) 139 i += length 140 141 return g 142 143 # vim: tabstop=4 expandtab shiftwidth=4