Skip to content

Commit a199886

Browse files
committed
speed up org-roam-node--completions using SQL query
use @d12frosted's example SQL to produce completion candidates
1 parent 8bed015 commit a199886

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

org-roam.el

+61-28
Original file line numberDiff line numberDiff line change
@@ -644,40 +644,73 @@ Throw an error if multiple choices exist."
644644
(t
645645
(user-error "Multiple nodes exist with title or alias \"%s\"" s)))))
646646

647+
(defun org-roam-node--to-candidate (node)
648+
"Return a minibuffer completion candidate given NODE."
649+
(let ((candidate-main (org-roam-node--format-entry node (1- (frame-width))))
650+
(tag-str (org-roam--tags-to-str (org-roam-node-tags node))))
651+
(cons (propertize (concat candidate-main
652+
(propertize tag-str 'invisible t))
653+
'node node)
654+
node)))
655+
647656
(defun org-roam-node--completions ()
648657
"Return an alist for node completion.
649658
The car is the displayed title or alias for the node, and the cdr
650659
is the `org-roam-node'.
651660
The displayed title is formatted according to `org-roam-node-display-template'."
652661
(setq org-roam--cached-display-format nil)
653-
(let ((files-table (org-roam--files-table))
654-
(tags-table (org-roam--tags-table)))
655-
(cl-loop for row in (append
656-
(org-roam-db-query [:select [file level pos title title id properties olp]
657-
:from nodes])
658-
(org-roam-db-query [:select [nodes:file level pos alias title node-id]
659-
:from aliases
660-
:left-join nodes
661-
:on (= aliases:node-id nodes:id)]))
662-
collect (pcase-let* ((`(,file ,level ,pos ,alias ,title ,id ,properties ,olp) row)
663-
(`(,file-hash ,file-atime ,file-mtime) (gethash file files-table))
664-
(node (org-roam-node-create :id id
665-
:level level
666-
:file file
667-
:file-hash file-hash
668-
:file-atime file-atime
669-
:file-mtime file-mtime
670-
:title alias
671-
:point pos
672-
:properties properties
673-
:olp olp
674-
:tags (gethash id tags-table)))
675-
(candidate-main (org-roam-node--format-entry node (1- (frame-width))))
676-
(tag-str (org-roam--tags-to-str (org-roam-node-tags node))))
677-
(cons (propertize (concat candidate-main
678-
(propertize tag-str 'invisible t))
679-
'node node)
680-
node)))))
662+
(let ((rows (org-roam-db-query
663+
"SELECT id, file, title, \"level\", todo, pos, priority, scheduled, deadline,
664+
properties, olp, atime, mtime,
665+
'(' || group_concat(tags, ' ') || ')' as tags, aliases, refs
666+
FROM
667+
(SELECT
668+
nodes.id as id,
669+
nodes.file as file,
670+
nodes.\"level\" as \"level\",
671+
nodes.todo as todo,
672+
nodes.pos as pos,
673+
nodes.priority as priority,
674+
nodes.scheduled as scheduled,
675+
nodes.deadline as deadline,
676+
nodes.title as title,
677+
nodes.properties as properties,
678+
nodes.olp as olp,
679+
files.atime as atime,
680+
files.mtime as mtime,
681+
tags.tag as tags,
682+
'(' || group_concat(aliases.alias, ' ') || ')' as aliases,
683+
'(' || group_concat(refs.ref, ' ') || ')' as refs
684+
FROM nodes
685+
LEFT JOIN files ON files.file = nodes.file
686+
LEFT JOIN tags ON tags.node_id = nodes.id
687+
LEFT JOIN aliases ON aliases.node_id = nodes.id
688+
LEFT JOIN refs ON refs.node_id = nodes.id
689+
GROUP BY nodes.id, tags.tag )
690+
GROUP BY id, aliases, refs;")))
691+
(cl-loop for row in rows
692+
append (pcase-let* ((`(,id ,file ,title ,level ,todo ,pos ,priority ,scheduled
693+
,deadline ,properties ,olp ,atime ,mtime ,tags ,aliases, refs) row)
694+
(all-titles (cons title aliases))
695+
(nodes (mapcar (lambda (t)
696+
(org-roam-node-create :id id
697+
:file file
698+
:file-atime atime
699+
:file-mtime mtime
700+
:level level
701+
:point pos
702+
:todo todo
703+
:priority priority
704+
:scheduled scheduled
705+
:deadline deadline
706+
:title t
707+
:properties properties
708+
:olp olp
709+
:tags tags
710+
:refs refs))
711+
;; NOTE: refs here do not have their type (e.g. //google.com, not http://google.com)
712+
all-titles)))
713+
(mapcar #'org-roam-node--to-candidate nodes)))))
681714

682715
(defcustom org-roam-node-annotation-function #'org-roam-node--annotation
683716
"The function used to return annotations in the minibuffer for Org-roam nodes.

0 commit comments

Comments
 (0)