paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | File access. |
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.fields import * |
paul@44 | 22 | from iixr.terms import * |
paul@44 | 23 | from iixr.positions import * |
paul@76 | 24 | from os import listdir, remove, rename # partition manipulation |
paul@76 | 25 | from shutil import copy # index updating |
paul@44 | 26 | from os.path import join |
paul@44 | 27 | |
paul@76 | 28 | try: |
paul@76 | 29 | set |
paul@76 | 30 | except NameError: |
paul@76 | 31 | from sets import Set as set |
paul@76 | 32 | |
paul@44 | 33 | # Constants. |
paul@44 | 34 | |
paul@44 | 35 | TERM_FILENAMES = "terms", "terms_index", "positions", "positions_index" |
paul@44 | 36 | FIELD_FILENAMES = "fields", "fields_index" |
paul@44 | 37 | |
paul@44 | 38 | # Utility functions. |
paul@44 | 39 | |
paul@76 | 40 | def get_partitions(pathname, prefix): |
paul@76 | 41 | |
paul@76 | 42 | """ |
paul@76 | 43 | Return a set of partition names for partitions residing at the given |
paul@76 | 44 | 'pathname' having the given 'prefix'. |
paul@76 | 45 | """ |
paul@76 | 46 | |
paul@76 | 47 | prefix_length = len(prefix) |
paul@76 | 48 | |
paul@76 | 49 | partitions = set() |
paul@76 | 50 | for filename in listdir(pathname): |
paul@76 | 51 | if filename.startswith(prefix): |
paul@76 | 52 | partitions.add(filename[prefix_length:]) |
paul@76 | 53 | return partitions |
paul@76 | 54 | |
paul@76 | 55 | def get_term_partitions(pathname): |
paul@76 | 56 | |
paul@76 | 57 | """ |
paul@76 | 58 | Return a set of term partition identifiers for partitions residing at the |
paul@76 | 59 | given 'pathname'. |
paul@76 | 60 | """ |
paul@76 | 61 | |
paul@76 | 62 | return get_partitions(pathname, "terms-") |
paul@76 | 63 | |
paul@76 | 64 | def get_field_partitions(pathname): |
paul@76 | 65 | |
paul@76 | 66 | """ |
paul@76 | 67 | Return a set of field partition identifiers for partitions residing at the |
paul@76 | 68 | given 'pathname'. |
paul@76 | 69 | """ |
paul@76 | 70 | |
paul@76 | 71 | return get_partitions(pathname, "fields-") |
paul@76 | 72 | |
paul@76 | 73 | def get_next_partition(partitions): |
paul@76 | 74 | return max([int(n) for n in partitions if n.isdigit()] or [-1]) + 1 |
paul@76 | 75 | |
paul@44 | 76 | def get_term_writer(pathname, partition, interval, doc_interval): |
paul@44 | 77 | |
paul@44 | 78 | """ |
paul@44 | 79 | Return a term dictionary writer using files under the given 'pathname' |
paul@44 | 80 | labelled according to the given 'partition', using the given indexing |
paul@44 | 81 | 'interval' for terms and 'doc_interval' for document position records. |
paul@44 | 82 | """ |
paul@44 | 83 | |
paul@44 | 84 | tdf = open(join(pathname, "terms-%s" % partition), "wb") |
paul@44 | 85 | info_writer = TermWriter(tdf) |
paul@44 | 86 | |
paul@44 | 87 | tdif = open(join(pathname, "terms_index-%s" % partition), "wb") |
paul@44 | 88 | index_writer = TermIndexWriter(tdif) |
paul@44 | 89 | |
paul@44 | 90 | tpf = open(join(pathname, "positions-%s" % partition), "wb") |
paul@44 | 91 | positions_writer = PositionWriter(tpf) |
paul@44 | 92 | |
paul@44 | 93 | tpif = open(join(pathname, "positions_index-%s" % partition), "wb") |
paul@44 | 94 | positions_index_writer = PositionIndexWriter(tpif) |
paul@44 | 95 | |
paul@44 | 96 | positions_dict_writer = PositionDictionaryWriter(positions_writer, positions_index_writer, doc_interval) |
paul@44 | 97 | |
paul@44 | 98 | return TermDictionaryWriter(info_writer, index_writer, positions_dict_writer, interval) |
paul@44 | 99 | |
paul@44 | 100 | def get_field_writer(pathname, partition, interval): |
paul@44 | 101 | |
paul@44 | 102 | """ |
paul@44 | 103 | Return a field dictionary writer using files under the given 'pathname' |
paul@44 | 104 | labelled according to the given 'partition', using the given indexing |
paul@44 | 105 | 'interval'. |
paul@44 | 106 | """ |
paul@44 | 107 | |
paul@44 | 108 | ff = open(join(pathname, "fields-%s" % partition), "wb") |
paul@44 | 109 | field_writer = FieldWriter(ff) |
paul@44 | 110 | |
paul@44 | 111 | fif = open(join(pathname, "fields_index-%s" % partition), "wb") |
paul@44 | 112 | field_index_writer = FieldIndexWriter(fif) |
paul@44 | 113 | |
paul@44 | 114 | return FieldDictionaryWriter(field_writer, field_index_writer, interval) |
paul@44 | 115 | |
paul@44 | 116 | def get_term_reader(pathname, partition): |
paul@44 | 117 | |
paul@44 | 118 | """ |
paul@44 | 119 | Return a term dictionary reader using files under the given 'pathname' |
paul@44 | 120 | labelled according to the given 'partition'. |
paul@44 | 121 | """ |
paul@44 | 122 | |
paul@44 | 123 | tdf = open(join(pathname, "terms-%s" % partition), "rb") |
paul@44 | 124 | info_reader = TermReader(tdf) |
paul@44 | 125 | |
paul@44 | 126 | tdif = open(join(pathname, "terms_index-%s" % partition), "rb") |
paul@44 | 127 | index_reader = TermIndexReader(tdif) |
paul@44 | 128 | |
paul@68 | 129 | pf = open(join(pathname, "positions-%s" % partition), "rb") |
paul@68 | 130 | position_reader = PositionReader(pf) |
paul@44 | 131 | |
paul@68 | 132 | pif = open(join(pathname, "positions_index-%s" % partition), "rb") |
paul@68 | 133 | position_index_reader = PositionIndexReader(pif) |
paul@44 | 134 | |
paul@68 | 135 | position_dict_reader = PositionDictionaryReader(position_reader, position_index_reader) |
paul@68 | 136 | |
paul@68 | 137 | return TermDictionaryReader(info_reader, index_reader, position_dict_reader) |
paul@44 | 138 | |
paul@44 | 139 | def get_field_reader(pathname, partition): |
paul@44 | 140 | |
paul@44 | 141 | """ |
paul@44 | 142 | Return a field dictionary reader using files under the given 'pathname' |
paul@44 | 143 | labelled according to the given 'partition'. |
paul@44 | 144 | """ |
paul@44 | 145 | |
paul@44 | 146 | ff = open(join(pathname, "fields-%s" % partition), "rb") |
paul@44 | 147 | field_reader = FieldReader(ff) |
paul@44 | 148 | |
paul@44 | 149 | fif = open(join(pathname, "fields_index-%s" % partition), "rb") |
paul@44 | 150 | field_index_reader = FieldIndexReader(fif) |
paul@44 | 151 | |
paul@44 | 152 | return FieldDictionaryReader(field_reader, field_index_reader) |
paul@44 | 153 | |
paul@58 | 154 | # Renaming. |
paul@58 | 155 | |
paul@44 | 156 | def rename_files(pathname, names, from_partition, to_partition): |
paul@44 | 157 | for name in names: |
paul@44 | 158 | rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) |
paul@44 | 159 | |
paul@44 | 160 | def rename_term_files(pathname, from_partition, to_partition): |
paul@44 | 161 | rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) |
paul@44 | 162 | |
paul@44 | 163 | def rename_field_files(pathname, from_partition, to_partition): |
paul@44 | 164 | rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition) |
paul@44 | 165 | |
paul@58 | 166 | # Removal/deletion. |
paul@58 | 167 | |
paul@44 | 168 | def remove_files(pathname, names, partition): |
paul@44 | 169 | for name in names: |
paul@44 | 170 | remove(join(pathname, "%s-%s" % (name, partition))) |
paul@44 | 171 | |
paul@44 | 172 | def remove_term_files(pathname, partition): |
paul@44 | 173 | remove_files(pathname, TERM_FILENAMES, partition) |
paul@44 | 174 | |
paul@44 | 175 | def remove_field_files(pathname, partition): |
paul@44 | 176 | remove_files(pathname, FIELD_FILENAMES, partition) |
paul@44 | 177 | |
paul@58 | 178 | # Copying. |
paul@58 | 179 | |
paul@58 | 180 | def copy_files(source, names, partition, destination, suffix): |
paul@58 | 181 | for name in names: |
paul@58 | 182 | filename = "%s-%s" % (name, partition) |
paul@58 | 183 | copy(join(source, filename), join(destination, filename + suffix)) |
paul@58 | 184 | |
paul@58 | 185 | def copy_term_files(source, partition, destination, suffix): |
paul@58 | 186 | copy_files(source, TERM_FILENAMES, partition, destination, suffix) |
paul@58 | 187 | |
paul@58 | 188 | def copy_field_files(source, partition, destination, suffix): |
paul@58 | 189 | copy_files(source, FIELD_FILENAMES, partition, destination, suffix) |
paul@58 | 190 | |
paul@44 | 191 | # vim: tabstop=4 expandtab shiftwidth=4 |