1 #!/usr/bin/env python 2 3 """ 4 File access. 5 6 Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 from iixr.fields import * 22 from iixr.terms import * 23 from iixr.positions import * 24 from os import remove, rename # partition manipulation 25 from shutil import copy # index updating 26 from os.path import join 27 28 # Constants. 29 30 TERM_FILENAMES = "terms", "terms_index", "positions", "positions_index" 31 FIELD_FILENAMES = "fields", "fields_index" 32 33 # Utility functions. 34 35 def get_term_writer(pathname, partition, interval, doc_interval): 36 37 """ 38 Return a term dictionary writer using files under the given 'pathname' 39 labelled according to the given 'partition', using the given indexing 40 'interval' for terms and 'doc_interval' for document position records. 41 """ 42 43 tdf = open(join(pathname, "terms-%s" % partition), "wb") 44 info_writer = TermWriter(tdf) 45 46 tdif = open(join(pathname, "terms_index-%s" % partition), "wb") 47 index_writer = TermIndexWriter(tdif) 48 49 tpf = open(join(pathname, "positions-%s" % partition), "wb") 50 positions_writer = PositionWriter(tpf) 51 52 tpif = open(join(pathname, "positions_index-%s" % partition), "wb") 53 positions_index_writer = PositionIndexWriter(tpif) 54 55 positions_dict_writer = PositionDictionaryWriter(positions_writer, positions_index_writer, doc_interval) 56 57 return TermDictionaryWriter(info_writer, index_writer, positions_dict_writer, interval) 58 59 def get_field_writer(pathname, partition, interval): 60 61 """ 62 Return a field dictionary writer using files under the given 'pathname' 63 labelled according to the given 'partition', using the given indexing 64 'interval'. 65 """ 66 67 ff = open(join(pathname, "fields-%s" % partition), "wb") 68 field_writer = FieldWriter(ff) 69 70 fif = open(join(pathname, "fields_index-%s" % partition), "wb") 71 field_index_writer = FieldIndexWriter(fif) 72 73 return FieldDictionaryWriter(field_writer, field_index_writer, interval) 74 75 def get_term_reader(pathname, partition): 76 77 """ 78 Return a term dictionary reader using files under the given 'pathname' 79 labelled according to the given 'partition'. 80 """ 81 82 tdf = open(join(pathname, "terms-%s" % partition), "rb") 83 info_reader = TermReader(tdf) 84 85 tdif = open(join(pathname, "terms_index-%s" % partition), "rb") 86 index_reader = TermIndexReader(tdif) 87 88 positions_opener = PositionOpener(join(pathname, "positions-%s" % partition)) 89 positions_index_opener = PositionIndexOpener(join(pathname, "positions_index-%s" % partition)) 90 91 positions_dict_reader = PositionDictionaryReader(positions_opener, positions_index_opener) 92 93 return TermDictionaryReader(info_reader, index_reader, positions_dict_reader) 94 95 def get_field_reader(pathname, partition): 96 97 """ 98 Return a field dictionary reader using files under the given 'pathname' 99 labelled according to the given 'partition'. 100 """ 101 102 ff = open(join(pathname, "fields-%s" % partition), "rb") 103 field_reader = FieldReader(ff) 104 105 fif = open(join(pathname, "fields_index-%s" % partition), "rb") 106 field_index_reader = FieldIndexReader(fif) 107 108 return FieldDictionaryReader(field_reader, field_index_reader) 109 110 # Renaming. 111 112 def rename_files(pathname, names, from_partition, to_partition): 113 for name in names: 114 rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) 115 116 def rename_term_files(pathname, from_partition, to_partition): 117 rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) 118 119 def rename_field_files(pathname, from_partition, to_partition): 120 rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition) 121 122 # Removal/deletion. 123 124 def remove_files(pathname, names, partition): 125 for name in names: 126 remove(join(pathname, "%s-%s" % (name, partition))) 127 128 def remove_term_files(pathname, partition): 129 remove_files(pathname, TERM_FILENAMES, partition) 130 131 def remove_field_files(pathname, partition): 132 remove_files(pathname, FIELD_FILENAMES, partition) 133 134 # Copying. 135 136 def copy_files(source, names, partition, destination, suffix): 137 for name in names: 138 filename = "%s-%s" % (name, partition) 139 copy(join(source, filename), join(destination, filename + suffix)) 140 141 def copy_term_files(source, partition, destination, suffix): 142 copy_files(source, TERM_FILENAMES, partition, destination, suffix) 143 144 def copy_field_files(source, partition, destination, suffix): 145 copy_files(source, FIELD_FILENAMES, partition, destination, suffix) 146 147 # vim: tabstop=4 expandtab shiftwidth=4