michaelklishin / welle

An expressive Clojure client for Riak with batteries included
http://clojureriak.info
92 stars 26 forks source link

Added skip-deserialize parameter to fetch and fetch-one #6

Closed jegt closed 11 years ago

jegt commented 11 years ago

Allows for bypassing the deserialization. Useful for when you have data with an unsupported content type or you just don't want the overhead of deserializing.

I'm new to clojure so I'm sure some of this could be done better, let me know if I should change anything.

michaelklishin commented 11 years ago

Thanks. I'm not against having this option but just curious, what kind of use case do you have? Maybe extending the deserializing multimethod will work? Or multimethod performance is not good enough for you?

Also, this option is not listed in the docs.

jegt commented 11 years ago

What made me start looking at this is we have a Riak cluster full of gziped json with the content type application/x-gzip. The service currently storing that data is written in Python but we are looking at maybe replacing it with one in Clojure so I tried getting some of the data using Welle and got an exception. We also have a situation where there is no real point in unpacking and parsing the data before sending it on to calling application, that would just waste cpu since we do not do anything with it in the service. So we want the raw data.

As I said, I'm new to clojure. Is there a way to add our own handler for our content type in our code without making changes to Welle? I still think an skip-deserialize option makes sense but that would solve our immediate problem.

I'll update the docs.

michaelklishin commented 11 years ago

There is. Serialization and deserialization implemented a Clojure feature called multimethods. They are extensible polymorphic functions.

So, to extend clojurewerkz.welle.conversion/deserialize for your custom content type, create a new namespace (it can be named anything, say, mycompany.myservice.welle), require clojurewerkz.welle.conversions and define one more implementation:

(ns myservice.welle
  (:require [clojurewerkz.welle.conversion :as cnv]))

(defmethod cnv/deserialize "application/x-gzip"
  [value _]
  (deserialize "application/json+gzip" value))

this will delegate to the existing implementation that assumes content type for gzipped JSON is "application/json+gzip".

I think this feature is fine, even though the fetch function already has a lot of options.

michaelklishin commented 11 years ago

Forgot to include a link to Clojure multimethods documentation

michaelklishin commented 11 years ago

@jegt Welle 1.5.0-beta1 is up on Clojars with this change. Thank you.

jegt commented 11 years ago

Thanks!