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 os.path import join 26 27 # Constants. 28 29 TERM_FILENAMES = "terms", "terms_index", "positions", "positions_index" 30 FIELD_FILENAMES = "fields", "fields_index" 31 32 # Utility functions. 33 34 def get_term_writer(pathname, partition, interval, doc_interval): 35 36 """ 37 Return a term dictionary writer using files under the given 'pathname' 38 labelled according to the given 'partition', using the given indexing 39 'interval' for terms and 'doc_interval' for document position records. 40 """ 41 42 tdf = open(join(pathname, "terms-%s" % partition), "wb") 43 info_writer = TermWriter(tdf) 44 45 tdif = open(join(pathname, "terms_index-%s" % partition), "wb") 46 index_writer = TermIndexWriter(tdif) 47 48 tpf = open(join(pathname, "positions-%s" % partition), "wb") 49 positions_writer = PositionWriter(tpf) 50 51 tpif = open(join(pathname, "positions_index-%s" % partition), "wb") 52 positions_index_writer = PositionIndexWriter(tpif) 53 54 positions_dict_writer = PositionDictionaryWriter(positions_writer, positions_index_writer, doc_interval) 55 56 return TermDictionaryWriter(info_writer, index_writer, positions_dict_writer, interval) 57 58 def get_field_writer(pathname, partition, interval): 59 60 """ 61 Return a field dictionary writer using files under the given 'pathname' 62 labelled according to the given 'partition', using the given indexing 63 'interval'. 64 """ 65 66 ff = open(join(pathname, "fields-%s" % partition), "wb") 67 field_writer = FieldWriter(ff) 68 69 fif = open(join(pathname, "fields_index-%s" % partition), "wb") 70 field_index_writer = FieldIndexWriter(fif) 71 72 return FieldDictionaryWriter(field_writer, field_index_writer, interval) 73 74 def get_term_reader(pathname, partition): 75 76 """ 77 Return a term dictionary reader using files under the given 'pathname' 78 labelled according to the given 'partition'. 79 """ 80 81 tdf = open(join(pathname, "terms-%s" % partition), "rb") 82 info_reader = TermReader(tdf) 83 84 tdif = open(join(pathname, "terms_index-%s" % partition), "rb") 85 index_reader = TermIndexReader(tdif) 86 87 positions_opener = PositionOpener(join(pathname, "positions-%s" % partition)) 88 positions_index_opener = PositionIndexOpener(join(pathname, "positions_index-%s" % partition)) 89 90 positions_dict_reader = PositionDictionaryReader(positions_opener, positions_index_opener) 91 92 return TermDictionaryReader(info_reader, index_reader, positions_dict_reader) 93 94 def get_field_reader(pathname, partition): 95 96 """ 97 Return a field dictionary reader using files under the given 'pathname' 98 labelled according to the given 'partition'. 99 """ 100 101 ff = open(join(pathname, "fields-%s" % partition), "rb") 102 field_reader = FieldReader(ff) 103 104 fif = open(join(pathname, "fields_index-%s" % partition), "rb") 105 field_index_reader = FieldIndexReader(fif) 106 107 return FieldDictionaryReader(field_reader, field_index_reader) 108 109 def rename_files(pathname, names, from_partition, to_partition): 110 for name in names: 111 rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) 112 113 def rename_term_files(pathname, from_partition, to_partition): 114 rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) 115 116 def rename_field_files(pathname, from_partition, to_partition): 117 rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition) 118 119 def remove_files(pathname, names, partition): 120 for name in names: 121 remove(join(pathname, "%s-%s" % (name, partition))) 122 123 def remove_term_files(pathname, partition): 124 remove_files(pathname, TERM_FILENAMES, partition) 125 126 def remove_field_files(pathname, partition): 127 remove_files(pathname, FIELD_FILENAMES, partition) 128 129 # vim: tabstop=4 expandtab shiftwidth=4