]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/DB/Load.pm
preload bugs on initial insert
[debbugs.git] / Debbugs / DB / Load.pm
index 47ce8aeaa777c0765de4ed85c06a1c023650fd66..f583f51be704c99d62b82a877ca48f9392e4ab64 100644 (file)
@@ -31,7 +31,10 @@ BEGIN{
      $DEBUG = 0 unless defined $DEBUG;
 
      @EXPORT = ();
-     %EXPORT_TAGS = (load_bug    => [qw(load_bug handle_load_bug_queue)],
+     %EXPORT_TAGS = (load_bug    => [qw(load_bug handle_load_bug_queue load_bug_log)],
+                    load_debinfo => [qw(load_debinfo)],
+                    load_package => [qw(load_packages)],
+                    load_suite => [qw(load_suite)],
                    );
      @EXPORT_OK = ();
      Exporter::export_ok_tags(keys %EXPORT_TAGS);
@@ -39,6 +42,7 @@ BEGIN{
 }
 
 use Params::Validate qw(validate_with :types);
+use List::MoreUtils qw(natatime);
 
 use Debbugs::Status qw(read_bug split_status_fields);
 use Debbugs::DB;
@@ -155,7 +159,7 @@ sub load_bug {
     for my $addr_type (keys %addr_map) {
         my @addrs = getparsedaddrs($data->{$addr_map{$addr_type}} // '');
         next unless @addrs;
-        $bug->{$addr_type} = $s->resultset('Correspondent')->find_or_create({addr => $addrs[0]->address()});
+        $bug->{$addr_type} = $s->resultset('Correspondent')->find_or_create({addr => lc($addrs[0]->address())});
         # insert the full name as well
         my $full_name = $addrs[0]->phrase();
         $full_name =~ s/^\"|\"$//g;
@@ -286,9 +290,19 @@ sub handle_load_bug_queue{
     }
 }
 
-=item load_bug_log
+=item load_bug_log -- load bug logs
 
-     load_bug_log(db => $db, bug => $bug_num);
+       load_bug_log(db  => $s,
+                    bug => $bug);
+
+
+=over
+
+=item db -- database 
+
+=item bug -- bug whose log should be loaded
+
+=back
 
 =cut
 
@@ -347,7 +361,7 @@ Commands to handle src and package version loading from debinfo files
 sub load_debinfo {
     my ($schema,$binname, $binver, $binarch, $srcname, $srcver) = @_;
     my $sp = $schema->resultset('SrcPkg')->find_or_create({pkg => $srcname});
-    my $sv = $schema->resultset('SrcVer')->find_or_create({src_pkg_id=>$sp->id(),
+    my $sv = $schema->resultset('SrcVer')->find_or_create({src_pkg=>$sp->id(),
                                                            ver => $srcver});
     my $arch = $schema->resultset('Arch')->find_or_create({arch => $binarch});
     my $bp = $schema->resultset('BinPkg')->find_or_create({pkg => $binname});
@@ -359,6 +373,226 @@ sub load_debinfo {
 }
 
 
+=back
+
+=head Packages
+
+=over
+
+=item load_package
+
+     load_package($schema,$suite,$component,$arch,$pkg)
+
+=cut
+
+sub load_packages {
+    my ($schema,$suite,$pkgs,$p) = @_;
+    my $suite_id = $schema->resultset('Suite')->
+       find_or_create({codename => $suite})->id;
+    my %maint_cache;
+    my %arch_cache;
+    my %source_cache;
+    my $src_max_last_modified = $schema->resultset('SrcAssociation')->
+       search_rs({suite => $suite_id},
+                {order_by => {-desc => ['me.modified']},
+                 rows => 1,
+                 page => 1
+                }
+                )->single();
+    my $bin_max_last_modified = $schema->resultset('BinAssociation')->
+       search_rs({suite => $suite_id},
+                {order_by => {-desc => ['me.modified']},
+                 rows => 1,
+                 page => 1
+                }
+                )->single();
+    my %maints;
+    my %sources;
+    my %bins;
+    for my $pkg_tuple (@{$pkgs}) {
+       my ($arch,$component,$pkg) = @{$pkg_tuple};
+       $maints{$pkg->{Maintainer}} = $pkg->{Maintainer};
+       if ($arch eq 'source') {
+           my $source = $pkg->{Package};
+           my $source_ver = $pkg->{Version};
+           $sources{$source}{$source_ver} = $pkg->{Maintainer};
+       } else {
+           my $source = $pkg->{Source} // $pkg->{Package};
+           my $source_ver = $pkg->{Version};
+           if ($source =~ /^\s*(\S+) \(([^\)]+)\)\s*$/) {
+               ($source,$source_ver) = ($1,$2);
+           }
+           $sources{$source}{$source_ver} = $pkg->{Maintainer};
+           $bins{$arch}{$pkg->{Package}} =
+              {arch => $arch,
+               bin => $pkg->{Package},
+               bin_ver => $pkg->{Version},
+               src_ver => $source_ver,
+               source  => $source,
+               maint   => $pkg->{Maintainer},
+              };
+       }
+    }
+    # Retrieve and Insert new maintainers
+    my $maints =
+       $schema->resultset('Maintainer')->
+       get_maintainers(keys %maints);
+    my $archs =
+       $schema->resultset('Arch')->
+       get_archs(keys %bins);
+    # We want all of the source package/versions which are in this suite to
+    # start with
+    my @sa_to_add;
+    my @sa_to_del;
+    my %included_sa;
+    # Calculate which source packages are no longer in this suite
+    for my $s ($schema->resultset('SrcPkg')->
+              src_pkg_and_ver_in_suite($suite)) {
+       if (not exists $sources{$s->{pkg}} or
+           not exists $sources{$s->{pkg}}{$s->{src_vers}{ver}}
+          ) {
+           push @sa_to_del,
+               $s->{src_associations}{id};
+       }
+       $included_sa{$s->{pkg}}{$s->{src_vers}} = 1;
+    }
+    # Calculate which source packages are newly in this suite
+    for my $s (keys %sources) {
+       for my $v (keys %{$sources{$s}}) {
+           if (not exists $included_sa{$s} and
+               not $included_sa{$s}{$v}) {
+               push @sa_to_add,
+                   [$s,$v,$sources{$s}{$v}];
+           } else {
+               $p->update() if defined $p;
+           }
+       }
+    }
+    # add new source packages
+    my $it = natatime 100, @sa_to_add;
+    while (my @v = $it->()) {
+       $schema->txn_do(
+           sub {
+               for my $svm (@_) {
+                   my $s_id = $schema->resultset('SrcPkg')->
+                       get_src_pkg_id($svm->[0]);
+                   my $sv_id = $schema->resultset('SrcVer')->
+                       get_src_ver_id($s_id,$svm->[1],$maints->{$svm->[2]});
+                   $schema->resultset('SrcAssociation')->
+                       insert_suite_src_ver_association($suite_id,$sv_id);
+               }
+           },
+                       @v
+                      );
+       $p->update($p->last_update()+
+                  scalar @v) if defined $p;
+    }
+    # remove associations for packages not in this suite
+    if (@sa_to_del) {
+       $schema->resultset('SrcAssociation')->
+           search_rs({id => \@sa_to_del})->delete();
+    }
+    # update packages in this suite to have a modification time of now
+    $schema->resultset('SrcAssociation')->
+       search_rs({suite => $suite_id})->
+       update({modified => 'NOW()'});
+    ## Handle binary packages
+    my @bin_to_del;
+    my @bin_to_add;
+    my %included_bin;
+    # calculate which binary packages are no longer in this suite
+    for my $b ($schema->resultset('BinPkg')->
+              bin_pkg_and_ver_in_suite($suite)) {
+       if (not exists $bins{$b->{arch}{arch}} or
+           not exists $bins{$b->{arch}{arch}}{$b->{pkg}} or
+           ($bins{$b->{arch}{arch}}{$b->{pkg}}{bin_ver} ne
+            $b->{bin_vers}{ver}
+           )
+          ) {
+           push @bin_to_del,
+               $b->{bin_associations}{id};
+       }
+       $included_bin{$b->{arch}{arch}}{$b->{pkg}} =
+           $b->{bin_vers}{ver};
+    }
+    # calculate which binary packages are newly in this suite
+    for my $a (keys %bins) {
+       for my $pkg (keys %{$bins{$a}}) {
+           if (not exists $included_bin{$a} or
+               not exists $included_bin{$a}{$pkg} or
+               $bins{$a}{$pkg}{bin_ver} ne
+               $included_bin{$a}{$pkg}) {
+               push @bin_to_add,
+                   $bins{$a}{$pkg};
+           } else {
+               $p->update() if defined $p;
+           }
+       }
+    }
+    $it = natatime 100, @bin_to_add;
+    while (my @v = $it->()) {
+       $schema->txn_do(
+       sub {
+           for my $bvm (@_) {
+               my $s_id = $schema->resultset('SrcPkg')->
+                   get_src_pkg_id($bvm->{source});
+               my $sv_id = $schema->resultset('SrcVer')->
+                   get_src_ver_id($s_id,$bvm->{src_ver},$maints->{$bvm->{maint}});
+               my $b_id = $schema->resultset('BinPkg')->
+                   get_bin_pkg_id($bvm->{bin});
+               my $bv_id = $schema->resultset('BinVer')->
+                   get_bin_ver_id($b_id,$bvm->{bin_ver},
+                                  $archs->{$bvm->{arch}},$sv_id);
+               $schema->resultset('BinAssociation')->
+                   insert_suite_bin_ver_association($suite_id,$bv_id);
+           }
+       },
+                       @v
+                      );
+       $p->update($p->last_update()+
+                  scalar @v) if defined $p;
+    }
+    if (@bin_to_del) {
+       $schema->resultset('BinAssociation')->
+           search_rs({id => \@bin_to_del})->delete();
+    }
+    $schema->resultset('BinAssociation')->
+       search_rs({suite => $suite_id})->
+       update({modified => 'NOW()'});
+
+}
+
+
+=back
+
+=cut
+
+=head Suites
+
+=over
+
+=item load_suite
+
+     load_suite($schema,$codename,$suite,$version,$active);
+
+=cut
+
+sub load_suite {
+    my ($schema,$codename,$suite,$version,$active) = @_;
+    if (ref($codename)) {
+       ($codename,$suite,$version) =
+           @{$codename}{qw(Codename Suite Version)};
+       $active = 1;
+    }
+    my $s = $schema->resultset('Suite')->find_or_create({codename => $codename});
+    $s->suite_name($suite);
+    $s->version($version);
+    $s->active($active);
+    $s->update();
+    return $s;
+
+}
+
 =back
 
 =cut