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