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

Feature: Datomic Client #505

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions client-test.clj
@@ -0,0 +1,62 @@
(ns client-test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script probably needs to be implemented as a proper test. See the postgres tests how to execute a test based on a feature flag.

(:require [datomic.client.api :as d]))

(System/setProperty
"java.home"
(System/getenv "GRAALVM_HOME"))

(def client
(d/client
{:server-type :peer-server
:endpoint "localhost:8080"
:secret "bar"
:access-key "foo"
:validate-hostnames false}))

(def conn
(d/connect client {:db-name "yellowdig"}))

(def db
(d/db conn))


(println "- transact \n")
(let [data [{:db/id "temp-foo" :test/name :foo}]
tx-res (d/transact conn {:tx-data data})]
(println (:tempids tx-res))
(d/pull
(:db-after tx-res)
{:eid (get (:tempids tx-res) "temp-foo")
:selector '[*]}))
(println "\n")

(println "- query \n")
(println
(ffirst
(d/q
'[:find (pull ?e [:user/username :user/firstname])
:where [?e :user/username "bingo"]]
db)))
(println "\n")


(println "- tx-range \n")
(println
(doseq [tx
(d/tx-range
conn
{:limit 10
:start nil
:end nil})]
(println (:id tx))))
(println "\n")

(println "- index range \n")
(println
(take 10
(d/index-range
(d/db conn)
{:attrid :network/name})))
(println "\n")


3 changes: 2 additions & 1 deletion deps.edn
Expand Up @@ -21,7 +21,8 @@
seancorfield/next.jdbc {:mvn/version "1.0.424"}
org.postgresql/postgresql {:mvn/version "42.2.12"}
org.hsqldb/hsqldb {:mvn/version "2.4.0"}
datascript {:mvn/version "0.18.11"}}
datascript {:mvn/version "0.18.11"}
com.datomic/client-pro {:mvn/version "0.9.57"}}
:aliases {:main
{:main-opts ["-m" "babashka.main"]}
:profile
Expand Down
37 changes: 37 additions & 0 deletions feature-datomic-client/babashka/impl/datomic_client.clj
@@ -0,0 +1,37 @@
(ns babashka.impl.datomic-client
(:require [datomic.client.api :as d]
;; these libs and others are dynamically
;; required by the client, depending on use
[datomic.client.api.sync]
[datomic.client.impl.pro]))

(def client-vars
'#{administer-system
as-of
client
connect
create-database
datoms
db
db-stats
delete-database
history
index-pull
index-range
list-databases
pull
q
qseq
since
sync
transact
tx-range
with
with-db})

(def client-namespace
(->> client-vars
(map (fn [v] [v (var-get (ns-resolve 'datomic.client.api v))]))
(into {})))


6 changes: 4 additions & 2 deletions project.clj
Expand Up @@ -8,7 +8,8 @@
:license {:name "Eclipse Public License 1.0"
:url "http://opensource.org/licenses/eclipse-1.0.php"}
:source-paths ["src" "sci/src" "babashka.curl/src" "babashka.pods/src"
"babashka.nrepl/src"]
"babashka.nrepl/src"
"feature-datomic-client"]
;; for debugging Reflector.java code:
;; :java-source-paths ["sci/reflector/src-java"]
:java-source-paths ["src-java"]
Expand All @@ -21,7 +22,8 @@
[cheshire "5.10.0"]
[fipp "0.6.22"]
[nrepl/bencode "1.1.0"]
[borkdude/sci.impl.reflector "0.0.1-java11"]]
[borkdude/sci.impl.reflector "0.0.1-java11"]
[com.datomic/client-pro "0.9.57"]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency needs to be hidden behind a profile like :feature/datomic-client

:profiles {:feature/xml {:source-paths ["feature-xml"]
:dependencies [[org.clojure/data.xml "0.2.0-alpha6"]]}
:feature/yaml {:source-paths ["feature-yaml"]
Expand Down
10 changes: 10 additions & 0 deletions resources-config.json
@@ -0,0 +1,10 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We manage resources dynamically in script/compile and script/compile.bat based on feature flags.

"resources": [
{
"pattern": ".*\\.pem$"
},
{
"pattern": ".*\\.json$"
}
]
}
2 changes: 2 additions & 0 deletions script/compile
Expand Up @@ -47,6 +47,7 @@ args=( "-jar" "$BABASHKA_JAR"
"--initialize-at-build-time"
"-H:Log=registerResource:"
"-H:EnableURLProtocols=http,https"
"-H:ResourceConfigurationFiles=resources-config.json"
"--enable-all-security-services"
"-H:+JNI"
"--verbose"
Expand Down Expand Up @@ -82,6 +83,7 @@ then
export BABAHSKA_FEATURE_TRANSIT=false
export BABASHKA_FEATURE_JAVA_TIME=false
export BABASHKA_FEATURE_JAVA_NIO=false
export BABASHKA_FEATURE_DATOMIC_CLIENT=false
fi

"$GRAALVM_HOME/bin/native-image" "${args[@]}"
11 changes: 11 additions & 0 deletions src/babashka/impl/classes.clj
Expand Up @@ -140,6 +140,17 @@
;; java.net.URL, see below
java.net.URLEncoder
java.net.URLDecoder
;;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these classes only need to be brought into the reflection config when the feature flag is enabled.

java.nio.HeapByteBuffer
~(symbol "[Ljava.nio.HeapByteBuffer;")
org.msgpack.template.builder.TemplateBuilderChain
org.msgpack.template.builder.FieldEntry
org.msgpack.template.builder.JavassistTemplateBuilder
org.msgpack.template.ListTemplate
org.msgpack.template.SetTemplate
org.msgpack.template.CollectionTemplate
org.msgpack.template.MapTemplate
;;
~@(when features/java-nio?
'[java.nio.file.OpenOption
java.nio.file.CopyOption
Expand Down
1 change: 1 addition & 0 deletions src/babashka/impl/features.clj
Expand Up @@ -15,3 +15,4 @@
(def postgresql? (= "true" (System/getenv "BABASHKA_FEATURE_POSTGRESQL")))
(def hsqldb? (= "true" (System/getenv "BABASHKA_FEATURE_HSQLDB")))
(def datascript? (= "true" (System/getenv "BABASHKA_FEATURE_DATASCRIPT")))
(def datomic-client? (= "true" (System/getenv "BABASHKA_FEATURE_DATOMIC_CLIENT")))
13 changes: 11 additions & 2 deletions src/babashka/main.clj
Expand Up @@ -54,6 +54,7 @@
(def pipe-signal-received? (constantly false))
(def handle-sigint! (constantly nil))))


(when features/xml?
(require '[babashka.impl.xml]))

Expand All @@ -76,6 +77,12 @@
(when features/datascript?
(require '[babashka.impl.datascript]))

(when features/datomic-client?
(require '[babashka.impl.datomic-client])
(System/setProperty
"java.library.path"
(str (System/getenv "GRAALVM_HOME") "/lib")))

(sci/alter-var-root sci/in (constantly *in*))
(sci/alter-var-root sci/out (constantly *out*))
(sci/alter-var-root sci/err (constantly *err*))
Expand Down Expand Up @@ -334,7 +341,8 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that
features/jdbc? (assoc 'jdbc 'next.jdbc)
features/core-async? (assoc 'async 'clojure.core.async)
features/csv? (assoc 'csv 'clojure.data.csv)
features/transit? (assoc 'transit 'cognitect.transit)))
features/transit? (assoc 'transit 'cognitect.transit)
features/datomic-client? (assoc 'datomic-client 'datomic.client.api)))

(def cp-state (atom nil))

Expand Down Expand Up @@ -378,7 +386,8 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that
'clojure.core.async.impl.protocols @(resolve 'babashka.impl.async/async-protocols-namespace))
features/csv? (assoc 'clojure.data.csv @(resolve 'babashka.impl.csv/csv-namespace))
features/transit? (assoc 'cognitect.transit @(resolve 'babashka.impl.transit/transit-namespace))
features/datascript? (assoc 'datascript.core @(resolve 'babashka.impl.datascript/datascript-namespace))))
features/datascript? (assoc 'datascript.core @(resolve 'babashka.impl.datascript/datascript-namespace))
features/datomic-client? (assoc 'datomic.client.api @(resolve 'babashka.impl.datomic-client/client-namespace))))

(def bindings
{'java.lang.System/exit exit ;; override exit, so we have more control
Expand Down