|
| 1 | +#include "search_index.h" |
| 2 | + |
| 3 | +#include <QDirIterator> |
| 4 | +#include <QMimeType> |
| 5 | +#include <QMimeDatabase> |
| 6 | +#include <QDebug> |
| 7 | +#include <QVector> |
| 8 | + |
| 9 | +#include <fstream> |
| 10 | +#include <set> |
| 11 | +#include <stdio.h> |
| 12 | + |
| 13 | +index_search::index_search(QMutex &mtx, QMap<QString, std::set<tgram>> &paths_to_tgram, QString const dir_path) : filewatcher(new QFileSystemWatcher), mtx(mtx), paths_to_tgram(paths_to_tgram), dir_path(dir_path){ |
| 14 | + |
| 15 | + connect(filewatcher.get(), SIGNAL(fileChanged(QString)), this, SLOT(add_to_map(QString))); |
| 16 | + connect(filewatcher.get(), SIGNAL(directoryChanged(QString)), this, SLOT(start_index(QString))); |
| 17 | + |
| 18 | + connect(filewatcher.get(), SIGNAL(fileChanged(QString)), this, SLOT(file_change_slot())); |
| 19 | + connect(filewatcher.get(), SIGNAL(directoryChanged(QString)), this, SLOT(file_change_slot())); |
| 20 | +} |
| 21 | + |
| 22 | +void index_search::file_change_slot(){ |
| 23 | + emit file_change(); |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +void index_search::start_index(){ |
| 28 | + start_index(dir_path); |
| 29 | +} |
| 30 | + |
| 31 | +index_search::~index_search(){} |
| 32 | + |
| 33 | + |
| 34 | +void index_search::start_index(QString cur_path) { |
| 35 | + QDirIterator it(cur_path, QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks | QDir::AllEntries, QDirIterator::Subdirectories); |
| 36 | + emit set_max_progress(0); |
| 37 | + emit set_progress(0); |
| 38 | + |
| 39 | + QVector<QString> paths; |
| 40 | + while (it.hasNext()) { |
| 41 | + if (QThread::currentThread()->isInterruptionRequested()) { |
| 42 | + break; |
| 43 | + } |
| 44 | + QFileInfo fi(it.next()); |
| 45 | + if(fi.permission(QFile::ReadUser)) { |
| 46 | + if(fi.isFile()) { |
| 47 | + paths.push_back(fi.filePath()); |
| 48 | + } |
| 49 | + filewatcher->addPath(fi.filePath()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + emit set_max_progress(paths.size()); |
| 54 | + |
| 55 | + for(int i = 0; i < paths.size(); ++i) { |
| 56 | + if (QThread::currentThread()->isInterruptionRequested()) { |
| 57 | + break; |
| 58 | + } |
| 59 | + emit set_progress(i); |
| 60 | + add_to_map(paths[i]); |
| 61 | + } |
| 62 | + |
| 63 | + emit set_max_progress(1); |
| 64 | + emit set_progress(1); |
| 65 | + emit index_end(); |
| 66 | +// emit finished(); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +void index_search::add_to_map(QString const &path) { |
| 71 | + static QMimeDatabase mimeDatabase; |
| 72 | + const QMimeType mimeType = mimeDatabase.mimeTypeForFile(path); |
| 73 | + if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + std::ifstream fin(path.toStdString(), std::ios::in | std::ios::binary); |
| 78 | + if (!fin) { |
| 79 | + qDebug()<<path; |
| 80 | + return; |
| 81 | + } |
| 82 | + std::set<tgram> tgram_set; |
| 83 | + std::vector<char> buffer23 * READ_BLOCK); |
| 84 | + size_t count = 0; |
| 85 | + bool flag = true; |
| 86 | + char symbol[2] = {0, 0}; |
| 87 | + // a s d a s д/2 д/2 a b s |
| 88 | + while (!fin.eof() && flag) { |
| 89 | + fin.read(buffer.data(), READ_BLOCK); |
| 90 | + |
| 91 | + count = static_cast<size_t>(fin.gcount()); |
| 92 | + |
| 93 | + for (size_t i = 0; i < count; ++i) { |
| 94 | + if(i >= 2) { |
| 95 | + tgram_set.insert(tgram(buffer[i-2], buffer[i-1], buffer[i])); |
| 96 | + } else if(symbol[0] != 0){ |
| 97 | + tgram_set.insert(tgram(symbol[0], symbol[1], buffer[i])); |
| 98 | + symbol[0] = symbol[1]; |
| 99 | + symbol[1] = buffer[i]; |
| 100 | + } |
| 101 | + } |
| 102 | + if(count >= 2) |
| 103 | + symbol[0] = buffer[count - 2]; |
| 104 | + if(count >= 1) |
| 105 | + symbol[1] = buffer[count - 1]; |
| 106 | + |
| 107 | + if(tgram_set.size() > 20000){ |
| 108 | + flag = false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if(flag) { |
| 113 | + mtx.lock(); |
| 114 | + paths_to_tgram.insert(path, tgram_set); |
| 115 | + mtx.unlock(); |
| 116 | + } |
| 117 | + |
| 118 | + fin.close(); |
| 119 | +} |
0 commit comments