-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinvalid_matches.cpp
More file actions
55 lines (47 loc) · 1.54 KB
/
invalid_matches.cpp
File metadata and controls
55 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "mine/invalid_matches.hpp"
#include "seq/seq_list.hpp"
#include "sys/file.hpp"
#include "sys/log.hpp"
#include "sys/setup.hpp"
const std::string FILENAME = "invalid_matches.txt";
InvalidMatches::InvalidMatches()
: scheduler(1800) // 30 minutes
{}
void InvalidMatches::load() {
// Migrate file from lists folder to cache folder
const auto lists_path = SequenceList::getListsHome() + FILENAME;
const auto cache_path = Setup::getCacheHome() + FILENAME;
if (isFile(lists_path) && !isFile(cache_path)) {
Log::get().info("Migrating \"" + FILENAME + "\" from lists to cache folder");
moveFile(lists_path, cache_path);
}
auto path = cache_path;
try {
SequenceList::loadMap(path, invalid_matches);
} catch (const std::exception&) {
Log::get().warn("Resetting corrupt file " + path);
invalid_matches.clear();
deleteFile();
}
}
bool InvalidMatches::hasTooMany(UID id) const {
auto it = invalid_matches.find(id);
if (it != invalid_matches.end() && it->second > 0) {
int64_t r = Random::get().gen() % it->second;
return r >= 100;
}
return false;
}
void InvalidMatches::insert(UID id) {
invalid_matches[id]++;
if (scheduler.isTargetReached()) {
scheduler.reset();
Log::get().info("Saving invalid matches stats for " +
std::to_string(invalid_matches.size()) + " sequences");
SequenceList::mergeMap(Setup::getCacheHome(), FILENAME, invalid_matches);
}
}
void InvalidMatches::deleteFile() {
auto path = Setup::getCacheHome() + FILENAME;
std::remove(path.c_str());
}