Mercurial > infra / scripts/org-graph-db-init.lisp
changeset 360: |
7e7981b3af10 |
parent: |
9fc8fc324c10
|
child: |
43afcbbe2d07 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Sun, 25 Aug 2024 20:35:51 -0400 |
permissions: |
-rw-r--r-- |
description: |
org-graph-db updates |
1 ;;; org-graph-db-init.lisp --- Initialize the org-graph-db-directory 6 #-user (ql:quickload :user) 8 (unless (find-package :org-graph-db) 11 :obj/db :obj/query :obj/id :obj/uuid))) 13 (in-package :org-graph-db) 15 (rocksdb:load-rocksdb) 17 (defun make-org-graph-schema () 19 (make-field :name "file" :type 'string) 20 (make-field :name "title" :type 'string) 21 (make-field :name "hash" :type 'octet-vector) 22 (make-field :name "atime" :type 'octet-vector) 23 (make-field :name "mtime" :type 'octet-vector) 24 (make-field :name "node" :type 'octet-vector) 25 (make-field :name "edge" :type 'octet-vector) 26 (make-field :name "node-tags" :type 'string) 27 (make-field :name "node-links" :type 'string) 28 (make-field :name "node-properties" :type 'string) 29 (make-field :name "node-priority" :type 'string) 30 (make-field :name "node-schedule" :type 'string) 31 (make-field :name "node-file" :type 'string) 32 (make-field :name "node-pos" :type 'octet-vector) 33 (make-field :name "node-state" :type 'string))) 35 (defparameter *org-graph-schema* (make-org-graph-schema)) 37 (defvar *org-graph-db* nil) 39 (defparameter *org-graph-db-directory* 40 (or (probe-file (car (cli:args))) 41 (merge-pathnames ".stash/org/graph/db/" (user-homedir-pathname)))) 43 (defun make-org-graph-db () 44 (create-db (namestring *org-graph-db-directory*) 45 :opts (default-rdb-opts))) 47 (define-condition org-id-locations-out-of-sync (simple-error) ()) 49 (defvar *emacs-org-id-locations-file* (merge-pathnames ".emacs.d/.org-id-locations" (user-homedir-pathname))) 51 (defun make-org-id-locations (&optional (file *emacs-org-id-locations-file*)) 52 (let ((tbl (make-hash-table :test 'equal))) 53 (with-open-file (file file) 54 (dolist (entry (read file)) 55 (if-let ((file (probe-file (car entry)))) 56 (setf (gethash (namestring file) tbl) (cdr entry)) 57 (signal 'org-id-locations-out-of-sync :format-control "~A" :format-arguments (list entry))))) 60 (defvar *org-graph-id-locations* (make-org-id-locations)) 62 (defun insert-org-files () 63 (log:info! "inserting org files") 64 (open-cf *org-graph-db* "file") 65 (maphash (lambda (k v) (insert-key *org-graph-db* k 66 (apply 'concatenate 'string v) 68 *org-graph-id-locations*) 69 (flush-db *org-graph-db*)) 71 (defun insert-org-nodes () 72 (log:info! "inserting org nodes") 73 (open-cf *org-graph-db* "node") 74 (dolist (v (hash-table-values *org-graph-id-locations*)) 76 (insert-key *org-graph-db* id "0" :cf "node")))) 78 (defun close-org-graph-db () 79 (when (db-open-p *org-graph-db*) 80 (close-db *org-graph-db*))) 82 (defun init-org-graph-db () 83 (ensure-directories-exist (make-pathname :directory (butlast (pathname-directory *org-graph-db-directory*))) :verbose t) 84 (with-db (db (load-schema (make-org-graph-db) *org-graph-schema*)) 87 (setq *org-graph-db* db) 91 (log:info! "created org-graph-db" db *org-graph-db-directory* *org-graph-schema*))) 93 (defun open-org-graph-db () 94 (unless (probe-file *org-graph-db-directory*) 96 (if (db-open-p *org-graph-db*) 98 (open-db (or *org-graph-db* (make-org-graph-db))))) 100 (defun destroy-org-graph-db (&optional force) 101 (when (probe-file *org-graph-db-directory*) 103 (with-db (db (or *org-graph-db* (make-org-graph-db))) 106 (log:info! "destroyed org-graph-db" db *org-graph-db-directory*)) 108 (sb-ext:delete-directory *org-graph-db-directory* :recursive t) 109 (setq *org-graph-db* nil)))))