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@69 | 6 | Copyright (C) 2009, 2010 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@74 | 31 | self.last_docnum = None |
paul@74 | 32 | self.docnum_size = None |
paul@44 | 33 | |
paul@44 | 34 | def write_fields(self, docnum, fields): |
paul@44 | 35 | |
paul@44 | 36 | """ |
paul@44 | 37 | Write for the given 'docnum', a list of 'fields' (integer, string pairs |
paul@44 | 38 | representing field identifiers and values respectively). |
paul@44 | 39 | """ |
paul@44 | 40 | |
paul@74 | 41 | # Find the size of document number values. |
paul@74 | 42 | |
paul@74 | 43 | if self.docnum_size is None: |
paul@74 | 44 | self.docnum_size = self.get_value_size(docnum) |
paul@74 | 45 | self.last_docnum = self.get_initial_value(self.docnum_size) |
paul@74 | 46 | |
paul@74 | 47 | # Write the number of values per document number. |
paul@44 | 48 | # Write the document number delta. |
paul@44 | 49 | |
paul@74 | 50 | output = array('B') |
paul@74 | 51 | vint_to_array(self.docnum_size, output) |
paul@74 | 52 | self.last_docnum = self.write_sequence(output, docnum, self.last_docnum, self.docnum_size, monotonic=0) |
paul@44 | 53 | |
paul@44 | 54 | # Write the number of fields. |
paul@44 | 55 | |
paul@74 | 56 | vint_to_array(len(fields), output) |
paul@74 | 57 | output.tofile(self.f) |
paul@44 | 58 | |
paul@44 | 59 | # Write the fields themselves. |
paul@44 | 60 | |
paul@44 | 61 | for i, field in fields: |
paul@44 | 62 | self.write_number(i) |
paul@44 | 63 | self.write_string(field, 1) # compress |
paul@44 | 64 | |
paul@44 | 65 | class FieldReader(FileReader): |
paul@44 | 66 | |
paul@44 | 67 | "Reading field data from files." |
paul@44 | 68 | |
paul@44 | 69 | def reset(self): |
paul@74 | 70 | self.last_docnum = None |
paul@44 | 71 | |
paul@44 | 72 | def read_fields(self): |
paul@44 | 73 | |
paul@44 | 74 | """ |
paul@44 | 75 | Read fields from the file, returning a tuple containing the document |
paul@44 | 76 | number and a list of field (identifier, value) pairs. |
paul@44 | 77 | """ |
paul@44 | 78 | |
paul@74 | 79 | # Read the number of values per document number. |
paul@74 | 80 | |
paul@74 | 81 | docnum_size = self.read_number() |
paul@44 | 82 | |
paul@74 | 83 | if self.last_docnum is None: |
paul@74 | 84 | self.last_docnum = self.get_initial_value(docnum_size) |
paul@74 | 85 | |
paul@74 | 86 | # Read the document number delta and add it to the last number. |
paul@74 | 87 | |
paul@74 | 88 | self.last_docnum = self.read_sequence(self.last_docnum, docnum_size, monotonic=0) |
paul@44 | 89 | |
paul@44 | 90 | # Read the number of fields. |
paul@44 | 91 | |
paul@44 | 92 | nfields = self.read_number() |
paul@44 | 93 | |
paul@44 | 94 | # Collect the fields. |
paul@44 | 95 | |
paul@44 | 96 | fields = [] |
paul@44 | 97 | i = 0 |
paul@44 | 98 | |
paul@44 | 99 | while i < nfields: |
paul@44 | 100 | identifier = self.read_number() |
paul@44 | 101 | value = self.read_string(1) # decompress |
paul@44 | 102 | fields.append((identifier, value)) |
paul@44 | 103 | i += 1 |
paul@44 | 104 | |
paul@44 | 105 | return self.last_docnum, fields |
paul@44 | 106 | |
paul@44 | 107 | def read_document_fields(self, docnum, offset): |
paul@44 | 108 | |
paul@44 | 109 | """ |
paul@44 | 110 | Read fields for 'docnum' at the given 'offset'. This permits the |
paul@44 | 111 | retrieval of details for the specified document, as well as scanning for |
paul@44 | 112 | later documents. |
paul@44 | 113 | """ |
paul@44 | 114 | |
paul@69 | 115 | self.seek(offset) |
paul@44 | 116 | bad_docnum, fields = self.read_fields() |
paul@44 | 117 | self.last_docnum = docnum |
paul@44 | 118 | return docnum, fields |
paul@44 | 119 | |
paul@44 | 120 | class FieldIndexWriter(FileWriter): |
paul@44 | 121 | |
paul@44 | 122 | "Writing field index details to files." |
paul@44 | 123 | |
paul@44 | 124 | def reset(self): |
paul@74 | 125 | self.last_docnum = None |
paul@74 | 126 | self.docnum_size = None |
paul@44 | 127 | self.last_offset = 0 |
paul@44 | 128 | |
paul@44 | 129 | def write_document(self, docnum, offset): |
paul@44 | 130 | |
paul@44 | 131 | """ |
paul@44 | 132 | Write for the given 'docnum', the 'offset' at which the fields for the |
paul@44 | 133 | document are stored in the fields file. |
paul@44 | 134 | """ |
paul@44 | 135 | |
paul@74 | 136 | # Find the size of document number values. |
paul@74 | 137 | |
paul@74 | 138 | if self.docnum_size is None: |
paul@74 | 139 | self.docnum_size = self.get_value_size(docnum) |
paul@74 | 140 | self.last_docnum = self.get_initial_value(self.docnum_size) |
paul@74 | 141 | |
paul@74 | 142 | # Write the number of values per document number. |
paul@74 | 143 | # Write the document number delta. |
paul@44 | 144 | |
paul@74 | 145 | output = array('B') |
paul@74 | 146 | vint_to_array(self.docnum_size, output) |
paul@74 | 147 | self.last_docnum = self.write_sequence(output, docnum, self.last_docnum, self.docnum_size, monotonic=0) |
paul@74 | 148 | output.tofile(self.f) |
paul@74 | 149 | |
paul@74 | 150 | # Write the offset delta. |
paul@74 | 151 | |
paul@44 | 152 | self.write_number(offset - self.last_offset) |
paul@44 | 153 | self.last_offset = offset |
paul@44 | 154 | |
paul@44 | 155 | class FieldIndexReader(FileReader): |
paul@44 | 156 | |
paul@44 | 157 | "Reading field index details from files." |
paul@44 | 158 | |
paul@44 | 159 | def reset(self): |
paul@74 | 160 | self.last_docnum = None |
paul@44 | 161 | self.last_offset = 0 |
paul@44 | 162 | |
paul@44 | 163 | def read_document(self): |
paul@44 | 164 | |
paul@44 | 165 | "Read a document number and field file offset." |
paul@44 | 166 | |
paul@74 | 167 | # Read the number of values per document number. |
paul@74 | 168 | |
paul@74 | 169 | docnum_size = self.read_number() |
paul@74 | 170 | |
paul@74 | 171 | if self.last_docnum is None: |
paul@74 | 172 | self.last_docnum = self.get_initial_value(docnum_size) |
paul@44 | 173 | |
paul@74 | 174 | # Read the document number delta and add it to the last number. |
paul@74 | 175 | |
paul@74 | 176 | self.last_docnum = self.read_sequence(self.last_docnum, docnum_size, monotonic=0) |
paul@74 | 177 | |
paul@74 | 178 | # Read the offset. |
paul@74 | 179 | |
paul@44 | 180 | self.last_offset += self.read_number() |
paul@44 | 181 | |
paul@44 | 182 | return self.last_docnum, self.last_offset |
paul@44 | 183 | |
paul@44 | 184 | class FieldDictionaryWriter: |
paul@44 | 185 | |
paul@44 | 186 | "Writing field dictionary details." |
paul@44 | 187 | |
paul@44 | 188 | def __init__(self, field_writer, field_index_writer, interval): |
paul@44 | 189 | self.field_writer = field_writer |
paul@44 | 190 | self.field_index_writer = field_index_writer |
paul@44 | 191 | self.interval = interval |
paul@44 | 192 | self.entry = 0 |
paul@44 | 193 | |
paul@44 | 194 | def write_fields(self, docnum, fields): |
paul@44 | 195 | |
paul@44 | 196 | "Write details of the document with the given 'docnum' and 'fields'." |
paul@44 | 197 | |
paul@44 | 198 | if self.entry % self.interval == 0: |
paul@55 | 199 | offset = self.field_writer.f.tell() |
paul@55 | 200 | self.field_writer.write_fields(docnum, fields) |
paul@44 | 201 | self.field_index_writer.write_document(docnum, offset) |
paul@55 | 202 | else: |
paul@55 | 203 | self.field_writer.write_fields(docnum, fields) |
paul@44 | 204 | |
paul@44 | 205 | self.entry += 1 |
paul@44 | 206 | |
paul@44 | 207 | def close(self): |
paul@44 | 208 | self.field_writer.close() |
paul@44 | 209 | self.field_index_writer.close() |
paul@44 | 210 | |
paul@44 | 211 | class FieldDictionaryReader: |
paul@44 | 212 | |
paul@44 | 213 | "Reading field dictionary details." |
paul@44 | 214 | |
paul@44 | 215 | def __init__(self, field_reader, field_index_reader): |
paul@44 | 216 | self.field_reader = field_reader |
paul@44 | 217 | self.field_index_reader = field_index_reader |
paul@44 | 218 | |
paul@67 | 219 | self.cache = {} |
paul@44 | 220 | self.docs = [] |
paul@44 | 221 | try: |
paul@44 | 222 | while 1: |
paul@44 | 223 | self.docs.append(self.field_index_reader.read_document()) |
paul@44 | 224 | except EOFError: |
paul@44 | 225 | pass |
paul@44 | 226 | |
paul@44 | 227 | # Large numbers for ordering purposes. |
paul@44 | 228 | |
paul@44 | 229 | if self.docs: |
paul@44 | 230 | self.max_offset = self.docs[-1][1] |
paul@44 | 231 | else: |
paul@44 | 232 | self.max_offset = None |
paul@44 | 233 | |
paul@44 | 234 | # Iterator convenience methods. |
paul@44 | 235 | |
paul@44 | 236 | def __iter__(self): |
paul@44 | 237 | self.rewind() |
paul@44 | 238 | return self |
paul@44 | 239 | |
paul@44 | 240 | def next(self): |
paul@44 | 241 | try: |
paul@44 | 242 | return self.read_fields() |
paul@44 | 243 | except EOFError: |
paul@44 | 244 | raise StopIteration |
paul@44 | 245 | |
paul@44 | 246 | # Sequential access methods. |
paul@44 | 247 | |
paul@44 | 248 | def rewind(self): |
paul@44 | 249 | self.field_reader.rewind() |
paul@44 | 250 | |
paul@44 | 251 | def read_fields(self): |
paul@44 | 252 | |
paul@44 | 253 | "Return the next document number and fields." |
paul@44 | 254 | |
paul@44 | 255 | return self.field_reader.read_fields() |
paul@44 | 256 | |
paul@44 | 257 | # Random access methods. |
paul@44 | 258 | |
paul@44 | 259 | def get_fields(self, docnum): |
paul@44 | 260 | |
paul@44 | 261 | "Read the fields of the document with the given 'docnum'." |
paul@44 | 262 | |
paul@67 | 263 | if self.cache.has_key(docnum): |
paul@67 | 264 | return self.cache[docnum] |
paul@67 | 265 | |
paul@44 | 266 | i = bisect_right(self.docs, (docnum, self.max_offset)) - 1 |
paul@44 | 267 | |
paul@44 | 268 | # Get the entry position providing the term or one preceding it. |
paul@44 | 269 | |
paul@44 | 270 | if i == -1: |
paul@44 | 271 | return None |
paul@44 | 272 | |
paul@44 | 273 | found_docnum, offset = self.docs[i] |
paul@44 | 274 | |
paul@44 | 275 | # Read from the fields file. |
paul@44 | 276 | |
paul@44 | 277 | found_docnum, fields = self.field_reader.read_document_fields(found_docnum, offset) |
paul@44 | 278 | |
paul@44 | 279 | # Scan for the document, if necessary. |
paul@44 | 280 | |
paul@44 | 281 | try: |
paul@44 | 282 | while docnum > found_docnum: |
paul@44 | 283 | found_docnum, fields = self.field_reader.read_fields() |
paul@44 | 284 | except EOFError: |
paul@44 | 285 | pass |
paul@44 | 286 | |
paul@44 | 287 | # If the document is found, return the fields. |
paul@44 | 288 | |
paul@44 | 289 | if docnum == found_docnum: |
paul@67 | 290 | |
paul@67 | 291 | # Store the fields in the cache, removing entries if the limit has |
paul@67 | 292 | # been reached. |
paul@67 | 293 | |
paul@67 | 294 | keys = self.cache.keys() |
paul@67 | 295 | |
paul@67 | 296 | if len(keys) == DOCUMENT_CACHE_LIMIT: |
paul@67 | 297 | del self.cache[keys[0]] |
paul@67 | 298 | |
paul@67 | 299 | self.cache[docnum] = fields |
paul@44 | 300 | return fields |
paul@44 | 301 | else: |
paul@44 | 302 | return None |
paul@44 | 303 | |
paul@44 | 304 | def close(self): |
paul@44 | 305 | self.field_reader.close() |
paul@44 | 306 | self.field_index_reader.close() |
paul@44 | 307 | |
paul@44 | 308 | # vim: tabstop=4 expandtab shiftwidth=4 |