1 #!/usr/bin/env python 2 3 """ 4 File access. 5 6 Copyright (C) 2009, 2010 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 pf = open(join(pathname, "positions-%s" % partition), "rb") 89 position_reader = PositionReader(pf) 90 91 pif = open(join(pathname, "positions_index-%s" % partition), "rb") 92 position_index_reader = PositionIndexReader(pif) 93 94 position_dict_reader = PositionDictionaryReader(position_reader, position_index_reader) 95 96 return TermDictionaryReader(info_reader, index_reader, position_dict_reader) 97 98 def get_field_reader(pathname, partition): 99 100 """ 101 Return a field dictionary reader using files under the given 'pathname' 102 labelled according to the given 'partition'. 103 """ 104 105 ff = open(join(pathname, "fields-%s" % partition), "rb") 106 field_reader = FieldReader(ff) 107 108 fif = open(join(pathname, "fields_index-%s" % partition), "rb") 109 field_index_reader = FieldIndexReader(fif) 110 111 return FieldDictionaryReader(field_reader, field_index_reader) 112 113 # Renaming. 114 115 def rename_files(pathname, names, from_partition, to_partition): 116 for name in names: 117 rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) 118 119 def rename_term_files(pathname, from_partition, to_partition): 120 rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) 121 122 def rename_field_files(pathname, from_partition, to_partition): 123 rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition) 124 125 # Removal/deletion. 126 127 def remove_files(pathname, names, partition): 128 for name in names: 129 remove(join(pathname, "%s-%s" % (name, partition))) 130 131 def remove_term_files(pathname, partition): 132 remove_files(pathname, TERM_FILENAMES, partition) 133 134 def remove_field_files(pathname, partition): 135 remove_files(pathname, FIELD_FILENAMES, partition) 136 137 # Copying. 138 139 def copy_files(source, names, partition, destination, suffix): 140 for name in names: 141 filename = "%s-%s" % (name, partition) 142 copy(join(source, filename), join(destination, filename + suffix)) 143 144 def copy_term_files(source, partition, destination, suffix): 145 copy_files(source, TERM_FILENAMES, partition, destination, suffix) 146 147 def copy_field_files(source, partition, destination, suffix): 148 copy_files(source, FIELD_FILENAMES, partition, destination, suffix) 149 150 # vim: tabstop=4 expandtab shiftwidth=4