infile.open(filename, std::ios::in | std::ios::binary);
g_log << Logger::Warning << "Found SBF file " << filename << endl;
// read the file into the sbf
- d_sbf.restore(infile);
+ d_sbf.lock()->restore(infile);
infile.close();
// now dump it out again with new thread id & process id
snapshotCurrent(std::this_thread::get_id());
std::stringstream iss;
{
// only lock while dumping to a stringstream
- std::lock_guard<std::mutex> lock(d_sbf_mutex);
- d_sbf.dump(iss);
+ d_sbf.lock()->dump(iss);
}
// Now write it out to the file
std::string ftmp = f.string() + ".XXXXXXXX";
#include <thread>
#include <boost/filesystem.hpp>
#include "dnsname.hh"
+#include "lock.hh"
#include "stable-bloom.hh"
namespace nod {
// Synchronization (at the instance level) is needed when snapshotting
class PersistentSBF {
public:
- PersistentSBF() : d_sbf{c_fp_rate, c_num_cells, c_num_dec} {}
- PersistentSBF(uint32_t num_cells) : d_sbf{c_fp_rate, num_cells, c_num_dec} {}
+ PersistentSBF() : d_sbf(bf::stableBF(c_fp_rate, c_num_cells, c_num_dec)) {}
+ PersistentSBF(uint32_t num_cells) : d_sbf(bf::stableBF(c_fp_rate, num_cells, c_num_dec)) {}
bool init(bool ignore_pid=false);
void setPrefix(const std::string& prefix) { d_prefix = prefix; } // Added to filenames in cachedir
void setCacheDir(const std::string& cachedir);
bool snapshotCurrent(std::thread::id tid); // Write the current file out to disk
void add(const std::string& data) {
// The only time this should block is when snapshotting
- std::lock_guard<std::mutex> lock(d_sbf_mutex);
- d_sbf.add(data);
+ d_sbf.lock()->add(data);
}
- bool test(const std::string& data) { return d_sbf.test(data); }
+ bool test(const std::string& data) { return d_sbf.lock()->test(data); }
bool testAndAdd(const std::string& data) {
// The only time this should block is when snapshotting
- std::lock_guard<std::mutex> lock(d_sbf_mutex);
- return d_sbf.testAndAdd(data);
+ return d_sbf.lock()->testAndAdd(data);
}
private:
void remove_tmp_files(const boost::filesystem::path&, std::lock_guard<std::mutex>&);
bool d_init{false};
- bf::stableBF d_sbf; // Stable Bloom Filter
+ LockGuarded<bf::stableBF> d_sbf; // Stable Bloom Filter
std::string d_cachedir;
std::string d_prefix = sbf_prefix;
- std::mutex d_sbf_mutex; // Per-instance mutex for snapshots
static std::mutex d_cachedir_mutex; // One mutex for all instances of this class
};
d_cells.set(i % d_num_cells);
}
}
- bool test(const std::string& data)
+ bool test(const std::string& data) const
{
auto hashes = hash(data);
for (auto& i : hashes) {
}
// This is a double hash implementation returning an array of
// k hashes
- std::vector<uint32_t> hash(const std::string& data)
+ std::vector<uint32_t> hash(const std::string& data) const
{
uint32_t h1, h2;
MurmurHash3_x86_32(data.c_str(), data.length(), 1, (void*)&h1);