Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
Adds dependencies and reformats (#11)
Browse files Browse the repository at this point in the history
Adds plugins for formatting (cljfmt) and linting (Kibit) to CI.
- Lints/formats code
- Adds Simple instructions to readme
  • Loading branch information
naomijub authored Apr 23, 2023
1 parent be11a78 commit 93ec63e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 73 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ jobs:
run: lein deps
- name: Run tests
run: lein test

formatter:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: lein deps
- name: Run cljfmt
run: lein cljfmt check
- name: Run kibit
run: lein kibit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pom.xml.asc
.lsp/
.clj-kondo/
.calva/
.eastwood
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

gRPC Plugin for XTDB

## Usage:

1. Install [leiningen](https://leiningen.org/)
2. execute `make all` after making changes to the `*.proto` files in `resources/`

* Execute server: `lein run`
* Run tests: `lein test`
* Format code, Check: `lein cljfmt check`, Fix: `lein cljfmt fix`
* Lint code, Check: `lein kibit`, Fix: `lein kibit --replace`


# Protojure stuff
## Usage
This is the output of a `lein new protojure` template run. The output is a set of sample
files demonstrating the use of the protojure lib + protoc plugin to expose a GRPC service.
Expand Down
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
:url "https://www.mit.edu/~amini/LICENSE.md"
:year 2023
:key "mit"}
:plugins [[lein-cljfmt "0.9.2"]]
:plugins [[lein-cljfmt "0.9.2"]
[lein-kibit "0.1.8"]
[jonase/eastwood "1.4.0"]]
:dependencies [[org.clojure/clojure "1.10.3"]
[com.xtdb/xtdb-core "1.22.1"]
[io.pedestal/pedestal.service "0.5.9"]
Expand Down
59 changes: 14 additions & 45 deletions src/com/xtdb/protos.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@

(defn convert-OptionString-value [origkeyval]
(cond
(get-in origkeyval [:value :none]) (update-in origkeyval [:value :none] new-Empty)
(get-in origkeyval [:value :some]) origkeyval
:default origkeyval))
(get-in origkeyval [:value :none]) (update-in origkeyval [:value :none] new-Empty)
(get-in origkeyval [:value :some]) origkeyval
:default origkeyval))

(defn write-OptionString-value [value os]
(let [field (first value)
k (when-not (nil? field) (key field))
v (when-not (nil? field) (val field))]
(case k
:none (serdes.core/write-embedded 1 v os)
:some (serdes.core/write-String 2 {:optimize false} v os)
nil)))


(case k
:none (serdes.core/write-embedded 1 v os)
:some (serdes.core/write-String 2 {:optimize false} v os)
nil)))

;;----------------------------------------------------------------------------------
;;----------------------------------------------------------------------------------
Expand All @@ -62,8 +60,7 @@
;-----------------------------------------------------------------------------
(defrecord Empty-record []
pb/Writer
(serialize [this os]
)
(serialize [this os])
pb/TypeReflection
(gettype [this]
"com.xtdb.protos.Empty"))
Expand All @@ -74,12 +71,7 @@
(defn cis->Empty
"CodedInputStream to Empty"
[is]
(->> (tag-map Empty-defaults
(fn [tag index]
(case index
[index (serdes.core/cis->undefined tag is)]))
is)
(map->Empty-record)))
(map->Empty-record (tag-map Empty-defaults (fn [tag index] (case index [index (serdes.core/cis->undefined tag is)])) is)))

(defn ecis->Empty
"Embedded CodedInputStream to Empty"
Expand All @@ -92,8 +84,7 @@
"
[init]
{:pre [(if (s/valid? ::Empty-spec init) true (throw (ex-info "Invalid input" (s/explain-data ::Empty-spec init))))]}
(-> (merge Empty-defaults init)
(map->Empty-record)))
(map->Empty-record (merge Empty-defaults init)))

(defn pb->Empty
"Protobuf to Empty"
Expand All @@ -119,15 +110,7 @@
(defn cis->OptionString
"CodedInputStream to OptionString"
[is]
(->> (tag-map OptionString-defaults
(fn [tag index]
(case index
1 [:value {:none (ecis->Empty is)}]
2 [:value {:some (serdes.core/cis->String is)}]

[index (serdes.core/cis->undefined tag is)]))
is)
(map->OptionString-record)))
(map->OptionString-record (tag-map OptionString-defaults (fn [tag index] (case index 1 [:value {:none (ecis->Empty is)}] 2 [:value {:some (serdes.core/cis->String is)}] [index (serdes.core/cis->undefined tag is)])) is)))

(defn ecis->OptionString
"Embedded CodedInputStream to OptionString"
Expand Down Expand Up @@ -174,27 +157,13 @@
(s/def :com.xtdb.protos.StatusResponse/estimate-num-keys int?)
(s/def :com.xtdb.protos.StatusResponse/size int?)


(s/def ::StatusResponse-spec (s/keys :opt-un [:com.xtdb.protos.StatusResponse/version :com.xtdb.protos.StatusResponse/index-version :com.xtdb.protos.StatusResponse/kv-store :com.xtdb.protos.StatusResponse/estimate-num-keys :com.xtdb.protos.StatusResponse/size ]))
(def StatusResponse-defaults {:version "" :index-version 0 :kv-store "" :estimate-num-keys 0 :size 0 })
(s/def ::StatusResponse-spec (s/keys :opt-un [:com.xtdb.protos.StatusResponse/version :com.xtdb.protos.StatusResponse/index-version :com.xtdb.protos.StatusResponse/kv-store :com.xtdb.protos.StatusResponse/estimate-num-keys :com.xtdb.protos.StatusResponse/size]))
(def StatusResponse-defaults {:version "" :index-version 0 :kv-store "" :estimate-num-keys 0 :size 0})

(defn cis->StatusResponse
"CodedInputStream to StatusResponse"
[is]
(->> (tag-map StatusResponse-defaults
(fn [tag index]
(case index
1 [:version (serdes.core/cis->String is)]
2 [:index-version (serdes.core/cis->Int32 is)]
3 [:kv-store (serdes.core/cis->String is)]
4 [:estimate-num-keys (serdes.core/cis->Int32 is)]
5 [:size (serdes.core/cis->Int64 is)]
6 [:revision (ecis->OptionString is)]
7 [:consumer-state (ecis->OptionString is)]

[index (serdes.core/cis->undefined tag is)]))
is)
(map->StatusResponse-record)))
(map->StatusResponse-record (tag-map StatusResponse-defaults (fn [tag index] (case index 1 [:version (serdes.core/cis->String is)] 2 [:index-version (serdes.core/cis->Int32 is)] 3 [:kv-store (serdes.core/cis->String is)] 4 [:estimate-num-keys (serdes.core/cis->Int32 is)] 5 [:size (serdes.core/cis->Int64 is)] 6 [:revision (ecis->OptionString is)] 7 [:consumer-state (ecis->OptionString is)] [index (serdes.core/cis->undefined tag is)])) is)))

(defn ecis->StatusResponse
"Embedded CodedInputStream to StatusResponse"
Expand Down
17 changes: 8 additions & 9 deletions src/com/xtdb/protos/GrpcApi/client.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
(defn Status
([client params] (Status client {} params))
([client metadata params]
(let [input (async/chan 1)
output (async/chan 1)
desc {:service "com.xtdb.protos.GrpcApi"
:method "Status"
:input {:f com.xtdb.protos/new-Empty :ch input}
:output {:f com.xtdb.protos/pb->StatusResponse :ch output}
:metadata metadata}]
(-> (send-unary-params input params)
(p/then (fn [_] (invoke-unary client desc output)))))))
(let [input (async/chan 1)
output (async/chan 1)
desc {:service "com.xtdb.protos.GrpcApi"
:method "Status"
:input {:f com.xtdb.protos/new-Empty :ch input}
:output {:f com.xtdb.protos/pb->StatusResponse :ch output}
:metadata metadata}]
(p/then (send-unary-params input params) (fn [_] (invoke-unary client desc output))))))

3 changes: 1 addition & 2 deletions src/com/xtdb/protos/GrpcApi/server.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
;;; GRPC com.xtdb.protos.GrpcApi Service Implementation
;;;----------------------------------------------------------------------------------
(ns com.xtdb.protos.GrpcApi.server
(:require [com.xtdb.protos :refer :all]
))
(:require [com.xtdb.protos :refer :all]))

;-----------------------------------------------------------------------------
; GRPC GrpcApi
Expand Down
2 changes: 1 addition & 1 deletion src/gxtdb/adapters/status.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns gxtdb.adapters.status
(:require [gxtdb.utils :as utils]))
(:require [gxtdb.utils :as utils]))

(defn edn->grpc [edn]
(->
Expand Down
2 changes: 1 addition & 1 deletion src/gxtdb/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

;; This is an adapted service map, that can be started and stopped
;; From the REPL you can call server/start and server/stop on this service
(defn runnable-service [xtdb-node] (-> xtdb-node service/service server/create-server ))
(defn runnable-service [xtdb-node] (-> xtdb-node service/service server/create-server))

(defn -run-test
"The entry-point for 'lein run-dev'"
Expand Down
17 changes: 8 additions & 9 deletions src/gxtdb/service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[io.pedestal.http.route :as route]
[io.pedestal.http.body-params :as body-params]
[ring.util.response :as ring-resp]

[gxtdb.adapters.status :as status-adapter]

;; -- XTDB --
Expand Down Expand Up @@ -39,7 +39,6 @@
;;
;; see http://pedestal.io/reference/request-map


(defonce xtdb-in-memory-node (xt/start-node {}))

(deftype GrpcAPI [xtdb-node]
Expand All @@ -61,20 +60,20 @@

;; -- PROTOC-GEN-CLOJURE --
;; Add the routes produced by Greeter->routes
(defn grpc-routes [xtdb-node] (reduce conj routes (proutes/->tablesyntax {:rpc-metadata api/rpc-metadata :interceptors common-interceptors :callback-context (GrpcAPI. xtdb-node) })))
(defn grpc-routes [xtdb-node] (reduce conj routes (proutes/->tablesyntax {:rpc-metadata api/rpc-metadata :interceptors common-interceptors :callback-context (GrpcAPI. xtdb-node)})))

(defn service [xtdb-node]
{:env :prod
::http/routes (grpc-routes xtdb-node)
(defn service [xtdb-node]
{:env :prod
::http/routes (grpc-routes xtdb-node)

;; -- PROTOC-GEN-CLOJURE --
;; We override the chain-provider with one provided by protojure.protobuf
;; and based on the Undertow webserver. This provides the proper support
;; for HTTP/2 trailers, which GRPCs rely on. A future version of pedestal
;; may provide this support, in which case we can go back to using
;; chain-providers from pedestal.
::http/type protojure.pedestal/config
::http/chain-provider protojure.pedestal/provider
::http/type protojure.pedestal/config
::http/chain-provider protojure.pedestal/provider

;;::http/host "localhost"
::http/port 8080})
::http/port 8080})
2 changes: 1 addition & 1 deletion src/gxtdb/utils.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns gxtdb.utils
(:require [clojure.instant :refer [read-instant-date]]
(:require [clojure.instant :refer [read-instant-date]]
[clojure.string :as str])
(:import java.text.SimpleDateFormat))

Expand Down
7 changes: 3 additions & 4 deletions test/gxtdb/service_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

(def xtdb-server (service/grpc-routes (xt/start-node *opts*)))


(def test-env (atom {}))

(defn create-service []
Expand All @@ -39,7 +38,7 @@
(use-fixtures :each wrap-service)
(deftest status-test
(testing "Status return xtdb with MemKv"
(is (=
(:kv-store @(Status @(connect {:uri (str "http://localhost:" (:port @test-env))}) {}))
"xtdb.mem_kv.MemKv"))))
(is (=
(:kv-store @(Status @(connect {:uri (str "http://localhost:" (:port @test-env))}) {}))
"xtdb.mem_kv.MemKv"))))

0 comments on commit 93ec63e

Please sign in to comment.