This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
forked from gigablast/open-source-search-engine
/
MatchList.cpp
135 lines (112 loc) · 3.66 KB
/
MatchList.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright (C) 2017 Privacore ApS - https://www.privacore.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// License TL;DR: If you change this file, you must publish your changes.
//
#include "MatchList.h"
#include "Log.h"
#include "Conf.h"
#include "Loop.h"
#include "JobScheduler.h"
#include <fstream>
#include <sys/stat.h>
#include <atomic>
template <class T>
MatchList<T>::MatchList(const char *filename)
: m_filename(filename)
, m_loading(false)
, m_matchList(new matchlist_t<T>)
, m_lastModifiedTime(0) {
}
template <class T>
bool MatchList<T>::init() {
log(LOG_INFO, "Initializing MatchList with %s", m_filename);
if (!g_loop.registerSleepCallback(60000, this, &reload, "MatchList<T>::reload", 0)) {
log(LOG_WARN, "MatchList<T>:: Failed to register callback.");
return false;
}
// we do a load here instead of using sleep callback with immediate set to true so
// we don't rely on g_loop being up and running to use matchlist
load();
return true;
}
template <class T>
void MatchList<T>::reload(int /*fd*/, void *state) {
if (g_jobScheduler.submit(reload, nullptr, state, thread_type_config_load, 0)) {
return;
}
// unable to submit job (load on main thread)
reload(state);
}
template <class T>
void MatchList<T>::reload(void *state) {
MatchList *matchList = static_cast<MatchList*>(state);
// don't load multiple times at the same time
if (matchList->m_loading.exchange(true)) {
return;
}
matchList->load();
matchList->m_loading = false;
}
template <class T>
bool MatchList<T>::load() {
logTrace(g_conf.m_logTraceMatchList, "Loading %s", m_filename);
struct stat st;
if (stat(m_filename, &st) != 0) {
// probably not found
log(LOG_INFO, "MatchList<T>::load: Unable to stat %s", m_filename);
return false;
}
if (m_lastModifiedTime != 0 && m_lastModifiedTime == st.st_mtime) {
// not modified. assume successful
logTrace(g_conf.m_logTraceMatchList, "%s not modified", m_filename);
return true;
}
matchlist_ptr_t<T> tmpMatchList(new matchlist_t<T>);
std::ifstream file(m_filename);
std::string line;
while (std::getline(file, line)) {
// ignore comments & empty lines
if (line.length() == 0 || line[0] == '#') {
continue;
}
addToMatchList(tmpMatchList, line);
logTrace(g_conf.m_logTraceMatchList, "Adding criteria '%s' to list", line.c_str());
}
swapMatchList(tmpMatchList);
m_lastModifiedTime = st.st_mtime;
logTrace(g_conf.m_logTraceMatchList, "Loaded %s", m_filename);
return true;
}
template <class T>
void MatchList<T>::addToMatchList(matchlist_ptr_t<T> &matchList, const std::string &line) {
gbshutdownLogicError();
}
template <>
void MatchList<std::string>::addToMatchList(matchlist_ptr_t<std::string> &matchList, const std::string &line) {
matchList->emplace_back(line);
}
template <class T>
matchlistconst_ptr_t<T> MatchList<T>::getMatchList() {
return m_matchList;
}
template <class T>
void MatchList<T>::swapMatchList(matchlistconst_ptr_t<T> matchList) {
std::atomic_store(&m_matchList, matchList);
}
// explicit instantiations
template class MatchList<std::string>;
template class MatchList<uint32_t>;