Skip to content

Instantly share code, notes, and snippets.

@zerokarmaleft
Created August 22, 2014 22:47
Show Gist options
  • Save zerokarmaleft/69f69b67dd77d586a2e7 to your computer and use it in GitHub Desktop.
Save zerokarmaleft/69f69b67dd77d586a2e7 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric
, FlexibleInstances
, OverloadedStrings
, ScopedTypeVariables
, TypeSynonymInstances #-}
module Main where
import Control.Monad
import Data.Aeson.Types
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.Builder as BSB
import qualified Data.Map as Map
import Data.Maybe
import GHC.Generics
import qualified Network.Riak as Riak
-- Books
--
data Book = Book
{ isbn :: String
, title :: String
, author :: String
, body :: String
, copies_owned :: Int
} deriving (Show, Generic)
instance ToJSON Book
instance FromJSON Book
bookKey :: Book -> BSL.ByteString
bookKey = BSB.toLazyByteString . BSB.stringUtf8 . isbn
-- Resolvers (simplest possible)
--
instance Riak.Resolvable Book where
resolve = const
instance Riak.Resolvable Int where
resolve = const
instance Riak.Resolvable String where
resolve = const
instance (Riak.Resolvable k, Riak.Resolvable v) => Riak.Resolvable (Map.Map k v) where
resolve = const
-- API helpers
-- tune N/R/W with low-level functions when necessary
get conn bucket key =
Riak.get conn bucket key Riak.Default
put conn bucket key val =
Riak.put conn bucket key Nothing val Riak.Default Riak.Default
modify conn bucket key f =
Riak.modify conn bucket key Riak.Default Riak.Default Riak.Default f
modify_ conn bucket key f =
Riak.modify_ conn bucket key Riak.Default Riak.Default Riak.Default (return . fromJust . liftM f)
delete conn bucket key =
Riak.delete conn bucket key Riak.Default
-- Sample data
--
book = Book
{ isbn = "1111979723"
, title = "Moby Dick"
, author = "Herman Melville"
, body = "Call me Ishmael. Some years ago..."
, copies_owned = 3
}
main :: IO ()
main =
do conn <- Riak.connect Riak.Client { Riak.host="127.0.0.1"
, Riak.port="49161"
, Riak.clientID="example"
}
-- ensure buckets are empty from previous runs
delete conn "test" "one"
delete conn "test" "two"
delete conn "test" "three"
delete conn "books" (bookKey book)
-- simple puts
let val1 = 1 :: Int
val2 = "two" :: String
val3 = Map.fromList [("myValue", 3)] :: Map.Map String Int
put conn "test" "one" val1
put conn "test" "two" val2
put conn "test" "three" val3
-- simple gets
fetched1 <- get conn "test" "one"
fetched2 <- get conn "test" "two"
fetched3 <- get conn "test" "three"
case fetched1 of
Just (val :: Int,_) -> putStrLn $ show (val1 == val)
Nothing -> putStrLn "Key was not found"
case fetched2 of
Just (val :: String,_) -> putStrLn $ show (val2 == val)
Nothing -> putStrLn "Key was not found"
case fetched3 of
Just (val :: Map.Map String Int,_) -> putStrLn $ show (val3 == val)
nothing -> putStrLn "Key was not found"
-- simple modification
modify_ conn "test" "three"
(Map.adjust (const (42 :: Int)) ("myValue" :: String))
-- simple deletes
delete conn "test" "one"
delete conn "test" "two"
-- delete conn "test" "three"
-- putting complex data
put conn "books" (bookKey book) book
delete conn "books" (bookKey book)
Riak.disconnect conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment