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 for i, record in enumerate(f.get_records()): 69 if i % interval == 0: 70 l.append((f.get_key(record), pos)) 71 pos += len(record) 72 73 return l 74 75 def find_with_index(f, l, term): 76 77 """ 78 Find in file 'f', using the given index list 'l', the given 'term', 79 returning a record employing the term or None if no such record was found. 80 """ 81 82 i = bisect.bisect_left(l, (term, None)) 83 found, pos = l[i] 84 85 # Since the index is more coarse than the underlying file, the bisect left 86 # operation will most likely point to an index entry for later records than 87 # the desired one. 88 89 if found > term: 90 i = max(0, i - 1) 91 found, pos = l[i] 92 93 f.seek(pos) 94 return find_in_file(f, term) 95 96 def find_in_file(f, term): 97 98 """ 99 Find in file 'f' the given 'term', returning a record employing the term or 100 None if no such record was found. 101 """ 102 103 for record in f.get_records(): 104 if term == f.get_key(record): 105 return record 106 107 return None 108 109 class Index: 110 111 "An index abstraction." 112 113 def __init__(self, entries, f): 114 self.entries = entries 115 self.f = f 116 117 def find(self, term): 118 return find_with_index(self.f, self.entries, term) 119 120 # vim: tabstop=4 expandtab shiftwidth=4