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@58 | 24 | from os import remove, rename # partition manipulation |
paul@58 | 25 | from shutil import copy # index updating |
paul@44 | 26 | from os.path import join |
paul@44 | 27 | |
paul@44 | 28 | # Constants. |
paul@44 | 29 | |
paul@44 | 30 | TERM_FILENAMES = "terms", "terms_index", "positions", "positions_index" |
paul@44 | 31 | FIELD_FILENAMES = "fields", "fields_index" |
paul@44 | 32 | |
paul@44 | 33 | # Utility functions. |
paul@44 | 34 | |
paul@44 | 35 | def get_term_writer(pathname, partition, interval, doc_interval): |
paul@44 | 36 | |
paul@44 | 37 | """ |
paul@44 | 38 | Return a term dictionary writer using files under the given 'pathname' |
paul@44 | 39 | labelled according to the given 'partition', using the given indexing |
paul@44 | 40 | 'interval' for terms and 'doc_interval' for document position records. |
paul@44 | 41 | """ |
paul@44 | 42 | |
paul@44 | 43 | tdf = open(join(pathname, "terms-%s" % partition), "wb") |
paul@44 | 44 | info_writer = TermWriter(tdf) |
paul@44 | 45 | |
paul@44 | 46 | tdif = open(join(pathname, "terms_index-%s" % partition), "wb") |
paul@44 | 47 | index_writer = TermIndexWriter(tdif) |
paul@44 | 48 | |
paul@44 | 49 | tpf = open(join(pathname, "positions-%s" % partition), "wb") |
paul@44 | 50 | positions_writer = PositionWriter(tpf) |
paul@44 | 51 | |
paul@44 | 52 | tpif = open(join(pathname, "positions_index-%s" % partition), "wb") |
paul@44 | 53 | positions_index_writer = PositionIndexWriter(tpif) |
paul@44 | 54 | |
paul@44 | 55 | positions_dict_writer = PositionDictionaryWriter(positions_writer, positions_index_writer, doc_interval) |
paul@44 | 56 | |
paul@44 | 57 | return TermDictionaryWriter(info_writer, index_writer, positions_dict_writer, interval) |
paul@44 | 58 | |
paul@44 | 59 | def get_field_writer(pathname, partition, interval): |
paul@44 | 60 | |
paul@44 | 61 | """ |
paul@44 | 62 | Return a field dictionary writer using files under the given 'pathname' |
paul@44 | 63 | labelled according to the given 'partition', using the given indexing |
paul@44 | 64 | 'interval'. |
paul@44 | 65 | """ |
paul@44 | 66 | |
paul@44 | 67 | ff = open(join(pathname, "fields-%s" % partition), "wb") |
paul@44 | 68 | field_writer = FieldWriter(ff) |
paul@44 | 69 | |
paul@44 | 70 | fif = open(join(pathname, "fields_index-%s" % partition), "wb") |
paul@44 | 71 | field_index_writer = FieldIndexWriter(fif) |
paul@44 | 72 | |
paul@44 | 73 | return FieldDictionaryWriter(field_writer, field_index_writer, interval) |
paul@44 | 74 | |
paul@44 | 75 | def get_term_reader(pathname, partition): |
paul@44 | 76 | |
paul@44 | 77 | """ |
paul@44 | 78 | Return a term dictionary reader using files under the given 'pathname' |
paul@44 | 79 | labelled according to the given 'partition'. |
paul@44 | 80 | """ |
paul@44 | 81 | |
paul@44 | 82 | tdf = open(join(pathname, "terms-%s" % partition), "rb") |
paul@44 | 83 | info_reader = TermReader(tdf) |
paul@44 | 84 | |
paul@44 | 85 | tdif = open(join(pathname, "terms_index-%s" % partition), "rb") |
paul@44 | 86 | index_reader = TermIndexReader(tdif) |
paul@44 | 87 | |
paul@68 | 88 | pf = open(join(pathname, "positions-%s" % partition), "rb") |
paul@68 | 89 | position_reader = PositionReader(pf) |
paul@44 | 90 | |
paul@68 | 91 | pif = open(join(pathname, "positions_index-%s" % partition), "rb") |
paul@68 | 92 | position_index_reader = PositionIndexReader(pif) |
paul@44 | 93 | |
paul@68 | 94 | position_dict_reader = PositionDictionaryReader(position_reader, position_index_reader) |
paul@68 | 95 | |
paul@68 | 96 | return TermDictionaryReader(info_reader, index_reader, position_dict_reader) |
paul@44 | 97 | |
paul@44 | 98 | def get_field_reader(pathname, partition): |
paul@44 | 99 | |
paul@44 | 100 | """ |
paul@44 | 101 | Return a field dictionary reader using files under the given 'pathname' |
paul@44 | 102 | labelled according to the given 'partition'. |
paul@44 | 103 | """ |
paul@44 | 104 | |
paul@44 | 105 | ff = open(join(pathname, "fields-%s" % partition), "rb") |
paul@44 | 106 | field_reader = FieldReader(ff) |
paul@44 | 107 | |
paul@44 | 108 | fif = open(join(pathname, "fields_index-%s" % partition), "rb") |
paul@44 | 109 | field_index_reader = FieldIndexReader(fif) |
paul@44 | 110 | |
paul@44 | 111 | return FieldDictionaryReader(field_reader, field_index_reader) |
paul@44 | 112 | |
paul@58 | 113 | # Renaming. |
paul@58 | 114 | |
paul@44 | 115 | def rename_files(pathname, names, from_partition, to_partition): |
paul@44 | 116 | for name in names: |
paul@44 | 117 | rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) |
paul@44 | 118 | |
paul@44 | 119 | def rename_term_files(pathname, from_partition, to_partition): |
paul@44 | 120 | rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) |
paul@44 | 121 | |
paul@44 | 122 | def rename_field_files(pathname, from_partition, to_partition): |
paul@44 | 123 | rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition) |
paul@44 | 124 | |
paul@58 | 125 | # Removal/deletion. |
paul@58 | 126 | |
paul@44 | 127 | def remove_files(pathname, names, partition): |
paul@44 | 128 | for name in names: |
paul@44 | 129 | remove(join(pathname, "%s-%s" % (name, partition))) |
paul@44 | 130 | |
paul@44 | 131 | def remove_term_files(pathname, partition): |
paul@44 | 132 | remove_files(pathname, TERM_FILENAMES, partition) |
paul@44 | 133 | |
paul@44 | 134 | def remove_field_files(pathname, partition): |
paul@44 | 135 | remove_files(pathname, FIELD_FILENAMES, partition) |
paul@44 | 136 | |
paul@58 | 137 | # Copying. |
paul@58 | 138 | |
paul@58 | 139 | def copy_files(source, names, partition, destination, suffix): |
paul@58 | 140 | for name in names: |
paul@58 | 141 | filename = "%s-%s" % (name, partition) |
paul@58 | 142 | copy(join(source, filename), join(destination, filename + suffix)) |
paul@58 | 143 | |
paul@58 | 144 | def copy_term_files(source, partition, destination, suffix): |
paul@58 | 145 | copy_files(source, TERM_FILENAMES, partition, destination, suffix) |
paul@58 | 146 | |
paul@58 | 147 | def copy_field_files(source, partition, destination, suffix): |
paul@58 | 148 | copy_files(source, FIELD_FILENAMES, partition, destination, suffix) |
paul@58 | 149 | |
paul@44 | 150 | # vim: tabstop=4 expandtab shiftwidth=4 |