Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

many minor adjustments for ClojureScript #357

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.597"}
org.clojure/clojurescript {:mvn/version "1.10.773"}
io.replikativ/hitchhiker-tree {:mvn/version "0.1.11"}
persistent-sorted-set/persistent-sorted-set {:mvn/version "0.1.2"}
org.clojure/tools.reader {:mvn/version "1.3.3"}
Expand All @@ -8,6 +8,7 @@
io.replikativ/superv.async {:mvn/version "0.2.11"}
io.lambdaforge/datalog-parser {:mvn/version "0.1.8"}
io.replikativ/zufall {:mvn/version "0.1.0"}
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}
junit/junit {:mvn/version "4.13.1"}}

:paths ["src" "target/classes"]
Expand Down
886 changes: 445 additions & 441 deletions src/datahike/api.cljc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/datahike/array.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns datahike.array
(:require [hitchhiker.tree.node :as n])
(:import [java.util Arrays]))
#?(:clj (:import [java.util Arrays])))

#?(:clj
(defn java8? []
Expand Down
69 changes: 39 additions & 30 deletions src/datahike/config.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
(:require [clojure.edn :as edn]
[clojure.spec.alpha :as s]
[zufall.core :as z]
[environ.core :refer [env]]
#?(:clj [environ.core :refer [env]])
[taoensso.timbre :as log]
[datahike.store :as ds])
(:import [java.net URI]))
#?(:clj (:import [java.net URI])))

#?(:cljs
(do
(def Exception js/Error)
(def env {})))

(s/def ::index #{:datahike.index/hitchhiker-tree :datahike.index/persistent-set})
(s/def ::keep-history? boolean?)
Expand Down Expand Up @@ -45,7 +50,7 @@
:path path
:host host
:port port
:id (str (java.util.UUID/randomUUID))}
:id (str #?(:clj (java.util.UUID/randomUUID) :cljs (random-uuid)))}
:level {:path path}
:file {:path path}))
:index index
Expand All @@ -57,13 +62,16 @@
(defn int-from-env
[key default]
(try
(Integer/parseInt (get env key (str default)))
(#?(:clj Integer/parseInt :cljs js/parseInt) (get env key (str default)))
(catch Exception _ default)))

(defn bool-from-env
[key default]
(try
(Boolean/parseBoolean (get env key default))
#?(:clj
(Boolean/parseBoolean (get env key default))
:cljs
(= "true" (get env key default)))
(catch Exception _ default)))

(defn map-from-env [key default]
Expand Down Expand Up @@ -141,7 +149,7 @@
(when (and attribute-refs? (= :read schema-flexibility))
(throw (ex-info "Attribute references cannot be used with schema-flexibility ':read'." config)))
(if (string? initial-tx)
(update merged-config :initial-tx (fn [path] (-> path slurp read-string)))
(update merged-config :initial-tx (fn [path] #?(:clj (-> path slurp edn/read-string))))
merged-config))))

;; deprecation begin
Expand All @@ -157,30 +165,31 @@
:opt-un [::username ::password ::path ::host ::port]))

(defn uri->config [uri]
(let [base-uri (URI. uri)
_ (when-not (= (.getScheme base-uri) "datahike")
(throw (ex-info "URI scheme is not datahike conform." {:uri uri})))
sub-uri (URI. (.getSchemeSpecificPart base-uri))
backend (keyword (.getScheme sub-uri))
[username password] (when-let [user-info (.getUserInfo sub-uri)]
(clojure.string/split user-info #":"))
credentials (when-not (and (nil? username) (nil? password))
{:username username
:password password})
port (.getPort sub-uri)
path (.getPath sub-uri)
host (.getHost sub-uri)
config (merge
{:backend backend
:uri uri}
credentials
(when host
{:host host})
(when-not (empty? path)
{:path path})
(when (<= 0 port)
{:port port}))]
config))
#?(:clj
(let [base-uri (URI. uri)
_ (when-not (= (.getScheme base-uri) "datahike")
(throw (ex-info "URI scheme is not datahike conform." {:uri uri})))
sub-uri (URI. (.getSchemeSpecificPart base-uri))
backend (keyword (.getScheme sub-uri))
[username password] (when-let [user-info (.getUserInfo sub-uri)]
(clojure.string/split user-info #":"))
credentials (when-not (and (nil? username) (nil? password))
{:username username
:password password})
port (.getPort sub-uri)
path (.getPath sub-uri)
host (.getHost sub-uri)
config (merge
{:backend backend
:uri uri}
credentials
(when host
{:host host})
(when-not (empty? path)
{:path path})
(when (<= 0 port)
{:port port}))]
config)))

(defn validate-config-depr [config]
(when-not (s/valid? :datahike/config-depr config)
Expand Down
60 changes: 33 additions & 27 deletions src/datahike/connector.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
[datahike.config :as dc]
[datahike.tools :as dt :refer [throwable-promise]]
[datahike.index.hitchhiker-tree.upsert :as ups]
[datahike.transactor :as t]
#?(:clj [datahike.transactor :as t])
[hitchhiker.tree.bootstrap.konserve :as kons]
[konserve.core :as k]
[konserve.cache :as kc]
[superv.async :refer [<?? S]]
[superv.async :refer [<? <?? S]]
[taoensso.timbre :as log]
[clojure.spec.alpha :as s]
[clojure.core.async :refer [go <!]]
[clojure.core.cache :as cache])
(:import [java.net URI]))
#?(:clj [clojure.core.cache :as cache]
:cljs [cljs.cache :as cache])))

(s/def ::connection #(instance? clojure.lang.Atom %))
#?(:cljs
(do
(def Exception js/Error)))

(s/def ::connection #(instance? Atom %))

(defn update-and-flush-db [connection tx-data update-fn]
(let [{:keys [db-after] :as tx-report} @(update-fn connection tx-data)
Expand All @@ -31,7 +35,7 @@
temporal-eavt-flushed (when keep-history? (di/-flush temporal-eavt backend))
temporal-aevt-flushed (when keep-history? (di/-flush temporal-aevt backend))
temporal-avet-flushed (when keep-history? (di/-flush temporal-avet backend))]
(<?? S (k/assoc-in store [:db]
(#?(:clj <?? :cljs <?) S (k/assoc-in store [:db]
(merge
{:schema schema
:rschema rschema
Expand All @@ -58,11 +62,12 @@
(defn transact!
[connection {:keys [tx-data]}]
{:pre [(d/conn? connection)]}
(let [p (throwable-promise)]
(go
(let [tx-report (<! (t/send-transaction! (:transactor @connection) tx-data 'datahike.core/transact))]
(deliver p tx-report)))
p))
#?(:clj
(let [p (throwable-promise)]
(go
(let [tx-report (<! (t/send-transaction! (:transactor @connection) tx-data 'datahike.core/transact))]
#?(deliver p tx-report)))
p)))

(defn transact [connection arg-map]
(let [arg (cond
Expand All @@ -80,14 +85,15 @@
(throw (.getCause e))))))

(defn load-entities [connection entities]
(let [p (throwable-promise)]
(go
(let [tx-report (<! (t/send-transaction! (:transactor @connection) entities 'datahike.core/load-entities))]
(deliver p tx-report)))
p))
#?(:clj
(let [p (throwable-promise)]
(go
(let [tx-report (<! (t/send-transaction! (:transactor @connection) entities 'datahike.core/load-entities))]
(deliver p tx-report)))
p)))

(defn release [connection]
(<?? S (t/shutdown (:transactor @connection)))
#?(:clj (<?? S (t/shutdown (:transactor @connection))))
(ds/release-store (get-in @connection [:config :store]) (:store @connection)))

;; deprecation begin
Expand All @@ -98,20 +104,20 @@
(-database-exists? [config]))

(extend-protocol IConfiguration
String
#?(:clj String :cljs string)
(-connect [uri]
(-connect (dc/uri->config uri)))

(-create-database [uri & opts]
(apply -create-database (dc/uri->config uri) opts))
(-create-database [uri opts]
(-create-database (dc/uri->config uri) opts))

(-delete-database [uri]
(-delete-database (dc/uri->config uri)))

(-database-exists? [uri]
(-database-exists? (dc/uri->config uri)))

clojure.lang.IPersistentMap
#?(:clj clojure.lang.IPersistentMap :cljs PersistentArrayMap)
(-database-exists? [config]
(let [config (dc/load-config config)
store-config (:store config)
Expand All @@ -122,7 +128,7 @@
(kc/ensure-cache
raw-store
(atom (cache/lru-cache-factory {} :threshold 1000)))))
stored-db (<?? S (k/get-in store [:db]))]
stored-db (#?(:clj <?? :cljs <?) S (k/get-in store [:db]))]
(ds/release-store store-config store)
(not (nil? stored-db)))
(do
Expand All @@ -142,7 +148,7 @@
(kc/ensure-cache
raw-store
(atom (cache/lru-cache-factory {} :threshold 1000)))))
stored-db (<?? S (k/get-in store [:db]))
stored-db (#?(:clj <?? :cljs <?) S (k/get-in store [:db]))
_ (when-not stored-db
(ds/release-store store-config store)
(dt/raise "Database does not exist." {:type :db-does-not-exist
Expand All @@ -165,22 +171,22 @@
:temporal-avet temporal-avet-key
:rschema rschema
:store store))]
(swap! conn assoc :transactor (t/create-transactor (:transactor config) conn update-and-flush-db))
#?(:clj (swap! conn assoc :transactor (t/create-transactor (:transactor config) conn update-and-flush-db)))
conn))

(-create-database [config & deprecated-config]
(-create-database [config deprecated-config]
(let [{:keys [keep-history? initial-tx] :as config} (dc/load-config config deprecated-config)
store-config (:store config)
store (kc/ensure-cache
(ds/empty-store store-config)
(atom (cache/lru-cache-factory {} :threshold 1000)))
stored-db (<?? S (k/get-in store [:db]))
stored-db (#?(:clj <?? :cljs <?) S (k/get-in store [:db]))
_ (when stored-db
(dt/raise "Database already exists." {:type :db-already-exists :config store-config}))
{:keys [eavt aevt avet temporal-eavt temporal-aevt temporal-avet schema rschema config max-tx op-count hash]}
(db/empty-db nil config)
backend (kons/->KonserveBackend store)]
(<?? S (k/assoc-in store [:db]
(#?(:clj <?? :cljs <?) S (k/assoc-in store [:db]
(merge {:schema schema
:max-tx max-tx
:op-count op-count
Expand Down
27 changes: 18 additions & 9 deletions src/datahike/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
; Query


(def ^{:arglists '([query & inputs])}
q dq/q)
(defn q [query & inputs]
(apply dq/q query inputs))


; Creating DB
Expand Down Expand Up @@ -364,13 +364,22 @@
(defn load-entities [conn entities]
{:pre [(conn? conn)]}
(let [res (-load-entities! conn entities)]
(reify
clojure.lang.IDeref
(deref [_] res)
clojure.lang.IBlockingDeref
(deref [_ _ _] res)
clojure.lang.IPending
(isRealized [_] true))))
#?(:cljs
(reify
IDeref
(-deref [_] res)
IDerefWithTimeout
(-deref-with-timeout [_ _ _] res)
IPending
(-realized? [_] true))
:clj
(reify
clojure.lang.IDeref
(deref [_] res)
clojure.lang.IBlockingDeref
(deref [_ _ _] res)
clojure.lang.IPending
(isRealized [_] true)))))

;; ersatz future without proper blocking
#?(:cljs
Expand Down
Loading