(fix): reimplement mtime sorting (#642)

This commit is contained in:
Leo Vivier
2020-05-16 13:32:23 +02:00
committed by GitHub
parent 8091f4598e
commit 3bc174a6f0
2 changed files with 62 additions and 27 deletions

View File

@ -57,7 +57,7 @@ when used with multiple Org-roam instances."
:type 'string :type 'string
:group 'org-roam) :group 'org-roam)
(defconst org-roam-db--version 4) (defconst org-roam-db--version 5)
(defconst org-roam-db--sqlite-available-p (defconst org-roam-db--sqlite-available-p
(with-demoted-errors "Org-roam initialization: %S" (with-demoted-errors "Org-roam initialization: %S"
(emacsql-sqlite-ensure-binary) (emacsql-sqlite-ensure-binary)
@ -121,7 +121,7 @@ SQL can be either the emacsql vector representation, or a string."
'((files '((files
[(file :unique :primary-key) [(file :unique :primary-key)
(hash :not-null) (hash :not-null)
(last-modified :not-null)]) (meta :not-null)])
(links (links
[(from :not-null) [(from :not-null)
@ -206,6 +206,13 @@ This is equivalent to removing the node from the graph."
file)))) file))))
;;;;; Insertion ;;;;; Insertion
(defun org-roam-db--insert-meta (file hash meta)
"Insert HASH and META for a FILE into the Org-roam cache."
(org-roam-db-query
[:insert :into files
:values $v1]
(list (vector file hash meta))))
(defun org-roam-db--insert-links (links) (defun org-roam-db--insert-links (links)
"Insert LINKS into the Org-roam cache." "Insert LINKS into the Org-roam cache."
(org-roam-db-query (org-roam-db-query
@ -306,6 +313,18 @@ connections, nil is returned."
files)) files))
;;;;; Updating ;;;;; Updating
(defun org-roam-db--update-meta ()
"Update the metadata of the current buffer into the cache."
(let* ((file (file-truename (buffer-file-name)))
(attr (file-attributes file))
(atime (file-attribute-access-time attr))
(mtime (file-attribute-modification-time attr))
(hash (secure-hash 'sha1 (current-buffer))))
(org-roam-db-query [:delete :from files
:where (= file $s1)]
file)
(org-roam-db--insert-meta file hash (list :atime atime :mtime mtime))))
(defun org-roam-db--update-titles () (defun org-roam-db--update-titles ()
"Update the title of the current buffer into the cache." "Update the title of the current buffer into the cache."
(let* ((file (file-truename (buffer-file-name))) (let* ((file (file-truename (buffer-file-name)))
@ -350,6 +369,7 @@ connections, nil is returned."
(current-buffer)))) (current-buffer))))
(with-current-buffer buf (with-current-buffer buf
(save-excursion (save-excursion
(org-roam-db--update-meta)
(org-roam-db--update-tags) (org-roam-db--update-tags)
(org-roam-db--update-titles) (org-roam-db--update-titles)
(org-roam-db--update-refs) (org-roam-db--update-refs)
@ -365,28 +385,31 @@ If FORCE, force a rebuild of the cache from scratch."
(org-roam-db) ;; To initialize the database, no-op if already initialized (org-roam-db) ;; To initialize the database, no-op if already initialized
(let* ((org-roam-files (org-roam--list-all-files)) (let* ((org-roam-files (org-roam--list-all-files))
(current-files (org-roam-db--get-current-files)) (current-files (org-roam-db--get-current-files))
(time (current-time))
all-files all-links all-titles all-refs all-tags) all-files all-links all-titles all-refs all-tags)
(dolist (file org-roam-files) (dolist (file org-roam-files)
(org-roam--with-temp-buffer (let* ((attr (file-attributes file))
(insert-file-contents file) (atime (file-attribute-access-time attr))
(let ((contents-hash (secure-hash 'sha1 (current-buffer)))) (mtime (file-attribute-modification-time attr)))
(unless (string= (gethash file current-files) (org-roam--with-temp-buffer
contents-hash) (insert-file-contents file)
(org-roam-db--clear-file file) (let ((contents-hash (secure-hash 'sha1 (current-buffer))))
(push (vector file contents-hash time) (unless (string= (gethash file current-files)
all-files) contents-hash)
(when-let (links (org-roam--extract-links file)) (org-roam-db--clear-file file)
(push links all-links)) (push (vector file contents-hash (list :atime atime :mtime mtime))
(when-let (tags (org-roam--extract-tags file)) all-files)
(push (vector file tags) all-tags)) (when-let (links (org-roam--extract-links file))
(let ((titles (org-roam--extract-titles))) (push links all-links))
(push (vector file titles) all-titles)) (when-let (tags (org-roam--extract-tags file))
(when-let* ((ref (org-roam--extract-ref)) (push (vector file tags) all-tags))
(type (car ref)) (let ((titles (org-roam--extract-titles)))
(key (cdr ref))) (push (vector file titles)
(setq all-refs (cons (vector key file type) all-refs)))) all-titles))
(remhash file current-files)))) (when-let* ((ref (org-roam--extract-ref))
(type (car ref))
(key (cdr ref)))
(setq all-refs (cons (vector key file type) all-refs))))
(remhash file current-files)))))
(dolist (file (hash-table-keys current-files)) (dolist (file (hash-table-keys current-files))
;; These files are no longer around, remove from cache... ;; These files are no longer around, remove from cache...
(org-roam-db--clear-file file)) (org-roam-db--clear-file file))

View File

@ -582,12 +582,18 @@ Only relevant when `org-roam-tag-sources' is non-nil."
(defun org-roam--get-title-path-completions () (defun org-roam--get-title-path-completions ()
"Return an alist for completion. "Return an alist for completion.
The car is the displayed title for completion, and the cdr is a The car is the displayed title for completion, and the cdr is the
plist containing the path to the file, and the original title." to the file."
(let* ((rows (org-roam-db-query [:select [titles:file titles:titles tags:tags] :from titles (let* ((rows (org-roam-db-query [:select [titles:file titles:titles tags:tags files:meta] :from titles
:left :join tags :left :join tags
:on (= titles:file tags:file)])) :on (= titles:file tags:file)
:left :join files
:on (= titles:file files:file)]))
completions) completions)
(seq-sort-by (lambda (x)
(plist-get (nth 3 x) :mtime))
#'time-less-p
rows)
(dolist (row rows completions) (dolist (row rows completions)
(pcase-let ((`(,file-path ,titles ,tags) row)) (pcase-let ((`(,file-path ,titles ,tags) row))
(let ((titles (or titles (list (org-roam--path-to-slug file-path))))) (let ((titles (or titles (list (org-roam--path-to-slug file-path)))))
@ -697,10 +703,16 @@ candidates (e.g. \"cite\" ,\"website\" ,etc.)
takes three arguments: the type, the ref, and the file of the takes three arguments: the type, the ref, and the file of the
current candidate. It should return t if that candidate is to be current candidate. It should return t if that candidate is to be
included as a candidate." included as a candidate."
(let ((rows (org-roam-db-query [:select [type ref file] :from refs])) (let ((rows (org-roam-db-query [:select [refs:type refs:ref refs:file ] :from refs
:left :join files
:on (= refs:file files:file)]))
(include-type (and interactive (include-type (and interactive
org-roam-include-type-in-ref-path-completions)) org-roam-include-type-in-ref-path-completions))
completions) completions)
(seq-sort-by (lambda (x)
(plist-get (nth 3 x) :mtime))
#'time-less-p
rows)
(dolist (row rows completions) (dolist (row rows completions)
(pcase-let ((`(,type ,ref ,file-path) row)) (pcase-let ((`(,type ,ref ,file-path) row))
(when (pcase filter (when (pcase filter