-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl-mango.lisp
278 lines (233 loc) · 10.6 KB
/
cl-mango.lisp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
;;; -*- mode: Lisp; Syntax: COMMON-LISP; Package: CL-MANGO -*-
(defpackage #:cl-mango
(:use #:cl
#:json-mop)
(:nicknames "mango" "MANGO")
(:import-from :alexandria :hash-table-keys
:alist-hash-table :when-let)
(:export *host*
*port*
*scheme*
*username*
*password*
*explain*
#:doc-put
#:doc-batch-put
#:doc-get
#:doc-find
#:doc-get-all
#:doc-delete
#:bulk-docs
#:query-view
#:make-selector
#:couch-query
#:unexpected-http-response
#:defmango))
(in-package #:cl-mango)
(eval-when (:compile-toplevel :load-toplevel)
(defvar *host* nil)
(defvar *port* nil)
(defvar *scheme* :http)
(defvar *username* nil)
(defvar *password* nil)
(defvar *explain* nil)
(setf drakma:*text-content-types* (list (cons "application" "json"))))
(defun make-request-uri (req-path)
(with-output-to-string (sink)
(puri:render-uri
(make-instance 'puri:uri :scheme *scheme*
:host *host*
:port *port*
:path req-path) sink)))
(define-condition unexpected-http-response ()
((status-code :initform nil
:initarg :status-code
:reader status-code)
(status-body :initform nil
:initarg :body
:reader status-body))
(:report (lambda (condition stream)
(format stream "~a ~a" (status-code condition) (status-body condition)))))
(defmacro couchdb-request (path &key
(parameters nil)
(content nil)
(method :get)
(content-type "application/json")
(accept "application/json")
(preserve-uri))
(alexandria:with-gensyms (body status warning)
`(multiple-value-bind (,body ,status)
(drakma:http-request (make-request-uri ,path)
:accept ,accept
:content-type ,content-type
:method ,method
:basic-authorization (list *username* *password*)
,@(when preserve-uri `(:preserve-uri t))
:external-format-in :utf8
:external-format-out :utf8
:connection-timeout 60
,@(when parameters `(:parameters ,parameters))
,@(when content `(:content ,content)))
(check-type ,status fixnum)
(if (not (member ,status (list 200 201)))
(error 'unexpected-http-response
:status-code ,status
:body ,body)
(progn ,body)))))
(defun doc-batch-put (db bundle)
(declare (type string db bundle))
(couchdb-request (format nil "/~a?batch=ok" db)
:method :post
:content bundle))
(defun doc-put (db bundle)
(declare (type string db bundle))
(couchdb-request (format nil "/~a" db)
:method :post
:content bundle))
(defun doc-get (db docid)
(declare (type string db docid))
(couchdb-request (format nil "/~a/~a" db docid)))
(defmacro make-selector (selector &key (limit 100) fields sort skip stale use-index r bookmark update stable execution-stats)
(let ((sink (gensym)))
`(with-output-to-string (,sink)
(yason:encode (alist-hash-table (list (cons "limit" ,limit)
,@(when skip
`((cons "skip" ,skip)))
,@(when sort
`((cons "sort" ,sort)))
,@(when fields
`((cons "fields" ,fields)))
,@(when execution-stats
`((cons "execution_stats" "true")))
,@(when stable
`(cons "stable" "true"))
,@(when stale
`(cons "stale" "true"))
,@(when update
`(cons "update" "true"))
,@(when bookmark
`(cons "bookmark" ,bookmark))
,@(when r
`(cons "r" ,r))
,@(when use-index
`(cons "use_index" ,use-index))
(cons "selector" (alist-hash-table ,selector))))
,sink))))
(defun doc-find (db query)
(declare (type string db query))
(couchdb-request (format nil "/~a/_find" db)
:method :post
:content query))
(defmacro couch-query (database selector &rest args)
`(doc-find ,database (make-selector ,selector ,@args)))
(defun doc-get-all (db &key (all-docs nil))
(let ((args (if all-docs
(format nil "/~a/_all_docs?include_docs=true" db)
(format nil "/~a/_all_docs" db))))
(couchdb-request args)))
(defun doc-delete (db docid revision)
(couchdb-request (format nil "/~a/~a?rev=~a" db docid revision)
:method :delete))
(defun bulk-docs (db bundle)
(couchdb-request (format nil "/~a/_bulk_docs" db)
:method :post
:content bundle))
(defmacro query-view (db view index parameters)
`(couchdb-request (format nil "/~a/_design/~a/_view/~a" ,db ,view ,index)
,@(when parameters `(:parameters ,parameters))
:method :get))
;;
;; Let's try for a better, less-orm-ish design.
;;
(defun symb (a b)
(intern (format nil "~a-~a" (symbol-name a) (symbol-name b))))
(defun %json-to-clos (bundle class &key (doc-name "docs"))
(check-type bundle string)
(mapcar (lambda (doc)
(json-mop:json-to-clos doc class))
(gethash doc-name (yason:parse bundle))))
(defun mango-get-all (db class)
(check-type class symbol)
(mapcar #'(lambda (doc)
(json-mop:json-to-clos (gethash "doc" doc) class))
(gethash "rows" (yason:parse (doc-get-all db)))))
(defun mango-find (db class query)
(check-type query list)
(%json-to-clos (doc-find db (make-selector query)) class))
(defun mango-update (db object)
(couchdb-request (format nil "/~a" db)
:method :post
:content (with-output-to-string (sink)
(json-mop:encode object sink))))
(defun allowed-slot-p (class name)
(declare (type symbol class)
(type string name))
(member name
(mapcar (lambda (slot)
(string-downcase
(symbol-name
(closer-mop:slot-definition-name slot))))
#+sbcl (sb-mop:class-direct-slots (find-class class))
#+lispworks (harlequin-common-lisp:class-direct-slots (find-class class))
#+ccl (ccl:class-direct-slots (find-class class)))
:test #'string=))
(defmacro defmango (name database slot-definitions)
(let* ((name-string (format nil "~a" name))
(name-symbol (intern (symbol-name name)))
(name-db-name (string-downcase database)))
`(progn
(defclass ,name () ((-id :initarg :-id
:json-type :string
:json-key "_id"
:accessor ,(symb name :-id))
(-rev :initarg :-rev
:json-type :string
:json-key "_rev"
:accessor ,(symb name :rev))
(type :initarg :type
:json-type :string
:json-key "type"
:initform (string-downcase ,name-string))
,@slot-definitions)
(:metaclass json-serializable-class))
(defun ,(symb name 'get-all) ()
(mango-find ,name-db-name
',name
(list (cons "type" (string-downcase ,name-string)))))
(defun ,(symb name 'get) (id)
(json-mop:json-to-clos (doc-get ,name-db-name id) ',name-symbol))
(defun ,(symb name 'put) (object)
(mango-update ,name-db-name object))
(defun ,(symb name 'update) (object)
(mango-update ,name-db-name object))
(defmacro ,(symb name 'find) (query &rest query-args)
`(let ((query-slots (mapcar #'car ,query)))
(if (remove-if #'null (mapcar #'(lambda (slot-name)
(allowed-slot-p ',',name-symbol
slot-name))
query-slots))
(let* ((new-query (append (list (cons "type"
(string-downcase
,',name-string)))
,query))
(selector (make-selector new-query
,@(when query-args
`(,@query-args)))))
(mapcar #'(lambda (doc)
(json-mop:json-to-clos doc ',',name-symbol))
(gethash "docs"
(yason:parse
(doc-find ,',name-db-name
selector))))))))
(defun ,(symb name 'delete) (object)
(doc-delete ,name-db-name (,(symb name :-id) object) (,(symb name :rev) object)))
(defun ,(symb name 'from-json) (string)
(json-mop:json-to-clos string ',name-symbol))
(defun ,(symb name 'to-json) (object)
(with-output-to-string (sink)
(json-mop:encode object sink)))
(defmacro ,(symb name 'create) (&rest args)
(alexandria:with-gensyms (new-instance result)
`(let* ((,new-instance (make-instance ',',name-symbol ,@args))
(,result (,',(symb name :put) ,new-instance)))
(gethash "id" (yason:parse ,result))))))))