(feature): add a cache for ROAM_KEY (#192)

This commit is contained in:
Jethro Kuan
2020-02-26 22:26:02 +08:00
committed by GitHub
parent 962ef23cce
commit 92d25b287e
4 changed files with 31 additions and 10 deletions

View File

@ -183,7 +183,8 @@ specified via the #+ROAM_ALIAS property."
"Build the org-roam caches in DIR."
(let ((backward-links (make-hash-table :test #'equal))
(forward-links (make-hash-table :test #'equal))
(file-titles (make-hash-table :test #'equal)))
(file-titles (make-hash-table :test #'equal))
(refs (make-hash-table :test #'equal)))
(let* ((org-roam-files (org-roam--find-files dir))
(file-items (mapcar (lambda (file)
(with-temp-buffer
@ -199,13 +200,18 @@ specified via the #+ROAM_ALIAS property."
(with-temp-buffer
(insert-file-contents file)
(when-let ((titles (org-roam--extract-titles)))
(puthash file titles file-titles)))
(puthash file titles file-titles))
(when-let ((ref (cdr (assoc "ROAM_KEY" (org-roam--extract-global-props '("ROAM_KEY"))))))
;; FIXME: this overrides previous refs, should probably have a
;; warning when ref is not unique
(puthash ref file refs)))
org-roam-files))
(list
:directory dir
:forward forward-links
:backward backward-links
:titles file-titles)))
:titles file-titles
:refs refs)))
(provide 'org-roam-utils)

View File

@ -170,7 +170,9 @@ If called interactively, then PARENTS is non-nil."
(backward-links :initarg :backward-links
:documentation "Cache containing backward links.")
(titles :initarg :titles
:documentation "Cache containing titles for org-roam files."))
:documentation "Cache containing titles for org-roam files.")
(refs :initarg :refs
:documentation "Cache with ref as key, and file path as value."))
"All cache for an org-roam directory.")
;;; Dynamic variables
@ -236,6 +238,10 @@ as a unique key."
"Cache containing titles for org-roam files."
(oref (org-roam--get-directory-cache) titles))
(defun org-roam--refs-cache ()
"Cache containing refs for org-roam files."
(oref (org-roam--get-directory-cache) refs))
(defun org-roam--ensure-cache-built ()
"Ensures that org-roam cache is built."
(unless (org-roam--cache-initialized-p)
@ -459,7 +465,8 @@ If PREFIX, downcase the title before insertion."
(oset obj initialized t)
(oset obj forward-links (plist-get cache :forward))
(oset obj backward-links (plist-get cache :backward))
(oset obj titles (plist-get cache :titles)))
(oset obj titles (plist-get cache :titles))
(oset obj refs (plist-get cache :refs)))
(unless org-roam-mute-cache-build
(message "Org-roam cache built!"))
(when on-success
@ -472,18 +479,19 @@ If PREFIX, downcase the title before insertion."
(oset cache initialized nil)
(oset cache forward-links (make-hash-table :test #'equal))
(oset cache backward-links (make-hash-table :test #'equal))
(oset cache titles (make-hash-table :test #'equal))))
(oset cache titles (make-hash-table :test #'equal))
(oset cache refs (make-hash-table :test #'equal))))
(defun org-roam--default-cache ()
"A default, uninitialized cache object."
(org-roam-cache :initialized nil
:forward-links (make-hash-table :test #'equal)
:backward-links (make-hash-table :test #'equal)
:titles (make-hash-table :test #'equal)))
:titles (make-hash-table :test #'equal)
:refs (make-hash-table :test #'equal)))
(defun org-roam--clear-file-from-cache (&optional filepath)
"Remove any related links to the file.
"Remove any related links to the file at FILEPATH.
This is equivalent to removing the node from the graph."
(let* ((path (or filepath
(buffer-file-name (current-buffer))))

View File

@ -0,0 +1 @@
#+ROAM_KEY: https://google.com/

View File

@ -72,7 +72,8 @@
(oset obj initialized t)
(oset obj forward-links (plist-get cache :forward))
(oset obj backward-links (plist-get cache :backward))
(oset obj titles (plist-get cache :titles)))))
(oset obj titles (plist-get cache :titles))
(oset obj refs (plist-get cache :refs)))))
;;; Tests
(describe "org-roam--build-cache-async"
@ -92,6 +93,7 @@
(expect (hash-table-count (org-roam--forward-links-cache)) :to-be 4)
(expect (hash-table-count (org-roam--backward-links-cache)) :to-be 5)
(expect (hash-table-count (org-roam--titles-cache)) :to-be 6)
(expect (hash-table-count (org-roam--refs-cache)) :to-be 1)
;; Forward cache
(let ((f1 (gethash (abs-path "f1.org")
@ -148,6 +150,10 @@
(expect (gethash (abs-path "no-title.org")
(org-roam--titles-cache)) :to-be nil)
;; Refs Cache
(expect (gethash "https://google.com/"
(org-roam--refs-cache)) :to-equal (abs-path "web_ref.org"))
;; Multi
(let ((org-roam-directory org-roam-directory2))
(org-roam--build-cache-async)