summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Bloomfilter.pm
diff options
context:
space:
mode:
authorDylan William Hardison <dylan@hardison.net>2018-04-10 23:35:23 +0200
committerGitHub <noreply@github.com>2018-04-10 23:35:23 +0200
commit8ba7aa6be8fb5689634c453fd505822d02f586a6 (patch)
tree6b3026bfc5302fdb453ae008e9101682c91b8740 /Bugzilla/Bloomfilter.pm
parentd427f996e5248e0dcb8d6a83b8234bd12d674b98 (diff)
downloadbugzilla-8ba7aa6be8fb5689634c453fd505822d02f586a6.tar.gz
bugzilla-8ba7aa6be8fb5689634c453fd505822d02f586a6.tar.xz
Bug 1453126 - Bugzilla::Bloomfilter should encourage preserving the input files for its filters
Diffstat (limited to 'Bugzilla/Bloomfilter.pm')
-rw-r--r--Bugzilla/Bloomfilter.pm34
1 files changed, 14 insertions, 20 deletions
diff --git a/Bugzilla/Bloomfilter.pm b/Bugzilla/Bloomfilter.pm
index 0d329b2ea..ba1d6d6c3 100644
--- a/Bugzilla/Bloomfilter.pm
+++ b/Bugzilla/Bloomfilter.pm
@@ -13,7 +13,8 @@ use warnings;
use Bugzilla::Constants;
use Algorithm::BloomFilter;
-use File::Temp qw(tempfile);
+use File::Slurper qw(write_binary read_binary read_lines);
+use File::Spec::Functions qw(catfile);
sub _new_bloom_filter {
my ($n) = @_;
@@ -24,44 +25,37 @@ sub _new_bloom_filter {
}
sub _filename {
- my ($name) = @_;
+ my ($name, $type) = @_;
my $datadir = bz_locations->{datadir};
- return sprintf("%s/%s.bloom", $datadir, $name);
+
+ return catfile($datadir, "$name.$type");
}
sub populate {
- my ($class, $name, $items) = @_;
+ my ($class, $name) = @_;
my $memcached = Bugzilla->memcached;
+ my @items = read_lines(_filename($name, 'list'));
+ my $filter = _new_bloom_filter(@items + 0);
- my $filter = _new_bloom_filter(@$items + 0);
- foreach my $item (@$items) {
- $filter->add($item);
- }
-
- my ($fh, $filename) = tempfile( "${name}XXXXXX", DIR => bz_locations->{datadir}, UNLINK => 0);
- binmode $fh, ':bytes';
- print $fh $filter->serialize;
- close $fh;
- rename($filename, _filename($name)) or die "failed to rename $filename: $!";
+ $filter->add($_) foreach @items;
+ write_binary(_filename($name, 'bloom'), $filter->serialize);
$memcached->clear_bloomfilter({name => $name});
}
sub lookup {
my ($class, $name) = @_;
my $memcached = Bugzilla->memcached;
- my $filename = _filename($name);
+ my $filename = _filename($name, 'bloom');
my $filter_data = $memcached->get_bloomfilter( { name => $name } );
if (!$filter_data && -f $filename) {
- open my $fh, '<:bytes', $filename;
- local $/ = undef;
- $filter_data = <$fh>;
- close $fh;
+ $filter_data = read_binary($filename);
$memcached->set_bloomfilter({ name => $name, filter => $filter_data });
}
- return Algorithm::BloomFilter->deserialize($filter_data);
+ return Algorithm::BloomFilter->deserialize($filter_data) if $filter_data;
+ return undef;
}
1;