chiark / gitweb /
routesearch: fix up web performance parameterisation
[ypp-sc-tools.db-live.git] / yarrg / commod-email-processor
index d2f0e819f6ef8c0f41626424fe13beb624d4f088..1cff462cda1c67d451ee8064b9ac952e8e096cae 100755 (executable)
 # are used without permission.  This program is not endorsed or
 # sponsored by Three Rings.
 
-# Emails are:
-#  multipart/mixed, containing
-#   text/plain; name="metadata"; charset="utf-8"
-#   Content-Disposition: inline; filename="metadata"
-#     ocean\t<ocean>           canonical mixed case
-#     island\t<island>         canonical mixed case
-#     timestamp\t<digits>      time_t (non-leap secs since start of 1970 UTC)
-#     clientname\t<cname>      may contain spaces
-#     clientversion\t<cversion>        may contain spaces
-#     clientfixes\t<cfixes>    space-delimited list
-#     clientspec\t<cspec>      <cname>\t<cversion>\t<cfixes>
-#     servername\t<sname>      may contain spaces
-#     serverversion\t<sversion>        may contain spaces
-#     serverfixes\t<sfixes>    space-delimited list
-#     serverspec\t<sspec>      <sname>\t<serverversion>\t<serverfixes>
-#   application/octet-stream; name="deduped.tsv.gz"
-#   Content-Disposition: attachment; filename="deduped.tsv.gz"
-#     <base64>
-
 use strict (qw(vars));
 
 use POSIX;
 use MIME::Parser;
 
+BEGIN {
+    my $selfdir= $0;
+    $selfdir =~ s,/+[^/]*$,,;
+    chdir("$selfdir") or die "$selfdir $!";
+}
+
 use Commods;
 use CommodsDatabase;
 
-setlocale(LC_CTYPE, "en_GB.UTF-8");
+set_ctype_utf8();
 my $parser= new MIME::Parser;
 our $entity;
 
@@ -111,7 +98,9 @@ sub main () {
     db_setocean($md{'ocean'});
     my $dbfn= db_filename();
     (stat $dbfn) or die "stat database $dbfn failed $!\n";
+    db_writer();
     db_connect();
+    db_onconflict(sub { print STDERR "temporary failure: @_\n"; exit 75; });
 
     my ($islandid) = $dbh->selectrow_array(
               "SELECT islands.islandid
@@ -134,33 +123,38 @@ sub main () {
             $islandid, $mid,
             map { $md{$_} } (qw(timestamp clientspec serverspec)));
 
-    my (%sth, %sub_cs, %cache_cs, %sth_insert);
+    my (%sth, %sub_cs, %cache_cs, %sth_insert, %sth_lookup);
 
     $sth_insert{'stall'}= $dbh->prepare(
                 "INSERT OR IGNORE
                         INTO stalls
                         (islandid, stallname) VALUES ($islandid, ?)
                 ");
-    $sth_insert{'commods'}= $dbh->prepare(
+    $sth_lookup{'stall'}= $dbh->prepare(
+                "SELECT stallid FROM stalls
+                       WHERE islandid == $islandid AND stallname == ?
+                ");
+    $sth_insert{'commod'}= $dbh->prepare(
                 "INSERT OR IGNORE
                         INTO commods
                         (commodname) VALUES (?)
                 ");
+    $sth_lookup{'commod'}= $dbh->prepare(
+                "SELECT commodid FROM commods
+                       WHERE commodname == ?
+                ");
 
     foreach my $cs (qw(stall commod)) {
-       my $sth_lookup= $dbh->prepare(
-                "SELECT ${cs}id FROM ${cs}s WHERE ${cs}name == ?;
-                ");
        $sub_cs{$cs}= sub {
            my ($name)= @_;
            my $r= $cache_cs{$cs}{$name};
            return $r if defined $r;
-           $sth_lookup->execute($name) or die;
-           ($r)= $sth_lookup->fetchrow_array();
+           $sth_lookup{$cs}->execute($name) or die;
+           ($r)= $sth_lookup{$cs}->fetchrow_array();
            if (!defined $r) {
                $sth_insert{$cs}->execute($name);
-               $sth_lookup->execute($name) or die;
-               ($r)= $sth_lookup->fetchrow_array();
+               $sth_lookup{$cs}->execute($name) or die;
+               ($r)= $sth_lookup{$cs}->fetchrow_array();
                die unless defined $r;
            }
            $cache_cs{$cs}{$name}= $r;
@@ -200,7 +194,7 @@ sub main () {
 
     pipethrough_run_finish($pt, 'gunzip <$deduped_tsv.gz');
 
-    print "\n";
+#    print "\n";
     $dbh->commit();
 
     # select * from ((buy natural join commods) natural join stalls) natural join islands;