Skip to content

Commit b94e80d

Browse files
author
Paula Gearon
committed
Configuring storage
1 parent ae15137 commit b94e80d

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/naga/cli.clj

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@
1111
[naga.data :as data]
1212
[naga.storage.memory.core])
1313
(:import [clojure.lang ExceptionInfo]
14+
[java.net URI]
1415
[java.io File]))
1516

16-
(def stores (map name (keys @store/registered-stores)))
17+
(def stores (set (map name (keys @store/registered-stores))))
1718

1819
(def valid-output? #(.exists (.getParentFile (.getAbsoluteFile (File. %)))))
1920
(def valid-input? #(.exists (File. %)))
2021

22+
(def uri #(try (URI. %) (catch Exception _)))
23+
2124
(def cli-options
2225
[[nil "--storage STRING" "Select store type"
23-
:validate [(set stores) "Must be a registered storage type."]]
26+
:validate [stores "Must be a registered storage type."]]
27+
[nil "--uri STRING" "URI for storage"
28+
:parse-fn uri
29+
:validate [identity "Invalid storage URL"]]
30+
[nil "--init STRING" "Initialization data"
31+
:validate [valid-input? "Invalid initialization data file"]]
2432
[nil "--json STRING" "Filename for input json"
2533
:validate [valid-input? "Input file does not exist."]]
2634
[nil "--out STRING" "Filename for output json"
@@ -43,6 +51,19 @@
4351
(str "Store types: " (vec stores))
4452
""]))
4553

54+
(defn storage-configuration
55+
"Reads storage parameters, and builds an appropriate configuration structure"
56+
[storage uri init]
57+
(let [store-from-uri (fn [u]
58+
;; may want this to be more complex in future
59+
(let [s (.getScheme u)]
60+
(stores s)))
61+
store-type (and storage (store-from-uri uri))]
62+
(when (and uri (nil? store-type)) (exit "Unable to determine storage type for: " uri))
63+
{:type (or store-type :memory)
64+
:uri uri
65+
:init init}))
66+
4667
(defn run-all
4768
"Runs a program, and returns the data processed, the results, and the stats.
4869
Takes an input stream. Returns a map of:
@@ -101,12 +122,11 @@
101122

102123
(defn json-program
103124
"Runs a program over data in a JSON file"
104-
[in-stream json-file out-file storage]
125+
[in-stream json-file out-file storage uri]
105126
(when-not out-file
106127
(exit 2 "No output json file specified"))
107-
(let [; TODO: handle storage URIs to determine type and connection
108-
; fresh-store (instantiate-storage storage)
109-
fresh-store (store/get-storage-handle {:type :memory})
128+
(let [store-config (storage-configuration storage uri)
129+
fresh-store (store/get-storage-handle store-config)
110130
{:keys [rules axioms]} (pabu/read-stream in-stream)
111131

112132
basic-store (store/assert-data fresh-store axioms)
@@ -125,15 +145,15 @@
125145

126146
(defn -main [& args]
127147
(try
128-
(let [{{:keys [halp json out storage]} :options,
148+
(let [{{:keys [halp json out storage uri]} :options,
129149
arguments :arguments :as opts} (parse-opts args cli-options)]
130150

131151
(when halp (exit 1 (usage opts)))
132152
(with-open [in-stream (if-let [filename (first arguments)]
133153
(io/input-stream filename)
134154
*in*)]
135155
(if json
136-
(json-program in-stream json out storage)
156+
(json-program in-stream json out storage uri)
137157
(logic-program in-stream))))
138158
(catch ExceptionInfo e
139159
(binding [*out* *err*]

src/naga/data.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(def Triple [s/Any s/Keyword s/Any])
1414

1515
(defn get-naga-first
16-
"Finds the naga/first property, in a map, and gets the value."
16+
"Finds the naga/first property in a map, and gets the value."
1717
[struct]
1818
(let [first-val? (fn [[k v]]
1919
(and (= "naga" (namespace k))
@@ -36,7 +36,9 @@
3636
(let [{r :naga/rest :as lm} (listmap n)
3737
f (get-naga-first lm)]
3838
(recur (conj nl f) r))))]
39-
(map (fn [n] [node (store/container-property *current-storage* n) n]) node-list)))
39+
(map
40+
(fn [n] [node (store/container-property *current-storage* n) n])
41+
node-list)))
4042

4143
(defmulti value-triples
4244
"Converts a value into a list of triples.

0 commit comments

Comments
 (0)