Parent: [fe7e72] (diff)

Child: [91e66a] (diff)

Download this file

rclm_idx.cpp    441 lines (408 with data), 13.5 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
 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* Copyright (C) 2005 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "autoconfig.h"
#include <signal.h>
#include "safeunistd.h"
#include <QMessageBox>
#include <QTimer>
#include "execmd.h"
#include "log.h"
#include "transcode.h"
#include "indexer.h"
#include "rclmain_w.h"
#include "specialindex.h"
using namespace std;
void RclMain::updateIdxStatus()
{
ConfSimple cs(theconfig->getIdxStatusFile().c_str(), 1);
QString msg = tr("Indexing in progress: ");
DbIxStatus status;
string val;
cs.get("phase", val);
status.phase = DbIxStatus::Phase(atoi(val.c_str()));
cs.get("fn", status.fn);
cs.get("docsdone", &status.docsdone);
cs.get("filesdone", &status.filesdone);
cs.get("fileerrors", &status.fileerrors);
cs.get("dbtotdocs", &status.dbtotdocs);
cs.get("totfiles", &status.totfiles);
QString phs;
switch (status.phase) {
case DbIxStatus::DBIXS_NONE:phs=tr("None");break;
case DbIxStatus::DBIXS_FILES: phs=tr("Updating");break;
case DbIxStatus::DBIXS_PURGE: phs=tr("Purge");break;
case DbIxStatus::DBIXS_STEMDB: phs=tr("Stemdb");break;
case DbIxStatus::DBIXS_CLOSING:phs=tr("Closing");break;
case DbIxStatus::DBIXS_DONE:phs=tr("Done");break;
case DbIxStatus::DBIXS_MONITOR:phs=tr("Monitor");break;
default: phs=tr("Unknown");break;
}
msg += phs + " ";
if (status.phase == DbIxStatus::DBIXS_FILES) {
char cnts[100];
if (status.dbtotdocs > 0) {
sprintf(cnts, "(%d docs/%d files/%d errors/%d tot files) ",
status.docsdone, status.filesdone, status.fileerrors,
status.totfiles);
} else {
sprintf(cnts, "(%d docs/%d files/%d errors) ",
status.docsdone, status.filesdone, status.fileerrors);
}
msg += QString::fromUtf8(cnts) + " ";
}
string mf;int ecnt = 0;
string fcharset = theconfig->getDefCharset(true);
if (!transcode(status.fn, mf, fcharset, "UTF-8", &ecnt) || ecnt) {
mf = url_encode(status.fn, 0);
}
msg += QString::fromUtf8(mf.c_str());
statusBar()->showMessage(msg, 4000);
}
// This is called by a periodic timer to check the status of
// indexing, a possible need to exit, and cleanup exited viewers
void RclMain::periodic100()
{
LOGDEB2("Periodic100\n" );
if (m_idxproc) {
// An indexing process was launched. If its' done, see status.
int status;
bool exited = m_idxproc->maybereap(&status);
if (exited) {
deleteZ(m_idxproc);
if (status) {
if (m_idxkilled) {
QMessageBox::warning(0, "Recoll",
tr("Indexing interrupted"));
m_idxkilled = false;
} else {
QMessageBox::warning(0, "Recoll",
tr("Indexing failed"));
}
} else {
// On the first run, show missing helpers. We only do this once
if (m_firstIndexing)
showMissingHelpers();
}
string reason;
maybeOpenDb(reason, 1);
} else {
// update/show status even if the status file did not
// change (else the status line goes blank during
// lengthy operations).
updateIdxStatus();
}
}
// Update the "start/stop indexing" menu entry, can't be done from
// the "start/stop indexing" slot itself
IndexerState prevstate = m_indexerState;
if (m_idxproc) {
m_indexerState = IXST_RUNNINGMINE;
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
fileToggleIndexingAction->setEnabled(true);
fileRebuildIndexAction->setEnabled(false);
actionSpecial_Indexing->setEnabled(false);
periodictimer->setInterval(200);
} else {
Pidfile pidfile(theconfig->getPidfile());
pid_t pid = pidfile.open();
if (pid == getpid()) {
// Locked by me
m_indexerState = IXST_NOTRUNNING;
fileToggleIndexingAction->setText(tr("Index locked"));
fileToggleIndexingAction->setEnabled(false);
fileRebuildIndexAction->setEnabled(false);
actionSpecial_Indexing->setEnabled(false);
periodictimer->setInterval(1000);
} else if (pid == 0) {
m_indexerState = IXST_NOTRUNNING;
fileToggleIndexingAction->setText(tr("Update &Index"));
fileToggleIndexingAction->setEnabled(true);
fileRebuildIndexAction->setEnabled(true);
actionSpecial_Indexing->setEnabled(true);
periodictimer->setInterval(1000);
} else {
// Real time or externally started batch indexer running
m_indexerState = IXST_RUNNINGNOTMINE;
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
fileToggleIndexingAction->setEnabled(true);
fileRebuildIndexAction->setEnabled(false);
actionSpecial_Indexing->setEnabled(false);
periodictimer->setInterval(200);
}
}
if ((prevstate == IXST_RUNNINGMINE || prevstate == IXST_RUNNINGNOTMINE)
&& m_indexerState == IXST_NOTRUNNING) {
showTrayMessage("Indexing done");
}
// Possibly cleanup the dead viewers
for (vector<ExecCmd*>::iterator it = m_viewers.begin();
it != m_viewers.end(); it++) {
int status;
if ((*it)->maybereap(&status)) {
deleteZ(*it);
}
}
vector<ExecCmd*> v;
for (vector<ExecCmd*>::iterator it = m_viewers.begin();
it != m_viewers.end(); it++) {
if (*it)
v.push_back(*it);
}
m_viewers = v;
if (recollNeedsExit)
fileExit();
}
bool RclMain::checkIdxPaths()
{
string badpaths;
vector<string> args {"recollindex", "-c", theconfig->getConfDir(), "-E"};
ExecCmd::backtick(args, badpaths);
if (!badpaths.empty()) {
int rep = QMessageBox::warning(
0, tr("Bad paths"), tr("Bad paths in configuration file:\n") +
QString::fromLocal8Bit(badpaths.c_str()),
QMessageBox::Ok, QMessageBox::Cancel, QMessageBox::NoButton);
if (rep == QMessageBox::Cancel)
return false;
}
return true;
}
// This gets called when the "update index" action is activated. It executes
// the requested action, and disables the menu entry. This will be
// re-enabled by the indexing status check
void RclMain::toggleIndexing()
{
switch (m_indexerState) {
case IXST_RUNNINGMINE:
if (m_idxproc) {
// Indexing was in progress, request stop. Let the periodic
// routine check for the results.
if (m_idxproc->requestChildExit()) {
m_idxkilled = true;
}
}
break;
case IXST_RUNNINGNOTMINE:
{
#ifdef _WIN32
QMessageBox::warning(0, tr("Warning"),
tr("The current indexing process "
"was not started from this "
"interface, can't kill it"),
QMessageBox::Ok, QMessageBox::NoButton);
#else
int rep =
QMessageBox::information(
0, tr("Warning"),
tr("The current indexing process was not started from this "
"interface. Click Ok to kill it "
"anyway, or Cancel to leave it alone"),
QMessageBox::Ok, QMessageBox::Cancel, QMessageBox::NoButton);
if (rep == QMessageBox::Ok) {
Pidfile pidfile(theconfig->getPidfile());
pid_t pid = pidfile.open();
if (pid > 0)
kill(pid, SIGTERM);
}
#endif
}
break;
case IXST_NOTRUNNING:
{
// Could also mean that no helpers are missing, but then we
// won't try to show a message anyway (which is what
// firstIndexing is used for)
string mhd;
m_firstIndexing = !theconfig->getMissingHelperDesc(mhd);
if (!checkIdxPaths()) {
return;
}
vector<string> args{"-c", theconfig->getConfDir()};
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
}
break;
case IXST_UNKNOWN:
return;
}
}
void RclMain::rebuildIndex()
{
switch (m_indexerState) {
case IXST_UNKNOWN:
case IXST_RUNNINGMINE:
case IXST_RUNNINGNOTMINE:
return; //?? Should not have been called
case IXST_NOTRUNNING:
{
if (m_idxproc) {
LOGERR("RclMain::rebuildIndex: current indexer exec not null\n" );
return;
}
int rep =
QMessageBox::warning(0, tr("Erasing index"),
tr("Reset the index and start "
"from scratch ?"),
QMessageBox::Ok,
QMessageBox::Cancel,
QMessageBox::NoButton);
if (rep == QMessageBox::Ok) {
#ifdef _WIN32
// Under windows, it's necessary to close the db here,
// else Xapian won't be able to do what it wants with the
// (open) files. Of course if there are several GUI
// instances, this won't work...
if (rcldb)
rcldb->close();
#endif // _WIN32
// Could also mean that no helpers are missing, but then we
// won't try to show a message anyway (which is what
// firstIndexing is used for)
string mhd;
m_firstIndexing = !theconfig->getMissingHelperDesc(mhd);
if (!checkIdxPaths()) {
return;
}
vector<string> args{"-c", theconfig->getConfDir(), "-z"};
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
}
}
break;
}
}
void SpecIdxW::onBrowsePB_clicked()
{
QString dir = myGetFileName(true, tr("Top indexed entity"), true);
targLE->setText(dir);
}
bool SpecIdxW::noRetryFailed()
{
return noRetryFailedCB->isChecked();
}
bool SpecIdxW::eraseFirst()
{
return eraseBeforeCB->isChecked();
}
std::vector<std::string> SpecIdxW::selpatterns()
{
vector<string> pats;
string text = qs2utf8s(selPatsLE->text());
if (!text.empty()) {
stringToStrings(text, pats);
}
return pats;
}
std::string SpecIdxW::toptarg()
{
return qs2utf8s(targLE->text());
}
void SpecIdxW::onTargLE_textChanged(const QString& text)
{
if (text.isEmpty())
selPatsLE->setEnabled(false);
else
selPatsLE->setEnabled(true);
}
static string execToString(const string& cmd, const vector<string>& args)
{
string command = cmd + " ";
for (vector<string>::const_iterator it = args.begin();
it != args.end(); it++) {
command += "{" + *it + "} ";
}
return command;
}
void RclMain::specialIndex()
{
LOGDEB("RclMain::specialIndex\n" );
switch (m_indexerState) {
case IXST_UNKNOWN:
case IXST_RUNNINGMINE:
case IXST_RUNNINGNOTMINE:
return; //?? Should not have been called
case IXST_NOTRUNNING:
default:
break;
}
if (m_idxproc) {
LOGERR("RclMain::rebuildIndex: current indexer exec not null\n" );
return;
}
if (!specidx) // ??
return;
vector<string> args{"-c", theconfig->getConfDir()};
string top = specidx->toptarg();
if (!top.empty()) {
args.push_back("-r");
}
if (specidx->eraseFirst()) {
if (top.empty()) {
args.push_back("-Z");
} else {
args.push_back("-e");
}
}
// The default for retrying differ depending if -r is set
if (top.empty()) {
if (!specidx->noRetryFailed()) {
args.push_back("-k");
}
} else {
if (specidx->noRetryFailed()) {
args.push_back("-K");
}
}
vector<string> selpats = specidx->selpatterns();
if (!selpats.empty() && top.empty()) {
QMessageBox::warning(0, tr("Selection patterns need topdir"),
tr("Selection patterns can only be used with a "
"start directory"),
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
for (vector<string>::const_iterator it = selpats.begin();
it != selpats.end(); it++) {
args.push_back("-p");
args.push_back(*it);
}
if (!top.empty()) {
args.push_back(top);
}
m_idxproc = new ExecCmd;
LOGINFO("specialIndex: exec: " << execToString("recollindex", args) <<endl);
m_idxproc->startExec("recollindex", args, false, false);
}
void RclMain::updateIdxForDocs(vector<Rcl::Doc>& docs)
{
if (m_idxproc) {
QMessageBox::warning(0, tr("Warning"),
tr("Can't update index: indexer running"),
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
vector<string> paths;
if (ConfIndexer::docsToPaths(docs, paths)) {
vector<string> args{"-c", theconfig->getConfDir(), "-e", "-i"};
args.insert(args.end(), paths.begin(), paths.end());
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
}
fileToggleIndexingAction->setEnabled(false);
actionSpecial_Indexing->setEnabled(false);
}