(feat)db: cache file title into files table (#1963)

adds a column `title` into the `files` table, and the `file-title` property to the node structure.
This commit is contained in:
Public Image Ltd
2021-11-12 16:47:34 +01:00
committed by GitHub
parent db573bbc4e
commit 1af1639ee0
2 changed files with 27 additions and 13 deletions

View File

@ -99,7 +99,7 @@ slow."
:group 'org-roam) :group 'org-roam)
;;; Variables ;;; Variables
(defconst org-roam-db-version 17) (defconst org-roam-db-version 18)
;; TODO Rename this ;; TODO Rename this
(defconst org-roam--sqlite-available-p (defconst org-roam--sqlite-available-p
@ -186,6 +186,7 @@ The query is expected to be able to fail, in this situation, run HANDLER."
(defconst org-roam-db--table-schemata (defconst org-roam-db--table-schemata
'((files '((files
[(file :unique :primary-key) [(file :unique :primary-key)
title
(hash :not-null) (hash :not-null)
(atime :not-null) (atime :not-null)
(mtime :not-null)]) (mtime :not-null)])
@ -294,10 +295,22 @@ If FILE is nil, clear the current buffer."
file)) file))
;;;; Updating tables ;;;; Updating tables
(defun org-roam-db--file-title ()
"In current Org buffer, get the title.
If there is no title, return the file name relative to
`org-roam-directory'."
(org-link-display-format
(or (cadr (assoc "TITLE" (org-collect-keywords '("title"))))
(file-name-sans-extension (file-relative-name
(buffer-file-name (buffer-base-buffer))
org-roam-directory)))))
(defun org-roam-db-insert-file () (defun org-roam-db-insert-file ()
"Update the files table for the current buffer. "Update the files table for the current buffer.
If UPDATE-P is non-nil, first remove the file in the database." If UPDATE-P is non-nil, first remove the file in the database."
(let* ((file (buffer-file-name)) (let* ((file (buffer-file-name))
(file-title (org-roam-db--file-title))
(attr (file-attributes file)) (attr (file-attributes file))
(atime (file-attribute-access-time attr)) (atime (file-attribute-access-time attr))
(mtime (file-attribute-modification-time attr)) (mtime (file-attribute-modification-time attr))
@ -305,7 +318,7 @@ If UPDATE-P is non-nil, first remove the file in the database."
(org-roam-db-query (org-roam-db-query
[:insert :into files [:insert :into files
:values $v1] :values $v1]
(list (vector file hash atime mtime))))) (list (vector file file-title hash atime mtime)))))
(defun org-roam-db-get-scheduled-time () (defun org-roam-db-get-scheduled-time ()
"Return the scheduled time at point in ISO8601 format." "Return the scheduled time at point in ISO8601 format."
@ -383,11 +396,7 @@ INFO is the org-element parsed buffer."
(org-roam-db-node-p)) (org-roam-db-node-p))
(when-let ((id (org-id-get))) (when-let ((id (org-id-get)))
(let* ((file (buffer-file-name (buffer-base-buffer))) (let* ((file (buffer-file-name (buffer-base-buffer)))
(title (org-link-display-format (title (org-roam-db--file-title))
(or (cadr (assoc "TITLE" (org-collect-keywords '("title"))
#'string-equal))
(file-name-sans-extension
(file-relative-name file org-roam-directory)))))
(pos (point)) (pos (point))
(todo nil) (todo nil)
(priority nil) (priority nil)

View File

@ -146,7 +146,7 @@ It takes a single argument REF, which is a propertized string."
(cl-defstruct (org-roam-node (:constructor org-roam-node-create) (cl-defstruct (org-roam-node (:constructor org-roam-node-create)
(:copier nil)) (:copier nil))
"A heading or top level file with an assigned ID property." "A heading or top level file with an assigned ID property."
file file-hash file-atime file-mtime file file-title file-hash file-atime file-mtime
id level point todo priority scheduled deadline title properties olp id level point todo priority scheduled deadline title properties olp
tags aliases refs) tags aliases refs)
@ -289,10 +289,10 @@ nodes."
:limit 1] :limit 1]
(org-roam-node-id node))))) (org-roam-node-id node)))))
(pcase-let* ((`(,file ,level ,pos ,todo ,priority ,scheduled ,deadline ,title ,properties ,olp) node-info) (pcase-let* ((`(,file ,level ,pos ,todo ,priority ,scheduled ,deadline ,title ,properties ,olp) node-info)
(`(,atime ,mtime) (car (org-roam-db-query [:select [atime mtime] (`(,atime ,mtime ,file-title) (car (org-roam-db-query [:select [atime mtime title]
:from files :from files
:where (= file $s1)] :where (= file $s1)]
file))) file)))
(tag-info (mapcar #'car (org-roam-db-query [:select [tag] :from tags (tag-info (mapcar #'car (org-roam-db-query [:select [tag] :from tags
:where (= node-id $s1)] :where (= node-id $s1)]
(org-roam-node-id node)))) (org-roam-node-id node))))
@ -303,6 +303,7 @@ nodes."
:where (= node-id $s1)] :where (= node-id $s1)]
(org-roam-node-id node))))) (org-roam-node-id node)))))
(setf (org-roam-node-file node) file (setf (org-roam-node-file node) file
(org-roam-node-file-title node) file-title
(org-roam-node-file-atime node) atime (org-roam-node-file-atime node) atime
(org-roam-node-file-mtime node) mtime (org-roam-node-file-mtime node) mtime
(org-roam-node-level node) level (org-roam-node-level node) level
@ -325,6 +326,7 @@ nodes."
"SELECT "SELECT
id, id,
file, file,
filetitle,
\"level\", \"level\",
todo, todo,
pos, pos,
@ -344,6 +346,7 @@ FROM
SELECT SELECT
id, id,
file, file,
filetitle,
\"level\", \"level\",
todo, todo,
pos, pos,
@ -374,6 +377,7 @@ FROM
nodes.olp as olp, nodes.olp as olp,
files.atime as atime, files.atime as atime,
files.mtime as mtime, files.mtime as mtime,
files.title as filetitle,
tags.tag as tags, tags.tag as tags,
aliases.alias as aliases, aliases.alias as aliases,
'(' || group_concat(RTRIM (refs.\"type\", '\"') || ':' || LTRIM(refs.ref, '\"'), ' ') || ')' as refs '(' || group_concat(RTRIM (refs.\"type\", '\"') || ':' || LTRIM(refs.ref, '\"'), ' ') || ')' as refs
@ -386,13 +390,14 @@ FROM
GROUP BY id, tags ) GROUP BY id, tags )
GROUP BY id"))) GROUP BY id")))
(cl-loop for row in rows (cl-loop for row in rows
append (pcase-let* ((`(,id ,file ,level ,todo ,pos ,priority ,scheduled ,deadline append (pcase-let* ((`(,id ,file ,file-title ,level ,todo ,pos ,priority ,scheduled ,deadline
,title ,properties ,olp ,atime ,mtime ,tags ,aliases ,refs) ,title ,properties ,olp ,atime ,mtime ,tags ,aliases ,refs)
row) row)
(all-titles (cons title aliases))) (all-titles (cons title aliases)))
(mapcar (lambda (temp-title) (mapcar (lambda (temp-title)
(org-roam-node-create :id id (org-roam-node-create :id id
:file file :file file
:file-title file-title
:file-atime atime :file-atime atime
:file-mtime mtime :file-mtime mtime
:level level :level level