module Hakyll.Core.Store
( Store
, StoreGet (..)
, makeStore
, storeSet
, storeGet
) where
import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar_)
import System.FilePath ((</>))
import System.Directory (doesFileExist)
import Data.Maybe (fromMaybe)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Binary (Binary, encodeFile, decodeFile)
import Data.Typeable (Typeable, TypeRep, cast, typeOf)
import Hakyll.Core.Identifier
import Hakyll.Core.Util.File
data Storable = forall a. (Binary a, Typeable a) => Storable a
data StoreGet a = Found a
| NotFound
| WrongType TypeRep TypeRep
deriving (Show, Eq)
data Store = Store
{
storeDirectory :: FilePath
,
storeMap :: MVar (Map FilePath Storable)
}
makeStore :: FilePath -> IO Store
makeStore directory = do
mvar <- newMVar M.empty
return Store
{ storeDirectory = directory
, storeMap = mvar
}
addToMap :: (Binary a, Typeable a) => Store -> FilePath -> a -> IO ()
addToMap store path value =
modifyMVar_ (storeMap store) $ return . M.insert path (Storable value)
makePath :: Store -> String -> Identifier a -> FilePath
makePath store name identifier = storeDirectory store </> name
</> group </> toFilePath identifier </> "hakyllstore"
where
group = fromMaybe "" $ identifierGroup identifier
storeSet :: (Binary a, Typeable a)
=> Store -> String -> Identifier a -> a -> IO ()
storeSet store name identifier value = do
makeDirectories path
encodeFile path value
addToMap store path value
where
path = makePath store name identifier
storeGet :: forall a. (Binary a, Typeable a)
=> Store -> String -> Identifier a -> IO (StoreGet a)
storeGet store name identifier = do
map' <- readMVar $ storeMap store
case M.lookup path map' of
Just (Storable s) -> return $ case cast s of
Nothing -> WrongType (typeOf s) $ typeOf (undefined :: a)
Just s' -> Found s'
Nothing -> do
exists <- doesFileExist path
if not exists
then return NotFound
else do v <- decodeFile path
addToMap store path v
return $ Found v
where
path = makePath store name identifier