summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-26 16:12:57 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-26 16:12:57 +0100
commit95f59be5a0be65c4eccdd020fc7938cd9afd7dde (patch)
treee30598581c3f0852009a900080bee0c92640b2c4
parentb30123f93cd7aa2deadd079e071899ac8f351993 (diff)
downloadhakyll-95f59be5a0be65c4eccdd020fc7938cd9afd7dde.tar.gz
Simple key-value store
-rw-r--r--src/Hakyll/Core/Store.hs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/Hakyll/Core/Store.hs b/src/Hakyll/Core/Store.hs
new file mode 100644
index 0000000..02b9b4e
--- /dev/null
+++ b/src/Hakyll/Core/Store.hs
@@ -0,0 +1,53 @@
+-- | A store for stroing and retreiving items
+--
+module Hakyll.Core.Store
+ ( Store
+ , makeStore
+ , storeSet
+ , storeGet
+ ) where
+
+import Control.Applicative ((<$>))
+import System.FilePath ((</>))
+import System.Directory (doesFileExist)
+
+import Data.Binary (Binary, encodeFile, decodeFile)
+
+import Hakyll.Core.Identifier
+import Hakyll.Core.Util.File
+
+-- | Data structure used for the store
+--
+data Store = Store
+ { storeDirectory :: FilePath
+ }
+
+-- | Initialize the store
+--
+makeStore :: FilePath -> IO Store
+makeStore directory = return Store {storeDirectory = directory}
+
+-- | Create a path
+--
+makePath :: Store -> String -> Identifier -> FilePath
+makePath store name identifier =
+ storeDirectory store </> name </> toFilePath identifier
+
+-- | Store an item
+--
+storeSet :: Binary a => Store -> String -> Identifier -> a -> IO ()
+storeSet store name identifier value = do
+ makeDirectories path
+ encodeFile path value
+ where
+ path = makePath store name identifier
+
+-- | Load an item
+--
+storeGet :: Binary a => Store -> String -> Identifier -> IO (Maybe a)
+storeGet store name identifier = do
+ exists <- doesFileExist path
+ if exists then Just <$> decodeFile path
+ else return Nothing
+ where
+ path = makePath store name identifier