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) = Metadata -> IO Metadata
forall (m :: * -> *) a. Monad m => a -> m a
return Metadata
forall a. Monoid a => a
mempty
| Bool
otherwise = do
Provider -> Identifier -> IO ()
load Provider
p Identifier
r
Store.Found (BinaryMetadata Metadata
md) <- Store -> [String] -> IO (Result BinaryMetadata)
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"]
Metadata -> IO 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 <- Store -> [String] -> IO (Result (Maybe String))
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"]
IO String -> (String -> IO String) -> Maybe String -> IO String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Provider -> Identifier -> IO String
resourceString Provider
p Identifier
r) String -> IO String
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
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
mmof (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
(Metadata
md, Maybe String
body) <- Provider -> Identifier -> IO (Metadata, Maybe String)
loadMetadata Provider
p Identifier
r
Store -> [String] -> BinaryMetadata -> IO ()
forall a. (Binary a, Typeable a) => Store -> [String] -> a -> IO ()
Store.set Store
store [String]
mdk (Metadata -> BinaryMetadata
BinaryMetadata Metadata
md)
Store -> [String] -> Maybe String -> IO ()
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"