(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." "Build the org-roam caches in DIR."
(let ((backward-links (make-hash-table :test #'equal)) (let ((backward-links (make-hash-table :test #'equal))
(forward-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)) (let* ((org-roam-files (org-roam--find-files dir))
(file-items (mapcar (lambda (file) (file-items (mapcar (lambda (file)
(with-temp-buffer (with-temp-buffer
@ -199,13 +200,18 @@ specified via the #+ROAM_ALIAS property."
(with-temp-buffer (with-temp-buffer
(insert-file-contents file) (insert-file-contents file)
(when-let ((titles (org-roam--extract-titles))) (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)) org-roam-files))
(list (list
:directory dir :directory dir
:forward forward-links :forward forward-links
:backward backward-links :backward backward-links
:titles file-titles))) :titles file-titles
:refs refs)))
(provide 'org-roam-utils) (provide 'org-roam-utils)

View File

@ -170,7 +170,9 @@ If called interactively, then PARENTS is non-nil."
(backward-links :initarg :backward-links (backward-links :initarg :backward-links
:documentation "Cache containing backward links.") :documentation "Cache containing backward links.")
(titles :initarg :titles (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.") "All cache for an org-roam directory.")
;;; Dynamic variables ;;; Dynamic variables
@ -236,6 +238,10 @@ as a unique key."
"Cache containing titles for org-roam files." "Cache containing titles for org-roam files."
(oref (org-roam--get-directory-cache) titles)) (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 () (defun org-roam--ensure-cache-built ()
"Ensures that org-roam cache is built." "Ensures that org-roam cache is built."
(unless (org-roam--cache-initialized-p) (unless (org-roam--cache-initialized-p)
@ -459,7 +465,8 @@ If PREFIX, downcase the title before insertion."
(oset obj initialized t) (oset obj initialized t)
(oset obj forward-links (plist-get cache :forward)) (oset obj forward-links (plist-get cache :forward))
(oset obj backward-links (plist-get cache :backward)) (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 (unless org-roam-mute-cache-build
(message "Org-roam cache built!")) (message "Org-roam cache built!"))
(when on-success (when on-success
@ -472,18 +479,19 @@ If PREFIX, downcase the title before insertion."
(oset cache initialized nil) (oset cache initialized nil)
(oset cache forward-links (make-hash-table :test #'equal)) (oset cache forward-links (make-hash-table :test #'equal))
(oset cache backward-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 () (defun org-roam--default-cache ()
"A default, uninitialized cache object." "A default, uninitialized cache object."
(org-roam-cache :initialized nil (org-roam-cache :initialized nil
:forward-links (make-hash-table :test #'equal) :forward-links (make-hash-table :test #'equal)
:backward-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) (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." This is equivalent to removing the node from the graph."
(let* ((path (or filepath (let* ((path (or filepath
(buffer-file-name (current-buffer)))) (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 initialized t)
(oset obj forward-links (plist-get cache :forward)) (oset obj forward-links (plist-get cache :forward))
(oset obj backward-links (plist-get cache :backward)) (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 ;;; Tests
(describe "org-roam--build-cache-async" (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--forward-links-cache)) :to-be 4)
(expect (hash-table-count (org-roam--backward-links-cache)) :to-be 5) (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--titles-cache)) :to-be 6)
(expect (hash-table-count (org-roam--refs-cache)) :to-be 1)
;; Forward cache ;; Forward cache
(let ((f1 (gethash (abs-path "f1.org") (let ((f1 (gethash (abs-path "f1.org")
@ -148,6 +150,10 @@
(expect (gethash (abs-path "no-title.org") (expect (gethash (abs-path "no-title.org")
(org-roam--titles-cache)) :to-be nil) (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 ;; Multi
(let ((org-roam-directory org-roam-directory2)) (let ((org-roam-directory org-roam-directory2))
(org-roam--build-cache-async) (org-roam--build-cache-async)