This repository was archived by the owner on Apr 30, 2020. It is now read-only.
forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDocRebuild.cpp
214 lines (171 loc) · 5.87 KB
/
DocRebuild.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// 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 "DocRebuild.h"
#include "XmlDoc.h"
#include "Msg0.h"
#include "RdbList.h"
#include "Conf.h"
#include "Errno.h"
DocRebuild g_docRebuild("docrebuild.txt", false);
DocRebuild g_docRebuildUrl("docrebuildurl.txt", true);
struct DocRebuildDocItem : public DocProcessDocItem {
DocRebuildDocItem(DocProcess *docProcess, const std::string &key, uint32_t firstIp, int64_t lastPos)
: DocProcessDocItem(docProcess, key, firstIp, lastPos)
, m_msg0()
, m_spiderdbList()
, m_spiderdbListRequested(false)
, m_spiderdbListProcessed(false)
, m_clearedXmlDoc(false) {
}
Msg0 m_msg0;
RdbList m_spiderdbList;
bool m_spiderdbListRequested;
bool m_spiderdbListProcessed;
bool m_clearedXmlDoc;
};
DocRebuild::DocRebuild(const char *filename, bool isUrl)
: DocProcess(filename, isUrl, false) {
}
DocProcessDocItem* DocRebuild::createDocItem(DocProcess *docProcess, const std::string &key, uint32_t firstIp, int64_t lastPos) {
return new DocRebuildDocItem(docProcess, key, firstIp, lastPos);
}
void DocRebuild::updateXmldoc(XmlDoc *xmlDoc) {
xmlDoc->m_recycleContent = true;
xmlDoc->m_docRebuild = true;
}
void DocRebuild::processDocItem(DocProcessDocItem *docItem) {
DocRebuildDocItem *rebuildDocItem = dynamic_cast<DocRebuildDocItem*>(docItem);
if (rebuildDocItem == nullptr) {
gbshutdownLogicError();
}
XmlDoc *xmlDoc = rebuildDocItem->m_xmlDoc;
// set callback
if (xmlDoc->m_masterLoop == nullptr) {
xmlDoc->m_masterLoop = processedDoc;
xmlDoc->m_masterState = rebuildDocItem;
}
// prepare
char **oldTitleRec = xmlDoc->getOldTitleRec();
if (!oldTitleRec || oldTitleRec == (char**)-1) {
return;
}
// oldTitleRec is mandatory for docrebuild
if (*oldTitleRec == nullptr) {
xmlDoc->m_indexCode = ENOTFOUND;
xmlDoc->m_indexCodeValid = true;
xmlDoc->logIt();
removePendingDoc(rebuildDocItem);
delete xmlDoc;
delete rebuildDocItem;
return;
}
XmlDoc **oldXmlDoc = xmlDoc->getOldXmlDoc();
if (!oldXmlDoc || oldXmlDoc == (XmlDoc**)-1) {
return;
}
if (!xmlDoc->m_contentValid && !xmlDoc->set2(*oldTitleRec, -1, "main", MAX_NICENESS)) {
xmlDoc->m_indexCode = ECORRUPTDATA;
xmlDoc->m_indexCodeValid = true;
xmlDoc->logIt();
removePendingDoc(rebuildDocItem);
delete xmlDoc;
delete rebuildDocItem;
return;
}
int32_t *firstIp = xmlDoc->getFirstIp();
if (!firstIp || firstIp == (int32_t*)-1) {
// blocked
return;
}
if (!rebuildDocItem->m_clearedXmlDoc) {
// logic copied from Repair.cpp
// rebuild the title rec! otherwise we re-add the old one
xmlDoc->m_titleRecBufValid = false;
xmlDoc->m_titleRecBuf.purge();
// recompute site, no more domain sites allowed
xmlDoc->m_siteValid = false;
xmlDoc->ptr_site = nullptr;
xmlDoc->size_site = 0;
// recalculate the sitenuminlinks
xmlDoc->m_siteNumInlinksValid = false;
// recalculate the langid
xmlDoc->m_langIdValid = false;
// recalcualte and store the link info
xmlDoc->m_linkInfo1Valid = false;
xmlDoc->ptr_linkInfo1 = nullptr;
xmlDoc->size_linkInfo1 = 0;
// re-get the tag rec from tagdb
xmlDoc->m_tagRecValid = false;
xmlDoc->m_tagRecDataValid = false;
xmlDoc->m_priority = -1;
xmlDoc->m_priorityValid = true;
xmlDoc->m_contentValid = true;
xmlDoc->m_content = xmlDoc->ptr_utf8Content;
xmlDoc->m_contentLen = xmlDoc->size_utf8Content - 1;
// update to latest version
#ifndef PRIVACORE_SAFE_VERSION
xmlDoc->m_version = g_conf.m_titleRecVersion;
#else
xmlDoc->m_version = TITLEREC_CURRENT_VERSION;
#endif
xmlDoc->m_versionValid = true;
rebuildDocItem->m_clearedXmlDoc = true;
}
// reset callback
if (xmlDoc->m_masterLoop == processedDoc) {
xmlDoc->m_masterLoop = nullptr;
xmlDoc->m_masterState = nullptr;
}
// set spider request
if (!rebuildDocItem->m_spiderdbListRequested) {
int64_t urlHash48 = xmlDoc->getFirstUrlHash48();
key128_t startKey = Spiderdb::makeKey(*firstIp, urlHash48, true, 0, false);
key128_t endKey = Spiderdb::makeKey(*firstIp, urlHash48, true, MAX_DOCID, false);
rebuildDocItem->m_spiderdbListRequested = true;
if (!rebuildDocItem->m_msg0.getList(-1, RDB_SPIDERDB_DEPRECATED, xmlDoc->m_collnum, &rebuildDocItem->m_spiderdbList, (const char *)&startKey,
(const char *)&endKey,
1000000, rebuildDocItem, processedDoc, 0, true, true, -1, 0, -1, 10000, false, false, -1)) {
// blocked
return;
}
}
if (!rebuildDocItem->m_spiderdbListProcessed) {
if (rebuildDocItem->m_spiderdbList.isEmpty()) {
xmlDoc->getRebuiltSpiderRequest(&xmlDoc->m_sreq);
xmlDoc->m_addSpiderRequest = true;
} else {
SpiderRequest *sreq = reinterpret_cast<SpiderRequest *>(rebuildDocItem->m_spiderdbList.getCurrentRec());
memcpy(&xmlDoc->m_sreq, sreq, sreq->m_dataSize + sizeof(key128_t) + 4);
}
xmlDoc->m_sreqValid = true;
rebuildDocItem->m_spiderdbListProcessed = true;
}
// done
if (xmlDoc->m_indexedDoc || xmlDoc->indexDoc()) {
removePendingDoc(rebuildDocItem);
delete xmlDoc;
delete rebuildDocItem;
}
}
int64_t DocRebuild::getMaxPending() const {
return g_conf.m_docRebuildMaxPending;
}
int64_t DocRebuild::getDelayMs() const {
return g_conf.m_docRebuildDelayMs;
}