remove arcology-directory from file keys in tables

this should make it a lot more straightforward to query, and the db
will be portable between my desktop and laptop with different org-roam-directorys
main
Ryan Rix 2020-08-27 14:07:57 -07:00
parent 8b732181c6
commit f9f3c43fcc
2 changed files with 37 additions and 26 deletions

View File

@ -39,7 +39,7 @@
(require 'arcology-db)
(require 'arcology)
(setq arcology-directory "~/org/")
(setq arcology-directory (file-truename "~/org/"))
(setq arcology-cached-keywords
'("ARCOLOGY_RSS"
"ARCOLOGY_KEY"

View File

@ -221,8 +221,9 @@ the current `arcology-directory'."
(defun arcology-db--clear-file (&optional filepath)
"Remove any related links to the file at FILEPATH.
This is equivalent to removing the node from the graph."
(let ((file (file-truename (or filepath
(buffer-file-name (buffer-base-buffer))))))
(let ((file (file-relative-name (file-truename (or filepath
(buffer-file-name (buffer-base-buffer))))
arcology-directory)))
(dolist (table (mapcar #'car arcology-db--table-schemata))
(arcology-db-query `[:delete :from ,table
:where (= ,(if (eq table 'links) 'from 'file) $s1)]
@ -231,6 +232,7 @@ This is equivalent to removing the node from the graph."
;;;;; Insertion
(defun arcology-db--insert-meta (file hash meta)
"Insert HASH and META for a FILE into the arcology cache."
(arcology-message "Meta: %s %s %s" file hash meta)
(arcology-db-query
[:insert :into files
:values $v1]
@ -386,7 +388,7 @@ connections, nil is returned."
(defun arcology-db--file-hash (&optional file-path)
"Compute the hash of FILE-PATH, a file or current buffer."
(let* ((file-p (and file-path))
(let* ((file-p (and file-path))
(file-path (or file-path
(buffer-file-name (current-buffer))))
(encrypted-p (and file-path
@ -407,19 +409,21 @@ connections, nil is returned."
;;;;; Updating
(defun arcology-db--update-meta ()
"Update the metadata of the current buffer into the cache."
(let* ((file (file-truename (buffer-file-name)))
(let* ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory))
(attr (file-attributes file))
(atime (file-attribute-access-time attr))
(mtime (file-attribute-modification-time attr))
(hash (org-roam-db--file-hash))))
(hash (org-roam-db--file-hash)))
(arcology-db-query [:delete :from files
:where (= file $s1)]
:where (= file $s1)]
file)
(arcology-db--insert-meta file hash (list :atime atime :mtime mtime)))
(arcology-db--insert-meta file hash (list :atime atime :mtime mtime))))
(defun arcology-db--update-titles ()
"Update the title of the current buffer into the cache."
(let* ((file (file-truename (buffer-file-name)))
(let* ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory))
(titles (or (arcology--extract-titles)
(list (arcology--path-to-slug file)))))
(arcology-db-query [:delete :from titles
@ -429,7 +433,8 @@ connections, nil is returned."
(defun arcology-db--update-tags ()
"Update the tags of the current buffer into the cache."
(let ((file (file-truename (buffer-file-name)))
(let ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory))
(tags (arcology--extract-tags)))
(arcology-db-query [:delete :from tags
:where (= file $s1)]
@ -439,7 +444,8 @@ connections, nil is returned."
(defun arcology-db--update-refs ()
"Update the ref of the current buffer into the cache."
(let ((file (file-truename (buffer-file-name))))
(let ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory)))
(arcology-db-query [:delete :from refs
:where (= file $s1)]
file)
@ -448,7 +454,8 @@ connections, nil is returned."
(defun arcology-db--update-links ()
"Update the file links of the current buffer in the cache."
(let ((file (file-truename (buffer-file-name))))
(let ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory)))
(arcology-db-query [:delete :from links
:where (= from $s1)]
file)
@ -457,7 +464,8 @@ connections, nil is returned."
(defun arcology-db--update-headlines ()
"Update the file headlines of the current buffer into the cache."
(let* ((file (file-truename (buffer-file-name))))
(let* ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory)))
(arcology-db-query [:delete :from headlines
:where (= file $s1)]
file)
@ -466,7 +474,8 @@ connections, nil is returned."
(defun arcology-db--update-keywords ()
"Update the keywords of the current buffer in the cache."
(let ((file (file-truename (buffer-file-name))))
(let ((file (file-relative-name (file-truename (buffer-file-name))
arcology-directory)))
(arcology-db-query [:delete :from keywords
:where (= file $s1)]
file)
@ -511,26 +520,28 @@ If FORCE, force a rebuild of the cache from scratch."
;; Two-step building
;; First step: Rebuild files and headlines
(dolist (file arcology-files)
(let* ((attr (file-attributes file))
(let* ((rfile (file-relative-name file arcology-directory))
(attr (file-attributes file))
(atime (file-attribute-access-time attr))
(mtime (file-attribute-modification-time attr)))
(let ((contents-hash (arcology-db--file-hash file)))
(unless (string= (gethash file current-files)
(unless (string= (gethash rfile current-files)
contents-hash)
(arcology--with-temp-buffer file
(arcology-db--clear-file file)
(arcology-db--clear-file rfile)
(arcology-db-query
[:insert :into files
:values $v1]
(vector file contents-hash (list :atime atime :mtime mtime)))
(vector rfile contents-hash (list :atime atime :mtime mtime)))
(setq file-count (1+ file-count))
(when-let ((headlines (arcology--extract-headlines file)))
(when (arcology-db--insert-headlines headlines)
(setq headline-count (1+ headline-count)))))))))
;; Second step: Rebuild the rest
(dolist (file arcology-files)
(let ((contents-hash (arcology-db--file-hash file)))
(unless (string= (gethash file current-files)
(let* ((rfile (file-relative-name file arcology-directory))
(contents-hash (arcology-db--file-hash file)))
(unless (string= (gethash rfile current-files)
contents-hash)
(arcology--with-temp-buffer file
(when-let (links (arcology--extract-links file))
@ -540,22 +551,22 @@ If FORCE, force a rebuild of the cache from scratch."
links)
(setq link-count (1+ link-count)))
(when-let (tags (arcology--extract-tags file))
(arcology-db--insert-tags file tags)
(arcology-db--insert-tags rfile tags)
(setq tag-count (1+ tag-count)))
(when-let* ((keywords (arcology--extract-keywords)))
(arcology-db--insert-keywords file keywords)
(arcology-db--insert-keywords rfile keywords)
(setq keyword-count (+ keyword-count (length keywords))))
(let ((titles (or (arcology--extract-titles)
(list (arcology--path-to-slug file)))))
(arcology-db--insert-titles file titles)
(arcology-db--insert-titles rfile titles)
(setq title-count (+ title-count (length titles))))
(when-let* ((ref (arcology--extract-ref)))
(when (arcology-db--insert-ref file ref)
(when (arcology-db--insert-ref rfile ref)
(setq ref-count (1+ ref-count))))))
(remhash file current-files)))
(remhash rfile current-files)))
(dolist (file (hash-table-keys current-files))
;; These files are no longer around, remove from cache...
(arcology-db--clear-file file)
(arcology-db--clear-file (file-relative-name file arcology-directory))
(setq deleted-count (1+ deleted-count))))
(arcology-message "files: Δ%s, headlines: Δ%s, links: Δ%s, tags: Δ%s, titles: Δ%s, refs: Δ%s, deleted: Δ%s"
file-count