couch-hs / source / Database / CouchDB / ViewServer / Reduce.hs

{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
{-# OPTIONS_HADDOCK prune #-}

module Database.CouchDB.ViewServer.Reduce
      -- * Map Functions
    , ViewReduce

    -- * JSON Parsing
    , module Database.CouchDB.ViewServer.Parse

    -- * ViewReduce Monads
    , logMsg

    , ReduceOutput
    , ReduceFunc
    , toReduceFunc
    , reduceFuncInterpreter
    , execReduceFunc
    ) where

import Data.Maybe
import Data.Typeable
import Data.Aeson (toJSON, ToJSON)
import Data.Aeson.Types (Value(..), Object, Parser, parseMaybe)

import Control.Applicative
import Control.Monad (Monad, MonadPlus)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Writer (WriterT, tell, runWriterT)
import qualified Language.Haskell.Interpreter as H

import Database.CouchDB.ViewServer.Internal
import Database.CouchDB.ViewServer.Parse

type ReduceOutput = (Value, [LogMessage])

{- | The monad within which a reduce computation takes place. This is a
     transformation of the 'Data.Aeson.Types.Parser' monad, which is accessible
     through the 'MonadParser' typeclass.
newtype ViewReduce a = ViewReduce { runViewReduce :: WriterT [LogMessage] Parser a }
    deriving(Monad, Functor, MonadPlus, Applicative, Alternative)

instance MonadParser ViewReduce where
    liftParser = ViewReduce . lift

{- | The type of your reduce functions as they are stored in CouchDB. The trivial

   > \keys values rereduce -> return Null
type ReduceSignature a = [Value] -> [Value] -> Bool -> ViewReduce a

newtype ReduceFunc = ReduceFunc { runReduceFunc :: ReduceSignature Value }
    deriving (Typeable)

toReduceFunc :: ToJSON a => ReduceSignature a -> ReduceFunc
toReduceFunc f = ReduceFunc $ \k v r -> toJSON <$> f k v r

reduceFuncInterpreter :: [H.OptionVal H.Interpreter] -> [(H.ModuleName, Maybe String)] -> String -> H.Interpreter ReduceFunc
reduceFuncInterpreter opts mods source = do
    H.set opts
    H.setImportsQ $ mods ++ [("Database.CouchDB.ViewServer.Reduce", Nothing)]
    H.interpret ("toReduceFunc " ++ H.parens source) ( :: ReduceFunc)

execReduceFunc :: ReduceFunc -> [Value] -> [Value] -> Bool -> ReduceOutput
execReduceFunc reduceFunc keys values rereduce = fromMaybe (Null, []) $ parseMaybe runWriterT (runViewReduce $ runReduceFunc reduceFunc keys values rereduce)

{- | Send a log message to the CouchDB server. Note that log messages are only
     sent if the computation succeeds. If you want to log a message in the event
     of a failure, look at 'Control.Applicative.Alternative'.
logMsg :: String -> ViewReduce ()
logMsg msg = ViewReduce $ tell [LogMessage msg]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.