Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Riak driver: Making a simple 'put' operation

Tags:

haskell

riak

I am trying to introduce myself to Riak with Haskell driver and I am stuck with a simple put operation. I am confused with the signature of the put function. and there isn't a single example anywhere out there.

So with this signature:

put :: (FromJSON c, ToJSON c, Resolvable c) => Connection -> Bucket 
       -> Key -> Maybe VClock -> c -> W -> DW -> IO (c, VClock)

I have a couple of questions.

What is a Maybe VClock? Do I have to generate it somehow or is it enough to just specify Nothing there? And why do I get this VClock back in the returned tuple?

Do I have to write FromJSON and ToJSON instances for every simple value I put even if it is a simple string value? Like if I want to put a value "Stitch" with the key "Name", how do I do it?

What is Resolvable instance? How do I make a Text or String value resolvable? I understand that I have to define the resolve function but I don't quite get what it means and how to do it.

like image 455
r.sendecky Avatar asked Apr 22 '14 04:04

r.sendecky


1 Answers

Maybe VClock is either the VClock of the object you are updating (Just vclock) or Nothing. You can specify Nothing here if there is no object for this key already in the database.

The VClock wrapped in IO (IO (c, VClock)) is the VClock of the inserted object, returned from the database.

Your datatype will need a ToJSON and a FromJSON instance to use that put function. Aeson has instructions for writing this automatically. You can store non-JSON data by using the Network.Riak.Value modules, but this is more complicated.

Resolvable is used for resolving two siblings and looks like this where a and b are MyDataTypes:

instance Resolvable MyDataType where resolve a b = a

Ideally your resolve function does something more intelligent than just picking a or b as the ordering is not guaranteed and may be called multiple times if there are multiple siblings.

here is some code for creating a pool of Riak connections you may find useful.

You will end up with something that looks like this:

put conn bucket key Nothing value Quorum Quorum

where value is your data. More on Quorum here.

like image 86
Chris Biscardi Avatar answered Nov 15 '22 07:11

Chris Biscardi