-
Notifications
You must be signed in to change notification settings - Fork 83
/
incremental.lisp
82 lines (72 loc) · 3.35 KB
/
incremental.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload 'cl-store))
(defpackage :coleslaw-incremental
(:use :cl)
(:import-from :alexandria #:when-let)
(:import-from :coleslaw #:*config*
#:content
#:index
#:discover
#:get-updated-files
#:find-content-by-path
#:add-document
#:delete-document
;; Private
#:all-subclasses
#:do-subclasses
#:read-content
#:construct
#:rel-path
#:repo
#:update-content-metadata)
(:export #:enable))
(in-package :coleslaw-incremental)
;; In contrast to the original incremental plans, full of shoving state into
;; the right place by hand and avoiding writing pages to disk that hadn't
;; changed, the new plan is to only avoid redundant parsing of content in
;; the git repo. The rest of coleslaw's operation is "fast enough".
;;
;; Prior to enabling the plugin a user must have a cl-store dump of the
;; database at ~/.coleslaw.db. There is a dump_db shell script in
;; examples to generate the database dump.
;;
;; We're gonna be a bit dirty here and monkey patch. The compilation model
;; still isn't an "exposed" part of Coleslaw. After some experimentation maybe
;; we'll settle on an interface.
(defun coleslaw::load-content ()
(let ((db-file (rel-path (user-homedir-pathname) ".coleslaw.db")))
(setf coleslaw::*site* (cl-store:restore db-file))
(loop for (status path) in (get-updated-files)
for file-path = (rel-path (repo-dir *config*) path)
do (update-content status file-path))
(update-content-metadata)
;; Discover's :before method will delete any possibly outdated indexes.
(do-subclasses (itype index)
(discover itype))
(cl-store:store coleslaw::*site* db-file)))
(defun update-content (status path)
(cond ((string= "D" status) (process-change :deleted path))
((string= "M" status) (process-change :modified path))
((string= "A" status) (process-change :added path))))
(defgeneric process-change (status path &key &allow-other-keys)
(:documentation "Updates the database as needed for the STATUS change to PATH.")
(:method :around (status path &key)
(let ((extension (pathname-type path))
(ctypes (all-subclasses (find-class 'content))))
;; If the updated file's extension doesn't match one of our content types,
;; we don't need to mess with it at all. Otherwise, since the class is
;; annoyingly tricky to determine, pass it along.
(when-let (ctype (find extension ctypes :test #'class-name-p))
(call-next-method status path :ctype ctype)))))
(defmethod process-change ((status (eql :deleted)) path &key)
(let ((old (find-content-by-path path)))
(delete-document old)))
(defmethod process-change ((status (eql :modified)) path &key ctype)
(let ((old (find-content-by-path path))
(new (construct ctype (read-content path))))
(delete-document old)
(add-document new)))
(defmethod process-change ((status (eql :added)) path &key ctype)
(let ((new (construct ctype (read-content path))))
(add-document new)))
(defun enable ())