@@ -1,5 +1,6 @@
1-- | A store for stroing and retreiving items
1-- | A store for stroing and retreiving items
3+{-# LANGUAGE ExistentialQuantification #-}
3module Hakyll.Core.Store
4module Hakyll.Core.Store
5 , makeStore
6 , makeStore
@@ -14,20 +15,22 @@ import Data.Map (Map)
14import qualified Data.Map as M
15import qualified Data.Map as M
16import Data.Binary (Binary, encodeFile, decodeFile)
17import Data.Binary (Binary, encodeFile, decodeFile)
17−import Data.Typeable (Typeable)
18+import Data.Typeable (Typeable, cast)
19−import Hakyll.Core.CompiledItem
20−import Hakyll.Core.Writable
21import Hakyll.Core.Identifier
20import Hakyll.Core.Identifier
22import Hakyll.Core.Util.File
21import Hakyll.Core.Util.File
23+-- | Items we can store
25+data Storable = forall a. (Binary a, Typeable a) => Storable a
24-- | Data structure used for the store
27-- | Data structure used for the store
26data Store = Store
29data Store = Store
27 { -- | All items are stored on the filesystem
30 { -- | All items are stored on the filesystem
28 storeDirectory :: FilePath
31 storeDirectory :: FilePath
29 , -- | And some items are also kept in-memory
32 , -- | And some items are also kept in-memory
30− storeMap :: MVar (Map FilePath CompiledItem)
33+ storeMap :: MVar (Map FilePath Storable)
33-- | Initialize the store
36-- | Initialize the store
@@ -42,10 +45,9 @@ makeStore directory = do
43-- | Auxiliary: add an item to the map
46-- | Auxiliary: add an item to the map
45−addToMap :: (Binary a, Typeable a, Writable a)
48+addToMap :: (Binary a, Typeable a) => Store -> FilePath -> a -> IO ()
46− => Store -> FilePath -> a -> IO ()
47addToMap store path value =
49addToMap store path value =
48− modifyMVar_ (storeMap store) $ return . M.insert path (compiledItem value)
50+ modifyMVar_ (storeMap store) $ return . M.insert path (Storable value)
50-- | Create a path
52-- | Create a path
@@ -55,7 +57,7 @@ makePath store name identifier =
56-- | Store an item
58-- | Store an item
58−storeSet :: (Binary a, Typeable a, Writable a)
60+storeSet :: (Binary a, Typeable a)
59 => Store -> String -> Identifier -> a -> IO ()
61 => Store -> String -> Identifier -> a -> IO ()
60storeSet store name identifier value = do
62storeSet store name identifier value = do
61 makeDirectories path
63 makeDirectories path
@@ -66,14 +68,14 @@ storeSet store name identifier value = do
67-- | Load an item
69-- | Load an item
69−storeGet :: (Binary a, Typeable a, Writable a)
71+storeGet :: (Binary a, Typeable a)
70 => Store -> String -> Identifier -> IO (Maybe a)
72 => Store -> String -> Identifier -> IO (Maybe a)
71storeGet store name identifier = do
73storeGet store name identifier = do
72 -- First check the in-memory map
74 -- First check the in-memory map
73 map' <- readMVar $ storeMap store
75 map' <- readMVar $ storeMap store
74 case M.lookup path map' of
76 case M.lookup path map' of
75 -- Found in the in-memory map
77 -- Found in the in-memory map
76− Just c -> return $ Just $ unCompiledItem c
78+ Just (Storable s) -> return $ cast s
77 -- Not found in the map, try the filesystem
79 -- Not found in the map, try the filesystem
78 Nothing -> do
80 Nothing -> do
79 exists <- doesFileExist path
81 exists <- doesFileExist path