paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | A simple (and sane) text indexing library. |
paul@1 | 5 | |
paul@1 | 6 | Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> |
paul@1 | 7 | |
paul@1 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1 | 11 | version. |
paul@1 | 12 | |
paul@1 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@1 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@1 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@1 | 16 | |
paul@1 | 17 | You should have received a copy of the GNU General Public License along |
paul@1 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 19 | """ |
paul@0 | 20 | |
paul@0 | 21 | # Foundation classes. |
paul@0 | 22 | |
paul@0 | 23 | class File: |
paul@0 | 24 | |
paul@0 | 25 | "A basic file abstraction." |
paul@0 | 26 | |
paul@0 | 27 | def __init__(self, f): |
paul@0 | 28 | self.f = f |
paul@0 | 29 | self.reset() |
paul@0 | 30 | |
paul@0 | 31 | def reset(self): |
paul@0 | 32 | pass |
paul@0 | 33 | |
paul@0 | 34 | def close(self): |
paul@0 | 35 | self.f.close() |
paul@0 | 36 | |
paul@0 | 37 | class FileWriter(File): |
paul@0 | 38 | |
paul@0 | 39 | "Writing basic data types to files." |
paul@0 | 40 | |
paul@0 | 41 | def write_number(self, number): |
paul@0 | 42 | |
paul@0 | 43 | "Write 'number' to the file using a variable length encoding." |
paul@0 | 44 | |
paul@0 | 45 | # Negative numbers are not supported. |
paul@0 | 46 | |
paul@0 | 47 | if number < 0: |
paul@0 | 48 | raise ValueError, "Number %r is negative." % number |
paul@0 | 49 | |
paul@0 | 50 | # Special case: one byte containing zero. |
paul@0 | 51 | |
paul@0 | 52 | elif number == 0: |
paul@0 | 53 | self.f.write(chr(1) + chr(0)) |
paul@0 | 54 | return |
paul@0 | 55 | |
paul@0 | 56 | # Write the number from least to most significant digits. |
paul@0 | 57 | |
paul@0 | 58 | nbytes = 0 |
paul@0 | 59 | bytes = [] |
paul@0 | 60 | |
paul@0 | 61 | while number != 0: |
paul@0 | 62 | lsd = number & 255 |
paul@0 | 63 | bytes.append(chr(lsd)) |
paul@0 | 64 | number = number >> 8 |
paul@0 | 65 | nbytes += 1 |
paul@0 | 66 | |
paul@0 | 67 | # Too large numbers are not supported. |
paul@0 | 68 | |
paul@0 | 69 | if nbytes > 255: |
paul@0 | 70 | raise ValueError, "Number %r is too large." % number |
paul@0 | 71 | |
paul@0 | 72 | bytes.insert(0, chr(nbytes)) |
paul@0 | 73 | record = "".join(bytes) |
paul@0 | 74 | self.f.write(record) |
paul@0 | 75 | |
paul@0 | 76 | class FileReader(File): |
paul@0 | 77 | |
paul@0 | 78 | "Reading basic data types from files." |
paul@0 | 79 | |
paul@0 | 80 | def read_number(self): |
paul@0 | 81 | |
paul@0 | 82 | "Read a number from the file." |
paul@0 | 83 | |
paul@0 | 84 | nbytes = ord(self.f.read(1)) |
paul@0 | 85 | |
paul@0 | 86 | # Read each byte, adding it to the number. |
paul@0 | 87 | |
paul@0 | 88 | bytes = self.f.read(nbytes) |
paul@0 | 89 | |
paul@0 | 90 | i = 0 |
paul@0 | 91 | shift = 0 |
paul@0 | 92 | number = 0 |
paul@0 | 93 | |
paul@0 | 94 | while i < nbytes: |
paul@0 | 95 | csd = ord(bytes[i]) |
paul@0 | 96 | number += (csd << shift) |
paul@0 | 97 | shift += 8 |
paul@0 | 98 | i += 1 |
paul@0 | 99 | |
paul@0 | 100 | return number |
paul@0 | 101 | |
paul@0 | 102 | # Specific classes. |
paul@0 | 103 | |
paul@0 | 104 | class PositionWriter(FileWriter): |
paul@0 | 105 | |
paul@0 | 106 | "Writing position information to files." |
paul@0 | 107 | |
paul@0 | 108 | def reset(self): |
paul@0 | 109 | self.last_docnum = 0 |
paul@0 | 110 | |
paul@0 | 111 | def write_positions(self, docnum, positions): |
paul@0 | 112 | |
paul@0 | 113 | "Write for the document 'docnum' the given 'positions'." |
paul@0 | 114 | |
paul@0 | 115 | if docnum < self.last_docnum: |
paul@0 | 116 | raise ValueError, "Document number %r is less than previous number %r." % (docnum, self.last_docnum) |
paul@0 | 117 | |
paul@0 | 118 | # Write the document number delta. |
paul@0 | 119 | |
paul@0 | 120 | self.write_number(docnum - self.last_docnum) |
paul@0 | 121 | |
paul@0 | 122 | # Write the number of positions. |
paul@0 | 123 | |
paul@0 | 124 | self.write_number(len(positions)) |
paul@0 | 125 | |
paul@0 | 126 | # Write the position deltas. |
paul@0 | 127 | |
paul@0 | 128 | last = 0 |
paul@0 | 129 | for position in positions: |
paul@0 | 130 | pos = position - last |
paul@0 | 131 | self.write_number(pos) |
paul@0 | 132 | last = position |
paul@0 | 133 | |
paul@0 | 134 | self.last_docnum = docnum |
paul@0 | 135 | |
paul@0 | 136 | def write_all_positions(self, doc_positions): |
paul@0 | 137 | |
paul@0 | 138 | """ |
paul@0 | 139 | Write all 'doc_positions' - a collection of tuples of the form (document |
paul@0 | 140 | number, position list) - to the file, returning the offset at which they |
paul@0 | 141 | were stored. |
paul@0 | 142 | """ |
paul@0 | 143 | |
paul@0 | 144 | # Reset the writer and record the current file offset. |
paul@0 | 145 | |
paul@0 | 146 | self.reset() |
paul@0 | 147 | offset = self.f.tell() |
paul@0 | 148 | |
paul@0 | 149 | # Write the number of documents. |
paul@0 | 150 | |
paul@0 | 151 | self.write_number(len(doc_positions)) |
paul@0 | 152 | |
paul@0 | 153 | # Write the positions. |
paul@0 | 154 | |
paul@0 | 155 | for docnum, positions in doc_positions: |
paul@0 | 156 | self.write_positions(docnum, positions) |
paul@0 | 157 | |
paul@0 | 158 | return offset |
paul@0 | 159 | |
paul@0 | 160 | class PositionReader(FileReader): |
paul@0 | 161 | |
paul@0 | 162 | "Reading position information from files." |
paul@0 | 163 | |
paul@0 | 164 | def reset(self): |
paul@0 | 165 | self.last_docnum = 0 |
paul@0 | 166 | |
paul@0 | 167 | def read_positions(self): |
paul@0 | 168 | |
paul@0 | 169 | "Read positions, returning a document number and a list of positions." |
paul@0 | 170 | |
paul@0 | 171 | # Read the document number delta and add it to the last number. |
paul@0 | 172 | |
paul@0 | 173 | self.last_docnum += self.read_number() |
paul@0 | 174 | |
paul@0 | 175 | # Read the number of positions. |
paul@0 | 176 | |
paul@0 | 177 | npositions = self.read_number() |
paul@0 | 178 | |
paul@0 | 179 | # Read the position deltas, adding each previous position to get the |
paul@0 | 180 | # appropriate collection of absolute positions. |
paul@0 | 181 | |
paul@0 | 182 | i = 0 |
paul@0 | 183 | last = 0 |
paul@0 | 184 | positions = [] |
paul@0 | 185 | |
paul@0 | 186 | while i < npositions: |
paul@0 | 187 | last += self.read_number() |
paul@0 | 188 | positions.append(last) |
paul@0 | 189 | i += 1 |
paul@0 | 190 | |
paul@0 | 191 | return self.last_docnum, positions |
paul@0 | 192 | |
paul@0 | 193 | def read_all_positions(self, offset): |
paul@0 | 194 | |
paul@0 | 195 | """ |
paul@0 | 196 | Read all positions from 'offset', seeking to that position in the file |
paul@0 | 197 | before reading. |
paul@0 | 198 | """ |
paul@0 | 199 | |
paul@0 | 200 | self.reset() |
paul@0 | 201 | self.f.seek(offset) |
paul@0 | 202 | |
paul@0 | 203 | # Read the number of documents. |
paul@0 | 204 | |
paul@0 | 205 | ndocuments = self.read_number() |
paul@0 | 206 | |
paul@0 | 207 | # Read all records. |
paul@0 | 208 | |
paul@0 | 209 | i = 0 |
paul@0 | 210 | doc_positions = [] |
paul@0 | 211 | |
paul@0 | 212 | while i < ndocuments: |
paul@0 | 213 | doc_positions.append(self.read_positions()) |
paul@0 | 214 | i += 1 |
paul@0 | 215 | |
paul@0 | 216 | return doc_positions |
paul@0 | 217 | |
paul@0 | 218 | # vim: tabstop=4 expandtab shiftwidth=4 |