+ my ($s,$binname, $binver, $binarch, $srcname, $srcver,$ct_date,$cache) = @_;
+ $cache //= {};
+ my $sp;
+ if (not defined $cache->{sp}{$srcname}) {
+ $cache->{sp}{$srcname} =
+ $s->resultset('SrcPkg')->find_or_create({pkg => $srcname});
+ }
+ $sp = $cache->{sp}{$srcname};
+ # update the creation date if the data we have is earlier
+ if (defined $ct_date and
+ (not defined $sp->creation or
+ $ct_date < $sp->creation)) {
+ $sp->creation($ct_date);
+ $sp->last_modified(DateTime->now);
+ $sp->update;
+ }
+ my $sv;
+ if (not defined $cache->{sv}{$srcname}{$srcver}) {
+ $cache->{sv}{$srcname}{$srcver} =
+ $s->resultset('SrcVer')->
+ find_or_create({src_pkg =>$sp->id(),
+ ver => $srcver});
+ }
+ $sv = $cache->{sv}{$srcname}{$srcver};
+ if (defined $ct_date and
+ (not defined $sv->upload_date() or $ct_date < $sv->upload_date())) {
+ $sv->upload_date($ct_date);
+ $sv->update;
+ }
+ my $arch;
+ if (not defined $cache->{arch}{$binarch}) {
+ $cache->{arch}{$binarch} =
+ $s->resultset('Arch')->
+ find_or_create({arch => $binarch},
+ )->id();
+ }
+ $arch = $cache->{arch}{$binarch};
+ my $bp;
+ if (not defined $cache->{bp}{$binname}) {
+ $cache->{bp}{$binname} =
+ $s->resultset('BinPkg')->
+ get_or_create_bin_pkg_id($binname);
+ }
+ $bp = $cache->{bp}{$binname};
+ $s->resultset('BinVer')->
+ get_bin_ver_id($bp,$binver,$arch,$sv->id());
+}
+
+
+=back
+
+=head2 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_or_create_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) {
+ $it = natatime 1000, @sa_to_del;
+ while (my @v = $it->()) {
+ $schema->
+ txn_do(sub {
+ $schema->resultset('SrcAssociation')->
+ search_rs({id => \@v})->
+ 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_or_create_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_or_create_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) {
+ $it = natatime 1000, @bin_to_del;
+ while (my @v = $it->()) {
+ $schema->
+ txn_do(sub {
+ $schema->resultset('BinAssociation')->
+ search_rs({id => \@v})->
+ delete();
+ });
+ }
+ }
+ $schema->resultset('BinAssociation')->
+ search_rs({suite => $suite_id})->
+ update({modified => 'NOW()'});
+