1-- | A store for stroing and retreiving items
3{-# LANGUAGE ExistentialQuantification, ScopedTypeVariables #-}
4module Hakyll.Core.Store
12import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar_)
13import System.FilePath ((</>))
14import System.Directory (doesFileExist)
15import Data.Maybe (fromMaybe)
17import qualified Data.Map as M
19import Data.Binary (Binary, encodeFile, decodeFile)
20import Data.Typeable (Typeable, TypeRep, cast, typeOf)
22import Hakyll.Core.Identifier
23import Hakyll.Core.Util.File
25-- | Items we can store
27data Storable = forall a. (Binary a, Typeable a) => Storable a
29-- | Result when an item from the store
31data StoreGet a = Found a
33 | WrongType TypeRep TypeRep
36-- | Data structure used for the store
39 { -- | All items are stored on the filesystem
40 storeDirectory :: FilePath
41 , -- | And some items are also kept in-memory
42 storeMap :: MVar (Map FilePath Storable)
45-- | Initialize the store
47makeStore :: FilePath -> IO Store
48makeStore directory = do
49 mvar <- newMVar M.empty
51 { storeDirectory = directory
55-- | Auxiliary: add an item to the map
57addToMap :: (Binary a, Typeable a) => Store -> FilePath -> a -> IO ()
58addToMap store path value =
59 modifyMVar_ (storeMap store) $ return . M.insert path (Storable value)
63makePath :: Store -> String -> Identifier a -> FilePath
64makePath store name identifier = storeDirectory store </> name
65 </> group </> toFilePath identifier </> "hakyllstore"
67 group = fromMaybe "" $ identifierGroup identifier
71storeSet :: (Binary a, Typeable a)
72 => Store -> String -> Identifier a -> a -> IO ()
73storeSet store name identifier value = do
76 addToMap store path value
78 path = makePath store name identifier
82storeGet :: forall a. (Binary a, Typeable a)
83 => Store -> String -> Identifier a -> IO (StoreGet a)
84storeGet store name identifier = do
85 -- First check the in-memory map
86 map' <- readMVar $ storeMap store
87 case M.lookup path map' of
88 -- Found in the in-memory map
89 Just (Storable s) -> return $ case cast s of
90 Nothing -> WrongType (typeOf s) $ typeOf (undefined :: a)
92 -- Not found in the map, try the filesystem
94 exists <- doesFileExist path
96 -- Not found in the filesystem either
98 -- Found in the filesystem
99 else do v <- decodeFile path
100 addToMap store path v
103 path = makePath store name identifier
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list