paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | High-level classes. |
paul@44 | 5 | |
paul@85 | 6 | Copyright (C) 2009, 2010, 2011 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.filesystem import * |
paul@97 | 22 | from iixr.terms import MultipleReader |
paul@96 | 23 | from itermerge import itermerge |
paul@76 | 24 | from os import mkdir # index discovery |
paul@44 | 25 | from os.path import exists |
paul@44 | 26 | |
paul@44 | 27 | # Constants. |
paul@44 | 28 | |
paul@44 | 29 | FLUSH_INTERVAL = 10000 |
paul@97 | 30 | INDEX_INTERVAL = 1000 |
paul@73 | 31 | OPEN_PARTITIONS = 20 |
paul@44 | 32 | |
paul@44 | 33 | # High-level classes. |
paul@44 | 34 | |
paul@44 | 35 | class Document: |
paul@44 | 36 | |
paul@44 | 37 | "A container of document information." |
paul@44 | 38 | |
paul@96 | 39 | def __init__(self, docnum): |
paul@44 | 40 | self.docnum = docnum |
paul@44 | 41 | self.terms = {} |
paul@44 | 42 | |
paul@44 | 43 | def add_position(self, term, position): |
paul@44 | 44 | |
paul@44 | 45 | """ |
paul@44 | 46 | Add a position entry for the given 'term', indicating the given |
paul@44 | 47 | 'position'. |
paul@44 | 48 | """ |
paul@44 | 49 | |
paul@44 | 50 | self.terms.setdefault(term, []).append(position) |
paul@44 | 51 | |
paul@44 | 52 | class IndexWriter: |
paul@44 | 53 | |
paul@96 | 54 | "Building term information and writing it to the term dictionary." |
paul@44 | 55 | |
paul@96 | 56 | def __init__(self, pathname, flush_interval): |
paul@44 | 57 | self.pathname = pathname |
paul@44 | 58 | self.flush_interval = flush_interval |
paul@44 | 59 | |
paul@96 | 60 | self.term_partition = get_next_partition(get_term_partitions(self.pathname)) |
paul@44 | 61 | |
paul@44 | 62 | self.terms = {} |
paul@44 | 63 | self.doc_counter = 0 |
paul@44 | 64 | |
paul@44 | 65 | def add_document(self, doc): |
paul@44 | 66 | |
paul@44 | 67 | """ |
paul@44 | 68 | Add the given document 'doc', updating the document counter and flushing |
paul@44 | 69 | terms and fields if appropriate. |
paul@44 | 70 | """ |
paul@44 | 71 | |
paul@85 | 72 | docnum = doc.docnum |
paul@85 | 73 | |
paul@44 | 74 | for term, positions in doc.terms.items(): |
paul@85 | 75 | self.terms.setdefault(term, {})[docnum] = positions |
paul@44 | 76 | |
paul@44 | 77 | self.doc_counter += 1 |
paul@85 | 78 | |
paul@96 | 79 | if self.flush_interval and self.doc_counter >= self.flush_interval: |
paul@44 | 80 | self.flush_terms() |
paul@44 | 81 | self.doc_counter = 0 |
paul@44 | 82 | |
paul@44 | 83 | def get_term_writer(self): |
paul@44 | 84 | |
paul@96 | 85 | "Return a term writer for the current partition." |
paul@44 | 86 | |
paul@96 | 87 | return get_term_writer(self.pathname, self.term_partition) |
paul@44 | 88 | |
paul@44 | 89 | def flush_terms(self): |
paul@44 | 90 | |
paul@96 | 91 | "Flush terms into the current term partition." |
paul@44 | 92 | |
paul@44 | 93 | # Get the terms in order. |
paul@44 | 94 | |
paul@96 | 95 | term_writer = self.get_term_writer() |
paul@96 | 96 | try: |
paul@96 | 97 | term_writer.write_terms(self.terms) |
paul@96 | 98 | finally: |
paul@96 | 99 | term_writer.close() |
paul@44 | 100 | |
paul@44 | 101 | self.terms = {} |
paul@96 | 102 | self.term_partition += 1 |
paul@44 | 103 | |
paul@44 | 104 | def close(self): |
paul@76 | 105 | if self.terms or not get_term_partitions(self.pathname): |
paul@44 | 106 | self.flush_terms() |
paul@96 | 107 | |
paul@44 | 108 | class Index: |
paul@44 | 109 | |
paul@44 | 110 | "An inverted index solution encapsulating the various components." |
paul@44 | 111 | |
paul@96 | 112 | def __init__(self, pathname, flush_interval=FLUSH_INTERVAL, |
paul@96 | 113 | open_partitions=OPEN_PARTITIONS): |
paul@64 | 114 | |
paul@44 | 115 | self.pathname = pathname |
paul@64 | 116 | self.flush_interval = flush_interval |
paul@73 | 117 | self.open_partitions = open_partitions |
paul@44 | 118 | self.reader = None |
paul@44 | 119 | self.writer = None |
paul@44 | 120 | |
paul@64 | 121 | def get_writer(self): |
paul@44 | 122 | |
paul@64 | 123 | "Return a writer." |
paul@44 | 124 | |
paul@96 | 125 | if self.writer is None: |
paul@96 | 126 | self._ensure_directory() |
paul@96 | 127 | self.writer = IndexWriter(self.pathname, self.flush_interval) |
paul@59 | 128 | return self.writer |
paul@59 | 129 | |
paul@59 | 130 | def _ensure_directory(self): |
paul@44 | 131 | if not exists(self.pathname): |
paul@44 | 132 | mkdir(self.pathname) |
paul@44 | 133 | |
paul@97 | 134 | def _get_readers(self, get_reader): |
paul@97 | 135 | |
paul@97 | 136 | "Return a list of underlying readers given the 'get_reader' function." |
paul@97 | 137 | |
paul@97 | 138 | # Get the partitions in order. |
paul@97 | 139 | |
paul@97 | 140 | partitions = list(get_term_partitions(self.pathname)) |
paul@97 | 141 | partitions.sort() |
paul@97 | 142 | |
paul@97 | 143 | # Return the readers. |
paul@97 | 144 | |
paul@97 | 145 | return [get_reader(self.pathname, partition) for partition in partitions] |
paul@97 | 146 | |
paul@96 | 147 | def get_reader(self, refresh=0): |
paul@44 | 148 | |
paul@44 | 149 | "Return a reader for the index." |
paul@44 | 150 | |
paul@96 | 151 | if refresh and self.reader is not None: |
paul@96 | 152 | self.reader.close() |
paul@96 | 153 | self.reader = None |
paul@58 | 154 | |
paul@96 | 155 | if self.reader is None: |
paul@96 | 156 | if not exists(self.pathname): |
paul@96 | 157 | raise OSError, "Index path %r does not exist." % self.pathname |
paul@97 | 158 | |
paul@97 | 159 | # Try and get combined readers. |
paul@97 | 160 | |
paul@97 | 161 | try: |
paul@97 | 162 | readers = self._get_readers(get_combined_term_reader) |
paul@97 | 163 | except IOError: |
paul@97 | 164 | readers = self._get_readers(get_term_reader) |
paul@97 | 165 | |
paul@97 | 166 | self.reader = MultipleReader(readers) |
paul@97 | 167 | |
paul@96 | 168 | return self.reader |
paul@58 | 169 | |
paul@97 | 170 | def merge(self, interval=INDEX_INTERVAL): |
paul@44 | 171 | |
paul@96 | 172 | "Merge the partitions in the index." |
paul@44 | 173 | |
paul@97 | 174 | # Get data readers. |
paul@97 | 175 | |
paul@97 | 176 | readers = self._get_readers(get_term_data_reader) |
paul@97 | 177 | reader = MultipleReader(readers, self.merge_data) |
paul@96 | 178 | writer = get_term_writer(self.pathname, "merged") |
paul@97 | 179 | index_writer = get_term_index_writer(self.pathname, "merged") |
paul@97 | 180 | |
paul@96 | 181 | try: |
paul@96 | 182 | writer.begin(*reader.get_sizes()) |
paul@97 | 183 | index_writer.begin() |
paul@97 | 184 | |
paul@97 | 185 | i = 0 |
paul@96 | 186 | for term, data in reader: |
paul@97 | 187 | if i % interval == 0: |
paul@97 | 188 | index_writer.write_term(term, writer.tell()) |
paul@97 | 189 | index_writer.end_record() |
paul@97 | 190 | |
paul@96 | 191 | writer.write_term_plus_remaining(term, data) |
paul@96 | 192 | writer.end_record() |
paul@97 | 193 | i += 1 |
paul@97 | 194 | |
paul@96 | 195 | finally: |
paul@97 | 196 | index_writer.close() |
paul@96 | 197 | writer.close() |
paul@96 | 198 | reader.close() |
paul@73 | 199 | |
paul@96 | 200 | for partition in get_term_partitions(self.pathname): |
paul@96 | 201 | remove_term_files(self.pathname, partition) |
paul@73 | 202 | |
paul@96 | 203 | rename_term_files(self.pathname, "merged", 0) |
paul@97 | 204 | self.reader = None |
paul@97 | 205 | self.writer = None |
paul@73 | 206 | |
paul@96 | 207 | def merge_data(self, a, b): |
paul@44 | 208 | |
paul@96 | 209 | """ |
paul@96 | 210 | Merge 'a' and 'b', modifying the data to permit concatenation. |
paul@96 | 211 | """ |
paul@58 | 212 | |
paul@96 | 213 | # Modify the record to indicate a continuation of the data. |
paul@59 | 214 | |
paul@96 | 215 | c = a + b |
paul@96 | 216 | c[len(a) - 1] = 1 |
paul@96 | 217 | return c |
paul@58 | 218 | |
paul@44 | 219 | def close(self): |
paul@44 | 220 | if self.reader is not None: |
paul@44 | 221 | self.reader.close() |
paul@44 | 222 | self.reader = None |
paul@44 | 223 | if self.writer is not None: |
paul@44 | 224 | self.writer.close() |
paul@44 | 225 | self.writer = None |
paul@44 | 226 | |
paul@44 | 227 | # vim: tabstop=4 expandtab shiftwidth=4 |