mirror of
https://github.com/org-roam/org-roam
synced 2025-08-01 12:17:21 -05:00
(internal): simplify internal db cache update api (#1186)
This commit is contained in:
288
org-roam-db.el
288
org-roam-db.el
@ -222,79 +222,133 @@ This is equivalent to removing the node from the graph."
|
|||||||
:where (= ,(if (eq table 'links) 'from 'file) $s1)]
|
:where (= ,(if (eq table 'links) 'from 'file) $s1)]
|
||||||
file))))
|
file))))
|
||||||
|
|
||||||
;;;;; Insertion
|
;;;;; Inserting
|
||||||
(defun org-roam-db--insert-meta (file hash meta)
|
(defun org-roam-db--insert-meta (&optional update-p)
|
||||||
"Insert HASH and META for a FILE into the Org-roam cache."
|
"Update the metadata of the current buffer into the cache.
|
||||||
(org-roam-db-query
|
If UPDATE-P is non-nil, first remove the meta for the file in the database."
|
||||||
[:insert :into files
|
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
||||||
:values $v1]
|
(attr (file-attributes file))
|
||||||
(list (vector file hash meta))))
|
(atime (file-attribute-access-time attr))
|
||||||
|
(mtime (file-attribute-modification-time attr))
|
||||||
|
(hash (org-roam-db--file-hash)))
|
||||||
|
(when update-p
|
||||||
|
(org-roam-db-query [:delete :from files
|
||||||
|
:where (= file $s1)]
|
||||||
|
file))
|
||||||
|
(org-roam-db-query
|
||||||
|
[:insert :into files
|
||||||
|
:values $v1]
|
||||||
|
(list (vector file hash (list :atime atime :mtime mtime))))))
|
||||||
|
|
||||||
(defun org-roam-db--insert-links (links)
|
(defun org-roam-db--insert-titles (&optional update-p)
|
||||||
"Insert LINKS into the Org-roam cache."
|
"Update the titles of the current buffer into the cache.
|
||||||
(org-roam-db-query
|
If UPDATE-P is non-nil, first remove titles for the file in the database.
|
||||||
[:insert :into links
|
Returns the number of rows inserted."
|
||||||
:values $v1]
|
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
||||||
links))
|
(titles (or (org-roam--extract-titles)
|
||||||
|
(list (org-roam--path-to-slug file))))
|
||||||
|
(rows (mapcar (lambda (title)
|
||||||
|
(vector file title)) titles)))
|
||||||
|
(when update-p
|
||||||
|
(org-roam-db-query [:delete :from titles
|
||||||
|
:where (= file $s1)]
|
||||||
|
file))
|
||||||
|
(org-roam-db-query
|
||||||
|
[:insert :into titles
|
||||||
|
:values $v1]
|
||||||
|
rows)
|
||||||
|
(length rows)))
|
||||||
|
|
||||||
(defun org-roam-db--insert-titles (file titles)
|
(defun org-roam-db--insert-ref (&optional update-p)
|
||||||
"Insert TITLES for a FILE into the Org-roam cache."
|
"Update the ref of the current buffer into the cache.
|
||||||
(org-roam-db-query
|
If UPDATE-P is non-nil, first remove the ref for the file in the database."
|
||||||
[:insert :into titles
|
(let ((file (or org-roam-file-name (buffer-file-name))))
|
||||||
:values $v1]
|
(when update-p
|
||||||
(mapcar (lambda (title)
|
(org-roam-db-query [:delete :from refs
|
||||||
(vector file title)) titles)))
|
:where (= file $s1)]
|
||||||
|
file))
|
||||||
|
(if-let ((ref (org-roam--extract-ref)))
|
||||||
|
(let ((key (cdr ref))
|
||||||
|
(type (car ref)))
|
||||||
|
(condition-case nil
|
||||||
|
(progn
|
||||||
|
(org-roam-db-query
|
||||||
|
[:insert :into refs :values $v1]
|
||||||
|
(list (vector key file type)))
|
||||||
|
1)
|
||||||
|
(error
|
||||||
|
(lwarn '(org-roam) :error
|
||||||
|
(format "Duplicate ref %s in:\n\nA: %s\nB: %s\n\nskipping..."
|
||||||
|
key
|
||||||
|
file
|
||||||
|
(caar (org-roam-db-query
|
||||||
|
[:select file :from refs
|
||||||
|
:where (= ref $v1)]
|
||||||
|
(vector key)))))
|
||||||
|
0)))
|
||||||
|
0)))
|
||||||
|
|
||||||
(defun org-roam-db--insert-ids (ids)
|
(defun org-roam-db--insert-links (&optional update-p)
|
||||||
"Insert IDS into the Org-roam cache.
|
"Update the file links of the current buffer in the cache.
|
||||||
Returns t if the insertion was successful, nil otherwise.
|
If UPDATE-P is non-nil, first remove the links for the file in the database.
|
||||||
Insertions can fail when there is an ID conflict."
|
Return the number of rows inserted."
|
||||||
(condition-case nil
|
(let ((file (or org-roam-file-name (buffer-file-name))))
|
||||||
(progn
|
(when update-p
|
||||||
(org-roam-db-query
|
(org-roam-db-query [:delete :from links
|
||||||
[:insert :into ids
|
:where (= from $s1)]
|
||||||
:values $v1]
|
file))
|
||||||
ids)
|
(if-let ((links (org-roam--extract-links)))
|
||||||
t)
|
|
||||||
(error
|
|
||||||
(unless (listp ids)
|
|
||||||
(setq ids (list ids)))
|
|
||||||
(lwarn '(org-roam) :error
|
|
||||||
(format "Duplicate IDs in %s, one of:\n\n%s\n\nskipping..."
|
|
||||||
(aref (car ids) 1)
|
|
||||||
(string-join (mapcar (lambda (hl)
|
|
||||||
(aref hl 0)) ids) "\n")))
|
|
||||||
nil)))
|
|
||||||
|
|
||||||
(defun org-roam-db--insert-tags (file tags)
|
|
||||||
"Insert TAGS for a FILE into the Org-roam cache."
|
|
||||||
(org-roam-db-query
|
|
||||||
[:insert :into tags
|
|
||||||
:values $v1]
|
|
||||||
(list (vector file tags))))
|
|
||||||
|
|
||||||
(defun org-roam-db--insert-ref (file ref)
|
|
||||||
"Insert REF for FILE into the Org-roam cache.
|
|
||||||
Returns t if successful, and nil otherwise.
|
|
||||||
Insertions can fail if the key is already in the database."
|
|
||||||
(let ((key (cdr ref))
|
|
||||||
(type (car ref)))
|
|
||||||
(condition-case nil
|
|
||||||
(progn
|
(progn
|
||||||
(org-roam-db-query
|
(org-roam-db-query
|
||||||
[:insert :into refs :values $v1]
|
[:insert :into links
|
||||||
(list (vector key file type)))
|
:values $v1]
|
||||||
t)
|
links)
|
||||||
(error
|
(length links))
|
||||||
(lwarn '(org-roam) :error
|
0)))
|
||||||
(format "Duplicate ref %s in:\n\nA: %s\nB: %s\n\nskipping..."
|
|
||||||
key
|
(defun org-roam-db--insert-ids (&optional update-p)
|
||||||
file
|
"Update the ids of the current buffer into the cache.
|
||||||
(caar (org-roam-db-query
|
If UPDATE-P is non-nil, first remove ids for the file in the database.
|
||||||
[:select file :from refs
|
Returns the number of rows inserted."
|
||||||
:where (= ref $v1)]
|
(let ((file (or org-roam-file-name (buffer-file-name))))
|
||||||
(vector key)))))
|
(when update-p
|
||||||
nil))))
|
(org-roam-db-query [:delete :from ids
|
||||||
|
:where (= file $s1)]
|
||||||
|
file))
|
||||||
|
(if-let ((ids (org-roam--extract-ids file)))
|
||||||
|
(condition-case nil
|
||||||
|
(progn
|
||||||
|
(org-roam-db-query
|
||||||
|
[:insert :into ids
|
||||||
|
:values $v1]
|
||||||
|
ids)
|
||||||
|
(length ids))
|
||||||
|
(error
|
||||||
|
(lwarn '(org-roam) :error
|
||||||
|
(format "Duplicate IDs in %s, one of:\n\n%s\n\nskipping..."
|
||||||
|
(aref (car ids) 1)
|
||||||
|
(string-join (mapcar (lambda (hl)
|
||||||
|
(aref hl 0)) ids) "\n")))
|
||||||
|
0))
|
||||||
|
0)))
|
||||||
|
|
||||||
|
(defun org-roam-db--insert-tags (&optional update-p)
|
||||||
|
"Insert tags for the current buffer into the Org-roam cache.
|
||||||
|
If UPDATE-P is non-nil, first remove tags for the file in the database.
|
||||||
|
Return the number of rows inserted."
|
||||||
|
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
||||||
|
(tags (org-roam--extract-tags file)))
|
||||||
|
(when update-p
|
||||||
|
(org-roam-db-query [:delete :from tags
|
||||||
|
:where (= file $s1)]
|
||||||
|
file))
|
||||||
|
(if tags
|
||||||
|
(progn (org-roam-db-query
|
||||||
|
[:insert :into tags
|
||||||
|
:values $v1]
|
||||||
|
(list (vector file tags)))
|
||||||
|
1)
|
||||||
|
0)))
|
||||||
|
|
||||||
;;;;; Fetching
|
;;;;; Fetching
|
||||||
(defun org-roam-db--get-current-files ()
|
(defun org-roam-db--get-current-files ()
|
||||||
@ -387,65 +441,6 @@ connections, nil is returned."
|
|||||||
(secure-hash 'sha1 (current-buffer)))))
|
(secure-hash 'sha1 (current-buffer)))))
|
||||||
|
|
||||||
;;;;; Updating
|
;;;;; Updating
|
||||||
(defun org-roam-db--update-meta ()
|
|
||||||
"Update the metadata of the current buffer into the cache."
|
|
||||||
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
|
||||||
(attr (file-attributes file))
|
|
||||||
(atime (file-attribute-access-time attr))
|
|
||||||
(mtime (file-attribute-modification-time attr))
|
|
||||||
(hash (org-roam-db--file-hash)))
|
|
||||||
(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 ()
|
|
||||||
"Update the title of the current buffer into the cache."
|
|
||||||
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
|
||||||
(titles (or (org-roam--extract-titles)
|
|
||||||
(list (org-roam--path-to-slug file)))))
|
|
||||||
(org-roam-db-query [:delete :from titles
|
|
||||||
:where (= file $s1)]
|
|
||||||
file)
|
|
||||||
(org-roam-db--insert-titles file titles)))
|
|
||||||
|
|
||||||
(defun org-roam-db--update-tags ()
|
|
||||||
"Update the tags of the current buffer into the cache."
|
|
||||||
(let* ((file (or org-roam-file-name (buffer-file-name)))
|
|
||||||
(tags (org-roam--extract-tags file)))
|
|
||||||
(org-roam-db-query [:delete :from tags
|
|
||||||
:where (= file $s1)]
|
|
||||||
file)
|
|
||||||
(when tags
|
|
||||||
(org-roam-db--insert-tags file tags))))
|
|
||||||
|
|
||||||
(defun org-roam-db--update-refs ()
|
|
||||||
"Update the ref of the current buffer into the cache."
|
|
||||||
(let ((file (or org-roam-file-name (buffer-file-name))))
|
|
||||||
(org-roam-db-query [:delete :from refs
|
|
||||||
:where (= file $s1)]
|
|
||||||
file)
|
|
||||||
(when-let ((ref (org-roam--extract-ref)))
|
|
||||||
(org-roam-db--insert-ref file ref))))
|
|
||||||
|
|
||||||
(defun org-roam-db--update-links ()
|
|
||||||
"Update the file links of the current buffer in the cache."
|
|
||||||
(let ((file (or org-roam-file-name (buffer-file-name))))
|
|
||||||
(org-roam-db-query [:delete :from links
|
|
||||||
:where (= from $s1)]
|
|
||||||
file)
|
|
||||||
(when-let ((links (org-roam--extract-links)))
|
|
||||||
(org-roam-db--insert-links links))))
|
|
||||||
|
|
||||||
(defun org-roam-db--update-ids ()
|
|
||||||
"Update the ids of the current buffer into the cache."
|
|
||||||
(let* ((file (or org-roam-file-name (buffer-file-name))))
|
|
||||||
(org-roam-db-query [:delete :from ids
|
|
||||||
:where (= file $s1)]
|
|
||||||
file)
|
|
||||||
(when-let ((ids (org-roam--extract-ids file)))
|
|
||||||
(org-roam-db--insert-ids ids))))
|
|
||||||
|
|
||||||
(defun org-roam-db--update-file (&optional file-path)
|
(defun org-roam-db--update-file (&optional file-path)
|
||||||
"Update Org-roam cache for FILE-PATH.
|
"Update Org-roam cache for FILE-PATH.
|
||||||
If the file does not exist anymore, remove it from the cache.
|
If the file does not exist anymore, remove it from the cache.
|
||||||
@ -460,13 +455,13 @@ If the file exists, update the cache with information."
|
|||||||
(save-buffer)))
|
(save-buffer)))
|
||||||
(org-roam--with-temp-buffer file-path
|
(org-roam--with-temp-buffer file-path
|
||||||
(emacsql-with-transaction (org-roam-db)
|
(emacsql-with-transaction (org-roam-db)
|
||||||
(org-roam-db--update-meta)
|
(org-roam-db--insert-meta 'update)
|
||||||
(org-roam-db--update-tags)
|
(org-roam-db--insert-tags 'update)
|
||||||
(org-roam-db--update-titles)
|
(org-roam-db--insert-titles 'update)
|
||||||
(org-roam-db--update-refs)
|
(org-roam-db--insert-ref 'update)
|
||||||
(when org-roam-enable-headline-linking
|
(when org-roam-enable-headline-linking
|
||||||
(org-roam-db--update-ids))
|
(org-roam-db--insert-ids 'update))
|
||||||
(org-roam-db--update-links)))))
|
(org-roam-db--insert-links 'update)))))
|
||||||
|
|
||||||
(defun org-roam-db-build-cache (&optional force)
|
(defun org-roam-db-build-cache (&optional force)
|
||||||
"Build the cache for `org-roam-directory'.
|
"Build the cache for `org-roam-directory'.
|
||||||
@ -507,28 +502,11 @@ If FORCE, force a rebuild of the cache from scratch."
|
|||||||
(vector file contents-hash (list :atime atime :mtime mtime)))
|
(vector file contents-hash (list :atime atime :mtime mtime)))
|
||||||
(setq file-count (1+ file-count))
|
(setq file-count (1+ file-count))
|
||||||
(when org-roam-enable-headline-linking
|
(when org-roam-enable-headline-linking
|
||||||
(when-let ((ids (org-roam--extract-ids file)))
|
(setq id-count (+ id-count (org-roam-db--insert-ids))))
|
||||||
(when (org-roam-db--insert-ids ids)
|
(setq link-count (+ link-count (org-roam-db--insert-links)))
|
||||||
(setq id-count (+ id-count (length ids))))))
|
(setq tag-count (+ tag-count (org-roam-db--insert-tags)))
|
||||||
(when-let (links (org-roam--extract-links file))
|
(setq title-count (+ title-count (org-roam-db--insert-titles)))
|
||||||
(org-roam-db-query
|
(setq ref-count (+ ref-count (org-roam-db--insert-ref))))
|
||||||
[:insert :into links
|
|
||||||
:values $v1]
|
|
||||||
links)
|
|
||||||
(setq link-count (1+ link-count)))
|
|
||||||
(when-let (tags (org-roam--extract-tags file))
|
|
||||||
(org-roam-db-query
|
|
||||||
[:insert :into tags
|
|
||||||
:values $v1]
|
|
||||||
(vector file tags))
|
|
||||||
(setq tag-count (1+ tag-count)))
|
|
||||||
(let ((titles (or (org-roam--extract-titles)
|
|
||||||
(list (org-roam--path-to-slug file)))))
|
|
||||||
(org-roam-db--insert-titles file titles)
|
|
||||||
(setq title-count (+ title-count (length titles))))
|
|
||||||
(when-let* ((ref (org-roam--extract-ref)))
|
|
||||||
(when (org-roam-db--insert-ref file ref)
|
|
||||||
(setq ref-count (1+ ref-count)))))
|
|
||||||
(file-error
|
(file-error
|
||||||
(setq org-roam-files (remove file org-roam-files))
|
(setq org-roam-files (remove file org-roam-files))
|
||||||
(org-roam-db--clear-file file)
|
(org-roam-db--clear-file file)
|
||||||
|
@ -1746,7 +1746,7 @@ Return added tag."
|
|||||||
(org-roam--set-global-prop
|
(org-roam--set-global-prop
|
||||||
"ROAM_TAGS"
|
"ROAM_TAGS"
|
||||||
(combine-and-quote-strings (seq-uniq (cons tag existing-tags))))
|
(combine-and-quote-strings (seq-uniq (cons tag existing-tags))))
|
||||||
(org-roam-db--update-tags)
|
(org-roam-db--insert-tags 'update)
|
||||||
tag))
|
tag))
|
||||||
|
|
||||||
(defun org-roam-tag-delete ()
|
(defun org-roam-tag-delete ()
|
||||||
@ -1759,7 +1759,7 @@ Return added tag."
|
|||||||
(org-roam--set-global-prop
|
(org-roam--set-global-prop
|
||||||
"ROAM_TAGS"
|
"ROAM_TAGS"
|
||||||
(combine-and-quote-strings (delete tag tags)))
|
(combine-and-quote-strings (delete tag tags)))
|
||||||
(org-roam-db--update-tags))
|
(org-roam-db--insert-tags 'update))
|
||||||
(user-error "No tag to delete")))
|
(user-error "No tag to delete")))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
Reference in New Issue
Block a user