root/gsdl/trunk/perllib/inexport.pm @ 18469

Revision 18469, 8.2 KB (checked in by davidb, 11 years ago)

Support for reindexing a document added

  • Property svn:executable set to *
Line 
1###########################################################################
2#
3# inexport.pm -- useful utilities to support import.pl and export.pl
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package inexport;
27
28use strict;
29
30use File::Basename;
31
32use util;
33use GDBMUtils;
34
35sub new_vs_old_import_diff
36{
37    my ($archive_info,$block_hash,$importdir) = @_;
38
39    # First convert all files to absolute form
40    # This is to support the situation where the import folder is not
41    # the default
42   
43    my $prev_all_files = $archive_info->{'prev_import_filelist'};
44    my $full_prev_all_files = {};
45
46    foreach my $prev_file (keys %$prev_all_files) {
47
48    if (!&util::filename_is_absolute($prev_file)) {
49        my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
50        $full_prev_all_files->{$full_prev_file} = $prev_file;
51    }
52    else {
53        $full_prev_all_files->{$prev_file} = $prev_file;
54    }
55    }
56
57
58    # Figure out which are the new files, existing files and so
59    # by implication the files from the previous import that are not
60    # there any more => mark them for deletion
61    foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
62   
63    my $full_curr_file = $curr_file;
64
65    # entry in 'all_files' is moved to either 'existing_files',
66    # 'deleted_files', or 'new_files'
67
68    if (!&util::filename_is_absolute($curr_file)) {
69        # add in import dir to make absolute
70        $full_curr_file = &util::filename_cat($importdir,$curr_file);
71    }
72
73    if (defined $block_hash->{'file_blocks'}->{$full_curr_file}) {
74        # If in block list, we want to ignore it
75        delete $block_hash->{'all_files'}->{$curr_file};
76
77        if (defined $full_prev_all_files->{$full_curr_file}) {
78        # also make sure it is gone from 'previous' list so
79        # not mistaken for a file that needs to be deleted
80        delete $full_prev_all_files->{$full_curr_file};
81        }
82        next;
83    }
84
85    # figure of if new file or not
86    if (defined $full_prev_all_files->{$full_curr_file}) {
87       
88        # had it before
89        $block_hash->{'existing_files'}->{$full_curr_file} = 1;
90
91        # Now remove it, so by end of loop only the files
92        # that need deleting are left
93
94        delete $full_prev_all_files->{$full_curr_file};
95        }
96    else {
97        $block_hash->{'new_files'}->{$full_curr_file} = 1;
98    }
99   
100    delete $block_hash->{'all_files'}->{$curr_file};
101    }
102
103    # By this point full_prev_all_files contains the files
104    # mentioned in archiveinf-src.db but are not in the 'import'
105    # folder (or whatever was specified through -importdir ...)
106
107    # This list can contain files that were created in the 'tmp' or
108    # 'cache' areas (such as screen-size and thumbnail images).
109    #
110    # In building the final list of files to delete, we test to see if
111    # it exists on the filesystem and if it does (unusual for a file
112    # that's allegedly deleted!) , supress it from going into the final
113    # list
114
115    my $collectdir = $ENV{'GSDLCOLLECTDIR'};
116
117    my @deleted_files = values %$full_prev_all_files;
118    map { my $curr_file = $_;
119      my $full_curr_file = $curr_file;
120
121      if (!&util::filename_is_absolute($curr_file)) {
122          # add in import dir to make absolute
123
124          $full_curr_file = &util::filename_cat($collectdir,$curr_file);
125      }
126
127
128      if (!-e $full_curr_file) {
129          $block_hash->{'deleted_files'}->{$curr_file} = 1;
130      }
131      } @deleted_files;
132}
133
134sub mark_docs_for_deletion
135{
136    my ($archive_info,$deleted_files_ref,$archivedir,$verbosity) = @_;
137
138    my $db_ext = &util::is_little_endian() ? ".ldb" : ".bdb";
139    my $doc_db = "archiveinf-doc$db_ext";
140    my $src_db = "archiveinf-src$db_ext";
141    my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
142    my $arcinfo_src_filename = &util::filename_cat ($archivedir, $src_db);
143
144
145    # record files marked for deletion in arcinfo
146    foreach my $file (@$deleted_files_ref) {
147    # use 'archiveinf-src' GDBM file to look up all the OIDs
148    # this file is used in (note in most cases, it's just one OID)
149   
150    my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
151    my $oids = $src_rec->{'oid'};
152    foreach my $oid (@$oids) {
153
154        # Find out if it's an assoc file or main doc
155
156        my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
157        if ($doc_rec->{'src-file'}->[0] eq $file) {
158        # It's the main doc
159        # => mark it for deletion
160   
161        if ($verbosity>1) {
162            print STDERR "$oid marked to be deleted from index on next buildcol.pl\n";
163        }
164        $archive_info->set_status_info($oid,"D");
165
166        my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
167        my ($index_status) = ($val =~ m/^<index-status>(.*)$/m);
168        if ($index_status ne "D") {
169            $val =~ s/^<index-status>(.*)$/<index-status>D/m;
170            &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
171            my $doc_file = $doc_rec->{'doc-file'}->[0];
172
173            my $doc_filename = &util::filename_cat($archivedir,$doc_file);
174
175
176            my ($doc_tailname, $doc_dirname, $suffix)
177            = File::Basename::fileparse($doc_filename, "\\.[^\\.]+\$");
178
179            print STDERR "Removing $doc_dirname\n" if ($verbosity>2);
180
181            &util::rm_r($doc_dirname);
182           
183        }
184        }
185        else {
186        # assoc file => mark it for re-indexing (safest thing to do)
187        my $curr_status = $archive_info->get_status_info($oid);
188
189
190        if (defined($curr_status) && (($curr_status ne "D") && ($curr_status ne "R"))) {
191            if ($verbosity > 1) {
192            print STDERR "$oid marked for (potential) reindexing\n";
193            print STDERR "  (because associated file $file deleted)\n";
194            }
195            $archive_info->set_status_info($oid,"R");
196
197            my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
198            $val =~ s/^<index-status>(.*)$/<index-status>R/m;
199            &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
200        }
201        }
202
203        GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
204    }
205    }
206}
207
208
209
210sub mark_docs_for_reindex
211{
212    my ($archive_info,$existing_files_ref,$archivedir,$verbosity) = @_;
213
214    # Reindexing is accomplished by deleting the previously indexed
215    # version of the document, and then allowing the new version to
216    # be indexed (as would a new document be indexed).
217    #
218    # The first step (marking for deletion) is implemented by this routine.
219    #
220    # By default in Greenstone a new version of an index will hash to
221    # a new unique OID, and the above strategy of reindex=delete+add
222    # works fine.  A special case arises when a persistent OID is
223    # allocated to a document (for instance through a metadata field),
224    # and the second step to reindexing (see XXXX) detects this and
225    # deals with it appropriately.
226
227    my $db_ext = &util::is_little_endian() ? ".ldb" : ".bdb";
228    my $doc_db = "archiveinf-doc$db_ext";
229    my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
230
231
232    my $archiveinf_timestamp = -M $arcinfo_doc_filename;
233
234    my $reindex_files_ref = [];
235
236    foreach my $existing_filename (@$existing_files_ref) {
237   
238    if (-M $existing_filename < $archiveinf_timestamp) {
239        # file is newer than last build
240       
241        my $existing_file = $existing_filename;
242        my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
243
244        $existing_file =~ s/^$collectdir(\\|\/)?//;
245       
246        print STDERR "**** Deleting existing file: $existing_file\n";
247
248        push(@$reindex_files_ref,$existing_file);
249    }
250
251    }
252   
253    mark_docs_for_deletion($archive_info,$reindex_files_ref,$archivedir,$verbosity);
254
255    return @$reindex_files_ref;
256}
257
258
259
2601;
Note: See TracBrowser for help on using the browser.