You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by mm...@apache.org on 2009/02/09 20:10:32 UTC
svn commit: r742680 -
/spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm
Author: mmartinec
Date: Mon Feb 9 19:10:31 2009
New Revision: 742680
URL: http://svn.apache.org/viewvc?rev=742680&view=rev
Log:
some rather cosmetic changes to BDB.pm: wrap long lines; remove some
redundant parenthesis; TAB -> SP in POD sections (doesn't come out
nice); uncomment most dbg calls for the time being, change debug
area id 'BDB:' -> 'bayes:' for consistency with other backends;
changed 'assert'-like idiom to: 'assert_condition or die' (previously
a mix of 'negative_condition and die' and 'positive_cond or die'
was used); test status of BerkeleyDB methods for zero as per docs,
instead of testing for boolean; avoid idiom 'my $v=expr or die/dbg'
separating assignment and a test ('my $x=0 or die' yields:
'Found = in conditional', while 'my($x)=0 or die' never dies)
Modified:
spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm
Modified: spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm?rev=742680&r1=742679&r2=742680&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/BayesStore/BDB.pm Mon Feb 9 19:10:31 2009
@@ -87,9 +87,10 @@
sub tie_db_readonly {
my($self) = @_;
- #dbg("BDB: tie_db_readonly");
- my $result = ($self->{already_tied} and $self->{is_locked} == 0) || $self->_tie_db(0);
- #dbg("BDB: tie_db_readonly result is $result");
+ dbg("bayes: tie_db_readonly");
+ my $result = ($self->{already_tied} && $self->{is_locked} == 0)
+ || $self->_tie_db(0);
+ dbg("bayes: tie_db_readonly result is $result");
return $result;
}
@@ -98,7 +99,7 @@
public instance (Boolean) tie_db_writable ()
Description:
-This method ensures that the database connetion is properly setup and
+This method ensures that the database connection is properly setup and
working. If necessary it will initialize the database so that they can
begin using the database immediately.
@@ -106,9 +107,10 @@
sub tie_db_writable {
my($self) = @_;
- #dbg("BDB: tie_db_writable");
- my $result = ($self->{already_tied} and $self->{is_locked} == 1) || $self->_tie_db(1);
- #dbg("BDB: tie_db_writable result is $result");
+ dbg("bayes: tie_db_writable");
+ my $result = ($self->{already_tied} && $self->{is_locked} == 1)
+ || $self->_tie_db(1);
+ dbg("bayes: tie_db_writable result is $result");
return $result;
}
@@ -117,8 +119,8 @@
private instance (Boolean) _tie_db (Boolean $writeable)
Description:
-This method ensures that the database connetion is properly setup and
-working. If it will initialize a users bayes variables so that they
+This method ensures that the database connection is properly setup and
+working. It will initialize a users bayes variables so that they
can begin using the database immediately.
=cut
@@ -126,28 +128,28 @@
sub _tie_db {
my($self, $writeable) = @_;
- #dbg("BDB: _tie_db($writeable)");
+ dbg("bayes: _tie_db($writeable)");
# Always notice state changes
$self->{is_locked} = $writeable;
- return 1 if($self->{already_tied});
+ return 1 if $self->{already_tied};
- #dbg("BDB: not already tied");
+ dbg("bayes: not already tied");
my $main = $self->{bayes}->{main};
if (!defined($main->{conf}->{bayes_path})) {
- #dbg("BDB: bayes_path not defined");
+ dbg("bayes: bayes_path not defined");
return 0;
}
- #dbg("BDB: Reading db configs");
+ dbg("bayes: Reading db configs");
$self->read_db_configs();
my $path = dirname $main->sed_path($main->{conf}->{bayes_path});
- #dbg("BDB: Path is $path");
+ dbg("bayes: Path is $path");
# Path must exist or we must be in writeable mode
if (-d $path) {
# All is cool
@@ -156,77 +158,93 @@
eval {
mkpath($path, 0, (oct($main->{conf}->{bayes_file_mode}) & 0777));
};
- warn("BDB: Couldn't create path: $@") if ($@);
+ warn("bayes: Couldn't create path: $@") if $@;
} else {
# FAIL
- warn("BDB: bayes_path doesn't exist and can't create: $path");
+ warn("bayes: bayes_path doesn't exist and can't create: $path");
return 0;
}
# Now we can set up our environment
my $flags = DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN;
- $flags |= DB_CREATE if($writeable);
+ $flags |= DB_CREATE if $writeable;
# DB_REGISTER|DB_RECOVER|
- #dbg("BDB: Creating environment: $path, $flags, $main->{conf}->{bayes_file_mode}");
- unless ($self->{env} = BerkeleyDB::Env->new(-Cachesize => 67108864, -Home => $path, -Flags => $flags, -Mode =>(oct($main->{conf}->{bayes_file_mode}) & 0666), -SetFlags => DB_LOG_AUTOREMOVE)) {
- #dbg("BDB: berkeleydb environment couldn't initialize: $BerkeleyDB::Error");
+ dbg("bayes: %s environment: %s, 0x%x, %s",
+ $writeable ? 'Creating' : 'Opening existing',
+ $path, $flags, $main->{conf}->{bayes_file_mode});
+ unless ($self->{env} = BerkeleyDB::Env->new(
+ -Cachesize => 67108864, -Home => $path, -Flags => $flags,
+ -Mode => (oct($main->{conf}->{bayes_file_mode}) & 0666),
+ -SetFlags => DB_LOG_AUTOREMOVE)) {
+ dbg("bayes: berkeleydb environment couldn't initialize: $BerkeleyDB::Error");
return 0;
}
$flags = $writeable ? DB_CREATE : 0;
- #dbg("BDB: Opening vars");
- unless ($self->{handles}->{vars} = BerkeleyDB::Btree->new(-Env => $self->{env}, -Filename => "vars.db", -Flags => $flags)) {
- warn("BDB: couldn't open vars.db: $BerkeleyDB::Error");
+ dbg("bayes: Opening vars");
+ unless ($self->{handles}->{vars} = BerkeleyDB::Btree->new(
+ -Env => $self->{env}, -Filename => "vars.db", -Flags => $flags)) {
+ warn("bayes: couldn't open vars.db: $BerkeleyDB::Error");
$self->untie_db;
return 0;
}
- #dbg("BDB: Looking for db_version");
+ dbg("bayes: Looking for db_version");
unless ($self->{db_version} = $self->_get(vars => "DB_VERSION")) {
if ($writeable) {
$self->{db_version} = $self->DB_VERSION;
- $self->{handles}->{vars}->db_put(DB_VERSION => $self->{db_version}) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(NTOKENS => 0) and die "Couldn't put record: $BerkeleyDB::Error";
- #dbg("BDB: new db, set db version " . $self->{db_version} . " and 0 tokens");
+ $self->{handles}->{vars}->db_put(DB_VERSION => $self->{db_version}) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(NTOKENS => 0) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ dbg("bayes: new db, set db version %s and 0 tokens",$self->{db_version});
} else {
- warn("BDB: vars.db not intialized: $BerkeleyDB::Error");
+ warn("bayes: vars.db not intialized: $BerkeleyDB::Error");
$self->untie_db;
return 0;
}
} elsif ($self->{db_version}) {
- #dbg("BDB: found bayes db version $self->{db_version}");
+ dbg("bayes: found bayes db version $self->{db_version}");
if ($self->{db_version} != $self->DB_VERSION) {
- warn("BDB: bayes db version $self->{db_version} is not able to be used, aborting: $BerkeleyDB::Error");
+ warn("bayes: bayes db version $self->{db_version} is not able to be used, aborting: $BerkeleyDB::Error");
$self->untie_db();
return 0;
}
}
- #dbg("BDB: Opening tokens");
- unless ($self->{handles}->{tokens} = BerkeleyDB::Btree->new(-Env => $self->{env}, -Filename => "tokens.db", -Flags => $flags, -Property => DB_REVSPLITOFF)) {
- warn("BDB: couldn't open tokens.db: $BerkeleyDB::Error");
+ dbg("bayes: Opening tokens");
+ unless ($self->{handles}->{tokens} = BerkeleyDB::Btree->new(
+ -Env => $self->{env}, -Filename => "tokens.db",
+ -Flags => $flags, -Property => DB_REVSPLITOFF)) {
+ warn("bayes: couldn't open tokens.db: $BerkeleyDB::Error");
$self->untie_db;
return 0;
}
- #dbg("BDB: Opening atime secondary DB");
- unless ($self->{handles}->{atime} = BerkeleyDB::Btree->new(-Env => $self->{env}, -Filename => "atime.db", -Flags => $flags, -Property => DB_DUP|DB_DUPSORT)) {
- warn("BDB: couldn't open atime.db: $BerkeleyDB::Error");
+ dbg("bayes: Opening atime secondary DB");
+ unless ($self->{handles}->{atime} = BerkeleyDB::Btree->new(
+ -Env => $self->{env}, -Filename => "atime.db",
+ -Flags => $flags, -Property => DB_DUP|DB_DUPSORT)) {
+ warn("bayes: couldn't open atime.db: $BerkeleyDB::Error");
$self->untie_db;
return 0;
}
- #dbg("BDB: Opening seen DB");
- unless ($self->{handles}->{seen} = BerkeleyDB::Btree->new(-Env => $self->{env}, -Filename => "seen.db", -Flags => $flags)) {
- warn("BDB: couldn't open tokens.db: $BerkeleyDB::Error");
+ dbg("bayes: Opening seen DB");
+ unless ($self->{handles}->{seen} = BerkeleyDB::Btree->new(
+ -Env => $self->{env}, -Filename => "seen.db", -Flags => $flags)) {
+ warn("bayes: couldn't open tokens.db: $BerkeleyDB::Error");
$self->untie_db;
return 0;
}
- # This MUST be outside the transaction that opens the DB, or it just doesn't work. Dunno Why.
- $self->{handles}->{tokens}->associate($self->{handles}->{atime}, \&_extract_atime) and die "Couldn't associate DBs: $BerkeleyDB::Error";
+ # This MUST be outside the transaction that opens the DB,
+ # or it just doesn't work. Dunno Why.
+ !$self->{handles}->{tokens}->associate($self->{handles}->{atime},
+ \&_extract_atime)
+ or die "Couldn't associate DBs: $BerkeleyDB::Error";
$self->{already_tied} = 1;
@@ -255,7 +273,7 @@
delete $self->{handles}->{$handle};
}
- $self->{env}->txn_checkpoint (128, 1) if $self->{env};
+ $self->{env}->txn_checkpoint(128, 1) if $self->{env};
delete $self->{env};
return undef;
@@ -263,9 +281,8 @@
=head2 calculate_expire_delta
-public instance (%) calculate_expire_delta (Integer $newest_atime,
- Integer $start,
- Integer $max_expire_mult)
+public instance (%) calculate_expire_delta (
+ Integer $newest_atime, Integer $start, Integer $max_expire_mult)
Description:
This method performs a calculation on the data to determine the
@@ -274,12 +291,13 @@
=cut
sub calculate_expire_delta {
- #dbg("BDB: calculate_expire_delta starting");
+ dbg("bayes: calculate_expire_delta starting");
my($self, $newest_atime, $start, $max_expire_mult) = @_;
my %delta; # use a hash since an array is going to be very sparse
- my $cursor = $self->{handles}->{atime}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $cursor = $self->{handles}->{atime}->db_cursor;
+ $cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
my($atime, $value) = ("", "");
@@ -287,7 +305,8 @@
# *secondary* index, avoiding the decoding overhead
while ($cursor->c_get($atime, $value, $next) == 0) {
- # Go through from $start * 1 to $start * 512, mark how many tokens we would expire
+ # Go through from $start * 1 to $start * 512, mark how many tokens
+ # we would expire
my $age = $newest_atime - $atime;
for (my $i = 1; $i <= $max_expire_mult; $i <<= 1) {
if ($age >= $start * $i) {
@@ -300,10 +319,11 @@
}
}
- $cursor->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ $cursor->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $cursor;
- #dbg("BDB: calculate_expire_delta done");
+ dbg("bayes: calculate_expire_delta done");
return %delta;
}
@@ -321,56 +341,63 @@
=cut
sub token_expiration {
- #dbg("BDB: Entering token_expiration");
+ dbg("bayes: Entering token_expiration");
my($self, $opts, $newdelta, @vars) = @_;
my($kept, $deleted, $hapaxes, $lowfreq) = (0, 0, 0, 0);
# Reset stray too-new tokens
{
- my $cursor = $self->{handles}->{atime}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $cursor = $self->{handles}->{atime}->db_cursor;
+ $cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
# Grab the token for a tight RWM loop
my($atime, $flag) = ($vars[10], DB_SET_RANGE|$rmw);
# Find the first token eq or gt the current newest
while ($cursor->c_pget($atime, my $token, my $value, $flag) == 0) {
my($ts, $th, $current) = _unpack_token($value);
- $self->{handles}->{tokens}->db_put($token, _pack_token($ts, $th, $atime)) and die "Couldn't put record: $BerkeleyDB::Error";
- $flag = $next|$rmw; # We need to adjust our flag to continue on from the first rec
+ $self->{handles}->{tokens}->db_put($token,
+ _pack_token($ts, $th, $atime)) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ # We need to adjust our flag to continue on from the first rec
+ $flag = $next|$rmw;
}
- $cursor->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ $cursor->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $cursor;
}
# Figure out how old is too old...
my $too_old = $vars[10] - $newdelta; # tooold = newest - delta
- #dbg("BDB: Too old is $too_old");
+ dbg("bayes: Too old is $too_old");
- #dbg("BDB: Getting db stats");
+ dbg("bayes: Getting db stats");
my $count;
# Estimate the number of keys to be deleted
{
my $stats = $self->{handles}->{atime}->db_stat(DB_FAST_STAT);
- #dbg("DBD: Stats: " . Dumper $stats);
- # Scan if we've never gotten stats before
- $stats = $self->{handles}->{atime}->db_stat if($stats->{bt_ndata} == 0);
- #dbg("DBD: Stats: " . Dumper $stats);
- if ($self->{handles}->{atime}->db_key_range($too_old, my $less, my $equal, my $greater) == 0) {
- #dbg("DBD: less is $less, equal is $equal, greater is $greater");
+ dbg("DBD: Stats: " . Dumper $stats);
+ # Scan if we've never gotten stats before
+ $stats = $self->{handles}->{atime}->db_stat if $stats->{bt_ndata} == 0;
+ dbg("DBD: Stats: " . Dumper $stats);
+ if ($self->{handles}->{atime}->db_key_range(
+ $too_old, my $less, my $equal, my $greater) == 0) {
+ dbg("DBD: less is $less, equal is $equal, greater is $greater");
$count = $stats->{bt_ndata} - $stats->{bt_ndata} * $greater;
}
}
- #dbg("BDB: Considering deleting $vars[3], $count");
+ dbg("bayes: Considering deleting $vars[3], $count");
# As long as too many tokens wouldn't be deleted
if ($vars[3] - $count >= 100000) {
- #dbg("BDB: Preparing to iterate");
+ dbg("bayes: Preparing to iterate");
- my $cursor = $self->{handles}->{atime}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $cursor = $self->{handles}->{atime}->db_cursor;
+ $cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
my ($atime, $oldest, $token, $value);
@@ -378,8 +405,8 @@
while ($cursor->c_pget($atime, $token, $value, $next) == 0) {
# We're traversing in order, so done
- $oldest = $atime, last if($atime >= $too_old);
- #dbg("BDB: Deleting record");
+ $oldest = $atime, last if $atime >= $too_old;
+ dbg("bayes: Deleting record");
$cursor->c_del;
$deleted++;
my($ts, $th, $atime) = _unpack_token($value);
@@ -390,31 +417,40 @@
}
}
- #dbg("BDB: Done with cursor");
- $cursor->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ dbg("bayes: Done with cursor");
+ $cursor->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $cursor;
- $kept = $self->_get (vars => "NTOKENS", $rmw) - $deleted;
- $self->{handles}->{vars}->db_put(NTOKENS => $kept) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(LAST_EXPIRE => time) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $oldest) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(LAST_EXPIRE_REDUCE => $deleted) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(LAST_ATIME_DELTA => $newdelta) and die "Couldn't put record: $BerkeleyDB::Error";
+ $kept = $self->_get(vars => "NTOKENS", $rmw) - $deleted;
+ $self->{handles}->{vars}->db_put(NTOKENS => $kept) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_EXPIRE => time) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $oldest) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_EXPIRE_REDUCE => $deleted) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_ATIME_DELTA => $newdelta) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
#$self->{handles}->{atime}->compact;
#$self->{handles}->{tokens}->compact;
#$self->{handles}->{vars}->compact;
} else {
- #dbg("BDB: Update vars to regenerate histogram");
+ dbg("bayes: Update vars to regenerate histogram");
# Make sure we regenerate our histogramn
$kept = $self->_get(vars => "NTOKENS");
- $self->{handles}->{vars}->db_put(LAST_EXPIRE => time) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(LAST_ATIME_DELTA => 0) and die "Couldn't put record: $BerkeleyDB::Error";
- $self->{handles}->{vars}->db_put(LAST_EXPIRE_REDUCE => 0) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_EXPIRE => time) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_ATIME_DELTA => 0) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_EXPIRE_REDUCE => 0) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
- #dbg("BDB: token_expiration done");
+ dbg("bayes: token_expiration done");
return($kept, $deleted, $hapaxes, $lowfreq);
}
@@ -438,14 +474,14 @@
public instance (String) seen_get (string $msgid)
Description:
-This method retrieves the stored value, if any, for C<$msgid>. The return value
-is the stored string ('s' for spam and 'h' for ham) or undef if C<$msgid> is not
-found.
+This method retrieves the stored value, if any, for C<$msgid>. The return
+value is the stored string ('s' for spam and 'h' for ham) or undef if C<$msgid>
+is not found.
=cut
sub seen_get {
- #dbg("BDB: Entering seen_get");
+ dbg("bayes: Entering seen_get");
my($self, $msgid) = @_;
my $value = $self->_get(seen => $msgid);
@@ -458,16 +494,17 @@
public (Boolean) seen_put (string $msgid, char $flag)
Description:
-This method records C<$msgid> as the type given by C<$flag>. C<$flag> is one of
-two values 's' for spam and 'h' for ham.
+This method records C<$msgid> as the type given by C<$flag>. C<$flag> is one
+of two values 's' for spam and 'h' for ham.
=cut
sub seen_put {
- #dbg("BDB: Entering seen_put");
+ dbg("bayes: Entering seen_put");
my($self, $msgid, $flag) = @_;
- $self->{handles}->{seen}->db_put($msgid, $flag) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{seen}->db_put($msgid, $flag) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
return 1;
}
@@ -482,7 +519,7 @@
=cut
sub seen_delete {
- #dbg("BDB: Entering seen_delete");
+ dbg("bayes: Entering seen_delete");
my($self, $msgid) = @_;
my $result;
@@ -535,17 +572,19 @@
=cut
sub get_storage_variables {
- #dbg("BDB: get_storage_variables starting");
+ dbg("bayes: get_storage_variables starting");
my($self) = @_;
my @values;
- for my $token (qw{LAST_JOURNAL_SYNC NSPAM NHAM NTOKENS LAST_EXPIRE OLDEST_TOKEN_AGE DB_VERSION LAST_JOURNAL_SYNC LAST_ATIME_DELTA LAST_EXPIRE_REDUCE NEWEST_TOKEN_AGE}) {
- my $value = $self->_get (vars => $token);
- $value = 0 unless($value and $value =~ /\d+/);
+ for my $token (qw{LAST_JOURNAL_SYNC NSPAM NHAM NTOKENS LAST_EXPIRE
+ OLDEST_TOKEN_AGE DB_VERSION LAST_JOURNAL_SYNC
+ LAST_ATIME_DELTA LAST_EXPIRE_REDUCE NEWEST_TOKEN_AGE}) {
+ my $value = $self->_get(vars => $token);
+ $value = 0 unless $value && $value =~ /\d+/;
push @values, $value;
}
- #dbg("BDB: get_storage_variables done");
+ dbg("bayes: get_storage_variables done");
return @values;
}
@@ -554,29 +593,32 @@
public instance () dump_tokens (String $template, String $regex, Array @vars)
Description:
-This method loops over all tokens, computing the probability for the token and then
-printing it out according to the passed in token.
+This method loops over all tokens, computing the probability for the token
+and then printing it out according to the passed in token.
=cut
sub dump_tokens {
- #dbg("BDB: dump_tokens starting");
+ dbg("bayes: dump_tokens starting");
my($self, $template, $regex, @vars) = @_;
- my $cursor = $self->{handles}->{tokens}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $cursor = $self->{handles}->{tokens}->db_cursor;
+ $cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
my ($token, $value) = ("", "");
while ($cursor->c_get($token, $value, $next) == 0) {
- next if(defined $regex && ($token !~ /$regex/o));
+ next if defined $regex && $token !~ /$regex/o;
my($ts, $th, $atime) = _unpack_token($value);
- my $prob = $self->{bayes}->compute_prob_for_token($token, $vars[1], $vars[2], $ts, $th) || 0.5;
+ my $prob = $self->{bayes}->compute_prob_for_token(
+ $token, $vars[1], $vars[2], $ts, $th) || 0.5;
my $encoded = unpack("H*",$token);
printf $template, $prob, $ts, $th, $atime, $encoded;
}
- $cursor->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ $cursor->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $cursor;
- #dbg("BDB: dump_tokens done");
+ dbg("bayes: dump_tokens done");
return 1;
}
@@ -590,9 +632,10 @@
=cut
sub set_last_expire {
- #dbg("BDB: Entering set_last_expire");
+ dbg("bayes: Entering set_last_expire");
my($self, $time) = @_;
- $self->{handles}->{vars}->db_put(LAST_EXPIRE => $time) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(LAST_EXPIRE => $time) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
return 1;
}
@@ -610,13 +653,14 @@
=cut
sub get_running_expire_tok {
- #dbg("BDB: Entering get_running_expire_tok");
+ dbg("bayes: Entering get_running_expire_tok");
my($self) = @_;
- my $value = $self->_get (vars => "RUNNING_EXPIRE") || "";
+ my $value = $self->_get(vars => "RUNNING_EXPIRE") || "";
my $result = $value if $value =~ /^\d+$/;
- #dbg("BDB: get_running_expire_tok exiting with $result");
+ dbg("bayes: get_running_expire_tok exiting with %s",
+ !defined $result ? 'UNDEF' : $result);
return $result;
}
@@ -633,7 +677,8 @@
my($self) = @_;
my $time = time;
- $self->{handles}->{vars}->db_put(RUNNING_EXPIRE => $time) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(RUNNING_EXPIRE => $time) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
return $time;
}
@@ -677,9 +722,9 @@
=cut
sub tok_get {
- #dbg("BDB: Entering tok_get");
+ dbg("bayes: Entering tok_get");
my($self, $token) = @_;
- my $array = $self->tok_get_all ([$token]);
+ my $array = $self->tok_get_all([$token]);
return !@$array ? () : @{$array->[0]};
}
@@ -688,13 +733,13 @@
public instance (\@) tok_get (@ $tokens)
Description:
-This method retrieves the specified tokens (C<$tokens>) from storage and returns
-an array ref of arrays spam count, ham acount and last access time.
+This method retrieves the specified tokens (C<$tokens>) from storage and
+returns an array ref of arrays spam count, ham acount and last access time.
=cut
sub tok_get_all {
- #dbg("BDB: Entering tok_get_all");
+ dbg("bayes: Entering tok_get_all");
my($self, @keys) = @_;
my @values;
@@ -704,16 +749,14 @@
}
}
- # #dbg("BDB: tok_get_all returning with " . Dump \@values);
+ # dbg("bayes: tok_get_all returning with " . Dump \@values);
return \@values;
}
=head2 tok_count_change
-public instance (Boolean) tok_count_change (Integer $dspam,
- Integer $dham,
- String $token,
- String $newatime)
+public instance (Boolean) tok_count_change (
+ Integer $dspam, Integer $dham, String $token, String $newatime)
Description:
This method takes a C<$spam_count> and C<$ham_count> and adds it to
@@ -722,17 +765,15 @@
=cut
sub tok_count_change {
- #dbg("BDB: Entering tok_count_change");
+ dbg("bayes: Entering tok_count_change");
my($self, $dspam, $dham, $token, $newatime) = @_;
- $self->multi_tok_count_change ($dspam, $dham, {$token => 1}, $newatime);
+ $self->multi_tok_count_change($dspam, $dham, {$token => 1}, $newatime);
}
=head2 multi_tok_count_change
-public instance (Boolean) multi_tok_count_change (Integer $dspam,
- Integer $dham,
- \% $tokens,
- String $newatime)
+public instance (Boolean) multi_tok_count_change (
+ Integer $dspam, Integer $dham, \% $tokens, String $newatime)
Description:
This method takes a C<$dspam> and C<$dham> and adds it to all of the
@@ -759,22 +800,24 @@
my $status = $self->{handles}->{tokens}->db_get($token => my $value, $rmw);
if ($status == 0) {
- my ($spam, $ham, $oldatime) = _unpack_token ($value);
+ my ($spam, $ham, $oldatime) = _unpack_token($value);
$spam += $dspam;
- $spam = 0 if ($spam < 0);
+ $spam = 0 if $spam < 0;
$ham += $dham;
- $ham = 0 if ($ham < 0);
+ $ham = 0 if $ham < 0;
my $newvalue = _pack_token($spam, $ham, $newatime);
- $self->{handles}->{tokens}->db_put($token => $newvalue) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{tokens}->db_put($token => $newvalue) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
elsif ($status == DB_NOTFOUND) {
my $spam = $dspam;
- $spam = 0 if ($spam < 0);
+ $spam = 0 if $spam < 0;
my $ham = $dham;
- $ham = 0 if ($ham < 0);
+ $ham = 0 if $ham < 0;
my $newvalue = _pack_token($spam, $ham, $newatime);
- $self->{handles}->{tokens}->db_put($token => $newvalue) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{tokens}->db_put($token => $newvalue) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
$newtokens++;
}
@@ -786,18 +829,21 @@
if ($newtokens) {
my $ntokens = $self->_get(vars => "NTOKENS", $rmw) || 0;
$ntokens += $newtokens;
- $ntokens = 0 if ($ntokens < 0);
- $self->{handles}->{vars}->db_put(NTOKENS => $ntokens) and die "Couldn't put record: $BerkeleyDB::Error";
+ $ntokens = 0 if $ntokens < 0;
+ $self->{handles}->{vars}->db_put(NTOKENS => $ntokens) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
my $newmagic = $self->_get(vars => "NEWEST_TOKEN_AGE", $rmw) || 0;
if ($newatime > $newmagic) {
- $self->{handles}->{vars}->db_put(NEWEST_TOKEN_AGE => $newatime) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{vars}->db_put(NEWEST_TOKEN_AGE => $newatime) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
my $oldmagic = $self->_get(vars => "OLDEST_TOKEN_AGE", $rmw) || time;
- if ($newatime and $newatime < $oldmagic) {
- $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $newatime) and die "Couldn't put record: $BerkeleyDB::Error";
+ if ($newatime && $newatime < $oldmagic) {
+ $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $newatime) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
return 1;
@@ -814,7 +860,7 @@
=cut
sub nspam_nham_get {
- #dbg("BDB: Entering nspam_nham_get");
+ dbg("bayes: Entering nspam_nham_get");
my($self) = @_;
my @vars = $self->get_storage_variables();
($vars[1], $vars[2]);
@@ -835,13 +881,15 @@
my $nspam = $self->_get(vars => "NSPAM", $rmw) || 0;
$nspam += ($ds || 0);
- $nspam = 0 if ($nspam < 0);
- $self->{handles}->{vars}->db_put(NSPAM => $nspam) and die "Couldn't put record: $BerkeleyDB::Error";
+ $nspam = 0 if $nspam < 0;
+ $self->{handles}->{vars}->db_put(NSPAM => $nspam) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
my $nham = $self->_get(vars => "NHAM", $rmw) || 0;
$nham += ($dh || 0);
- $nham = 0 if ($nham < 0);
- $self->{handles}->{vars}->db_put(NHAM => $nham) and die "Couldn't put record: $BerkeleyDB::Error";
+ $nham = 0 if $nham < 0;
+ $self->{handles}->{vars}->db_put(NHAM => $nham) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
return 1;
}
@@ -862,7 +910,7 @@
sub tok_touch {
my($self, $token, $atime) = @_;
- return $self->tok_touch_all ([$token], $atime);
+ return $self->tok_touch_all([$token], $atime);
}
=head2 tok_touch_all
@@ -888,9 +936,10 @@
for my $token (@{$tokens}) {
my $status = $self->{handles}->{tokens}->db_get($token => my $value, $rmw);
if ($status == 0) {
- my ($spam, $ham, $oldatime) = _unpack_token ($value);
- my $newvalue = _pack_token ($spam, $ham, $newatime);
- $self->{handles}->{tokens}->db_put($token => $newvalue) and die "Couldn't put record: $BerkeleyDB::Error";
+ my ($spam, $ham, $oldatime) = _unpack_token($value);
+ my $newvalue = _pack_token($spam, $ham, $newatime);
+ $self->{handles}->{tokens}->db_put($token => $newvalue) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
elsif ($status == DB_NOTFOUND) {
@@ -917,7 +966,7 @@
sub cleanup {
my ($self) = @_;
- #dbg("Running cleanup");
+ dbg("Running cleanup");
return 1;
}
@@ -943,9 +992,9 @@
=cut
-sub sync {
+sub sync {
my($self, $opts) = @_;
- #dbg("Running sync");
+ dbg("Running sync");
return 1;
}
@@ -960,7 +1009,7 @@
=cut
sub perform_upgrade {
- #dbg("BDB: Entering perform_upgrade");
+ dbg("bayes: Entering perform_upgrade");
return 1;
}
@@ -977,11 +1026,11 @@
=cut
sub clear_database {
- #dbg("BDB: Entering clear_database");
+ dbg("bayes: Entering clear_database");
my($self) = @_;
$self->untie_db();
- #dbg("BDB: removing db.");
+ dbg("bayes: removing db.");
my $main = $self->{bayes}->{main};
my $path = $main->sed_path($main->{conf}->{bayes_path});
eval {rmpath($path)};
@@ -998,7 +1047,7 @@
=cut
sub backup_database {
- #dbg("BDB: Entering backup_database");
+ dbg("bayes: Entering backup_database");
my($self) = @_;
return 0 unless $self->tie_db_writable;
my @vars = $self->get_storage_variables;
@@ -1007,7 +1056,8 @@
print "v\t$vars[1]\tnum_spam\n";
print "v\t$vars[2]\tnum_nonspam\n";
- my $tokens = $self->{handles}->{tokens}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $tokens = $self->{handles}->{tokens}->db_cursor;
+ $tokens or die "Couldn't get cursor: $BerkeleyDB::Error";
my($token, $value) = ("", "");
while ($tokens->c_get($token, $value, $next) == 0) {
@@ -1016,17 +1066,20 @@
print "t\t$ts\t$th\t$atime\t$encoded\n";
}
- $tokens->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ $tokens->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $tokens;
- my $seen = $self->{handles}->{seen}->db_cursor or die "Couldn't get cursor: $BerkeleyDB::Error";
+ my $seen = $self->{handles}->{seen}->db_cursor;
+ $seen or die "Couldn't get cursor: $BerkeleyDB::Error";
$token = "";
while ($seen->c_get($token, $value, $next) == 0) {
print "s\t$token\t$value\n";
}
- $seen->c_close and die "Couldn't close cursor: $BerkeleyDB::Error";
+ $seen->c_close == 0
+ or die "Couldn't close cursor: $BerkeleyDB::Error";
undef $seen;
$self->untie_db();
@@ -1047,19 +1100,20 @@
=cut
sub restore_database {
- #dbg("BDB: Entering restore_database");
+ dbg("bayes: Entering restore_database");
my ($self, $filename, $showdots) = @_;
local *DUMPFILE;
if (!open(DUMPFILE, '<', $filename)) {
- #dbg("BDB: unable to open backup file $filename: $!");
+ dbg("bayes: unable to open backup file $filename: $!");
return 0;
}
# This is the critical phase (moving sql around), so don't allow it
# to be interrupted.
local $SIG{'INT'} = 'IGNORE';
- local $SIG{'HUP'} = 'IGNORE' if (!Mail::SpamAssassin::Util::am_running_on_windows());
+ local $SIG{'HUP'} = 'IGNORE'
+ if !Mail::SpamAssassin::Util::am_running_on_windows();
local $SIG{'TERM'} = 'IGNORE';
unless ($self->clear_database()) {
@@ -1090,12 +1144,12 @@
if ($line =~ m/^v\s+(\d+)\s+db_version/) {
$db_version = $1;
} else {
- #dbg("BDB: database version must be the first line in the backup file, correct and re-run");
+ dbg("bayes: database version must be the first line in the backup file, correct and re-run");
return 0;
}
unless ($db_version == 2 || $db_version == 3) {
- warn("BDB: database version $db_version is unsupported, must be version 2 or 3");
+ warn("bayes: database version $db_version is unsupported, must be version 2 or 3");
return 0;
}
@@ -1107,18 +1161,18 @@
$line_count++;
if ($line_count % 1000 == 0) {
- print STDERR "." if ($showdots);
+ print STDERR "." if $showdots;
}
if ($line =~ /^v\s+/) { # variable line
my @parsed_line = split(/\s+/, $line, 3);
my $value = $parsed_line[1] + 0;
if ($parsed_line[2] eq 'num_spam') {
- $num_spam = $value;
+ $num_spam = $value;
} elsif ($parsed_line[2] eq 'num_nonspam') {
- $num_ham = $value;
+ $num_ham = $value;
} else {
- #dbg("BDB: restore_database: skipping unknown line: $line");
+ dbg("bayes: restore_database: skipping unknown line: $line");
}
} elsif ($line =~ /^t\s+/) { # token line
my @parsed_line = split(/\s+/, $line, 5);
@@ -1131,42 +1185,43 @@
my @warnings;
if ($spam_count < 0) {
- $spam_count = 0;
- push(@warnings, 'spam count < 0, resetting');
- $token_warn_p = 1;
+ $spam_count = 0;
+ push(@warnings, 'spam count < 0, resetting');
+ $token_warn_p = 1;
}
if ($ham_count < 0) {
- $ham_count = 0;
- push(@warnings, 'ham count < 0, resetting');
- $token_warn_p = 1;
+ $ham_count = 0;
+ push(@warnings, 'ham count < 0, resetting');
+ $token_warn_p = 1;
}
if ($spam_count == 0 && $ham_count == 0) {
- #dbg("BDB: token has zero spam and ham count, skipping");
- next;
+ dbg("bayes: token has zero spam and ham count, skipping");
+ next;
}
if ($atime > time()) {
- $atime = time();
- push(@warnings, 'atime > current time, resetting');
- $token_warn_p = 1;
+ $atime = time();
+ push(@warnings, 'atime > current time, resetting');
+ $token_warn_p = 1;
}
if ($token_warn_p) {
- #dbg("BDB: token ($token) has the following warnings:\n".join("\n",@warnings));
+ dbg("bayes: token (%s) has the following warnings:\n%s",
+ $token, join("\n",@warnings));
}
if ($db_version < 3) {
- # versions < 3 use plain text tokens, so we need to convert to hash
- $token = substr(sha1($token), -5);
+ # versions < 3 use plain text tokens, so we need to convert to hash
+ $token = substr(sha1($token), -5);
} else {
- # turn unpacked binary token back into binary value
- $token = pack("H*",$token);
+ # turn unpacked binary token back into binary value
+ $token = pack("H*",$token);
}
unless ($self->_put_token($token, $spam_count, $ham_count, $atime)) {
- #dbg("BDB: error inserting token for line: $line");
- $token_error_count++;
+ dbg("bayes: error inserting token for line: $line");
+ $token_error_count++;
}
$token_count++;
} elsif ($line =~ /^s\s+/) { # seen line
@@ -1175,69 +1230,69 @@
my $msgid = $parsed_line[2];
unless ($flag eq 'h' || $flag eq 's') {
- #dbg("BDB: unknown seen flag ($flag) for line: $line, skipping");
- next;
+ dbg("bayes: unknown seen flag ($flag) for line: $line, skipping");
+ next;
}
unless ($msgid) {
- #dbg("BDB: blank msgid for line: $line, skipping");
- next;
+ dbg("bayes: blank msgid for line: $line, skipping");
+ next;
}
unless ($self->seen_put($msgid, $flag)) {
- #dbg("BDB: error inserting msgid in seen table for line: $line");
- $seen_error_count++;
+ dbg("bayes: error inserting msgid in seen table for line: $line");
+ $seen_error_count++;
}
} else {
- #dbg("BDB: skipping unknown line: $line");
+ dbg("bayes: skipping unknown line: $line");
next;
}
if ($token_error_count >= 20) {
- warn "BDB: encountered too many errors (20) while parsing token line, reverting to empty database and exiting\n";
+ warn "bayes: encountered too many errors (20) while parsing token line, reverting to empty database and exiting\n";
$self->clear_database();
return 0;
}
if ($seen_error_count >= 20) {
- warn "BDB: encountered too many errors (20) while parsing seen lines, reverting to empty database and exiting\n";
+ warn "bayes: encountered too many errors (20) while parsing seen lines, reverting to empty database and exiting\n";
$self->clear_database();
return 0;
}
}
defined $line || $!==0 or
- $!==EBADF ? dbg("BDB: error reading dump file: $!")
+ $!==EBADF ? dbg("bayes: error reading dump file: $!")
: die "error reading dump file: $!";
close(DUMPFILE) or die "Can't close dump file: $!";
- print STDERR "\n" if ($showdots);
+ print STDERR "\n" if $showdots;
unless (defined($num_spam)) {
- #dbg("BDB: unable to find num spam, please check file");
+ dbg("bayes: unable to find num spam, please check file");
$error_p = 1;
}
unless (defined($num_ham)) {
- #dbg("BDB: unable to find num ham, please check file");
+ dbg("bayes: unable to find num ham, please check file");
$error_p = 1;
}
if ($error_p) {
- #dbg("BDB: error(s) while attempting to load $filename, clearing database, correct and re-run");
+ dbg("bayes: error(s) while attempting to load $filename, clearing database, correct and re-run");
$self->clear_database();
return 0;
}
if ($num_spam || $num_ham) {
unless ($self->nspam_nham_change($num_spam, $num_ham)) {
- #dbg("BDB: error updating num spam and num ham, clearing database");
+ dbg("bayes: error updating num spam and num ham, clearing database");
$self->clear_database();
return 0;
}
}
- #dbg("BDB: parsed $line_count lines");
- #dbg("BDB: created database with $token_count tokens based on $num_spam spam messages and $num_ham ham messages");
+ dbg("bayes: parsed $line_count lines");
+ dbg("bayes: created database with $token_count tokens based on $num_spam spam messages and $num_ham ham messages");
$self->untie_db();
@@ -1255,7 +1310,7 @@
=cut
sub db_readable {
- #dbg("BDB: Entering db_readable");
+ dbg("bayes: Entering db_readable");
my($self) = @_;
return $self->{already_tied};
}
@@ -1271,7 +1326,7 @@
=cut
sub db_writable {
- #dbg("BDB: Entering db_writeable");
+ dbg("bayes: Entering db_writeable");
my($self) = @_;
return($self->{already_tied} and $self->{is_locked});
}
@@ -1283,19 +1338,19 @@
String $index)
Description:
-This method ensures that the database connetion is properly setup and
+This method ensures that the database connection is properly setup and
working. If appropriate it will initialize a users bayes variables so
that they can begin using the database immediately.
=cut
sub _extract_atime {
- #dbg("BDB: Entering _extract_atime");
+ #dbg("bayes: Entering _extract_atime");
my ($token, $value) = @_;
my($ts, $th, $atime) = _unpack_token($value);
- #dbg("BDB: _extract_atime found $atime for $token");
+ #dbg("bayes: _extract_atime found $atime for $token");
$_[2] = $atime;
- #dbg("BDB: Leaving db_writeable");
+ #dbg("bayes: Leaving db_writeable");
return 0;
}
@@ -1308,60 +1363,67 @@
=cut
sub _put_token {
- #dbg("BDB: Entering _put_token");
+ dbg("bayes: Entering _put_token");
my($self, $token, $ts, $th, $atime) = @_;
$ts ||= 0;
$th ||= 0;
- #dbg("BDB: $token has spam $ts, ham $th, atime $atime");
+ dbg("bayes: $token has spam $ts, ham $th, atime $atime");
my $value = $self->_get(tokens => $token, $rmw);
my $exists_already = defined $value ? 1 : 0;
- #dbg("BDB: $token exists: $exists_already");
+ dbg("bayes: $token exists: $exists_already");
if ($ts == 0 && $th == 0) {
- return unless($exists_already); # If the token doesn't exist, just return
+ return unless $exists_already; # If the token doesn't exist, just return
my $ntokens = $self->_get(vars => "NTOKENS", $rmw);
- $self->{handles}->{vars}->db_put(NTOKENS => --$ntokens) and die "Couldn't put record: $BerkeleyDB::Error";
- #dbg("BDB: ntokens is $ntokens");
+ $self->{handles}->{vars}->db_put(NTOKENS => --$ntokens) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ dbg("bayes: ntokens is $ntokens");
my $status = $self->{handles}->{tokens}->db_del($token);
- die "Couldn't delete record: $BerkeleyDB::Error" unless ($status == 0 or $status == DB_NOTFOUND);
- #dbg("BDB: $token deleted");
+ $status == 0 || $status == DB_NOTFOUND
+ or die "Couldn't delete record: $BerkeleyDB::Error";
+ dbg("bayes: $token deleted");
} else {
- unless($exists_already) { # If the token doesn't exist, raise the token count
+ unless ($exists_already) {
+ # If the token doesn't exist, raise the token count
my $ntokens = $self->_get(vars => "NTOKENS", $rmw);
- $self->{handles}->{vars}->db_put(NTOKENS => ++$ntokens) and die "Couldn't put record: $BerkeleyDB::Error";
- #dbg("BDB: ntokens is $ntokens");
+ $self->{handles}->{vars}->db_put(NTOKENS => ++$ntokens) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
+ dbg("bayes: ntokens is $ntokens");
}
my $newmagic = $self->_get(vars => "NEWEST_TOKEN_AGE", $rmw) || 0;
- #dbg("BDB: NEWEST_TOKEN_AGE is $newmagic");
+ dbg("bayes: NEWEST_TOKEN_AGE is $newmagic");
if ($atime > $newmagic) {
- #dbg("BDB: Updating NEWEST_TOKEN_AGE");
- $self->{handles}->{vars}->db_put(NEWEST_TOKEN_AGE => $atime) and die "Couldn't put record: $BerkeleyDB::Error";
+ dbg("bayes: Updating NEWEST_TOKEN_AGE");
+ $self->{handles}->{vars}->db_put(NEWEST_TOKEN_AGE => $atime) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
my $oldmagic = $self->_get(vars => "OLDEST_TOKEN_AGE", $rmw) || time;
- #dbg("BDB: OLDEST_TOKEN_AGE is $oldmagic");
- if ($atime and $atime < $oldmagic) {
- #dbg("BDB: Updating OLDEST_TOKEN_AGE to $atime");
- $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $atime) and die "Couldn't put record: $BerkeleyDB::Error";
+ dbg("bayes: OLDEST_TOKEN_AGE is $oldmagic");
+ if ($atime && $atime < $oldmagic) {
+ dbg("bayes: Updating OLDEST_TOKEN_AGE to $atime");
+ $self->{handles}->{vars}->db_put(OLDEST_TOKEN_AGE => $atime) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
my $value = _pack_token($ts, $th, $atime);
- #dbg("BDB: Setting $token to $value");
- #dbg("BDB: Handle is $self->{handles}->{tokens}");
+ dbg("bayes: Setting $token to $value");
+ dbg("bayes: Handle is $self->{handles}->{tokens}");
- $self->{handles}->{tokens}->db_put($token, $value) and die "Couldn't put record: $BerkeleyDB::Error";
+ $self->{handles}->{tokens}->db_put($token, $value) == 0
+ or die "Couldn't put record: $BerkeleyDB::Error";
}
- #dbg("BDB: Leaving _put_token");
+ dbg("bayes: Leaving _put_token");
return 1;
}
@@ -1381,12 +1443,12 @@
# Savings: roughly halves size of toks db, at the cost of a ~10% slowdown.
-use constant FORMAT_FLAG => 0xc0; # 11000000
-use constant ONE_BYTE_FORMAT => 0xc0; # 11000000
-use constant TWO_LONGS_FORMAT => 0x00; # 00000000
+use constant FORMAT_FLAG => 0xc0; # 11000000
+use constant ONE_BYTE_FORMAT => 0xc0; # 11000000
+use constant TWO_LONGS_FORMAT => 0x00; # 00000000
-use constant ONE_BYTE_SSS_BITS => 0x38; # 00111000
-use constant ONE_BYTE_HHH_BITS => 0x07; # 00000111
+use constant ONE_BYTE_SSS_BITS => 0x38; # 00111000
+use constant ONE_BYTE_HHH_BITS => 0x07; # 00000111
sub _unpack_token {
my $value = shift || 0;
@@ -1396,11 +1458,12 @@
if (($packed & FORMAT_FLAG) == ONE_BYTE_FORMAT) {
return (($packed & ONE_BYTE_SSS_BITS) >> 3,
$packed & ONE_BYTE_HHH_BITS,
- $ts || 0); # The one-byte-format uses that first 32-bit long as atime
+ $ts || 0);
+ # The one-byte-format uses that first 32-bit long as atime
} elsif (($packed & FORMAT_FLAG) == TWO_LONGS_FORMAT) {
return ($ts || 0, $th || 0, $atime || 0);
} else {
- warn "BDB: unknown packing format for bayes db, please re-learn: $packed";
+ warn "bayes: unknown packing format for bayes db, please re-learn: $packed";
return (0, 0, 0);
}
}