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@2 | 21 | from os.path import commonprefix # to find common string prefixes |
paul@2 | 22 | |
paul@0 | 23 | # Foundation classes. |
paul@0 | 24 | |
paul@0 | 25 | class File: |
paul@0 | 26 | |
paul@0 | 27 | "A basic file abstraction." |
paul@0 | 28 | |
paul@0 | 29 | def __init__(self, f): |
paul@0 | 30 | self.f = f |
paul@0 | 31 | self.reset() |
paul@0 | 32 | |
paul@0 | 33 | def reset(self): |
paul@0 | 34 | pass |
paul@0 | 35 | |
paul@0 | 36 | def close(self): |
paul@0 | 37 | self.f.close() |
paul@0 | 38 | |
paul@0 | 39 | class FileWriter(File): |
paul@0 | 40 | |
paul@0 | 41 | "Writing basic data types to files." |
paul@0 | 42 | |
paul@0 | 43 | def write_number(self, number): |
paul@0 | 44 | |
paul@0 | 45 | "Write 'number' to the file using a variable length encoding." |
paul@0 | 46 | |
paul@0 | 47 | # Negative numbers are not supported. |
paul@0 | 48 | |
paul@0 | 49 | if number < 0: |
paul@0 | 50 | raise ValueError, "Number %r is negative." % number |
paul@0 | 51 | |
paul@0 | 52 | # Special case: one byte containing zero. |
paul@0 | 53 | |
paul@0 | 54 | elif number == 0: |
paul@0 | 55 | self.f.write(chr(1) + chr(0)) |
paul@0 | 56 | return |
paul@0 | 57 | |
paul@0 | 58 | # Write the number from least to most significant digits. |
paul@0 | 59 | |
paul@0 | 60 | nbytes = 0 |
paul@0 | 61 | bytes = [] |
paul@0 | 62 | |
paul@0 | 63 | while number != 0: |
paul@0 | 64 | lsd = number & 255 |
paul@0 | 65 | bytes.append(chr(lsd)) |
paul@0 | 66 | number = number >> 8 |
paul@0 | 67 | nbytes += 1 |
paul@0 | 68 | |
paul@0 | 69 | # Too large numbers are not supported. |
paul@0 | 70 | |
paul@0 | 71 | if nbytes > 255: |
paul@0 | 72 | raise ValueError, "Number %r is too large." % number |
paul@0 | 73 | |
paul@0 | 74 | bytes.insert(0, chr(nbytes)) |
paul@0 | 75 | record = "".join(bytes) |
paul@0 | 76 | self.f.write(record) |
paul@0 | 77 | |
paul@2 | 78 | def write_unsigned_byte(self, number): |
paul@2 | 79 | |
paul@2 | 80 | "Write 'number' to the file using a single byte." |
paul@2 | 81 | |
paul@2 | 82 | if not (0 <= number <= 255): |
paul@2 | 83 | raise ValueError, "Number %r is out of range." % number |
paul@2 | 84 | |
paul@2 | 85 | self.f.write(chr(number)) |
paul@2 | 86 | |
paul@2 | 87 | def write_string(self, s): |
paul@2 | 88 | |
paul@2 | 89 | "Write 's' to the file, recording its length." |
paul@2 | 90 | |
paul@2 | 91 | length = len(s) |
paul@2 | 92 | |
paul@2 | 93 | if not (0 <= length <= 255): |
paul@2 | 94 | raise ValueError, "String %r is too long." % s |
paul@2 | 95 | |
paul@2 | 96 | self.write_unsigned_byte(length) |
paul@2 | 97 | self.f.write(s) |
paul@2 | 98 | |
paul@0 | 99 | class FileReader(File): |
paul@0 | 100 | |
paul@0 | 101 | "Reading basic data types from files." |
paul@0 | 102 | |
paul@0 | 103 | def read_number(self): |
paul@0 | 104 | |
paul@0 | 105 | "Read a number from the file." |
paul@0 | 106 | |
paul@0 | 107 | nbytes = ord(self.f.read(1)) |
paul@0 | 108 | |
paul@0 | 109 | # Read each byte, adding it to the number. |
paul@0 | 110 | |
paul@0 | 111 | bytes = self.f.read(nbytes) |
paul@0 | 112 | |
paul@0 | 113 | i = 0 |
paul@0 | 114 | shift = 0 |
paul@0 | 115 | number = 0 |
paul@0 | 116 | |
paul@0 | 117 | while i < nbytes: |
paul@0 | 118 | csd = ord(bytes[i]) |
paul@0 | 119 | number += (csd << shift) |
paul@0 | 120 | shift += 8 |
paul@0 | 121 | i += 1 |
paul@0 | 122 | |
paul@0 | 123 | return number |
paul@0 | 124 | |
paul@2 | 125 | def read_unsigned_byte(self): |
paul@2 | 126 | |
paul@2 | 127 | "Read a number from the file, consuming a single byte." |
paul@2 | 128 | |
paul@2 | 129 | return ord(self.f.read(1)) |
paul@2 | 130 | |
paul@2 | 131 | def read_string(self): |
paul@2 | 132 | |
paul@2 | 133 | "Read a string from the file." |
paul@2 | 134 | |
paul@2 | 135 | length = self.read_unsigned_byte() |
paul@2 | 136 | return self.f.read(length) |
paul@2 | 137 | |
paul@0 | 138 | # Specific classes. |
paul@0 | 139 | |
paul@0 | 140 | class PositionWriter(FileWriter): |
paul@0 | 141 | |
paul@0 | 142 | "Writing position information to files." |
paul@0 | 143 | |
paul@0 | 144 | def reset(self): |
paul@0 | 145 | self.last_docnum = 0 |
paul@0 | 146 | |
paul@0 | 147 | def write_positions(self, docnum, positions): |
paul@0 | 148 | |
paul@0 | 149 | "Write for the document 'docnum' the given 'positions'." |
paul@0 | 150 | |
paul@0 | 151 | if docnum < self.last_docnum: |
paul@0 | 152 | raise ValueError, "Document number %r is less than previous number %r." % (docnum, self.last_docnum) |
paul@0 | 153 | |
paul@0 | 154 | # Write the document number delta. |
paul@0 | 155 | |
paul@0 | 156 | self.write_number(docnum - self.last_docnum) |
paul@0 | 157 | |
paul@0 | 158 | # Write the number of positions. |
paul@0 | 159 | |
paul@0 | 160 | self.write_number(len(positions)) |
paul@0 | 161 | |
paul@0 | 162 | # Write the position deltas. |
paul@0 | 163 | |
paul@0 | 164 | last = 0 |
paul@0 | 165 | for position in positions: |
paul@0 | 166 | pos = position - last |
paul@0 | 167 | self.write_number(pos) |
paul@0 | 168 | last = position |
paul@0 | 169 | |
paul@0 | 170 | self.last_docnum = docnum |
paul@0 | 171 | |
paul@0 | 172 | def write_all_positions(self, doc_positions): |
paul@0 | 173 | |
paul@0 | 174 | """ |
paul@0 | 175 | Write all 'doc_positions' - a collection of tuples of the form (document |
paul@0 | 176 | number, position list) - to the file, returning the offset at which they |
paul@0 | 177 | were stored. |
paul@0 | 178 | """ |
paul@0 | 179 | |
paul@0 | 180 | # Reset the writer and record the current file offset. |
paul@0 | 181 | |
paul@0 | 182 | self.reset() |
paul@0 | 183 | offset = self.f.tell() |
paul@0 | 184 | |
paul@0 | 185 | # Write the number of documents. |
paul@0 | 186 | |
paul@0 | 187 | self.write_number(len(doc_positions)) |
paul@0 | 188 | |
paul@0 | 189 | # Write the positions. |
paul@0 | 190 | |
paul@0 | 191 | for docnum, positions in doc_positions: |
paul@0 | 192 | self.write_positions(docnum, positions) |
paul@0 | 193 | |
paul@0 | 194 | return offset |
paul@0 | 195 | |
paul@0 | 196 | class PositionReader(FileReader): |
paul@0 | 197 | |
paul@0 | 198 | "Reading position information from files." |
paul@0 | 199 | |
paul@0 | 200 | def reset(self): |
paul@0 | 201 | self.last_docnum = 0 |
paul@0 | 202 | |
paul@0 | 203 | def read_positions(self): |
paul@0 | 204 | |
paul@0 | 205 | "Read positions, returning a document number and a list of positions." |
paul@0 | 206 | |
paul@0 | 207 | # Read the document number delta and add it to the last number. |
paul@0 | 208 | |
paul@0 | 209 | self.last_docnum += self.read_number() |
paul@0 | 210 | |
paul@0 | 211 | # Read the number of positions. |
paul@0 | 212 | |
paul@0 | 213 | npositions = self.read_number() |
paul@0 | 214 | |
paul@0 | 215 | # Read the position deltas, adding each previous position to get the |
paul@0 | 216 | # appropriate collection of absolute positions. |
paul@0 | 217 | |
paul@0 | 218 | i = 0 |
paul@0 | 219 | last = 0 |
paul@0 | 220 | positions = [] |
paul@0 | 221 | |
paul@0 | 222 | while i < npositions: |
paul@0 | 223 | last += self.read_number() |
paul@0 | 224 | positions.append(last) |
paul@0 | 225 | i += 1 |
paul@0 | 226 | |
paul@0 | 227 | return self.last_docnum, positions |
paul@0 | 228 | |
paul@0 | 229 | def read_all_positions(self, offset): |
paul@0 | 230 | |
paul@0 | 231 | """ |
paul@0 | 232 | Read all positions from 'offset', seeking to that position in the file |
paul@0 | 233 | before reading. |
paul@0 | 234 | """ |
paul@0 | 235 | |
paul@0 | 236 | self.reset() |
paul@0 | 237 | self.f.seek(offset) |
paul@0 | 238 | |
paul@0 | 239 | # Read the number of documents. |
paul@0 | 240 | |
paul@0 | 241 | ndocuments = self.read_number() |
paul@0 | 242 | |
paul@0 | 243 | # Read all records. |
paul@0 | 244 | |
paul@0 | 245 | i = 0 |
paul@0 | 246 | doc_positions = [] |
paul@0 | 247 | |
paul@0 | 248 | while i < ndocuments: |
paul@0 | 249 | doc_positions.append(self.read_positions()) |
paul@0 | 250 | i += 1 |
paul@0 | 251 | |
paul@0 | 252 | return doc_positions |
paul@0 | 253 | |
paul@2 | 254 | class TermWriter(FileWriter): |
paul@2 | 255 | |
paul@2 | 256 | "Writing term information to files." |
paul@2 | 257 | |
paul@2 | 258 | def reset(self): |
paul@2 | 259 | self.last_term = "" |
paul@2 | 260 | self.last_offset = 0 |
paul@2 | 261 | |
paul@2 | 262 | def write_term(self, term, offset): |
paul@2 | 263 | |
paul@2 | 264 | """ |
paul@2 | 265 | Write the given 'term' and its position file 'offset' to the term |
paul@2 | 266 | information file. |
paul@2 | 267 | """ |
paul@2 | 268 | |
paul@2 | 269 | # Too long terms are not currently supported. |
paul@2 | 270 | |
paul@2 | 271 | if len(term) > 255: |
paul@2 | 272 | raise ValueError, "Term %r is too long." % term |
paul@2 | 273 | |
paul@2 | 274 | # Write the prefix length and term suffix. |
paul@2 | 275 | |
paul@2 | 276 | common = len(commonprefix([self.last_term, term])) |
paul@2 | 277 | suffix = term[common:] |
paul@2 | 278 | |
paul@2 | 279 | self.write_unsigned_byte(common) |
paul@2 | 280 | self.write_string(suffix) |
paul@2 | 281 | |
paul@2 | 282 | # Write the offset delta. |
paul@2 | 283 | |
paul@2 | 284 | self.write_number(offset - self.last_offset) |
paul@2 | 285 | |
paul@2 | 286 | self.last_term = term |
paul@2 | 287 | self.last_offset = offset |
paul@2 | 288 | |
paul@2 | 289 | class TermReader(FileReader): |
paul@2 | 290 | |
paul@2 | 291 | "Reading term information from files." |
paul@2 | 292 | |
paul@2 | 293 | def reset(self): |
paul@2 | 294 | self.last_term = "" |
paul@2 | 295 | self.last_offset = 0 |
paul@2 | 296 | |
paul@2 | 297 | def read_term(self): |
paul@2 | 298 | |
paul@2 | 299 | """ |
paul@2 | 300 | Read a term and its position file offset from the term information file. |
paul@2 | 301 | """ |
paul@2 | 302 | |
paul@2 | 303 | # Read the prefix length and term suffix. |
paul@2 | 304 | |
paul@2 | 305 | common = self.read_unsigned_byte() |
paul@2 | 306 | suffix = self.read_string() |
paul@2 | 307 | |
paul@2 | 308 | self.last_term = self.last_term[:common] + suffix |
paul@2 | 309 | |
paul@2 | 310 | # Read the offset delta. |
paul@2 | 311 | |
paul@2 | 312 | self.last_offset += self.read_number() |
paul@2 | 313 | |
paul@2 | 314 | return self.last_term, self.last_offset |
paul@2 | 315 | |
paul@0 | 316 | # vim: tabstop=4 expandtab shiftwidth=4 |