Skip to content

Instantly share code, notes, and snippets.

@nkpart
Created May 17, 2011 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkpart/977611 to your computer and use it in GitHub Desktop.
Save nkpart/977611 to your computer and use it in GitHub Desktop.
Riak mapreduce with haskell problem
{-# LANGUAGE OverloadedStrings #-}
module TestRiak where
import qualified Network.Riak as R
import qualified Network.Riak.Protocol.MapReduce as R
import qualified Network.Riak.Types as R
import Data.Monoid
import Data.Aeson
import qualified Data.Vector as V
(<>) :: Monoid a => a -> a -> a
(<>) = mappend
-- I use this function to grab all the phases from a job. I'm not sure if this is right thing to do.
generateWhile :: (a -> Bool) -> IO a -> IO [a]
generateWhile p a = loop a [] where
loop a vs = do
v <- a
if p v then do
loop a (v:vs)
else
return (v:vs)
uuid1 = "cc4c94f3-b0ba-49cd-bd94-317653a5fd17"
uuid2 = "c5d657f3-5944-4849-aebf-a99519e6767e"
main :: IO ()
main = do
r <- R.connect R.defaultClient
let findUuid uuid = R.mapReduce r $ R.JSON . encode $ object [
"inputs" .= String "events",
"query" .= (Array $ V.fromList $ [ object [
"map" .= object [
"language" .= String "javascript",
"source" .= String ("function(value, keyData, arg) { var data = Riak.mapValuesJson(value)[0]; if (data.uuid == \"" <> uuid <> "\") { return [data]; } else { return [] } }"),
"keep" .= False
]
],
object [
"reduce" .= object [
"language" .= String "javascript",
"source" .= String ("function (values) { return values; }"),
"keep" .= True
]
]
])
]
print =<< generateWhile (maybe True (not . id) . R.done) (findUuid uuid1)
print =<< generateWhile (maybe True (not . id) . R.done) (findUuid uuid2)
-- Instead of seeing 2 different objects coming back, I'm seeing the results from the second query being printed out twice.
-- I suspect there's something going on here with laziness, but I can't work it out. I've splattered some seq's around the place but it didn't change anything.
@OJ
Copy link

OJ commented May 17, 2011

Yeah this looks very Haskell client specific to me dude. I think you should get into #riak on freenode and ask Bryan. He'd be able to sort that out pronto me thinks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment