/
db.clj
76 lines (63 loc) · 3.22 KB
/
db.clj
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
(ns clojure-lsp.db
(:require
[clojure-lsp.settings :as settings]
[clojure-lsp.shared :as shared]
[clojure.core.async :as async]
[clojure.java.io :as io]
[datalevin.core :as d]
[taoensso.timbre :as log]))
(set! *warn-on-reflection* true)
(def analysis-table-name "analysis-table")
(defonce db (atom {:documents {}}))
(defonce current-changes-chan (async/chan 1))
(defonce diagnostics-chan (async/chan 1))
(defonce edits-chan (async/chan 1))
(def version 1)
(defn ^:private get-sqlite-db-file [project-root]
(io/file (str project-root) ".lsp" ".cache" "sqlite.db"))
(defn ^:private get-datalevin-db-file [project-root-path db]
(let [configured (some-> (settings/get db [:cache-path])
io/file)
default (io/file (str project-root-path) ".lsp" ".cache")]
^java.io.File (or configured default)))
(defn ^:private get-db-file-path [project-root-path db]
(let [file ^java.io.File (get-datalevin-db-file project-root-path db)]
(if (.isAbsolute file)
(.getAbsolutePath file)
(.getAbsolutePath (io/file (str project-root-path) file)))))
(defn make-db [project-root db]
(d/open-kv (get-db-file-path project-root db)))
(defn ^:private remove-old-db-file! [project-root-path]
(let [old-db-file (get-sqlite-db-file project-root-path)]
(when (shared/file-exists? old-db-file)
(io/delete-file old-db-file))))
(defn save-deps! [project-root-path project-hash kondo-config-hash classpath analysis db]
(remove-old-db-file! project-root-path)
(shared/logging-time
"Saving analysis cache to Datalevin db took %s secs"
(let [datalevin-db (make-db project-root-path db)]
(d/open-dbi datalevin-db analysis-table-name)
(d/transact-kv datalevin-db [[:del analysis-table-name :project-analysis]])
(d/transact-kv datalevin-db [[:put analysis-table-name :project-analysis {:version version
:project-root (str project-root-path)
:project-hash project-hash
:kondo-config-hash kondo-config-hash
:classpath classpath
:analysis analysis}]])
(d/close-kv datalevin-db))))
(defn read-deps [project-root-path db]
(try
(shared/logging-time
"Reading analysis cache from Datalevin db took %s secs"
(let [datalevin-db (make-db project-root-path db)]
(d/open-dbi datalevin-db analysis-table-name)
(when-let [project-analysis (d/get-value datalevin-db analysis-table-name :project-analysis)]
(when (and (= (str project-root-path) (:project-root project-analysis))
(= version (:version project-analysis)))
project-analysis))))
(catch Throwable e
(log/error "Could not load project cache from DB" e))))
(defn db-exists? [project-root-path db]
(shared/file-exists? (get-datalevin-db-file project-root-path db)))
(defn remove-db! [project-root-path db]
(io/delete-file (get-datalevin-db-file project-root-path db)))