branch: externals/org-gnosis
commit 2a66b95da7b23cf57a374ad4b2a79a15d2385a0a
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    [Refactor] Simplify tags cleanup with a single SQL query.
---
 org-gnosis.el | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/org-gnosis.el b/org-gnosis.el
index 3a9a39b2df..f86f5f5e52 100644
--- a/org-gnosis.el
+++ b/org-gnosis.el
@@ -422,14 +422,10 @@ If JOURNAL is non-nil, update file as a journal entry."
                    (org-gnosis--delete 'nodes `(= id ,node)))))))
 
 (defun org-gnosis-tags--cleanup-orphaned ()
-  "Remove orphaned tags that have no associated nodes.
-Efficient: only checks tags not in node-tag table."
-  (let* ((all-tags (org-gnosis-select 'tag 'tags nil t))
-         (used-tags (org-gnosis-select 'tag 'node-tag nil t))
-         (orphaned (cl-set-difference all-tags used-tags :test #'equal)))
-    (when orphaned
-      (dolist (tag orphaned)
-        (emacsql (org-gnosis-db-get) [:delete :from tags :where (= tag $s1)] 
tag)))))
+  "Remove orphaned tags that have no associated nodes."
+  (emacsql (org-gnosis-db-get)
+           [:delete :from tags
+            :where (not-in tag [:select :distinct tag :from node-tag])]))
 
 (defun org-gnosis-update-file (&optional file)
   "Update contents of FILE in database.

Reply via email to