1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/iixr/filesystem.py Tue Sep 15 00:15:11 2009 +0200
1.3 @@ -0,0 +1,129 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +File access.
1.8 +
1.9 +Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
1.17 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.18 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.19 +
1.20 +You should have received a copy of the GNU General Public License along
1.21 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.22 +"""
1.23 +
1.24 +from iixr.fields import *
1.25 +from iixr.terms import *
1.26 +from iixr.positions import *
1.27 +from os import remove, rename # partition manipulation
1.28 +from os.path import join
1.29 +
1.30 +# Constants.
1.31 +
1.32 +TERM_FILENAMES = "terms", "terms_index", "positions", "positions_index"
1.33 +FIELD_FILENAMES = "fields", "fields_index"
1.34 +
1.35 +# Utility functions.
1.36 +
1.37 +def get_term_writer(pathname, partition, interval, doc_interval):
1.38 +
1.39 + """
1.40 + Return a term dictionary writer using files under the given 'pathname'
1.41 + labelled according to the given 'partition', using the given indexing
1.42 + 'interval' for terms and 'doc_interval' for document position records.
1.43 + """
1.44 +
1.45 + tdf = open(join(pathname, "terms-%s" % partition), "wb")
1.46 + info_writer = TermWriter(tdf)
1.47 +
1.48 + tdif = open(join(pathname, "terms_index-%s" % partition), "wb")
1.49 + index_writer = TermIndexWriter(tdif)
1.50 +
1.51 + tpf = open(join(pathname, "positions-%s" % partition), "wb")
1.52 + positions_writer = PositionWriter(tpf)
1.53 +
1.54 + tpif = open(join(pathname, "positions_index-%s" % partition), "wb")
1.55 + positions_index_writer = PositionIndexWriter(tpif)
1.56 +
1.57 + positions_dict_writer = PositionDictionaryWriter(positions_writer, positions_index_writer, doc_interval)
1.58 +
1.59 + return TermDictionaryWriter(info_writer, index_writer, positions_dict_writer, interval)
1.60 +
1.61 +def get_field_writer(pathname, partition, interval):
1.62 +
1.63 + """
1.64 + Return a field dictionary writer using files under the given 'pathname'
1.65 + labelled according to the given 'partition', using the given indexing
1.66 + 'interval'.
1.67 + """
1.68 +
1.69 + ff = open(join(pathname, "fields-%s" % partition), "wb")
1.70 + field_writer = FieldWriter(ff)
1.71 +
1.72 + fif = open(join(pathname, "fields_index-%s" % partition), "wb")
1.73 + field_index_writer = FieldIndexWriter(fif)
1.74 +
1.75 + return FieldDictionaryWriter(field_writer, field_index_writer, interval)
1.76 +
1.77 +def get_term_reader(pathname, partition):
1.78 +
1.79 + """
1.80 + Return a term dictionary reader using files under the given 'pathname'
1.81 + labelled according to the given 'partition'.
1.82 + """
1.83 +
1.84 + tdf = open(join(pathname, "terms-%s" % partition), "rb")
1.85 + info_reader = TermReader(tdf)
1.86 +
1.87 + tdif = open(join(pathname, "terms_index-%s" % partition), "rb")
1.88 + index_reader = TermIndexReader(tdif)
1.89 +
1.90 + positions_opener = PositionOpener(join(pathname, "positions-%s" % partition))
1.91 + positions_index_opener = PositionIndexOpener(join(pathname, "positions_index-%s" % partition))
1.92 +
1.93 + positions_dict_reader = PositionDictionaryReader(positions_opener, positions_index_opener)
1.94 +
1.95 + return TermDictionaryReader(info_reader, index_reader, positions_dict_reader)
1.96 +
1.97 +def get_field_reader(pathname, partition):
1.98 +
1.99 + """
1.100 + Return a field dictionary reader using files under the given 'pathname'
1.101 + labelled according to the given 'partition'.
1.102 + """
1.103 +
1.104 + ff = open(join(pathname, "fields-%s" % partition), "rb")
1.105 + field_reader = FieldReader(ff)
1.106 +
1.107 + fif = open(join(pathname, "fields_index-%s" % partition), "rb")
1.108 + field_index_reader = FieldIndexReader(fif)
1.109 +
1.110 + return FieldDictionaryReader(field_reader, field_index_reader)
1.111 +
1.112 +def rename_files(pathname, names, from_partition, to_partition):
1.113 + for name in names:
1.114 + rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition)))
1.115 +
1.116 +def rename_term_files(pathname, from_partition, to_partition):
1.117 + rename_files(pathname, TERM_FILENAMES, from_partition, to_partition)
1.118 +
1.119 +def rename_field_files(pathname, from_partition, to_partition):
1.120 + rename_files(pathname, FIELD_FILENAMES, from_partition, to_partition)
1.121 +
1.122 +def remove_files(pathname, names, partition):
1.123 + for name in names:
1.124 + remove(join(pathname, "%s-%s" % (name, partition)))
1.125 +
1.126 +def remove_term_files(pathname, partition):
1.127 + remove_files(pathname, TERM_FILENAMES, partition)
1.128 +
1.129 +def remove_field_files(pathname, partition):
1.130 + remove_files(pathname, FIELD_FILENAMES, partition)
1.131 +
1.132 +# vim: tabstop=4 expandtab shiftwidth=4