mirror of https://github.com/logseq/logseq
fix: add transact and datoms load
parent
67cb8d6213
commit
b7ce9a7ca5
|
@ -1,8 +1,9 @@
|
|||
{:deps
|
||||
;; External deps should be kept in sync with https://github.com/logseq/nbb-logseq/blob/main/bb.edn
|
||||
{datascript/datascript {:git/url "https://github.com/logseq/datascript" ;; fork
|
||||
:sha "f0922f4d10714636711bc0176409290e44ce2feb"}
|
||||
com.cognitect/transit-cljs {:mvn/version "0.8.280"}
|
||||
{datascript/datascript {:git/url "https://github.com/logseq/datascript" ;; fork
|
||||
:sha "f0922f4d10714636711bc0176409290e44ce2feb"}
|
||||
com.cognitect/transit-cljs {:mvn/version "0.8.280"}
|
||||
cljs-bean/cljs-bean {:mvn/version "1.5.0"}
|
||||
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}}
|
||||
:aliases
|
||||
{:clj-kondo
|
||||
|
|
|
@ -3,7 +3,19 @@
|
|||
(:require ["path" :as node-path]
|
||||
["better-sqlite3" :as sqlite3]
|
||||
[clojure.string :as string]
|
||||
[cljs-bean.core :as bean]))
|
||||
[cljs-bean.core :as bean]
|
||||
[datascript.storage :refer [IStorage]]
|
||||
[cognitect.transit :as t]
|
||||
[cljs-bean.core :as bean]
|
||||
[cljs.cache :as cache]
|
||||
[datascore.core :as d]))
|
||||
|
||||
(defn- write-transit [data]
|
||||
(t/write (t/writer :json) data))
|
||||
|
||||
(defn- read-transit [s]
|
||||
(t/read (t/reader :json) s))
|
||||
|
||||
|
||||
;; Notice: this works only on Node.js environment, it doesn't support browser yet.
|
||||
|
||||
|
@ -36,6 +48,10 @@
|
|||
[repo]
|
||||
(get @databases (sanitize-db-name repo)))
|
||||
|
||||
(defn get-conn
|
||||
[repo]
|
||||
(get @conns (sanitize-db-name repo)))
|
||||
|
||||
(defn prepare
|
||||
[^object db sql db-name]
|
||||
(when db
|
||||
|
@ -57,13 +73,6 @@
|
|||
graph-dir (node-path/join graphs-dir db-name')]
|
||||
[db-name' (node-path/join graph-dir "db.sqlite")]))
|
||||
|
||||
(defn open-db!
|
||||
[graphs-dir db-name]
|
||||
(let [[db-sanitized-name db-full-path] (get-db-full-path graphs-dir db-name)
|
||||
db (new sqlite db-full-path nil)]
|
||||
(create-kvs-table! db db-name)
|
||||
(swap! databases assoc db-sanitized-name db)))
|
||||
|
||||
(defn- clj-list->sql
|
||||
"Turn clojure list into SQL list
|
||||
'(1 2 3 4)
|
||||
|
@ -148,6 +157,51 @@
|
|||
(str "select content from kvs where addr = " addr))
|
||||
first)))
|
||||
|
||||
(defn sqlite-storage
|
||||
[repo {:keys [threshold]
|
||||
:or {threshold 4096}}]
|
||||
(let [cache (cache/lru-cache-factory {} :threshold threshold)]
|
||||
(reify IStorage
|
||||
(-store [_ addr+data-seq]
|
||||
(prn :debug :store {:addr-data addr+data-seq})
|
||||
(let [data (map
|
||||
(fn [[addr data]]
|
||||
{:addr addr
|
||||
:content (write-transit data)})
|
||||
addr+data-seq)]
|
||||
(upsert-addr-content! repo (bean/->js data))))
|
||||
(-restore [_ addr]
|
||||
(when-let [content (if (cache/has? cache addr)
|
||||
(do
|
||||
(cache/hit cache addr)
|
||||
(cache/lookup cache addr))
|
||||
(when-let [result (restore-data-from-addr repo addr)]
|
||||
(cache/miss cache addr result)
|
||||
result))]
|
||||
(prn {:content content})
|
||||
(read-transit content))))))
|
||||
|
||||
(defn open-db!
|
||||
[graphs-dir db-name]
|
||||
(let [[db-sanitized-name db-full-path] (get-db-full-path graphs-dir db-name)
|
||||
db (new sqlite db-full-path nil)]
|
||||
(create-kvs-table! db db-name)
|
||||
(swap! databases assoc db-sanitized-name db)
|
||||
(let [storage (sqlite-storage db-name {})
|
||||
conn (or (d/restore-conn storage)
|
||||
(d/create-conn nil {:storage storage}))]
|
||||
(swap! conns assoc db-name conn))))
|
||||
|
||||
(defn transact!
|
||||
[repo tx-data tx-meta]
|
||||
)
|
||||
(when-let [conn (get-conn repo)]
|
||||
(d/transact! conn tx-data tx-meta)))
|
||||
|
||||
(defn load-data
|
||||
"Get all datoms remove :block/content"
|
||||
[repo]
|
||||
(when-let [conn (get-conn repo)]
|
||||
(let [db @conn]
|
||||
(->> (d/datoms db :eavt)
|
||||
(remove (fn [e]
|
||||
(= :block/content (:a e))))))))
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
(ns logseq.db.sqlite.storage
|
||||
(:require [datascript.storage :refer [IStorage]]
|
||||
[logseq.db.sqlite.db :as sqlite-db]
|
||||
[cognitect.transit :as t]
|
||||
[cljs-bean.core :as bean]
|
||||
[cljs.cache :as cache]))
|
||||
|
||||
(defn- write-transit [data]
|
||||
(t/write (t/writer :json) data))
|
||||
|
||||
(defn- read-transit [s]
|
||||
(t/read (t/reader :json) s))
|
||||
|
||||
(defn sqlite-storage
|
||||
[repo {:keys [threshold]
|
||||
:or {threshold 4096}}]
|
||||
(let [cache (cache/lru-cache-factory {} :threshold threshold)]
|
||||
(reify IStorage
|
||||
(-store [_ addr+data-seq]
|
||||
(prn :debug :store {:addr-data addr+data-seq})
|
||||
(let [data (map
|
||||
(fn [[addr data]]
|
||||
{:addr addr
|
||||
:content (write-transit data)})
|
||||
addr+data-seq)]
|
||||
(sqlite-db/upsert-addr-content! repo (bean/->js data))))
|
||||
(-restore [_ addr]
|
||||
(when-let [content (if (cache/has? cache addr)
|
||||
(do
|
||||
(cache/hit cache addr)
|
||||
(cache/lookup cache addr))
|
||||
(when-let [result (sqlite-db/restore-data-from-addr repo addr)]
|
||||
(cache/miss cache addr result)
|
||||
result))]
|
||||
(prn {:content content})
|
||||
(read-transit content))))))
|
||||
|
||||
(comment
|
||||
(require '[datascript.core :as d])
|
||||
(def repo "my-test")
|
||||
;; create new db
|
||||
(electron.db/new-db! repo)
|
||||
|
||||
(def storage (sqlite-storage repo {}))
|
||||
(def conn (or (d/restore-conn storage)
|
||||
(d/create-conn nil {:storage storage})))
|
||||
|
||||
(d/transact! conn [{:db/id 10
|
||||
:data 1}])
|
||||
|
||||
(prn "Entity 10 data: " (:data (d/entity @conn 10))))
|
Loading…
Reference in New Issue