Просмотр исходного кода

Browser search: a rebuild no longer empties the index first, so searches meanwhile answer at once

"Rebuild search index" cleared the whole index before walking the disk again. A search typed during the rebuild
then found its folder un-indexed and had to list it again itself, and its first (partial) answer only came after
500 ms. On macOS, where the walk is slower (CI: the Tests job failed on browser162_search_is_served_before_a_background_pass
with 440-527 ms), and on any slow drive, the Browser search stalled for about half a second or more.

Now a rebuild keeps the index readable: every folder reached is listed again even when its time did not change
(Index::beginRebuild / refresh relistBefore), and folders no walk reached since the rebuild began are dropped when it
ends (Index::sweep). A search that has to index a folder also shows what is already indexed at once instead of after
500 ms. New test: browser164_rebuild_keeps_the_index_readable_and_drops_what_is_gone.
Ka1-S 1 месяц назад
Родитель
Сommit
96ea61c8c5
5 измененных файлов с 95 добавлено и 8 удалено
  1. 32 2
      src/core/SearchIndex.cpp
  2. 12 2
      src/core/SearchIndex.h
  3. 17 4
      src/core/SearchService.cpp
  4. 1 0
      src/core/SearchService.h
  5. 33 0
      tests/Browser162Tests.inc

+ 32 - 2
src/core/SearchIndex.cpp

@@ -36,7 +36,32 @@ void Index::drop (const std::string& folder, char separator)
         drop (join (folder, s, separator), separator);
 }
 
-int Index::refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop, std::mutex* guard)
+void Index::sweep (std::uint64_t since)
+{
+    for (auto f = folders.begin(); f != folders.end();)
+    {
+        if (f->second.seenIn < since)
+        {
+            f = folders.erase (f);
+            ++gen;
+        }
+        else
+            ++f;
+    }
+    for (auto r = complete.begin(); r != complete.end();)
+    {
+        if (folders.count (*r) == 0)
+        {
+            r = complete.erase (r);
+            ++gen;
+        }
+        else
+            ++r;
+    }
+}
+
+int Index::refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop, std::mutex* guard,
+                    std::uint64_t relistBefore)
 {
     if (! fs.folderTime || ! fs.list || root.empty())
         return 0;
@@ -66,9 +91,13 @@ int Index::refresh (const std::string& root, const FileSystem& fs, const std::fu
                 continue;
             }
             const auto known = folders.find (folder);
-            dirty = known == folders.end() || known->second.time != time; // new or "dirty": list it again
+            // new, "dirty" or not listed since a rebuild began: list it again
+            dirty = known == folders.end() || known->second.time != time || known->second.listedIn < relistBefore;
             if (! dirty)
+            {
+                known->second.seenIn = mark;
                 subs = known->second.subfolders;
+            }
         }
 
         if (dirty)
@@ -88,6 +117,7 @@ int Index::refresh (const std::string& root, const FileSystem& fs, const std::fu
             auto& entry = folders[folder];
             ++gen;
             entry.time = time;
+            entry.listedIn = entry.seenIn = mark;
             entry.files = std::move (listing.files);
             entry.subfolders = std::move (listing.subfolders);
             subs = entry.subfolders;

+ 12 - 2
src/core/SearchIndex.h

@@ -59,8 +59,10 @@ public:
     // Brings the folders under `root` up to date (adds root when new). Returns how many folders were listed again;
     // stops early (leaving the rest for the next refresh) when shouldStop returns true (checked between folders).
     // With `guard`, the index is only locked while it is read or changed, never while the disk is read, so searches
-    // of what is already indexed are not held up by a long walk (round 162, issue #9).
-    int refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop = {}, std::mutex* guard = nullptr);
+    // of what is already indexed are not held up by a long walk (round 162, issue #9). Folders not listed since
+    // `relistBefore` (a beginRebuild() mark) are listed again even when their time did not change.
+    int refresh (const std::string& root, const FileSystem& fs, const std::function<bool()>& shouldStop = {}, std::mutex* guard = nullptr,
+                 std::uint64_t relistBefore = 0);
 
     // Files whose name matches `query` under the given roots (full paths), in folder order, at most `max`.
     std::vector<std::string> find (const std::string& query, const std::vector<std::string>& roots, std::size_t max, bool& truncated) const;
@@ -75,6 +77,12 @@ public:
         ++gen;
     }
 
+    // A rebuild without forgetting the index first (round 164): searches keep reading what is indexed while every
+    // folder is listed again (refresh with the returned mark as relistBefore); sweep (mark) then drops the folders no
+    // refresh reached since the mark (gone from the disk, or outside the rebuilt trees).
+    std::uint64_t beginRebuild() noexcept { return ++mark; }
+    void sweep (std::uint64_t since);
+
     // Changes whenever the index does (round 164: what was worked out from it is out of date).
     std::uint64_t generation() const noexcept { return gen; }
 
@@ -101,10 +109,12 @@ private:
     {
         std::int64_t time = 0;
         std::vector<std::string> files, subfolders;
+        std::uint64_t listedIn = 0, seenIn = 0; // the beginRebuild() mark current when it was last listed / reached
     };
     std::map<std::string, Folder> folders; // full path -> contents
     std::set<std::string> complete;
     std::uint64_t gen = 0;
+    std::uint64_t mark = 0;
     static std::string join (const std::string& folder, const std::string& name, char separator);
     void drop (const std::string& folder, char separator);
 };

+ 17 - 4
src/core/SearchService.cpp

@@ -265,15 +265,21 @@ void Service::runSearch (SearchJob& job)
         const auto disk = counting (fs, listed);
         auto lastShown = now(), lastSaved = now();
         std::size_t savedAt = 0;
+        bool shown = false;
         auto stop = [&] {
             if (superseded (job.id))
                 return true;
             const auto t = now();
-            if (t - lastShown >= 500)
+            // what is already indexed is shown at once (round 164), then partial results every 500 ms
+            if (! shown || t - lastShown >= 500)
             {
+                const bool first = ! shown;
+                shown = true;
                 lastShown = t;
                 setStatus ([&] (IndexStatus& s) { s.listed = listed; });
                 const auto partial = find (job, true);
+                if (first && partial.paths.empty())
+                    return false;
                 const std::lock_guard<std::mutex> guard (lock);
                 if (! stopping && latestSearch == job.id && job.done)
                     job.done (partial);
@@ -346,9 +352,11 @@ bool Service::runPass (Pass& pass)
 {
     if (pass.rebuild)
     {
+        // the index is not forgotten first (searches meanwhile still read it, round 164): every folder is listed again
+        // and the ones no walk reached are dropped at the end
         {
             const std::lock_guard<std::mutex> guard (indexLock);
-            index.clear();
+            pass.rebuildMark = index.beginRebuild();
         }
         {
             const std::lock_guard<std::mutex> guard (lock);
@@ -392,7 +400,7 @@ bool Service::runPass (Pass& pass)
             s.current = root;
             s.listed = listed;
         });
-        index.refresh (root, disk, stop, &indexLock);
+        index.refresh (root, disk, stop, &indexLock, pass.rebuildMark);
         if (interrupted())
         {
             if (listed > savedAt)
@@ -412,7 +420,12 @@ bool Service::runPass (Pass& pass)
         pass.roots.erase (pass.roots.begin());
     }
 
-    if (listed > savedAt)
+    if (pass.rebuildMark != 0)
+    {
+        const std::lock_guard<std::mutex> guard (indexLock);
+        index.sweep (pass.rebuildMark);
+    }
+    if (listed > savedAt || pass.rebuildMark != 0)
         saveIndex();
     const auto changed = listed + pass.listedBefore > 0;
     setStatus ([&] (IndexStatus& s) {

+ 1 - 0
src/core/SearchService.h

@@ -103,6 +103,7 @@ private:
     {
         std::vector<std::string> roots;
         bool rebuild = false;
+        std::uint64_t rebuildMark = 0;  // Index::beginRebuild() of a rebuild that started (0: an ordinary pass)
         std::size_t listedBefore = 0; // folders listed again before the pass was interrupted
     };
 

+ 33 - 0
tests/Browser162Tests.inc

@@ -244,3 +244,36 @@ TEST (browser162_search_is_served_before_a_background_pass)
     CHECK (service.waitIdle (30000));
     CHECK (service.status().folders == 2551 && ! service.status().busy);
 }
+
+TEST (browser164_rebuild_keeps_the_index_readable_and_drops_what_is_gone)
+{
+    // "Rebuild search index" lists every folder again even when its time did not change, without forgetting the index
+    // first (searches meanwhile read it); folders the rebuild no longer reaches are dropped at its end
+    b162::SlowDisk disk (5, 0);
+    lynx::search::Service service (disk.fs(), {});
+    b162::Inbox inbox;
+    service.search ("kick", { "/lib" }, 500, inbox.sink());
+    CHECK (service.waitIdle (10000));
+    auto r = inbox.take();
+    CHECK (r.size() == 1 && r[0].paths.size() == 5 * 5 * 4);
+    const auto listedFirst = disk.lists.load();
+    {
+        // the disk changes behind the folders' times: a2 is gone, a file appears in a3/b1
+        const std::lock_guard<std::mutex> g (disk.m);
+        auto& subs = disk.disk["/lib"].subs;
+        subs.erase (std::find (subs.begin(), subs.end(), "a2"));
+        disk.disk["/lib/a3/b1"].files.push_back ("kick new.wav");
+    }
+    service.refresh ({ "/lib" }, false); // an ordinary pass trusts the times
+    CHECK (service.waitIdle (10000));
+    CHECK (disk.lists.load() == listedFirst);
+    service.refresh ({ "/lib" }, true);
+    CHECK (service.waitIdle (10000));
+    CHECK (disk.lists.load() == listedFirst + 1 + 4 * 6);
+    CHECK (service.status().folders == 1 + 4 * 6 && service.status().files == 4 * 5 * 20 + 1);
+    service.search ("kick", { "/lib" }, 500, inbox.sink());
+    CHECK (service.waitIdle (10000));
+    r = inbox.take();
+    CHECK (r.size() == 1 && ! r[0].partial && r[0].paths.size() == 4 * 5 * 4 + 1);
+    CHECK (std::none_of (r[0].paths.begin(), r[0].paths.end(), [] (const std::string& p) { return p.rfind ("/lib/a2/", 0) == 0; }));
+}