-
Notifications
You must be signed in to change notification settings - Fork 26
/
core.clj
134 lines (110 loc) · 4.17 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
;; Copyright (c) 2012-2014 Michael S. Klishin
;;
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns clojurewerkz.welle.core
(:import com.basho.riak.client.raw.RawClient
[com.basho.riak.client.raw.http HTTPClientConfig HTTPClientConfig$Builder
HTTPClusterClient HTTPClusterConfig]
[com.basho.riak.client.raw.pbc PBClientAdapter PBClusterClient
PBClusterConfig PBClientConfig$Builder]
com.basho.riak.client.raw.config.ClusterConfig
clojurewerkz.welle.HTTPClient))
;;
;; API
;;
(def ^{:private true :const true} default-host "127.0.0.1")
(def ^{:private true :const true} default-http-port 8098)
(def ^{:private true :const true} default-pb-port 8087)
(def ^{:private true :const true} default-url "http://127.0.0.1:8098/riak")
(def ^{:const true} default-cluster-connection-limit 32)
(defn ^HTTPClient
connect
"Creates an HTTP client for a given URL, optionally with a custom client ID.
With no arguments, connects to localhost on the default Riak port."
([]
(connect default-url))
([^String url]
(doto (HTTPClient. (com.basho.riak.client.http.RiakClient. ^String url))
(.generateAndSetClientId)))
([^String url ^bytes client-id]
(let [^HTTPClient c (connect url)]
(.setClientId c client-id)
c)))
(defn ^PBClientAdapter connect-via-pb
"Creates a Protocol Buffers client for the given host and port, or, by
default, to localhost on the default Riak PB port."
([]
(connect-via-pb default-host default-pb-port))
([^String host ^long port]
(doto (PBClientAdapter. (com.basho.riak.pbc.RiakClient. host port))
(.generateAndSetClientId))))
(defprotocol HTTPClusterConfigurator
(http-cluster-config-from [self]))
(extend-type HTTPClusterConfig
HTTPClusterConfigurator
(http-cluster-config-from [self] self))
(extend-type java.util.Collection
HTTPClusterConfigurator
(http-cluster-config-from [endpoints]
(let [res (HTTPClusterConfig. default-cluster-connection-limit)]
(doseq [^String endpoint endpoints]
(.addClient res (-> (HTTPClientConfig$Builder.)
(.withUrl endpoint)
(.build))))
res)))
(defn ^RawClient
connect-to-cluster
"Creates an HTTP cluster client."
[endpoints]
(let [^ClusterConfig cc (http-cluster-config-from endpoints)]
(HTTPClusterClient. cc)))
(defprotocol PBClusterConfigurator
(pbc-cluster-config-from [self]))
(extend-type PBClusterConfig
PBClusterConfigurator
(pbc-cluster-config-from [self] self))
(extend-type java.util.Collection
PBClusterConfigurator
(pbc-cluster-config-from [endpoints]
(let [res (PBClusterConfig. default-cluster-connection-limit)]
(doseq [^String endpoint endpoints]
(let [[host port-str] (seq (.split endpoint ":" 2))
port (when port-str (Integer/parseInt port-str))]
(.addClient res (-> (PBClientConfig$Builder.)
(.withHost host)
(.withPort (or port
default-pb-port))
(.build)))))
res)))
(defn ^PBClusterClient
connect-to-cluster-via-pb
"Creates a Protocol Buffers cluster client given a sequence of string
endpoints."
[endpoints]
(let [^ClusterConfig cc (pbc-cluster-config-from endpoints)]
(PBClusterClient. cc)))
(defn ping
"Pings a client."
[^RawClient client]
(.ping client))
(defn shutdown
"Shuts down a client."
[^RawClient client]
(.shutdown client))
(defn get-client-id
"The client ID used by a given client."
[^RawClient client]
(.getClientId client))
(defn stats
"Returns statistics for a client."
[^RawClient client]
(.stats client))
(defn get-base-url
"Returns base HTTP transport URL (e.g. http://127.0.0.1:8098)"
[^HTTPClient client]
(.getBaseUrl client))