(internal): normalize titles in database (#908)

Instead of storing titles as a list within in the Org-roam cache, e.g.
file, ("title1" "title2"). We normalize it and store it as:

file, "title1"
file, "title2"
This commit is contained in:
Jethro Kuan
2020-07-10 14:36:54 +08:00
committed by GitHub
parent 6770c3eaf5
commit efba3c2bf0
3 changed files with 34 additions and 36 deletions

View File

@ -3,6 +3,7 @@
## 1.2.1 (TBD) ## 1.2.1 (TBD)
### Breaking Changes ### Breaking Changes
- [#908](https://github.com/org-roam/org-roam/pull/908) Normalized titles in database. May break external packages that rely on unnormalized titles.
### Features ### Features
- [#814](https://github.com/org-roam/org-roam/pull/814) Implement `org-roam-insert-immediate` - [#814](https://github.com/org-roam/org-roam/pull/814) Implement `org-roam-insert-immediate`

View File

@ -48,6 +48,7 @@
(declare-function org-roam--extract-headlines "org-roam") (declare-function org-roam--extract-headlines "org-roam")
(declare-function org-roam--extract-links "org-roam") (declare-function org-roam--extract-links "org-roam")
(declare-function org-roam--list-all-files "org-roam") (declare-function org-roam--list-all-files "org-roam")
(declare-function org-roam--path-to-slug "org-roam")
(declare-function org-roam-buffer--update-maybe "org-roam-buffer") (declare-function org-roam-buffer--update-maybe "org-roam-buffer")
;;;; Options ;;;; Options
@ -75,7 +76,7 @@ value like `most-positive-fixnum'."
:type 'int :type 'int
:group 'org-roam) :group 'org-roam)
(defconst org-roam-db--version 6) (defconst org-roam-db--version 7)
(defvar org-roam-db--connection (make-hash-table :test #'equal) (defvar org-roam-db--connection (make-hash-table :test #'equal)
"Database connection to Org-roam database.") "Database connection to Org-roam database.")
@ -152,7 +153,7 @@ SQL can be either the emacsql vector representation, or a string."
(titles (titles
[(file :not-null) [(file :not-null)
titles]) title])
(refs (refs
[(ref :unique :not-null) [(ref :unique :not-null)
@ -242,7 +243,8 @@ This is equivalent to removing the node from the graph."
(org-roam-db-query (org-roam-db-query
[:insert :into titles [:insert :into titles
:values $v1] :values $v1]
(list (vector file titles)))) (mapcar (lambda (title)
(vector file title)) titles)))
(defun org-roam-db--insert-headlines (headlines) (defun org-roam-db--insert-headlines (headlines)
"Insert HEADLINES into the Org-roam cache. "Insert HEADLINES into the Org-roam cache.
@ -306,10 +308,10 @@ Insertions can fail if the key is already in the database."
(defun org-roam-db--get-titles (file) (defun org-roam-db--get-titles (file)
"Return the titles of FILE from the cache." "Return the titles of FILE from the cache."
(caar (org-roam-db-query [:select [titles] :from titles (caar (org-roam-db-query [:select [title] :from titles
:where (= file $s1)] :where (= file $s1)
file :limit 1]
:limit 1))) file)))
(defun org-roam-db--connected-component (file) (defun org-roam-db--connected-component (file)
"Return all files reachable from/connected to FILE, including the file itself. "Return all files reachable from/connected to FILE, including the file itself.
@ -381,11 +383,12 @@ connections, nil is returned."
(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)))
(title (org-roam--extract-titles))) (titles (or (org-roam--extract-titles)
(list (org-roam--path-to-slug file)))))
(org-roam-db-query [:delete :from titles (org-roam-db-query [:delete :from titles
:where (= file $s1)] :where (= file $s1)]
file) file)
(org-roam-db--insert-titles file title))) (org-roam-db--insert-titles file titles)))
(defun org-roam-db--update-tags () (defun org-roam-db--update-tags ()
"Update the tags of the current buffer into the cache." "Update the tags of the current buffer into the cache."
@ -496,12 +499,10 @@ If FORCE, force a rebuild of the cache from scratch."
:values $v1] :values $v1]
(vector file tags)) (vector file tags))
(setq tag-count (1+ tag-count))) (setq tag-count (1+ tag-count)))
(let ((titles (org-roam--extract-titles))) (let ((titles (or (org-roam--extract-titles)
(org-roam-db-query (list (org-roam--path-to-slug file)))))
[:insert :into titles (org-roam-db--insert-titles file titles)
:values $v1] (setq title-count (+ title-count (length titles))))
(vector file titles))
(setq title-count (1+ title-count)))
(when-let* ((ref (org-roam--extract-ref))) (when-let* ((ref (org-roam--extract-ref)))
(when (org-roam-db--insert-ref file ref) (when (org-roam-db--insert-ref file ref)
(setq ref-count (1+ ref-count))))) (setq ref-count (1+ ref-count)))))

View File

@ -818,7 +818,7 @@ Examples:
"Return an alist for completion. "Return an alist for completion.
The car is the displayed title for completion, and the cdr is the The car is the displayed title for completion, and the cdr is the
to the file." to the file."
(let* ((rows (org-roam-db-query [:select [titles:file titles:titles tags:tags files:meta] :from titles (let* ((rows (org-roam-db-query [:select [files:file titles:title 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 :left :join files
@ -829,15 +829,13 @@ to the file."
#'time-less-p #'time-less-p
rows) rows)
(dolist (row rows completions) (dolist (row rows completions)
(pcase-let ((`(,file-path ,titles ,tags) row)) (pcase-let ((`(,file-path ,title ,tags) row))
(let ((titles (or titles (list (org-roam--path-to-slug file-path))))) (let ((k (concat
(dolist (title titles)
(let ((k (concat
(when tags (when tags
(format "(%s) " (s-join org-roam-tag-separator tags))) (format "(%s) " (s-join org-roam-tag-separator tags)))
title)) title))
(v (list :path file-path :title title))) (v (list :path file-path :title title)))
(push (cons k v) completions)))))))) (push (cons k v) completions))))))
(defun org-roam--get-index-path () (defun org-roam--get-index-path ()
"Return the path to the index in `org-roam-directory'. "Return the path to the index in `org-roam-directory'.
@ -878,7 +876,7 @@ FILTER can either be a string or a function:
current candidate. It should return t if that candidate is to current candidate. It should return t if that candidate is to
be included as a candidate." be included as a candidate."
(let ((rows (org-roam-db-query (let ((rows (org-roam-db-query
[:select [refs:type refs:ref refs:file titles:titles tags:tags] [:select [refs:type refs:ref refs:file titles:title tags:tags]
:from titles :from titles
:left :join tags :left :join tags
:on (= titles:file tags:file) :on (= titles:file tags:file)
@ -890,26 +888,24 @@ FILTER can either be a string or a function:
#'time-less-p #'time-less-p
rows) rows)
(dolist (row rows completions) (dolist (row rows completions)
(pcase-let ((`(,type ,ref ,file-path ,titles ,tags) row)) (pcase-let ((`(,type ,ref ,file-path ,title ,tags) row))
(let ((titles (or titles (list (org-roam--path-to-slug file-path))))) (when (pcase filter
(when (pcase filter
('nil t) ('nil t)
((pred stringp) (string= type filter)) ((pred stringp) (string= type filter))
((pred functionp) (funcall filter type ref file-path)) ((pred functionp) (funcall filter type ref file-path))
(wrong-type (signal 'wrong-type-argument (wrong-type (signal 'wrong-type-argument
`((stringp functionp) `((stringp functionp)
,wrong-type)))) ,wrong-type))))
(dolist (title titles) (let ((k (if (eq arg 1)
(let ((k (if (eq arg 1) (concat
(concat (when org-roam-include-type-in-ref-path-completions
(when org-roam-include-type-in-ref-path-completions (format "{%s} " type))
(format "{%s} " type)) (when tags
(when tags (format "(%s) " (s-join org-roam-tag-separator tags)))
(format "(%s) " (s-join org-roam-tag-separator tags))) (format "%s (%s)" title ref))
(format "%s (%s)" title ref)) ref))
ref)) (v (list :path file-path :type type :ref ref)))
(v (list :path file-path :type type :ref ref))) (push (cons k v) completions)))))))
(push (cons k v) completions)))))))))
(defun org-roam--find-file (file) (defun org-roam--find-file (file)
"Open FILE using `org-roam-find-file-function' or `find-file'." "Open FILE using `org-roam-find-file-function' or `find-file'."