on master help sign in
Don't require Writable for storing things
92aa446Jasper Van der Jeugt
src/Hakyll/Core/CompiledItem.hs+2−1
@@ -7,7 +7,7 @@
7--
7--
8-- * we need a 'Writable' instance so the results can be saved.
8-- * we need a 'Writable' instance so the results can be saved.
9--
9--
10−{-# LANGUAGE ExistentialQuantification #-}
10+{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}
11module Hakyll.Core.CompiledItem
11module Hakyll.Core.CompiledItem
12 ( CompiledItem (..)
12 ( CompiledItem (..)
13 , compiledItem
13 , compiledItem
@@ -24,6 +24,7 @@ import Hakyll.Core.Writable
24--
24--
25data CompiledItem = forall a. (Binary a, Typeable a, Writable a)
25data CompiledItem = forall a. (Binary a, Typeable a, Writable a)
26 => CompiledItem a
26 => CompiledItem a
27+ deriving (Typeable)
27
28
28instance Writable CompiledItem where
29instance Writable CompiledItem where
29 write p (CompiledItem x) = write p x
30 write p (CompiledItem x) = write p x
src/Hakyll/Core/Store.hs+12−10
@@ -1,5 +1,6 @@
1-- | A store for stroing and retreiving items
1-- | A store for stroing and retreiving items
2--
2--
3+{-# LANGUAGE ExistentialQuantification #-}
3module Hakyll.Core.Store
4module Hakyll.Core.Store
4 ( Store
5 ( 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
15
16
16import Data.Binary (Binary, encodeFile, decodeFile)
17import Data.Binary (Binary, encodeFile, decodeFile)
17−import Data.Typeable (Typeable)
18+import Data.Typeable (Typeable, cast)
18
19
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
22
23+-- | Items we can store
24+--
25+data Storable = forall a. (Binary a, Typeable a) => Storable a
26+
24-- | Data structure used for the store
27-- | Data structure used for the store
25--
28--
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)
31 }
34 }
32
35
33-- | Initialize the store
36-- | Initialize the store
@@ -42,10 +45,9 @@ makeStore directory = do
42
45
43-- | Auxiliary: add an item to the map
46-- | Auxiliary: add an item to the map
44--
47--
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)
49
51
50-- | Create a path
52-- | Create a path
51--
53--
@@ -55,7 +57,7 @@ makePath store name identifier =
55
57
56-- | Store an item
58-- | Store an item
57--
59--
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
66
68
67-- | Load an item
69-- | Load an item
68--
70--
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