paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Specific classes for storing document information. |
paul@44 | 5 | |
paul@44 | 6 | Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@44 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@44 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@44 | 16 | |
paul@44 | 17 | You should have received a copy of the GNU General Public License along |
paul@44 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 19 | """ |
paul@44 | 20 | |
paul@44 | 21 | from iixr.files import * |
paul@44 | 22 | from bisect import bisect_right # to find terms in the dictionary index |
paul@44 | 23 | |
paul@67 | 24 | DOCUMENT_CACHE_LIMIT = 10000 |
paul@67 | 25 | |
paul@44 | 26 | class FieldWriter(FileWriter): |
paul@44 | 27 | |
paul@44 | 28 | "Writing field data to files." |
paul@44 | 29 | |
paul@44 | 30 | def reset(self): |
paul@44 | 31 | self.last_docnum = 0 |
paul@44 | 32 | |
paul@44 | 33 | def write_fields(self, docnum, fields): |
paul@44 | 34 | |
paul@44 | 35 | """ |
paul@44 | 36 | Write for the given 'docnum', a list of 'fields' (integer, string pairs |
paul@44 | 37 | representing field identifiers and values respectively). |
paul@44 | 38 | """ |
paul@44 | 39 | |
paul@44 | 40 | # Write the document number delta. |
paul@44 | 41 | |
paul@44 | 42 | self.write_number(docnum - self.last_docnum) |
paul@44 | 43 | |
paul@44 | 44 | # Write the number of fields. |
paul@44 | 45 | |
paul@44 | 46 | self.write_number(len(fields)) |
paul@44 | 47 | |
paul@44 | 48 | # Write the fields themselves. |
paul@44 | 49 | |
paul@44 | 50 | for i, field in fields: |
paul@44 | 51 | self.write_number(i) |
paul@44 | 52 | self.write_string(field, 1) # compress |
paul@44 | 53 | |
paul@44 | 54 | self.last_docnum = docnum |
paul@44 | 55 | |
paul@44 | 56 | class FieldReader(FileReader): |
paul@44 | 57 | |
paul@44 | 58 | "Reading field data from files." |
paul@44 | 59 | |
paul@44 | 60 | def reset(self): |
paul@44 | 61 | self.last_docnum = 0 |
paul@44 | 62 | |
paul@44 | 63 | def read_fields(self): |
paul@44 | 64 | |
paul@44 | 65 | """ |
paul@44 | 66 | Read fields from the file, returning a tuple containing the document |
paul@44 | 67 | number and a list of field (identifier, value) pairs. |
paul@44 | 68 | """ |
paul@44 | 69 | |
paul@44 | 70 | # Read the document number. |
paul@44 | 71 | |
paul@44 | 72 | self.last_docnum += self.read_number() |
paul@44 | 73 | |
paul@44 | 74 | # Read the number of fields. |
paul@44 | 75 | |
paul@44 | 76 | nfields = self.read_number() |
paul@44 | 77 | |
paul@44 | 78 | # Collect the fields. |
paul@44 | 79 | |
paul@44 | 80 | fields = [] |
paul@44 | 81 | i = 0 |
paul@44 | 82 | |
paul@44 | 83 | while i < nfields: |
paul@44 | 84 | identifier = self.read_number() |
paul@44 | 85 | value = self.read_string(1) # decompress |
paul@44 | 86 | fields.append((identifier, value)) |
paul@44 | 87 | i += 1 |
paul@44 | 88 | |
paul@44 | 89 | return self.last_docnum, fields |
paul@44 | 90 | |
paul@44 | 91 | def read_document_fields(self, docnum, offset): |
paul@44 | 92 | |
paul@44 | 93 | """ |
paul@44 | 94 | Read fields for 'docnum' at the given 'offset'. This permits the |
paul@44 | 95 | retrieval of details for the specified document, as well as scanning for |
paul@44 | 96 | later documents. |
paul@44 | 97 | """ |
paul@44 | 98 | |
paul@52 | 99 | self.f.seek(offset) |
paul@44 | 100 | bad_docnum, fields = self.read_fields() |
paul@44 | 101 | self.last_docnum = docnum |
paul@44 | 102 | return docnum, fields |
paul@44 | 103 | |
paul@44 | 104 | class FieldIndexWriter(FileWriter): |
paul@44 | 105 | |
paul@44 | 106 | "Writing field index details to files." |
paul@44 | 107 | |
paul@44 | 108 | def reset(self): |
paul@44 | 109 | self.last_docnum = 0 |
paul@44 | 110 | self.last_offset = 0 |
paul@44 | 111 | |
paul@44 | 112 | def write_document(self, docnum, offset): |
paul@44 | 113 | |
paul@44 | 114 | """ |
paul@44 | 115 | Write for the given 'docnum', the 'offset' at which the fields for the |
paul@44 | 116 | document are stored in the fields file. |
paul@44 | 117 | """ |
paul@44 | 118 | |
paul@44 | 119 | # Write the document number and offset deltas. |
paul@44 | 120 | |
paul@44 | 121 | self.write_number(docnum - self.last_docnum) |
paul@44 | 122 | self.write_number(offset - self.last_offset) |
paul@44 | 123 | |
paul@44 | 124 | self.last_docnum = docnum |
paul@44 | 125 | self.last_offset = offset |
paul@44 | 126 | |
paul@44 | 127 | class FieldIndexReader(FileReader): |
paul@44 | 128 | |
paul@44 | 129 | "Reading field index details from files." |
paul@44 | 130 | |
paul@44 | 131 | def reset(self): |
paul@44 | 132 | self.last_docnum = 0 |
paul@44 | 133 | self.last_offset = 0 |
paul@44 | 134 | |
paul@44 | 135 | def read_document(self): |
paul@44 | 136 | |
paul@44 | 137 | "Read a document number and field file offset." |
paul@44 | 138 | |
paul@44 | 139 | # Read the document number delta and offset. |
paul@44 | 140 | |
paul@44 | 141 | self.last_docnum += self.read_number() |
paul@44 | 142 | self.last_offset += self.read_number() |
paul@44 | 143 | |
paul@44 | 144 | return self.last_docnum, self.last_offset |
paul@44 | 145 | |
paul@44 | 146 | class FieldDictionaryWriter: |
paul@44 | 147 | |
paul@44 | 148 | "Writing field dictionary details." |
paul@44 | 149 | |
paul@44 | 150 | def __init__(self, field_writer, field_index_writer, interval): |
paul@44 | 151 | self.field_writer = field_writer |
paul@44 | 152 | self.field_index_writer = field_index_writer |
paul@44 | 153 | self.interval = interval |
paul@44 | 154 | self.entry = 0 |
paul@44 | 155 | |
paul@44 | 156 | def write_fields(self, docnum, fields): |
paul@44 | 157 | |
paul@44 | 158 | "Write details of the document with the given 'docnum' and 'fields'." |
paul@44 | 159 | |
paul@44 | 160 | if self.entry % self.interval == 0: |
paul@55 | 161 | offset = self.field_writer.f.tell() |
paul@55 | 162 | self.field_writer.write_fields(docnum, fields) |
paul@44 | 163 | self.field_index_writer.write_document(docnum, offset) |
paul@55 | 164 | else: |
paul@55 | 165 | self.field_writer.write_fields(docnum, fields) |
paul@44 | 166 | |
paul@44 | 167 | self.entry += 1 |
paul@44 | 168 | |
paul@44 | 169 | def close(self): |
paul@44 | 170 | self.field_writer.close() |
paul@44 | 171 | self.field_index_writer.close() |
paul@44 | 172 | |
paul@44 | 173 | class FieldDictionaryReader: |
paul@44 | 174 | |
paul@44 | 175 | "Reading field dictionary details." |
paul@44 | 176 | |
paul@44 | 177 | def __init__(self, field_reader, field_index_reader): |
paul@44 | 178 | self.field_reader = field_reader |
paul@44 | 179 | self.field_index_reader = field_index_reader |
paul@44 | 180 | |
paul@67 | 181 | self.cache = {} |
paul@44 | 182 | self.docs = [] |
paul@44 | 183 | try: |
paul@44 | 184 | while 1: |
paul@44 | 185 | self.docs.append(self.field_index_reader.read_document()) |
paul@44 | 186 | except EOFError: |
paul@44 | 187 | pass |
paul@44 | 188 | |
paul@44 | 189 | # Large numbers for ordering purposes. |
paul@44 | 190 | |
paul@44 | 191 | if self.docs: |
paul@44 | 192 | self.max_offset = self.docs[-1][1] |
paul@44 | 193 | else: |
paul@44 | 194 | self.max_offset = None |
paul@44 | 195 | |
paul@44 | 196 | # Iterator convenience methods. |
paul@44 | 197 | |
paul@44 | 198 | def __iter__(self): |
paul@44 | 199 | self.rewind() |
paul@44 | 200 | return self |
paul@44 | 201 | |
paul@44 | 202 | def next(self): |
paul@44 | 203 | try: |
paul@44 | 204 | return self.read_fields() |
paul@44 | 205 | except EOFError: |
paul@44 | 206 | raise StopIteration |
paul@44 | 207 | |
paul@44 | 208 | # Sequential access methods. |
paul@44 | 209 | |
paul@44 | 210 | def rewind(self): |
paul@44 | 211 | self.field_reader.rewind() |
paul@44 | 212 | |
paul@44 | 213 | def read_fields(self): |
paul@44 | 214 | |
paul@44 | 215 | "Return the next document number and fields." |
paul@44 | 216 | |
paul@44 | 217 | return self.field_reader.read_fields() |
paul@44 | 218 | |
paul@44 | 219 | # Random access methods. |
paul@44 | 220 | |
paul@44 | 221 | def get_fields(self, docnum): |
paul@44 | 222 | |
paul@44 | 223 | "Read the fields of the document with the given 'docnum'." |
paul@44 | 224 | |
paul@67 | 225 | if self.cache.has_key(docnum): |
paul@67 | 226 | return self.cache[docnum] |
paul@67 | 227 | |
paul@44 | 228 | i = bisect_right(self.docs, (docnum, self.max_offset)) - 1 |
paul@44 | 229 | |
paul@44 | 230 | # Get the entry position providing the term or one preceding it. |
paul@44 | 231 | |
paul@44 | 232 | if i == -1: |
paul@44 | 233 | return None |
paul@44 | 234 | |
paul@44 | 235 | found_docnum, offset = self.docs[i] |
paul@44 | 236 | |
paul@44 | 237 | # Read from the fields file. |
paul@44 | 238 | |
paul@44 | 239 | found_docnum, fields = self.field_reader.read_document_fields(found_docnum, offset) |
paul@44 | 240 | |
paul@44 | 241 | # Scan for the document, if necessary. |
paul@44 | 242 | |
paul@44 | 243 | try: |
paul@44 | 244 | while docnum > found_docnum: |
paul@44 | 245 | found_docnum, fields = self.field_reader.read_fields() |
paul@44 | 246 | except EOFError: |
paul@44 | 247 | pass |
paul@44 | 248 | |
paul@44 | 249 | # If the document is found, return the fields. |
paul@44 | 250 | |
paul@44 | 251 | if docnum == found_docnum: |
paul@67 | 252 | |
paul@67 | 253 | # Store the fields in the cache, removing entries if the limit has |
paul@67 | 254 | # been reached. |
paul@67 | 255 | |
paul@67 | 256 | keys = self.cache.keys() |
paul@67 | 257 | |
paul@67 | 258 | if len(keys) == DOCUMENT_CACHE_LIMIT: |
paul@67 | 259 | del self.cache[keys[0]] |
paul@67 | 260 | |
paul@67 | 261 | self.cache[docnum] = fields |
paul@44 | 262 | return fields |
paul@44 | 263 | else: |
paul@44 | 264 | return None |
paul@44 | 265 | |
paul@44 | 266 | def close(self): |
paul@44 | 267 | self.field_reader.close() |
paul@44 | 268 | self.field_index_reader.close() |
paul@44 | 269 | |
paul@44 | 270 | # vim: tabstop=4 expandtab shiftwidth=4 |