SearchIndex.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "core/SearchIndex.h"
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <sstream>
  5. namespace lynx::search
  6. {
  7. std::string Index::join (const std::string& folder, const std::string& name, char separator)
  8. {
  9. if (! folder.empty() && (folder.back() == '/' || folder.back() == '\\'))
  10. return folder + name;
  11. return folder + separator + name;
  12. }
  13. bool Index::isUnder (const std::string& path, const std::string& root)
  14. {
  15. if (root.empty() || path.size() < root.size() || path.compare (0, root.size(), root) != 0)
  16. return false;
  17. if (path.size() == root.size())
  18. return true;
  19. const auto last = root.back();
  20. const auto next = path[root.size()];
  21. return last == '/' || last == '\\' || next == '/' || next == '\\';
  22. }
  23. void Index::drop (const std::string& folder, char separator)
  24. {
  25. const auto found = folders.find (folder);
  26. if (found == folders.end())
  27. return;
  28. const auto subfolders = found->second.subfolders;
  29. folders.erase (found);
  30. for (const auto& s : subfolders)
  31. drop (join (folder, s, separator), separator);
  32. }
  33. int Index::refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop)
  34. {
  35. if (! fs.folderTime || ! fs.list || root.empty())
  36. return 0;
  37. int listed = 0;
  38. std::vector<std::string> pending { root };
  39. while (! pending.empty())
  40. {
  41. if (shouldStop && shouldStop())
  42. break;
  43. const auto folder = pending.back();
  44. pending.pop_back();
  45. if (folder.size() > 1024) // a runaway path (a link loop the file system did not report): not followed
  46. continue;
  47. const auto time = fs.folderTime (folder);
  48. if (time < 0)
  49. {
  50. drop (folder, fs.separator);
  51. continue;
  52. }
  53. auto known = folders.find (folder);
  54. if (known == folders.end() || known->second.time != time) // new or "dirty": list it again
  55. {
  56. auto listing = fs.list (folder);
  57. ++listed;
  58. if (! listing.exists)
  59. {
  60. drop (folder, fs.separator);
  61. continue;
  62. }
  63. if (known != folders.end())
  64. for (const auto& old : known->second.subfolders)
  65. if (std::find (listing.subfolders.begin(), listing.subfolders.end(), old) == listing.subfolders.end())
  66. drop (join (folder, old, fs.separator), fs.separator);
  67. auto& entry = folders[folder];
  68. entry.time = time;
  69. entry.files = std::move (listing.files);
  70. entry.subfolders = std::move (listing.subfolders);
  71. known = folders.find (folder);
  72. }
  73. // depth-first, in name order (pushed reversed)
  74. const auto& subs = known->second.subfolders;
  75. for (auto s = subs.rbegin(); s != subs.rend(); ++s)
  76. pending.push_back (join (folder, *s, fs.separator));
  77. }
  78. return listed;
  79. }
  80. std::vector<std::string> Index::find (const std::string& query, const std::vector<std::string>& roots, std::size_t max, bool& truncated) const
  81. {
  82. truncated = false;
  83. std::vector<std::string> found;
  84. const auto parsed = parse (query);
  85. for (const auto& [path, folder] : folders)
  86. {
  87. if (std::none_of (roots.begin(), roots.end(), [&path] (const std::string& r) { return isUnder (path, r); }))
  88. continue;
  89. const auto separator = path.find ('\\') != std::string::npos ? '\\' : '/';
  90. for (const auto& name : folder.files)
  91. {
  92. if (! matches (parsed, name))
  93. continue;
  94. if (found.size() >= max)
  95. {
  96. truncated = true;
  97. return found;
  98. }
  99. found.push_back (join (path, name, separator));
  100. }
  101. }
  102. return found;
  103. }
  104. std::size_t Index::fileCount() const noexcept
  105. {
  106. std::size_t n = 0;
  107. for (const auto& f : folders)
  108. n += f.second.files.size();
  109. return n;
  110. }
  111. std::string Index::toText() const
  112. {
  113. std::string out = "LYNX browser index 1\n";
  114. for (const auto& [path, folder] : folders)
  115. {
  116. out += "D\t" + std::to_string (folder.time) + "\t" + path + "\n";
  117. for (const auto& f : folder.files)
  118. out += "F\t" + f + "\n";
  119. for (const auto& s : folder.subfolders)
  120. out += "S\t" + s + "\n";
  121. }
  122. return out;
  123. }
  124. bool Index::fromText (const std::string& text)
  125. {
  126. folders.clear();
  127. std::istringstream in (text);
  128. std::string line;
  129. if (! std::getline (in, line) || line != "LYNX browser index 1")
  130. return false;
  131. Folder* current = nullptr;
  132. while (std::getline (in, line))
  133. {
  134. if (! line.empty() && line.back() == '\r')
  135. line.pop_back();
  136. if (line.size() < 2 || line[1] != '\t')
  137. continue;
  138. const auto rest = line.substr (2);
  139. if (line[0] == 'D')
  140. {
  141. const auto tab = rest.find ('\t');
  142. if (tab == std::string::npos)
  143. {
  144. current = nullptr;
  145. continue;
  146. }
  147. auto& f = folders[rest.substr (tab + 1)];
  148. f.time = std::strtoll (rest.substr (0, tab).c_str(), nullptr, 10);
  149. current = &f;
  150. }
  151. else if (current != nullptr && line[0] == 'F')
  152. current->files.push_back (rest);
  153. else if (current != nullptr && line[0] == 'S')
  154. current->subfolders.push_back (rest);
  155. }
  156. return true;
  157. }
  158. } // namespace lynx::search