2019-07-19 03:52:53 +03:00
|
|
|
module Vere.Log ( EventLog, identity, nextEv
|
|
|
|
, new, existing
|
|
|
|
, streamEvents, appendEvents
|
2019-06-25 04:10:41 +03:00
|
|
|
) where
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-08-03 03:09:53 +03:00
|
|
|
import ClassyPrelude hiding (init)
|
2019-07-19 03:52:53 +03:00
|
|
|
import Data.Acquire
|
2019-08-03 03:09:53 +03:00
|
|
|
import Data.Conduit
|
2019-05-30 02:19:07 +03:00
|
|
|
import Database.LMDB.Raw
|
|
|
|
import Foreign.Marshal.Alloc
|
2019-07-12 22:18:14 +03:00
|
|
|
import Foreign.Ptr
|
|
|
|
import Noun
|
2019-05-30 23:19:26 +03:00
|
|
|
import Vere.Pier.Types
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
import Foreign.Storable (peek, poke, sizeOf)
|
2019-05-30 02:19:07 +03:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Unsafe as BU
|
2019-05-31 05:53:00 +03:00
|
|
|
import qualified Data.Vector as V
|
2019-05-29 03:32:39 +03:00
|
|
|
|
2019-05-29 03:16:30 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Env = MDB_env
|
|
|
|
type Txn = MDB_txn
|
|
|
|
type Dbi = MDB_dbi
|
|
|
|
type Cur = MDB_cursor
|
|
|
|
|
|
|
|
data EventLog = EventLog
|
|
|
|
{ env :: Env
|
|
|
|
, _metaTbl :: Dbi
|
|
|
|
, eventsTbl :: Dbi
|
|
|
|
, identity :: LogIdentity
|
|
|
|
, numEvents :: IORef EventId
|
|
|
|
}
|
|
|
|
|
|
|
|
nextEv :: EventLog -> IO EventId
|
|
|
|
nextEv = fmap succ . readIORef . numEvents
|
|
|
|
|
|
|
|
lastEv :: EventLog -> IO EventId
|
|
|
|
lastEv = readIORef . numEvents
|
|
|
|
|
|
|
|
|
|
|
|
data EventLogExn
|
|
|
|
= NoLogIdentity
|
|
|
|
| MissingEvent EventId
|
2019-07-21 22:56:18 +03:00
|
|
|
| BadNounInLogIdentity ByteString DecodeErr ByteString
|
2019-07-19 03:52:53 +03:00
|
|
|
| BadKeyInEventLog
|
|
|
|
| BadWriteLogIdentity LogIdentity
|
|
|
|
| BadWriteEvent EventId
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
|
|
|
|
-- Instances -------------------------------------------------------------------
|
|
|
|
|
|
|
|
instance Exception EventLogExn where
|
|
|
|
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
-- Open/Close an Event Log -----------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
rawOpen :: FilePath -> IO Env
|
|
|
|
rawOpen 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 []
|
|
|
|
pure env
|
|
|
|
|
|
|
|
create :: FilePath -> LogIdentity -> IO EventLog
|
|
|
|
create dir id = do
|
|
|
|
env <- rawOpen dir
|
|
|
|
(m, e) <- createTables env
|
|
|
|
clearEvents env e
|
|
|
|
writeIdent env m id
|
|
|
|
EventLog env m e id <$> newIORef 0
|
|
|
|
where
|
|
|
|
createTables env =
|
|
|
|
with (writeTxn env) $ \txn -> do
|
|
|
|
m <- mdb_dbi_open txn (Just "META") [MDB_CREATE]
|
|
|
|
e <- mdb_dbi_open txn (Just "EVENTS") [MDB_CREATE, MDB_INTEGERKEY]
|
|
|
|
pure (m, e)
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
open :: FilePath -> IO EventLog
|
|
|
|
open dir = do
|
2019-07-19 03:52:53 +03:00
|
|
|
env <- rawOpen dir
|
|
|
|
(m, e) <- openTables env
|
|
|
|
id <- getIdent env m
|
|
|
|
numEvs <- getNumEvents env e
|
|
|
|
EventLog env m e id <$> newIORef numEvs
|
|
|
|
where
|
|
|
|
openTables env =
|
|
|
|
with (openTxn env) $ \txn ->
|
|
|
|
(,) <$> mdb_dbi_open txn (Just "META") []
|
|
|
|
<*> mdb_dbi_open txn (Just "EVENTS") [MDB_INTEGERKEY]
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
close :: EventLog -> IO ()
|
2019-07-19 03:52:53 +03:00
|
|
|
close (EventLog env meta events _ _) = do
|
|
|
|
mdb_dbi_close env meta
|
|
|
|
mdb_dbi_close env events
|
|
|
|
mdb_env_sync_flush env
|
|
|
|
mdb_env_close env
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Create a new event log or open an existing one. -----------------------------
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
existing :: FilePath -> Acquire EventLog
|
|
|
|
existing dir = mkAcquire (open dir) close
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
new :: FilePath -> LogIdentity -> Acquire EventLog
|
|
|
|
new dir id = mkAcquire (create dir id) close
|
2019-05-30 02:43:51 +03:00
|
|
|
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Read/Write Log Identity -----------------------------------------------------
|
2019-05-30 02:43:51 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
openTxn :: Env -> Acquire Txn
|
|
|
|
openTxn env = mkAcquire begin commit
|
|
|
|
where
|
|
|
|
begin = mdb_txn_begin env Nothing True
|
|
|
|
commit = mdb_txn_commit
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
readTxn :: Env -> Acquire Txn
|
|
|
|
readTxn env = mkAcquire begin abort
|
|
|
|
where
|
|
|
|
begin = mdb_txn_begin env Nothing True
|
|
|
|
abort = mdb_txn_abort
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
writeTxn :: Env -> Acquire Txn
|
|
|
|
writeTxn env = mkAcquireType begin finalize
|
|
|
|
where
|
|
|
|
begin = mdb_txn_begin env Nothing False
|
|
|
|
finalize txn = \case
|
|
|
|
ReleaseNormal -> mdb_txn_commit txn
|
|
|
|
ReleaseEarly -> mdb_txn_commit txn
|
|
|
|
ReleaseException -> mdb_txn_abort txn
|
|
|
|
|
|
|
|
cursor :: Txn -> Dbi -> Acquire Cur
|
|
|
|
cursor txn dbi = mkAcquire open close
|
|
|
|
where
|
|
|
|
open = mdb_cursor_open txn dbi
|
|
|
|
close = mdb_cursor_close
|
|
|
|
|
|
|
|
getIdent :: Env -> Dbi -> IO LogIdentity
|
|
|
|
getIdent env dbi =
|
|
|
|
getTbl env >>= traverse decodeIdent >>= \case
|
|
|
|
Nothing -> throwIO NoLogIdentity
|
|
|
|
Just li -> pure li
|
|
|
|
where
|
|
|
|
decodeIdent :: (Noun, Noun, Noun) -> IO LogIdentity
|
|
|
|
decodeIdent = fromNounExn . toNoun
|
|
|
|
|
|
|
|
getTbl :: Env -> IO (Maybe (Noun, Noun, Noun))
|
|
|
|
getTbl env = do
|
|
|
|
with (readTxn env) $ \txn -> do
|
|
|
|
who <- getMb txn dbi "who"
|
|
|
|
fake <- getMb txn dbi "is-fake"
|
|
|
|
life <- getMb txn dbi "life"
|
|
|
|
pure $ (,,) <$> who <*> fake <*> life
|
|
|
|
|
|
|
|
writeIdent :: Env -> Dbi -> LogIdentity -> IO ()
|
|
|
|
writeIdent env metaTbl ident@LogIdentity{..} = do
|
|
|
|
let flags = compileWriteFlags []
|
|
|
|
with (writeTxn env) $ \txn -> do
|
|
|
|
x <- putNoun flags txn metaTbl "who" (toNoun who)
|
|
|
|
y <- putNoun flags txn metaTbl "is-fake" (toNoun isFake)
|
|
|
|
z <- putNoun flags txn metaTbl "life" (toNoun lifecycleLen)
|
|
|
|
unless (x && y && z) $ do
|
|
|
|
throwIO (BadWriteLogIdentity ident)
|
2019-05-30 02:19:07 +03:00
|
|
|
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Latest Event Number ---------------------------------------------------------
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
getNumEvents :: Env -> Dbi -> IO Word64
|
|
|
|
getNumEvents env eventsTbl =
|
|
|
|
with (readTxn env) $ \txn ->
|
|
|
|
with (cursor txn eventsTbl) $ \cur ->
|
|
|
|
withKVPtrs nullVal nullVal $ \pKey pVal ->
|
|
|
|
mdb_cursor_get MDB_LAST cur pKey pVal >>= \case
|
|
|
|
False -> pure 0
|
|
|
|
True -> peek pKey >>= mdbValToWord64
|
|
|
|
|
|
|
|
|
|
|
|
-- Write Events ----------------------------------------------------------------
|
|
|
|
|
|
|
|
clearEvents :: Env -> Dbi -> IO ()
|
|
|
|
clearEvents env eventsTbl =
|
|
|
|
with (writeTxn env) $ \txn ->
|
|
|
|
with (cursor txn eventsTbl) $ \cur ->
|
|
|
|
withKVPtrs nullVal nullVal $ \pKey pVal -> do
|
|
|
|
let loop = mdb_cursor_get MDB_LAST cur pKey pVal >>= \case
|
|
|
|
False -> pure ()
|
|
|
|
True -> do mdb_cursor_del (compileWriteFlags []) cur
|
|
|
|
loop
|
|
|
|
loop
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
appendEvents :: EventLog -> Vector ByteString -> IO ()
|
2019-07-19 03:52:53 +03:00
|
|
|
appendEvents log !events = do
|
|
|
|
numEvs <- readIORef (numEvents log)
|
|
|
|
next <- nextEv log
|
|
|
|
doAppend $ zip [next..] $ toList events
|
|
|
|
writeIORef (numEvents log) (numEvs + word (length events))
|
|
|
|
where
|
|
|
|
flags = compileWriteFlags [MDB_NOOVERWRITE]
|
|
|
|
doAppend = \kvs ->
|
|
|
|
with (writeTxn $ env log) \txn ->
|
|
|
|
for_ kvs $ \(k,v) -> do
|
|
|
|
putEvent flags txn (eventsTbl log) k v >>= \case
|
|
|
|
True -> pure ()
|
2019-07-21 22:56:18 +03:00
|
|
|
False -> throwIO (BadWriteEvent k)
|
2019-05-30 02:19:07 +03:00
|
|
|
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Read Events -----------------------------------------------------------------
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
streamEvents :: EventLog -> Word64 -> ConduitT () ByteString IO ()
|
2019-07-19 03:52:53 +03:00
|
|
|
streamEvents log first = do
|
|
|
|
last <- liftIO $ lastEv log
|
2019-07-24 07:03:04 +03:00
|
|
|
-- traceM ("streamEvents: " <> show (first, last))
|
2019-07-19 03:52:53 +03:00
|
|
|
batch <- liftIO (readBatch log first)
|
|
|
|
unless (null batch) $ do
|
|
|
|
for_ batch yield
|
|
|
|
streamEvents log (first + word (length batch))
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
readBatch :: EventLog -> Word64 -> IO (V.Vector ByteString)
|
2019-07-19 03:52:53 +03:00
|
|
|
readBatch log first = start
|
|
|
|
where
|
|
|
|
start = do
|
|
|
|
last <- lastEv log
|
|
|
|
if (first > last)
|
|
|
|
then pure mempty
|
|
|
|
else readRows $ fromIntegral $ min 1000 $ ((last+1) - first)
|
|
|
|
|
|
|
|
assertFound :: EventId -> Bool -> IO ()
|
|
|
|
assertFound id found = do
|
|
|
|
unless found $ throwIO $ MissingEvent id
|
|
|
|
|
|
|
|
readRows count =
|
|
|
|
withWordPtr first $ \pIdx ->
|
|
|
|
withKVPtrs (MDB_val 8 (castPtr pIdx)) nullVal $ \pKey pVal ->
|
|
|
|
with (readTxn $ env log) $ \txn ->
|
|
|
|
with (cursor txn $ eventsTbl log) $ \cur -> do
|
|
|
|
assertFound first =<< mdb_cursor_get MDB_SET_KEY cur pKey pVal
|
|
|
|
fetchRows count cur pKey pVal
|
|
|
|
|
|
|
|
fetchRows count cur pKey pVal = do
|
|
|
|
V.generateM count $ \i -> do
|
|
|
|
key <- peek pKey >>= mdbValToWord64
|
2019-07-21 22:56:18 +03:00
|
|
|
val <- peek pVal >>= mdbValToBytes
|
2019-07-19 03:52:53 +03:00
|
|
|
idx <- pure (first + word i)
|
|
|
|
unless (key == idx) $ throwIO $ MissingEvent idx
|
|
|
|
when (count /= succ i) $ do
|
|
|
|
assertFound idx =<< mdb_cursor_get MDB_NEXT cur pKey pVal
|
|
|
|
pure val
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
nullVal :: MDB_val
|
|
|
|
nullVal = MDB_val 0 nullPtr
|
|
|
|
|
|
|
|
word :: Int -> Word64
|
|
|
|
word = fromIntegral
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
assertExn :: Exception e => Bool -> e -> IO ()
|
|
|
|
assertExn True _ = pure ()
|
|
|
|
assertExn False e = throwIO e
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
eitherExn :: Exception e => Either a b -> (a -> e) -> IO b
|
|
|
|
eitherExn eat exn = either (throwIO . exn) pure eat
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
byteStringAsMdbVal :: ByteString -> (MDB_val -> IO a) -> IO a
|
|
|
|
byteStringAsMdbVal bs k =
|
2019-07-12 22:24:44 +03:00
|
|
|
BU.unsafeUseAsCStringLen bs $ \(ptr,sz) ->
|
2019-06-25 04:10:41 +03:00
|
|
|
k (MDB_val (fromIntegral sz) (castPtr ptr))
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
mdbValToWord64 :: MDB_val -> IO Word64
|
|
|
|
mdbValToWord64 (MDB_val sz ptr) = do
|
2019-07-19 03:52:53 +03:00
|
|
|
assertExn (sz == 8) BadKeyInEventLog
|
2019-06-25 04:10:41 +03:00
|
|
|
peek (castPtr ptr)
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
withWord64AsMDBval :: Word64 -> (MDB_val -> IO a) -> IO a
|
|
|
|
withWord64AsMDBval w cb = do
|
|
|
|
withWordPtr w $ \p ->
|
|
|
|
cb (MDB_val (fromIntegral (sizeOf w)) (castPtr p))
|
|
|
|
|
|
|
|
withWordPtr :: Word64 -> (Ptr Word64 -> IO a) -> IO a
|
|
|
|
withWordPtr w cb = do
|
|
|
|
allocaBytes (sizeOf w) (\p -> poke p w >> cb p)
|
|
|
|
|
|
|
|
|
|
|
|
-- Lower-Level Operations ------------------------------------------------------
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
getMb :: Txn -> Dbi -> ByteString -> IO (Maybe Noun)
|
|
|
|
getMb txn db key =
|
2019-07-12 22:24:44 +03:00
|
|
|
byteStringAsMdbVal key $ \mKey ->
|
2019-07-21 22:56:18 +03:00
|
|
|
mdb_get txn db mKey >>= traverse (mdbValToNoun key)
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
mdbValToBytes :: MDB_val -> IO ByteString
|
|
|
|
mdbValToBytes (MDB_val sz ptr) = do
|
|
|
|
BU.unsafePackCStringLen (castPtr ptr, fromIntegral sz)
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
mdbValToNoun :: ByteString -> MDB_val -> IO Noun
|
|
|
|
mdbValToNoun key (MDB_val sz ptr) = do
|
2019-06-25 04:10:41 +03:00
|
|
|
bs <- BU.unsafePackCStringLen (castPtr ptr, fromIntegral sz)
|
2019-07-21 22:56:18 +03:00
|
|
|
let res = cueBS bs
|
|
|
|
eitherExn res (\err -> BadNounInLogIdentity key err bs)
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
putNoun :: MDB_WriteFlags -> Txn -> Dbi -> ByteString -> Noun -> IO Bool
|
2019-06-25 04:10:41 +03:00
|
|
|
putNoun flags txn db key val =
|
|
|
|
byteStringAsMdbVal key $ \mKey ->
|
2019-07-19 03:52:53 +03:00
|
|
|
byteStringAsMdbVal (jamBS val) $ \mVal ->
|
|
|
|
mdb_put flags txn db mKey mVal
|
2019-06-25 04:10:41 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
putEvent :: MDB_WriteFlags -> Txn -> Dbi -> Word64 -> ByteString -> IO Bool
|
|
|
|
putEvent flags txn db id bs = do
|
2019-06-25 04:10:41 +03:00
|
|
|
withWord64AsMDBval id $ \idVal -> do
|
2019-07-24 07:03:04 +03:00
|
|
|
-- traceM ("putEvent: " <> show (id, length bs))
|
2019-06-25 04:10:41 +03:00
|
|
|
byteStringAsMdbVal bs $ \mVal -> do
|
2019-07-19 03:52:53 +03:00
|
|
|
mdb_put flags txn db idVal mVal
|