Optimize index when getting tags to ensure deleted notes are accounted for
authorAdam Dullage <redacted>
Tue, 14 May 2024 11:13:31 +0000 (12:13 +0100)
committerAdam Dullage <redacted>
Tue, 14 May 2024 11:13:31 +0000 (12:13 +0100)
server/notes/file_system/file_system.py

index 318022bc5067d9a8eb156dffea98706150b69fd6..fbb34766ba85cde33c1bd85aecc1b0e1ac5be5ef 100644 (file)
@@ -51,7 +51,7 @@ class FileSystemNotes(BaseNotes):
                 f"'{self.storage_path}' is not a valid directory."\r
             )\r
         self.index = self._load_index()\r
-        self._sync_index()\r
+        self._sync_index(optimize=True)\r
 \r
     def create(self, data: NoteCreate) -> Note:\r
         """Create a new note."""\r
@@ -145,8 +145,10 @@ class FileSystemNotes(BaseNotes):
             return tuple(self._search_result_from_hit(hit) for hit in results)\r
 \r
     def get_tags(self) -> list[str]:\r
-        """Return a list of all indexed tags."""\r
-        self._sync_index()\r
+        """Return a list of all indexed tags. The index is synchronized and\r
+        optimised before returning the tags to ensure the most up-to-date list\r
+        is returned."""\r
+        self._sync_index(optimize=True)\r
         with self.index.reader() as reader:\r
             tags = reader.field_terms("tags")\r
             return [tag for tag in tags]\r
@@ -223,7 +225,7 @@ class FileSystemNotes(BaseNotes):
             )\r
         ]\r
 \r
-    def _sync_index(self, clean: bool = False) -> None:\r
+    def _sync_index(self, optimize: bool = False, clean: bool = False) -> None:\r
         """Synchronize the index with the notes directory.\r
         Specify clean=True to completely rebuild the index"""\r
         indexed = set()\r
@@ -258,7 +260,7 @@ class FileSystemNotes(BaseNotes):
                     writer, self._get_by_filename(filename)\r
                 )\r
                 logger.info(f"'{filename}' added to index")\r
-        writer.commit()\r
+        writer.commit(optimize=optimize)\r
 \r
     @classmethod\r
     def _pre_process_search_term(cls, term):\r
git clone https://git.99rst.org/PROJECT