This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcl-riak.lisp
50 lines (44 loc) · 2.25 KB
/
cl-riak.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
;;;; cl-riak.lisp
;;;; Copyright (c) 2011, Brian Hetro <[email protected]>
(in-package #:cl-riak)
;;; "cl-riak" goes here. Hacks and glory await!
(setf drakma:*text-content-types* '(("text" . nil) ("application" . "json")))
(defun get (key &key bucket (server "localhost:8098"))
(let ((request-url (concatenate 'string "http://" server "/riak/" (url-encode bucket :utf-8) "/" (url-encode key :utf-8))))
(multiple-value-bind (response status headers)
(drakma:http-request request-url)
(let ((vclock (cdr (assoc :x-riak-vclock headers))))
(when (= status 200) (values response vclock))))))
(defun mapred (mapreduce &key (server "localhost:8098"))
(let ((request-url (concatenate 'string "http://" server "/mapred")))
(multiple-value-bind (response status headers)
(drakma:http-request request-url
:method :post
:content-type "application/json"
:content mapreduce)
(when (= status 200) response))))
(defun delete (key &key bucket (server "localhost:8098"))
(let ((request-url (concatenate 'string "http://" server "/riak/" (url-encode bucket :utf-8) "/" (url-encode key :utf-8))))
(multiple-value-bind (response status headers)
(drakma:http-request request-url
:method :delete)
(cond ((or (= status 204)
(= status 404)) t)
(t nil)))))
(defun set (key value &key bucket (server "localhost:8098") (content-type "text/plain") vclock)
(let ((request-url (concatenate 'string "http://" server "/riak/" (url-encode bucket :utf-8)
(when key (concatenate 'string "/" (url-encode key :utf-8))))))
(multiple-value-bind (response status headers)
(drakma:http-request request-url
:method (if key :put :post)
:content-type content-type
:content value
:parameters '(("returnbody" . "true"))
:additional-headers (when vclock '(("X-Riak-Vclock" . vclock))))
(let* ((vclock (cdr (assoc :x-riak-vclock headers)))
(location (cdr (assoc :location headers)))
(key-name (lastcar (split-sequence #\/ location))))
(cond ((= status 200) (values response vclock)) ; key = value
((= status 201) (values key-name bucket)) ; key is nil; return the key name and the bucket
((= status 204) t) ; "No Content". Used when returnbody is false
(t nil))))))