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