-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport.pl
executable file
·389 lines (316 loc) · 8.27 KB
/
import.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env perl
# This file is part of the fcc-db ULS Amateur import project.
#
# Copyright 2016-2021, Josh Cepek
#
# This project is available under the GPLv3 license: see LICENSE.md.
use strict;
use warnings;
use Getopt::Long ();
use DBI ();
Getopt::Long::Configure(qw[ bundling ]);
BEGIN {
require File::Spec;
my ($vol, $dir) = File::Spec->splitpath($0);
unshift @INC, File::Spec->catdir($dir, 'lib');
};
use QKTech::ULS::IO7z ();
use QKTech::ULS::Data ();
use QKTech::ULS::Table ();
use constant {
MONTH_TO_DEC => {
JAN => '01', FEB => '02', MAR => '03',
APR => '04', MAY => '05', JUN => '06',
JUL => '07', AUG => '08', SEP => '09',
OCT => '10', NOV => '11', DEC => '12',
},
TZ_TO_OFF => {
EDT => '-04:00',
EST => '-05:00',
},
};
main(\@ARGV);
exit(0);
sub usage_exit {
print <<_EOF;
ULS Amateur Radio DB Import Tool
SYNOPSIS
$0
-d DB_FILE [-n -s SCHEMA -i INDEXES]
[-c CONFIG_INI]
Options can be moved to config using long-names as keys (no dashes) under
an INI section called [config].
LONG-OPTION NAMES
-d: --db
-n: --new (in INI file, set to 1 for true; 0/omitted for false)
-s: --schema
-i: --indexes
_EOF
exit 0;
}
sub parse_options {
my $argv = shift; # \@ARGV
my $Opts = {
new => 0, # create fresh DB, overwriting any old one?
};
my $ini_opts = sub {
my ($opt, $conf) = (@_);
(-r $conf) or fatal("Config missing or not readable");
require Config::IniFiles;
local @Config::IniFiles::errors; # surpress 'only used once' warning
my $cfg = Config::IniFiles->new( -file => $conf )
or fatal("INI parse error(s):", @Config::IniFiles::errors);
for (qw[ new db schema indexes ]) {
my $val = $cfg->val('config', $_);
$Opts->{$_} = $val if (defined $val);
}
};
Getopt::Long::GetOptionsFromArray( $argv,
'help|h|?' => \&usage_exit,
'config|c=s' => $ini_opts,
'new|n!' => \$Opts->{new},
'db|database|d=s' => \$Opts->{db},
'schema|s=s' => \$Opts->{schema},
'indexes|i=s' => \$Opts->{indexes},
) or die( "Options error" );
if ($Opts->{new}) {
(defined $Opts->{db}) or fatal("Missing --db file");
(defined $Opts->{schema}) or fatal("Missing --schema file");
(defined $Opts->{indexes}) or fatal("Missing --indexes file");
if (-f $Opts->{db}) {
unlink($Opts->{db}) or fatal("Removing existing db failed: $!");
}
}
return $Opts;
}
sub fatal {
printf(STDERR "%s\n", $_) for (@_);
exit 1;
}
sub main {
my $argv = shift; # \@ARGV
my $Opts = parse_options( $argv );
my $io7z = QKTech::ULS::IO7z->new();
$io7z->locate() or fatal("7z locate failed");
# Sanity-check remaining arguments, vetting as compresed archives.
check_archives( $argv, $io7z );
# Connect DB, creating or opening as required.
my $dbh;
my $index_sql = undef; # holds reference to full index SQL text
if ($Opts->{new}) {
($dbh, $index_sql) = mk_db(
db => $Opts->{db},
schema => $Opts->{schema},
indexes => $Opts->{indexes},
);
}
else {
$dbh = open_db( db => $Opts->{db} );
}
# Loop over remaining arguments, processing archives.
for my $arch (@$argv) {
runtime_schema( dbh => $dbh ); # (re)set ephemeral processing
my $tables = archive_prep(
new => $Opts->{new},
arch => $arch,
dbh => $dbh,
io7z => $io7z, # will set the archive path in-object
);
table_import(
tables => $tables,
dbh => $dbh,
io7z => $io7z, # uses existing archive path
);
$dbh->commit() if (not $Opts->{new});
}
continue {
$io7z->close();
if ($Opts->{new}) { # new weekly post-import needs
print( STDERR "Building indexes.." );
index_db( $dbh, $index_sql );
print( STDERR " Analyze DB.." );
$dbh->do('ANALYZE');
$dbh->commit();
$dbh->do('PRAGMA journal_mode = DELETE');
print( STDERR " Done.\n" );
$Opts->{new} = 0;
}
}
# Finish, setting DB tunables for normal use.
$dbh->{AutoCommit} = 1;
$dbh->do('ANALYZE');
}
sub check_archives {
my ($argv, $io7z) = (@_);
# Ensure each archive lists contents successfully.
for my $arch (@$argv) {
$io7z->setSource( $arch )
or fatal("archive access failure: " . $io7z->error());
$io7z->list()
or fatal("archive check failure: " . $io7z->error());
}
$io7z->close();
}
sub archive_prep {
my (%opts) = (@_);
my $arch = $opts{arch};
my $dbh = $opts{dbh};
my $io7z = $opts{io7z};
my $new = $opts{new};
$io7z->setSource( $arch )
or fatal("7z source-set fail: " . $io7z->error());
$io7z->extract('counts')
or fatal("7z extract fail: " . $io7z->error());
my @tables;
my $iso8601 = undef;
LINE:
while( defined(my $line = $io7z->readline()) ) {
for ($line) {
# Convert create-date line to ISO-8601
if ( m/^File Creation Date:
\s+\w+ # weekday
\s+(\w+) # month ($1)
\s+(\d+) # day ($2)
\s+(\d{2}:\d{2}:\d{2}) # HH:MM:SS ($3)
\s+(\w+) # timezone ($4)
\s+(\d{4}) # year ($5)
$/x
) {
# Convert Mo/TZ to usable ISO-8601 terms.
my $mo = MONTH_TO_DEC()->{uc($1)} // 13;
my $tz = TZ_TO_OFF()->{uc($4)} // '-04:00';
$iso8601 = "$5-$mo-$2T$3$tz";
}
# Prep each table file of interest for query-data
elsif ( m/(([A-Z]{2})\.dat)$/
) {
my $ulsData = QKTech::ULS::Data->new();
my $prep = $ulsData->can("query_$2") or next LINE;
my $table = QKTech::ULS::Table->new();
$table->define(
name => $2,
source => "$1",
iso8601 => $iso8601,
new => $new,
) or fatal("Table $2 failed defined: " . $table->error());
$ulsData->$prep( $table ); # prep table via Data's query_* method
push(@tables, $table);
}
}
}
# TODO: future date safety checks applying dalies
return \@tables;
}
sub table_import {
my (%opts) = (@_);
my $dbh = $opts{dbh};
my $io7z = $opts{io7z};
my $tables = $opts{tables};
# Prepare all table queries
for my $table (@$tables) {
my $rc = $table->prepare( dbh => $dbh );
if (not $rc) {
fatal( sprintf("Table %s prepare failed: %s",
$table->get('name'),
$table->error
) );
}
}
# Perform table imports
for my $table (@$tables) {
my $rc = $table->import(
dbh => $dbh,
io7z => $io7z,
);
if (not $rc) {
fatal( sprintf("Table %s import failed: %s",
$table->get('name'),
$table->error
) );
}
}
}
sub mk_db {
my %args = (@_);
my $db = $args{db};
my $schema = $args{schema};
my $indexes = $args{indexes};
# Need readable schema:
fatal("Can't read schema: $!") if ( not -r $schema );
# Read in index file, returned for later processing:
open(my $fh_idx, '<', $indexes) or fatal("Index open failed: $!");
my @index_sql = <$fh_idx>;
close($fh_idx);
# Read in full schema:
open(my $fh_schema, '<', $schema)
or fatal("Schema open failed: $!");
my @schema = <$fh_schema>;
close($fh_schema);
if (-f $db) {
unlink($db) or fatal("Removing old db failed: $!");
}
# New DB:
my $dbh = DBI->connect("dbi:SQLite:db=$db",
'', '', {
AutoCommit => 1,
PrintError => 0,
RaiseError => 1,
sqlite_use_immediate_transaction => 0,
sqlite_allow_multiple_statements => 1,
}
) or fatal("DB connect failed: $DBI::errstr");
# Enable FK enforcement:
db_enable_fk( $dbh );
# MEMORY mode, for import:
$dbh->do('PRAGMA journal_mode = MEMORY');
$dbh->{AutoCommit} = 0;
# Apply schema:
$dbh->do( "@schema" );
# Disable multiple-statements, no longer needed:
$dbh->{sqlite_allow_multiple_statements} = 0;
return ($dbh, \@index_sql);
}
sub open_db {
my %args = (@_);
my $db = $args{db};
# Need readable DB:
if ( not -r $db ) {
fatal("Database not readable: $db");
}
# open DB:
my $dbh = DBI->connect("dbi:SQLite:db=$db",
'', '', {
AutoCommit => 1,
PrintError => 0,
RaiseError => 1,
sqlite_use_immediate_transaction => 0,
}
) or fatal("DB connect failed: $DBI::errstr");
# Enable FK enforcement:
db_enable_fk( $dbh );
$dbh->{AutoCommit} = 0;
return $dbh;
}
sub db_enable_fk {
my $dbh = shift;
$dbh->do('PRAGMA foreign_keys(1);');
my $fk_row = $dbh->selectrow_arrayref('PRAGMA foreign_keys')
or fatal("FK row check failed to execute");
if ($fk_row->[0] != 1) {
fatal("FK is not enabled");
}
}
sub runtime_schema {
my %args = (@_);
my $dbh = $args{dbh} or fatal("No dbh to prep runtime");
$dbh->do('DROP TABLE IF EXISTS temp.t_vc_seen');
$dbh->do('CREATE TEMP TABLE t_vc_seen (sys_id INTEGER PRIMARY KEY)');
}
sub index_db {
my ($dbh, $indexes) = (@_);
$dbh->{AutoCommit} = 1;
$dbh->{sqlite_allow_multiple_statements} = 1;
$dbh->do( "@$indexes" );
$dbh->{sqlite_allow_multiple_statements} = 0;
$dbh->{AutoCommit} = 0;
}