Skip to content

Instantly share code, notes, and snippets.

@fmw
Last active December 15, 2015 01:39
Show Gist options
  • Save fmw/5181316 to your computer and use it in GitHub Desktop.
Save fmw/5181316 to your computer and use it in GitHub Desktop.
(ns riaktest.core
(:require [clojure.string :as string]
[clj-time.core :as time-core]
[clj-time.format :as time-format]
[clojurewerkz.welle.core :as wc]
[clojurewerkz.welle.mr :as mr]
[clojurewerkz.welle.kv :as wkv]
[clojurewerkz.welle.buckets :as wbuckets]))
(wc/connect!)
;;(load "riaktest/core")(require '[riaktest.core :as rt])
;;(require '[clojurewerkz.welle.kv :as wkv])
;;(require '[clojurewerkz.welle.buckets :as wbuckets])
;; create the bucket like this:
;;(wbuckets/update "pages")
;; add a dummy page with 5 different retrieval states:
(comment
(dotimes [n 5]
(let [datestamp (rfc3339-datestamp!)]
(rt/store-page! "pages"
"http://example.com/"
datestamp
{:uri "http://example.com/"
:datestamp datestamp
:value (str n)})
(Thread/sleep 2000))))
;; utility fn to get rid of test data:
(comment
(wkv/delete-all "pages" (wbuckets/keys-in "pages")))
(defn rfc3339-datestamp!
"Returns the RFC3339 current time (e.g. 2012-07-09T09:31:01.579Z)."
[]
(time-format/unparse
(time-format/formatters :date-time)
(time-core/now)))
(defn store-page!
"Stores the provided value in given bucket, using a key which starts
with the uri argument, which is followed the ^ character and
finally an RFC3339 datestamp."
[bucket uri datestamp value]
;; application/clojure is another possible :content-type,
;; but pr-str with text/plain gives much cleaner results
;; (i.e. a string value of {:a :b} instead of
;; "#=(clojure.lang.PersistentArrayMap/create {:a :b})"
(wkv/store bucket
(str uri "^" datestamp)
(pr-str value)
:content-type "text/plain"))
(def kv-js-fn "function(obj) { return [[obj.key, obj.values[0].data]];}")
(defn get-page-states
"Accepts a riak bucket and the uri for the requested page. Returns a
sequence of pairs, with the first element in each pair being the
datestamp for the individual state and the second element its
value."
[bucket uri]
(map (fn [[k v]]
[(second (string/split k #"\^"))
(read-string v)])
(mr/map-reduce
{:inputs {:bucket bucket
:key_filters [["starts_with" (str uri "^")]]}
:query [{:map {:language "javascript"
:source kv-js-fn
:keep false}}
{:reduce {:language "erlang"
:module "riak_kv_mapreduce"
:function "reduce_sort"}}]})))
;; example values returned by the get-page-states fn:
(comment
[["2013-03-17T12:23:01.036Z"
{:uri "http://example.com/",
:datestamp "2013-03-17T12:23:01.036Z",
:value "0"}]
["2013-03-17T12:23:03.041Z"
{:uri "http://example.com/",
:datestamp "2013-03-17T12:23:03.041Z",
:value "1"}]
["2013-03-17T12:23:05.049Z"
{:uri "http://example.com/",
:datestamp "2013-03-17T12:23:05.049Z",
:value "2"}]
["2013-03-17T12:23:07.056Z"
{:uri "http://example.com/",
:datestamp "2013-03-17T12:23:07.056Z",
:value "3"}]
["2013-03-17T12:23:09.064Z"
{:uri "http://example.com/",
:datestamp "2013-03-17T12:23:09.064Z",
:value "4"}]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment