~ubuntu-branches/ubuntu/utopic/spamassassin/utopic-updates

« back to all changes in this revision

Viewing changes to lib/Mail/SpamAssassin/BayesStore/PgSQL.pm

  • Committer: Package Import Robot
  • Author(s): Noah Meyerhans
  • Date: 2014-02-14 22:45:15 UTC
  • mfrom: (0.8.1) (0.6.2) (5.1.22 sid)
  • Revision ID: package-import@ubuntu.com-20140214224515-z1es2twos8xh7n2y
Tags: 3.4.0-1
* New upstream version! (Closes: 738963, 738872, 738867)
* Scrub the environment when switching to the debian-spamd user in
  postinst and cron.daily. (Closes: 738951)
* Enhancements to postinst to better manage ownership of
  /var/lib/spamassassin, via Iain Lane <iain.lane@canonical.com>
  (Closes: 738974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
305
305
  unless (defined($rows)) {
306
306
    dbg("bayes: set_running_expire_tok: SQL error: ".$self->{_dbh}->errstr());
307
307
    $self->{_dbh}->rollback();
308
 
    return undef;
 
308
    return;
309
309
  }
310
310
 
311
311
  $self->{_dbh}->commit();
640
640
 
641
641
=cut
642
642
 
 
643
# tok_touch_all as proposed in bug 6444, executes one update for each token;
 
644
# might run faster with some (older?) versions of PostgreSQL
 
645
# (to switch: rename this one to tok_touch_all and stash the other one away)
 
646
#
 
647
sub tok_touch_all_6444 {
 
648
  my ($self, $tokens, $atime) = @_;
 
649
 
 
650
  return 0 unless (defined($self->{_dbh}));
 
651
 
 
652
  return 1 unless (scalar(@{$tokens}));
 
653
 
 
654
  my $sql =
 
655
    "UPDATE bayes_token SET atime=? WHERE id=? AND token=? AND atime < ?";
 
656
 
 
657
  my $sth = $self->{_dbh}->prepare_cached($sql);
 
658
  unless (defined($sth)) {
 
659
    dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
 
660
    return 0;
 
661
  }
 
662
 
 
663
  my $bytea_type = { pg_type => DBD::Pg::PG_BYTEA };
 
664
  $sth->bind_param(1, $atime);
 
665
  $sth->bind_param(2, $self->{_userid});
 
666
# $sth->bind_param(3, $token, $bytea_type);  # later
 
667
  $sth->bind_param(4, $atime);
 
668
 
 
669
  $self->{_dbh}->begin_work();
 
670
 
 
671
  my $n_updates = 0;
 
672
  foreach my $token (@$tokens) {
 
673
    $sth->bind_param(3, $token, $bytea_type);
 
674
    my $rc = $sth->execute();
 
675
    unless ($rc) {
 
676
      dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
 
677
      $self->{_dbh}->rollback();
 
678
      return 0;
 
679
    }
 
680
    my $rows = $sth->rows;
 
681
    unless (defined($rows) && $rows >= 0) {
 
682
      dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
 
683
      $self->{_dbh}->rollback();
 
684
      return 0;
 
685
    }
 
686
    if ($rows > 0) { $n_updates++ }
 
687
  }
 
688
 
 
689
  # if we didn't update a row then no need to update newest_token_age
 
690
  if ($n_updates) {  # update newest_token_age
 
691
    # need to check newest_token_age
 
692
    # no need to check oldest_token_age since we would only update if the
 
693
    # atime was newer than what is in the database
 
694
    my $sql_upd_age = "UPDATE bayes_vars SET newest_token_age = ?".
 
695
                      " WHERE id = ? AND newest_token_age < ?";
 
696
    my $rows = $self->{_dbh}->do($sql_upd_age,
 
697
                                 undef, $atime, $self->{_userid}, $atime);
 
698
    unless (defined($rows) && $rows >= 0) {
 
699
      dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
 
700
      $self->{_dbh}->rollback();
 
701
      return 0;
 
702
    }
 
703
  }
 
704
  $self->{_dbh}->commit();
 
705
  dbg("bayes: tok_touch_all: updated %d tokens", $n_updates);
 
706
  return 1;
 
707
}
 
708
 
 
709
# original tok_touch_all (not the one proposed in bug 6444),
 
710
# executes one update for all token using an IN operator;
 
711
# seems to run faster with PostgreSQL 8.3.14 than the alternative 
 
712
#
643
713
sub tok_touch_all {
644
714
  my ($self, $tokens, $atime) = @_;
645
715
 
724
794
  return 1;
725
795
}
726
796
 
727
 
 
728
 
sub tok_touch_allold {
729
 
  my ($self, $tokens, $atime) = @_;
730
 
 
731
 
  return 0 unless (defined($self->{_dbh}));
732
 
 
733
 
  return 1 unless (scalar(@{$tokens}));
734
 
 
735
 
  my $tokenarray = join(",", map { '"' . _quote_bytea($_) . '"' } sort @{$tokens});
736
 
 
737
 
  my $sth = $self->{_dbh}->prepare("select touch_tokens($self->{_userid}, $self->{_esc_prefix}'{$tokenarray}', $atime)");
738
 
 
739
 
  unless (defined($sth)) {
740
 
    dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
741
 
    $self->{_dbh}->rollback();
742
 
    return 0;
743
 
  }
744
 
 
745
 
  my $rc = $sth->execute();
746
 
 
747
 
  unless ($rc) {
748
 
    dbg("bayes: tok_touch_all: SQL error: ".$self->{_dbh}->errstr());
749
 
    $self->{_dbh}->rollback();
750
 
    return 0;
751
 
  }
752
 
 
753
 
  $sth->finish();
754
 
 
755
 
  $self->{_dbh}->commit();
756
 
 
757
 
  return 1;
758
 
}
759
 
 
760
797
=head2 cleanup
761
798
 
762
799
public instance (Boolean) cleanup ()