module Database.Immutable.Write
(
I.Id(..)
, I.Limit(..)
, I.incId
, I.subIds
, writeDB
, fromList
) where
import qualified Data.ByteString as B
import Data.IORef
import Data.Monoid ((<>))
import qualified Data.Serialize as S
import Data.Word
import qualified Database.Immutable.Internal as I
import System.IO
fromList
:: S.Serialize a
=> [a]
-> B.ByteString
fromList = foldr B.append B.empty . (map S.encode)
writeDB
:: S.Serialize a
=> FilePath
-> ((a -> IO ()) -> IO r)
-> IO r
writeDB path f = do
count <- newIORef (0 :: Word32)
r <- withFile path WriteMode $ \handle -> f $ \a -> do
modifyIORef count (+1)
B.hPut handle (S.encode a)
withFile (path <> ".meta") WriteMode $ \handle -> do
count' <- readIORef count
B.hPut handle (S.encode count')
pure r