Dusplay proxy
[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
40     if ($setUdb) {
41         my $udb;
42         if ($xml !~ /<(e:)?databaseInfo.*?>/) {
43             $udb = qq[<i:udb xmlns:i="http://indexdata.com/irspy/1.0">irspy-a$i</i:udb>];
44             $xml =~ s/\/serverInfo>/$&<databaseInfo>$udb<\/databaseInfo>/;
45             print "x";
46         } else {
47             $udb = qq[<i:udb xmlns:i="http://indexdata.com/irspy/1.0">irspy-$i</i:udb>];
48             $xml =~ s/<(e:)?databaseInfo.*?>/$&$udb/;
49         }
50     }
51
52     update($conn, $xml);
53 }
54 print " $n/$n (100%)\n" if $n % 50 != 0;
55 commit($conn);
56 print "committed\n";
57
58
59 # These might be better as ZOOM::Connection methods
60 sub update {
61     my($conn, $xml) = @_;
62
63     return if $noAction;
64     my $p = $conn->package();
65     $p->option(action => $delete ? "recordDelete" : "specialUpdate");
66     $p->option(record => $xml);
67     $p->send("update");
68     $p->destroy();
69 }
70
71 sub commit {
72     my($conn) = @_;
73
74     my $p = $conn->package();
75     $p->send("commit");
76     $p->destroy();
77 }