Per Brandon Uhlmann's suggestion, speed up bibloading by automatically disabling...
[Evergreen.git] / Open-ILS / src / extras / import / parallel_pg_loader.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use lib '/openils/lib/perl5/';
6
7 use OpenSRF::System;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenSRF::Utils::JSON;
12 use FileHandle;
13
14 use Time::HiRes qw/time/;
15 use Getopt::Long;
16
17 my @files;
18 my ($config, $output, @auto, @order, @wipe) =
19         ('/openils/conf/opensrf_core.xml', 'pg_loader-output');
20
21 GetOptions(
22         'config=s'      => \$config,
23         'output=s'      => \$output,
24         'wipe=s'        => \@wipe,
25         'autoprimary=s' => \@auto,
26         'order=s'       => \@order,
27 );
28
29 my $pwd = `pwd`;
30 chop($pwd);
31
32 my %lineset;
33 my %fieldcache;
34
35 OpenSRF::System->bootstrap_client( config_file => $config );
36 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
37
38 my $main_out = FileHandle->new(">$output.sql") if ($output);
39
40 binmode($main_out,'utf8');
41
42 $main_out->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
43 $main_out->print("BEGIN;\n\n");
44
45 my %out_files;
46 for my $h (@order) {
47         $out_files{$h} = FileHandle->new(">$output.$h.sql");
48         binmode($out_files{$h},'utf8');
49 }
50
51 my $count = 0;
52 my $starttime = time;
53 while ( my $rec = <> ) {
54         next unless ($rec);
55
56         my $row;
57         try {
58                 $row = OpenSRF::Utils::JSON->JSON2perl($rec);
59         } catch Error with {
60                 my $e = shift;
61                 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
62         };
63         next unless ($row);
64
65         my $class = $row->class_name;
66         my $hint = $row->json_hint;
67
68         next unless ( grep /$hint/, @order );
69
70         if (!$fieldcache{$hint}) {
71                 my @cols = $row->real_fields;
72                 if (grep { $_ eq $hint} @auto) {
73                         @cols = grep { $_ ne $class->Identity } @cols;
74                 }
75
76                 $fieldcache{$hint} =
77                         { table => $class->Table,
78                           sequence => $class->Sequence,
79                           pkey => $class->Identity,
80                           fields => \@cols,
81                         };
82
83                 my $fields = join(',', @{ $fieldcache{$hint}{fields} });
84                 $main_out->print( "DELETE FROM $fieldcache{$hint}{table};\n" ) if (grep {$_ eq $hint } @wipe);
85                 # Speed up loading of bib records
86                 if ($hint eq 'mfr') {
87                         $main_out->print("\nSELECT reporter.disable_materialized_simple_record_trigger();\n");
88                 }
89                 $main_out->print( "COPY $fieldcache{$hint}{table} ($fields) FROM '$pwd/$output.$hint.sql';\n" );
90
91         }
92
93         my $line = [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
94         my @data;
95         my $x = 0;
96         for my $d (@$line) {
97                 if (!defined($d)) {
98                         $d = '\N';
99                 } else {
100                         $d =~ s/\f/\\f/gos;
101                         $d =~ s/\n/\\n/gos;
102                         $d =~ s/\r/\\r/gos;
103                         $d =~ s/\t/\\t/gos;
104                         $d =~ s/\\/\\\\/gos;
105                 }
106                 if ($hint eq 'bre' and $fieldcache{$hint}{fields}[$x] eq 'quality') {
107                         $d = int($d);
108                 }
109                 push @data, $d;
110                 $x++;
111         }
112         $out_files{$hint}->print( join("\t", @data)."\n" );
113
114         if (!($count % 500)) {
115                 print STDERR "\r$count\t". $count / (time - $starttime);
116         }
117
118         $count++;
119 }
120
121 if (grep /^mfr$/, %out_files) {
122         $main_out->print("SELECT reporter.enable_materialized_simple_record_trigger();\n");
123 }
124 $main_out->print("-- COMMIT;\n\n");
125 $main_out->close;