Haskell Riak driver: Making a simple 'put' operation

haskell, riak

Solution

I hope following code of simple put and get operation may help you:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE UndecidableInstances #-}

module Main where
import Data.Aeson
import qualified Network.Riak as Riak 
import Network.Riak.Types
import Data.ByteString.Char8 hiding (putStrLn)
import Data.ByteString.Lazy as L hiding (pack,putStrLn)
import GHC.Generics hiding (R)

-- convert String to lazy ByteString
toBS :: String -> L.ByteString
toBS str = L.fromChunks (pack (str):[])

putBucket :: Bucket
putBucket = toBS "Bucket"

putKey :: Key
putKey = toBS "key"

r :: R
r = Default

w :: W
w = Default

dw :: DW
dw = Default

rw :: RW
rw = Default 

-- declare a data and create its instances
data Coord = Coord { x :: Double, y :: Double } deriving (Generic, Show)

instance FromJSON Coord

instance ToJSON Coord

instance (Show Coord) => Riak.Resolvable Coord where
    resolve a b = a

value :: Coord
value = Coord {
  x = 2.2,
  y = 3.3
}


main :: IO ()
main = do
    -- establish a connection
    let client = Riak.defaultClient
    con <- Riak.connect client
    -- simple put operation
    put_ret <- (Riak.put con putBucket putKey Nothing value w dw)
    putStrLn (show put_ret)
    buckets <- Riak.listBuckets con
    print buckets
    -- simple get operation
    get_ret <- (Riak.get con putBucket putKey r) :: IO ( Maybe (Coord, VClock))
    putStrLn (show get_ret)
    -- delete a value
    Riak.delete con putBucket putKey rw
    -- if try to get that value we will find "Nothing"
    get_ret <- (Riak.get con putBucket putKey r) :: IO ( Maybe (Coord, VClock))
    putStrLn (show get_ret)
    -- print the final bucket list
    buckets <- Riak.listBuckets con
    print buckets

Problem

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.

Original source