on master help sign in
Simple key-value store
commit95f59be5a0be65c4eccdd020fc7938cd9afd7dde authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 26 Dec 2010, 16:12 (16 years ago) parentb30123f
1 file changed+53
src/Hakyll/Core/Store.hs+53
src/Hakyll/Core/Store.hsnew file+53
@@ -0,0 +1,53 @@
1+-- | A store for stroing and retreiving items
2+--
3+module Hakyll.Core.Store
4+ ( Store
5+ , makeStore
6+ , storeSet
7+ , storeGet
8+ ) where
9+
10+import Control.Applicative ((<$>))
11+import System.FilePath ((</>))
12+import System.Directory (doesFileExist)
13+
14+import Data.Binary (Binary, encodeFile, decodeFile)
15+
16+import Hakyll.Core.Identifier
17+import Hakyll.Core.Util.File
18+
19+-- | Data structure used for the store
20+--
21+data Store = Store
22+ { storeDirectory :: FilePath
23+ }
24+
25+-- | Initialize the store
26+--
27+makeStore :: FilePath -> IO Store
28+makeStore directory = return Store {storeDirectory = directory}
29+
30+-- | Create a path
31+--
32+makePath :: Store -> String -> Identifier -> FilePath
33+makePath store name identifier =
34+ storeDirectory store </> name </> toFilePath identifier
35+
36+-- | Store an item
37+--
38+storeSet :: Binary a => Store -> String -> Identifier -> a -> IO ()
39+storeSet store name identifier value = do
40+ makeDirectories path
41+ encodeFile path value
42+ where
43+ path = makePath store name identifier
44+
45+-- | Load an item
46+--
47+storeGet :: Binary a => Store -> String -> Identifier -> IO (Maybe a)
48+storeGet store name identifier = do
49+ exists <- doesFileExist path
50+ if exists then Just <$> decodeFile path
51+ else return Nothing
52+ where
53+ path = makePath store name identifier