--------------------------------------------------------------------------------
module Hakyll.Core.Provider.MetadataCache
    ( resourceMetadata
    , resourceBody
    , resourceInvalidateMetadataCache
    ) where


--------------------------------------------------------------------------------
import           Control.Monad                 (unless)
import           Hakyll.Core.Identifier
import           Hakyll.Core.Metadata
import           Hakyll.Core.Provider.Internal
import           Hakyll.Core.Provider.Metadata
import qualified Hakyll.Core.Store             as Store


--------------------------------------------------------------------------------
resourceMetadata :: Provider -> Identifier -> IO Metadata
resourceMetadata :: Provider -> Identifier -> IO Metadata
resourceMetadata Provider
p Identifier
r
    | Bool -> Bool
not (Provider -> Identifier -> Bool
resourceExists Provider
p Identifier
r) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty
    | Bool
otherwise                = do
        -- TODO keep time in md cache
        Provider -> Identifier -> IO ()
load Provider
p Identifier
r
        Store.Found (BinaryMetadata Metadata
md) <- forall a.
(Binary a, Typeable a) =>
Store -> [String] -> IO (Result a)
Store.get (Provider -> Store
providerStore Provider
p)
            [String
name, Identifier -> String
toFilePath Identifier
r, String
"metadata"]
        forall (m :: * -> *) a. Monad m => a -> m a
return Metadata
md


--------------------------------------------------------------------------------
resourceBody :: Provider -> Identifier -> IO String
resourceBody :: Provider -> Identifier -> IO String
resourceBody Provider
p Identifier
r = do
    Provider -> Identifier -> IO ()
load Provider
p Identifier
r
    Store.Found Maybe String
bd <- forall a.
(Binary a, Typeable a) =>
Store -> [String] -> IO (Result a)
Store.get (Provider -> Store
providerStore Provider
p)
        [String
name, Identifier -> String
toFilePath Identifier
r, String
"body"]
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Provider -> Identifier -> IO String
resourceString Provider
p Identifier
r) forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
bd


--------------------------------------------------------------------------------
resourceInvalidateMetadataCache :: Provider -> Identifier -> IO ()
resourceInvalidateMetadataCache :: Provider -> Identifier -> IO ()
resourceInvalidateMetadataCache Provider
p Identifier
r = do
    Store -> [String] -> IO ()
Store.delete (Provider -> Store
providerStore Provider
p) [String
name, Identifier -> String
toFilePath Identifier
r, String
"metadata"]
    Store -> [String] -> IO ()
Store.delete (Provider -> Store
providerStore Provider
p) [String
name, Identifier -> String
toFilePath Identifier
r, String
"body"]


--------------------------------------------------------------------------------
load :: Provider -> Identifier -> IO ()
load :: Provider -> Identifier -> IO ()
load Provider
p Identifier
r = do
    Bool
mmof <- Store -> [String] -> IO Bool
Store.isMember Store
store [String]
mdk
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
mmof forall a b. (a -> b) -> a -> b
$ do
        (Metadata
md, Maybe String
body) <- Provider -> Identifier -> IO (Metadata, Maybe String)
loadMetadata Provider
p Identifier
r
        forall a. (Binary a, Typeable a) => Store -> [String] -> a -> IO ()
Store.set Store
store [String]
mdk (Metadata -> BinaryMetadata
BinaryMetadata Metadata
md)
        forall a. (Binary a, Typeable a) => Store -> [String] -> a -> IO ()
Store.set Store
store [String]
bk  Maybe String
body
  where
    store :: Store
store = Provider -> Store
providerStore Provider
p
    mdk :: [String]
mdk   = [String
name, Identifier -> String
toFilePath Identifier
r, String
"metadata"]
    bk :: [String]
bk    = [String
name, Identifier -> String
toFilePath Identifier
r, String
"body"]


--------------------------------------------------------------------------------
name :: String
name :: String
name = String
"Hakyll.Core.Resource.Provider.MetadataCache"