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