54dfa628b853b234ece13dc0b6e68524788beac6
[irspy-moved-to-github.git] / bin / reindex.pl
1 #!/usr/bin/perl
2
3 # Run as:
4 #       ./reindex.pl user=admin,password=SWORDFISH,localhost:8018/IR-Explain---1
5 #       ./reindex.pl -d -q zeerex.reliability=0 localhost:8018/IR-Explain---1
6
7 use strict;
8 use warnings;
9 use ZOOM;
10 use Getopt::Long;
11
12 my $setUdb = 0;
13 my $delete = 0;
14 my $noAction = 0;
15 my $query = 'cql.allRecords=1';
16 if (!GetOptions(
17          'setUdb' => \$setUdb,
18          'delete' => \$delete,
19          'noAction' => \$noAction,
20          'query=s' => \$query,
21     ) || @ARGV != 1) {
22     print STDERR "Usage: $0 [-s|--setUdb] [-d|--delete] [-n|--noaction] [-q <query>] <target>\n";
23     exit 1;
24 }
25
26 my $conn = new ZOOM::Connection($ARGV[0]);
27 $conn->option(preferredRecordSyntax => "xml");
28 $conn->option(elementSetName => "zebra::data");
29 my $rs = $conn->search(new ZOOM::Query::CQL($query));
30
31 my $n = $rs->size();
32 $| = 1;
33 print "$0: reindexing $n records\n";
34 foreach my $i (1..$n) {
35     print ".";
36     print " $i/$n (", int($i*100/$n), "%)\n" if $i % 50 == 0;
37     my $rec = $rs->record($i-1);
38     my $xml = $rec->render();
39     if ($xml !~ /<(e:)?databaseInfo.*?>/) {
40         # There is an undeletable phantom record: ignore it
41         next;
42     }
43
44     if ($setUdb) {
45         my $udb = qq[<i:udb xmlns:i="http://indexdata.com/irspy/1.0">irspy-$i</i:udb>];
46         $xml =~ s/<(e:)?databaseInfo.*?>/$&$udb/;
47     }
48
49     update($conn, $xml);
50 }
51 print " $n/$n (100%)\n" if $n % 50 != 0;
52 commit($conn);
53 print "committed\n";
54
55
56 # These might be better as ZOOM::Connection methods
57 sub update {
58     my($conn, $xml) = @_;
59
60     return if $noAction;
61     my $p = $conn->package();
62     $p->option(action => $delete ? "recordDelete" : "specialUpdate");
63     $p->option(record => $xml);
64     $p->send("update");
65     $p->destroy();
66 }
67
68 sub commit {
69     my($conn) = @_;
70
71     my $p = $conn->package();
72     $p->send("commit");
73     $p->destroy();
74 }