shrub/pkg/hair/lib/Vere/Log.hs

52 lines
1.4 KiB
Haskell
Raw Normal View History

module Vere.Log where
import Database.LMDB.Raw
2019-05-29 03:32:39 +03:00
import ClassyPrelude hiding ((<|))
import Data.Void
import Data.ByteString.Unsafe
2019-05-29 03:32:39 +03:00
import GHC.Ptr (castPtr)
import Data.List.NonEmpty (NonEmpty(..), (<|))
--------------------------------------------------------------------------------
data State = State
{ env :: MDB_env
, q :: TQueue Void
}
2019-05-29 03:32:39 +03:00
--------------------------------------------------------------------------------
init :: FilePath -> IO State
init dir = do
env <- mdb_env_create
mdb_env_set_maxdbs env 3
mdb_env_set_mapsize env (40 * 1024 * 1024 * 1024)
mdb_env_open env dir []
tq <- newTQueueIO
pure (State env tq)
shutdown :: State -> IO ()
shutdown s = mdb_env_close (env s)
2019-05-29 03:32:39 +03:00
--------------------------------------------------------------------------------
2019-05-29 03:32:39 +03:00
readQueue :: TQueue a -> STM (NonEmpty a)
readQueue q =
readTQueue q >>= go . singleton
where
go acc = tryReadTQueue q >>= \case
2019-05-29 03:32:39 +03:00
Nothing -> pure (reverse acc)
Just item -> go (item <| acc)
byteStringAsMdbVal :: ByteString -> (MDB_val -> IO a) -> IO a
byteStringAsMdbVal bs k =
unsafeUseAsCStringLen bs \(ptr,sz) ->
k (MDB_val (fromIntegral sz) (castPtr ptr))
put :: MDB_WriteFlags -> MDB_txn -> MDB_dbi -> ByteString -> ByteString
-> IO Bool
put flags txn db key val =
byteStringAsMdbVal key \mKey ->
byteStringAsMdbVal val \mVal ->
mdb_put flags txn db mKey mVal