SearchIndex.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. ++gen;
  31. for (const auto& s : subfolders)
  32. drop (join (folder, s, separator), separator);
  33. }
  34. void Index::sweep (std::uint64_t since)
  35. {
  36. for (auto f = folders.begin(); f != folders.end();)
  37. {
  38. if (f->second.seenIn < since)
  39. {
  40. f = folders.erase (f);
  41. ++gen;
  42. }
  43. else
  44. ++f;
  45. }
  46. for (auto r = complete.begin(); r != complete.end();)
  47. {
  48. if (folders.count (*r) == 0)
  49. {
  50. r = complete.erase (r);
  51. ++gen;
  52. }
  53. else
  54. ++r;
  55. }
  56. }
  57. int Index::refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop, std::mutex* guard,
  58. std::uint64_t relistBefore)
  59. {
  60. if (! fs.folderTime || ! fs.list || root.empty())
  61. return 0;
  62. auto hold = [guard] { return guard != nullptr ? std::unique_lock<std::mutex> (*guard) : std::unique_lock<std::mutex>(); };
  63. int listed = 0;
  64. std::vector<std::string> pending { root };
  65. while (! pending.empty())
  66. {
  67. if (shouldStop && shouldStop())
  68. break;
  69. const auto folder = pending.back();
  70. pending.pop_back();
  71. if (folder.size() > 1024) // a runaway path (a link loop the file system did not report): not followed
  72. continue;
  73. const auto time = fs.folderTime (folder); // (the disk: without the lock)
  74. std::vector<std::string> subs;
  75. bool dirty = false;
  76. {
  77. const auto lock = hold();
  78. if (time < 0)
  79. {
  80. drop (folder, fs.separator);
  81. continue;
  82. }
  83. const auto known = folders.find (folder);
  84. // new, "dirty" or not listed since a rebuild began: list it again
  85. dirty = known == folders.end() || known->second.time != time || known->second.listedIn < relistBefore;
  86. if (! dirty)
  87. {
  88. known->second.seenIn = mark;
  89. subs = known->second.subfolders;
  90. }
  91. }
  92. if (dirty)
  93. {
  94. auto listing = fs.list (folder);
  95. ++listed;
  96. const auto lock = hold();
  97. if (! listing.exists)
  98. {
  99. drop (folder, fs.separator);
  100. continue;
  101. }
  102. if (const auto known = folders.find (folder); known != folders.end())
  103. for (const auto& old : known->second.subfolders)
  104. if (std::find (listing.subfolders.begin(), listing.subfolders.end(), old) == listing.subfolders.end())
  105. drop (join (folder, old, fs.separator), fs.separator);
  106. auto& entry = folders[folder];
  107. ++gen;
  108. entry.time = time;
  109. entry.listedIn = entry.seenIn = mark;
  110. entry.files = std::move (listing.files);
  111. entry.subfolders = std::move (listing.subfolders);
  112. subs = entry.subfolders;
  113. }
  114. // depth-first, in name order (pushed reversed)
  115. for (auto s = subs.rbegin(); s != subs.rend(); ++s)
  116. pending.push_back (join (folder, *s, fs.separator));
  117. }
  118. return listed;
  119. }
  120. std::vector<std::string> Index::find (const std::string& query, const std::vector<std::string>& roots, std::size_t max, bool& truncated) const
  121. {
  122. truncated = false;
  123. std::vector<std::string> found;
  124. const auto parsed = parse (query);
  125. for (const auto& [path, folder] : folders)
  126. {
  127. if (std::none_of (roots.begin(), roots.end(), [&path] (const std::string& r) { return isUnder (path, r); }))
  128. continue;
  129. const auto separator = path.find ('\\') != std::string::npos ? '\\' : '/';
  130. for (const auto& name : folder.files)
  131. {
  132. if (! matches (parsed, name))
  133. continue;
  134. if (found.size() >= max)
  135. {
  136. truncated = true;
  137. return found;
  138. }
  139. found.push_back (join (path, name, separator));
  140. }
  141. }
  142. return found;
  143. }
  144. FolderMatches Index::folderMatches (const std::function<bool (const std::string&)>& accept) const
  145. {
  146. FolderMatches result;
  147. result.generation = gen;
  148. for (const auto& [path, folder] : folders)
  149. {
  150. if (result.some.count (path) > 0 || std::none_of (folder.files.begin(), folder.files.end(), accept))
  151. continue;
  152. // this folder and every indexed folder above it hold something
  153. auto at = path;
  154. while (result.some.insert (at).second)
  155. {
  156. const auto cut = at.find_last_of ("/\\");
  157. if (cut == std::string::npos || cut == 0)
  158. break;
  159. auto parent = at.substr (0, cut);
  160. if (folders.count (parent) == 0)
  161. {
  162. parent += at[cut]; // a drive's root keeps its separator ("C:\\")
  163. if (folders.count (parent) == 0)
  164. break;
  165. }
  166. at = std::move (parent);
  167. }
  168. }
  169. for (const auto& [path, folder] : folders)
  170. if (result.some.count (path) == 0 && isComplete (path))
  171. result.none.insert (path);
  172. return result;
  173. }
  174. bool Index::isComplete (const std::string& folder) const
  175. {
  176. return std::any_of (complete.begin(), complete.end(), [this, &folder] (const std::string& r) { return isUnder (folder, r) && folders.count (r) > 0; });
  177. }
  178. std::size_t Index::fileCount() const noexcept
  179. {
  180. std::size_t n = 0;
  181. for (const auto& f : folders)
  182. n += f.second.files.size();
  183. return n;
  184. }
  185. std::string Index::toText() const
  186. {
  187. std::string out = "LYNX browser index 1\n";
  188. for (const auto& r : complete)
  189. out += "R\t" + r + "\n";
  190. for (const auto& [path, folder] : folders)
  191. {
  192. out += "D\t" + std::to_string (folder.time) + "\t" + path + "\n";
  193. for (const auto& f : folder.files)
  194. out += "F\t" + f + "\n";
  195. for (const auto& s : folder.subfolders)
  196. out += "S\t" + s + "\n";
  197. }
  198. return out;
  199. }
  200. bool Index::fromText (const std::string& text)
  201. {
  202. clear();
  203. ++gen;
  204. std::istringstream in (text);
  205. std::string line;
  206. if (! std::getline (in, line) || line != "LYNX browser index 1")
  207. return false;
  208. Folder* current = nullptr;
  209. while (std::getline (in, line))
  210. {
  211. if (! line.empty() && line.back() == '\r')
  212. line.pop_back();
  213. if (line.size() < 2 || line[1] != '\t')
  214. continue;
  215. const auto rest = line.substr (2);
  216. if (line[0] == 'D')
  217. {
  218. const auto tab = rest.find ('\t');
  219. if (tab == std::string::npos)
  220. {
  221. current = nullptr;
  222. continue;
  223. }
  224. auto& f = folders[rest.substr (tab + 1)];
  225. f.time = std::strtoll (rest.substr (0, tab).c_str(), nullptr, 10);
  226. current = &f;
  227. }
  228. else if (line[0] == 'R')
  229. complete.insert (rest);
  230. else if (current != nullptr && line[0] == 'F')
  231. current->files.push_back (rest);
  232. else if (current != nullptr && line[0] == 'S')
  233. current->subfolders.push_back (rest);
  234. }
  235. return true;
  236. }
  237. } // namespace lynx::search