2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
High-Level Event-Log Interface
|
|
|
|
|
2019-08-21 02:32:46 +03:00
|
|
|
TODO Effects storage logic is messy.
|
|
|
|
-}
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
module Urbit.EventLog.LMDB
|
|
|
|
( LogIdentity(..)
|
|
|
|
, EventLog
|
|
|
|
, identity
|
|
|
|
, nextEv
|
|
|
|
, lastEv
|
|
|
|
, new
|
|
|
|
, existing
|
|
|
|
, streamEvents
|
|
|
|
, appendEvents
|
|
|
|
, trimEvents
|
|
|
|
, streamEffectsRows
|
|
|
|
, writeEffectsRow
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
import Data.RAcquire
|
2019-05-30 02:19:07 +03:00
|
|
|
import Database.LMDB.Raw
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
import Data.Conduit (ConduitT, yield)
|
|
|
|
import Foreign.Marshal.Alloc (allocaBytes)
|
|
|
|
import Foreign.Ptr (Ptr, castPtr, nullPtr)
|
|
|
|
import Foreign.Storable (peek, poke, sizeOf)
|
|
|
|
import RIO (HasLogFunc, RIO, display, logDebug, runRIO)
|
|
|
|
import Urbit.Noun (DecodeErr, Noun, Ship)
|
|
|
|
import Urbit.Noun (deriveNoun, fromNounExn, toNoun)
|
|
|
|
import Urbit.Noun (cueBS, jamBS)
|
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
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
-- Public Types ----------------------------------------------------------------
|
|
|
|
|
|
|
|
data LogIdentity = LogIdentity
|
2020-10-02 18:10:32 +03:00
|
|
|
{ who :: !Ship
|
|
|
|
, isFake :: !Bool
|
|
|
|
, lifecycleLen :: !Word
|
2020-06-09 00:27:58 +03:00
|
|
|
} deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
deriveNoun ''LogIdentity
|
|
|
|
|
|
|
|
|
2019-07-19 03:52:53 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Env = MDB_env
|
2019-08-29 03:26:59 +03:00
|
|
|
type Val = MDB_val
|
2019-07-19 03:52:53 +03:00
|
|
|
type Txn = MDB_txn
|
|
|
|
type Dbi = MDB_dbi
|
|
|
|
type Cur = MDB_cursor
|
|
|
|
|
|
|
|
data EventLog = EventLog
|
2020-06-10 22:22:45 +03:00
|
|
|
{ env :: Env
|
|
|
|
, _metaTbl :: Dbi
|
|
|
|
, eventsTbl :: Dbi
|
|
|
|
, effectsTbl :: Dbi
|
|
|
|
, identity :: LogIdentity
|
|
|
|
, numEvents :: TVar Word64
|
|
|
|
}
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
nextEv :: EventLog -> STM Word64
|
2020-06-03 02:03:04 +03:00
|
|
|
nextEv = fmap (+1) . lastEv
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
lastEv :: EventLog -> STM Word64
|
2020-06-03 02:03:04 +03:00
|
|
|
lastEv = readTVar . numEvents
|
2019-07-19 03:52:53 +03:00
|
|
|
|
|
|
|
data EventLogExn
|
2020-06-10 22:22:45 +03:00
|
|
|
= NoLogIdentity
|
|
|
|
| MissingEvent Word64
|
|
|
|
| BadNounInLogIdentity ByteString DecodeErr ByteString
|
|
|
|
| BadKeyInEventLog
|
|
|
|
| BadWriteLogIdentity LogIdentity
|
|
|
|
| BadWriteEvent Word64
|
|
|
|
| BadWriteEffect Word64
|
|
|
|
deriving Show
|
2019-07-19 03:52:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Instances -------------------------------------------------------------------
|
|
|
|
|
|
|
|
instance Exception EventLogExn where
|
|
|
|
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
io :: MonadIO m => IO a -> m a
|
|
|
|
io = liftIO
|
|
|
|
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
-- Open/Close an Event Log -----------------------------------------------------
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
rawOpen :: MonadIO m => FilePath -> m Env
|
|
|
|
rawOpen dir = io $ do
|
2019-07-19 03:52:53 +03:00
|
|
|
env <- mdb_env_create
|
|
|
|
mdb_env_set_maxdbs env 3
|
2019-12-13 13:50:54 +03:00
|
|
|
mdb_env_set_mapsize env (100 * 1024 * 1024 * 1024)
|
2019-07-19 03:52:53 +03:00
|
|
|
mdb_env_open env dir []
|
|
|
|
pure env
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
create :: HasLogFunc e => FilePath -> LogIdentity -> RIO e EventLog
|
2019-07-19 03:52:53 +03:00
|
|
|
create dir id = do
|
2019-08-29 03:26:59 +03:00
|
|
|
logDebug $ display (pack @Text $ "Creating LMDB database: " <> dir)
|
|
|
|
logDebug $ display (pack @Text $ "Log Identity: " <> show id)
|
2019-08-21 02:32:46 +03:00
|
|
|
env <- rawOpen dir
|
|
|
|
(m, e, f) <- createTables env
|
2019-07-19 03:52:53 +03:00
|
|
|
clearEvents env e
|
|
|
|
writeIdent env m id
|
2020-06-03 02:03:04 +03:00
|
|
|
EventLog env m e f id <$> newTVarIO 0
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
|
|
|
createTables env =
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (writeTxn env) $ \txn -> io $
|
2019-08-21 02:32:46 +03:00
|
|
|
(,,) <$> mdb_dbi_open txn (Just "META") [MDB_CREATE]
|
|
|
|
<*> mdb_dbi_open txn (Just "EVENTS") [MDB_CREATE, MDB_INTEGERKEY]
|
|
|
|
<*> mdb_dbi_open txn (Just "EFFECTS") [MDB_CREATE, MDB_INTEGERKEY]
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
open :: HasLogFunc e => FilePath -> RIO e EventLog
|
2019-06-25 04:10:41 +03:00
|
|
|
open dir = do
|
2019-08-29 03:26:59 +03:00
|
|
|
logDebug $ display (pack @Text $ "Opening LMDB database: " <> dir)
|
2019-08-21 02:32:46 +03:00
|
|
|
env <- rawOpen dir
|
|
|
|
(m, e, f) <- openTables env
|
|
|
|
id <- getIdent env m
|
2019-08-29 03:26:59 +03:00
|
|
|
logDebug $ display (pack @Text $ "Log Identity: " <> show id)
|
2019-08-21 02:32:46 +03:00
|
|
|
numEvs <- getNumEvents env e
|
2020-06-03 02:03:04 +03:00
|
|
|
EventLog env m e f id <$> newTVarIO numEvs
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
|
|
|
openTables env =
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (writeTxn env) $ \txn -> io $
|
2019-08-21 02:32:46 +03:00
|
|
|
(,,) <$> mdb_dbi_open txn (Just "META") []
|
|
|
|
<*> mdb_dbi_open txn (Just "EVENTS") [MDB_INTEGERKEY]
|
|
|
|
<*> mdb_dbi_open txn (Just "EFFECTS") [MDB_CREATE, MDB_INTEGERKEY]
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
close :: HasLogFunc e => FilePath -> EventLog -> RIO e ()
|
|
|
|
close dir (EventLog env meta events effects _ _) = do
|
|
|
|
logDebug $ display (pack @Text $ "Closing LMDB database: " <> dir)
|
|
|
|
io $ do mdb_dbi_close env meta
|
|
|
|
mdb_dbi_close env events
|
|
|
|
mdb_dbi_close env effects
|
|
|
|
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-08-29 03:26:59 +03:00
|
|
|
existing :: HasLogFunc e => FilePath -> RAcquire e EventLog
|
|
|
|
existing dir = mkRAcquire (open dir) (close dir)
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
new :: HasLogFunc e => FilePath -> LogIdentity -> RAcquire e EventLog
|
|
|
|
new dir id = mkRAcquire (create dir id) (close dir)
|
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
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-08-21 03:42:53 +03:00
|
|
|
A read-only transaction that commits at the end.
|
|
|
|
|
|
|
|
Use this when opening database handles.
|
|
|
|
-}
|
2019-08-29 03:26:59 +03:00
|
|
|
_openTxn :: Env -> RAcquire e Txn
|
|
|
|
_openTxn env = mkRAcquire begin commit
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
2019-08-29 03:26:59 +03:00
|
|
|
begin = io $ mdb_txn_begin env Nothing True
|
|
|
|
commit = io . mdb_txn_commit
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-08-21 03:42:53 +03:00
|
|
|
A read-only transaction that aborts at the end.
|
|
|
|
|
|
|
|
Use this when reading data from already-opened databases.
|
|
|
|
-}
|
2019-08-29 03:26:59 +03:00
|
|
|
readTxn :: Env -> RAcquire e Txn
|
|
|
|
readTxn env = mkRAcquire begin abort
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
2019-08-29 03:26:59 +03:00
|
|
|
begin = io $ mdb_txn_begin env Nothing True
|
|
|
|
abort = io . mdb_txn_abort
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-08-21 03:42:53 +03:00
|
|
|
A read-write transaction that commits upon sucessful completion and
|
|
|
|
aborts on exception.
|
|
|
|
|
|
|
|
Use this when reading data from already-opened databases.
|
|
|
|
-}
|
2019-08-29 03:26:59 +03:00
|
|
|
writeTxn :: Env -> RAcquire e Txn
|
|
|
|
writeTxn env = mkRAcquireType begin finalize
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
2019-08-29 03:26:59 +03:00
|
|
|
begin = io $ mdb_txn_begin env Nothing False
|
|
|
|
finalize txn = io . \case
|
2019-07-19 03:52:53 +03:00
|
|
|
ReleaseNormal -> mdb_txn_commit txn
|
|
|
|
ReleaseEarly -> mdb_txn_commit txn
|
|
|
|
ReleaseException -> mdb_txn_abort txn
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
cursor :: Txn -> Dbi -> RAcquire e Cur
|
|
|
|
cursor txn dbi = mkRAcquire open close
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
2019-08-29 03:26:59 +03:00
|
|
|
open = io $ mdb_cursor_open txn dbi
|
|
|
|
close = io . mdb_cursor_close
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
getIdent :: HasLogFunc e => Env -> Dbi -> RIO e LogIdentity
|
|
|
|
getIdent env dbi = do
|
|
|
|
logDebug "Reading log identity"
|
2019-07-19 03:52:53 +03:00
|
|
|
getTbl env >>= traverse decodeIdent >>= \case
|
|
|
|
Nothing -> throwIO NoLogIdentity
|
|
|
|
Just li -> pure li
|
|
|
|
where
|
2019-08-29 03:26:59 +03:00
|
|
|
decodeIdent :: (Noun, Noun, Noun) -> RIO e LogIdentity
|
2019-07-19 03:52:53 +03:00
|
|
|
decodeIdent = fromNounExn . toNoun
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
getTbl :: Env -> RIO e (Maybe (Noun, Noun, Noun))
|
2019-07-19 03:52:53 +03:00
|
|
|
getTbl env = do
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (readTxn env) $ \txn -> do
|
2019-07-19 03:52:53 +03:00
|
|
|
who <- getMb txn dbi "who"
|
|
|
|
fake <- getMb txn dbi "is-fake"
|
|
|
|
life <- getMb txn dbi "life"
|
|
|
|
pure $ (,,) <$> who <*> fake <*> life
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
writeIdent :: HasLogFunc e => Env -> Dbi -> LogIdentity -> RIO e ()
|
2019-07-19 03:52:53 +03:00
|
|
|
writeIdent env metaTbl ident@LogIdentity{..} = do
|
2019-08-29 03:26:59 +03:00
|
|
|
logDebug "Writing log identity"
|
2019-07-19 03:52:53 +03:00
|
|
|
let flags = compileWriteFlags []
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (writeTxn env) $ \txn -> do
|
2019-07-19 03:52:53 +03:00
|
|
|
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-08-29 03:26:59 +03:00
|
|
|
getNumEvents :: Env -> Dbi -> RIO e Word64
|
2019-07-19 03:52:53 +03:00
|
|
|
getNumEvents env eventsTbl =
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (readTxn env) $ \txn ->
|
|
|
|
rwith (cursor txn eventsTbl) $ \cur ->
|
|
|
|
withKVPtrs' nullVal nullVal $ \pKey pVal ->
|
|
|
|
io $ mdb_cursor_get MDB_LAST cur pKey pVal >>= \case
|
2019-07-19 03:52:53 +03:00
|
|
|
False -> pure 0
|
|
|
|
True -> peek pKey >>= mdbValToWord64
|
|
|
|
|
|
|
|
|
|
|
|
-- Write Events ----------------------------------------------------------------
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
clearEvents :: Env -> Dbi -> RIO e ()
|
2019-07-19 03:52:53 +03:00
|
|
|
clearEvents env eventsTbl =
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (writeTxn env) $ \txn ->
|
|
|
|
rwith (cursor txn eventsTbl) $ \cur ->
|
|
|
|
withKVPtrs' nullVal nullVal $ \pKey pVal -> do
|
|
|
|
let loop = io (mdb_cursor_get MDB_LAST cur pKey pVal) >>= \case
|
2019-07-19 03:52:53 +03:00
|
|
|
False -> pure ()
|
2019-08-29 03:26:59 +03:00
|
|
|
True -> do io $ mdb_cursor_del (compileWriteFlags []) cur
|
2019-07-19 03:52:53 +03:00
|
|
|
loop
|
|
|
|
loop
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
appendEvents :: EventLog -> Vector ByteString -> RIO e ()
|
2019-07-19 03:52:53 +03:00
|
|
|
appendEvents log !events = do
|
2020-06-03 02:03:04 +03:00
|
|
|
numEvs <- atomically $ readTVar (numEvents log)
|
2019-08-14 03:52:59 +03:00
|
|
|
next <- pure (numEvs + 1)
|
2019-07-19 03:52:53 +03:00
|
|
|
doAppend $ zip [next..] $ toList events
|
2020-06-03 02:03:04 +03:00
|
|
|
atomically $ writeTVar (numEvents log) (numEvs + word (length events))
|
2019-07-19 03:52:53 +03:00
|
|
|
where
|
|
|
|
flags = compileWriteFlags [MDB_NOOVERWRITE]
|
|
|
|
doAppend = \kvs ->
|
2019-08-29 03:26:59 +03:00
|
|
|
rwith (writeTxn $ env log) $ \txn ->
|
2019-07-19 03:52:53 +03:00
|
|
|
for_ kvs $ \(k,v) -> do
|
2019-08-21 02:32:46 +03:00
|
|
|
putBytes flags txn (eventsTbl log) k v >>= \case
|
2019-07-19 03:52:53 +03:00
|
|
|
True -> pure ()
|
2019-07-21 22:56:18 +03:00
|
|
|
False -> throwIO (BadWriteEvent k)
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
writeEffectsRow :: MonadIO m => EventLog -> Word64 -> ByteString -> m ()
|
2020-06-05 00:10:33 +03:00
|
|
|
writeEffectsRow log k v = io $ runRIO () $ do
|
|
|
|
let flags = compileWriteFlags []
|
|
|
|
rwith (writeTxn $ env log) $ \txn ->
|
|
|
|
putBytes flags txn (effectsTbl log) k v >>= \case
|
|
|
|
True -> pure ()
|
|
|
|
False -> throwIO (BadWriteEffect k)
|
2019-08-21 02:32:46 +03:00
|
|
|
|
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-08-30 02:48:46 +03:00
|
|
|
trimEvents :: HasLogFunc e => EventLog -> Word64 -> RIO e ()
|
|
|
|
trimEvents log start = do
|
2020-06-03 02:03:04 +03:00
|
|
|
last <- atomically (lastEv log)
|
2019-08-30 03:23:48 +03:00
|
|
|
rwith (writeTxn $ env log) $ \txn ->
|
|
|
|
for_ [start..last] $ \eId ->
|
|
|
|
withWordPtr eId $ \pKey -> do
|
|
|
|
let key = MDB_val 8 (castPtr pKey)
|
|
|
|
found <- io $ mdb_del txn (eventsTbl log) key Nothing
|
|
|
|
unless found $
|
|
|
|
throwIO (MissingEvent eId)
|
2020-06-03 02:03:04 +03:00
|
|
|
atomically $ writeTVar (numEvents log) (pred start)
|
2019-08-30 02:48:46 +03:00
|
|
|
|
2020-06-05 00:10:33 +03:00
|
|
|
streamEvents :: MonadIO m => EventLog -> Word64 -> ConduitT () ByteString m ()
|
2019-07-19 03:52:53 +03:00
|
|
|
streamEvents log first = do
|
2020-06-05 00:10:33 +03:00
|
|
|
batch <- io $ runRIO () $ readBatch log first
|
|
|
|
unless (null batch) $ do
|
|
|
|
for_ batch yield
|
|
|
|
streamEvents log (first + word (length batch))
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
streamEffectsRows :: forall e. HasLogFunc e
|
2020-06-09 00:27:58 +03:00
|
|
|
=> EventLog -> Word64
|
2019-08-29 03:26:59 +03:00
|
|
|
-> ConduitT () (Word64, ByteString) (RIO e) ()
|
2019-08-21 02:32:46 +03:00
|
|
|
streamEffectsRows log = go
|
|
|
|
where
|
2020-06-09 00:27:58 +03:00
|
|
|
go :: Word64 -> ConduitT () (Word64, ByteString) (RIO e) ()
|
2019-08-21 02:32:46 +03:00
|
|
|
go next = do
|
2019-08-29 03:26:59 +03:00
|
|
|
batch <- lift $ readRowsBatch (env log) (effectsTbl log) next
|
2019-08-21 02:32:46 +03:00
|
|
|
unless (null batch) $ do
|
|
|
|
for_ batch yield
|
|
|
|
go (next + fromIntegral (length batch))
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-08-21 02:32:46 +03:00
|
|
|
Read 1000 rows from the events table, starting from event `first`.
|
|
|
|
|
|
|
|
Throws `MissingEvent` if an event was missing from the log.
|
|
|
|
-}
|
2019-08-29 03:26:59 +03:00
|
|
|
readBatch :: EventLog -> Word64 -> RIO e (V.Vector ByteString)
|
2019-07-19 03:52:53 +03:00
|
|
|
readBatch log first = start
|
|
|
|
where
|
|
|
|
start = do
|
2020-06-03 02:03:04 +03:00
|
|
|
last <- atomically (lastEv log)
|
2019-07-19 03:52:53 +03:00
|
|
|
if (first > last)
|
|
|
|
then pure mempty
|
|
|
|
else readRows $ fromIntegral $ min 1000 $ ((last+1) - first)
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
assertFound :: Word64 -> Bool -> RIO e ()
|
2019-07-19 03:52:53 +03:00
|
|
|
assertFound id found = do
|
|
|
|
unless found $ throwIO $ MissingEvent id
|
|
|
|
|
|
|
|
readRows count =
|
|
|
|
withWordPtr first $ \pIdx ->
|
2019-08-29 03:26:59 +03:00
|
|
|
withKVPtrs' (MDB_val 8 (castPtr pIdx)) nullVal $ \pKey pVal ->
|
|
|
|
rwith (readTxn $ env log) $ \txn ->
|
|
|
|
rwith (cursor txn $ eventsTbl log) $ \cur -> do
|
|
|
|
assertFound first =<< io (mdb_cursor_get MDB_SET_KEY cur pKey pVal)
|
2019-07-19 03:52:53 +03:00
|
|
|
fetchRows count cur pKey pVal
|
|
|
|
|
|
|
|
fetchRows count cur pKey pVal = do
|
2019-08-29 03:26:59 +03:00
|
|
|
env <- ask
|
|
|
|
V.generateM count $ \i -> runRIO env $ do
|
|
|
|
key <- io $ peek pKey >>= mdbValToWord64
|
|
|
|
val <- io $ 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
|
2019-08-29 03:26:59 +03:00
|
|
|
assertFound idx =<< io (mdb_cursor_get MDB_NEXT cur pKey pVal)
|
2019-07-19 03:52:53 +03:00
|
|
|
pure val
|
2019-05-30 02:19:07 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-08-21 02:32:46 +03:00
|
|
|
Read 1000 rows from the database, starting from key `first`.
|
|
|
|
-}
|
2020-09-25 02:43:03 +03:00
|
|
|
readRowsBatch :: forall e. HasLogFunc e
|
2019-08-29 03:26:59 +03:00
|
|
|
=> Env -> Dbi -> Word64 -> RIO e (V.Vector (Word64, ByteString))
|
2019-08-21 02:32:46 +03:00
|
|
|
readRowsBatch env dbi first = readRows
|
|
|
|
where
|
|
|
|
readRows = do
|
2019-08-30 02:48:46 +03:00
|
|
|
logDebug $ display ("(readRowsBatch) From: " <> tshow first)
|
2019-08-21 02:32:46 +03:00
|
|
|
withWordPtr first $ \pIdx ->
|
2019-08-29 03:26:59 +03:00
|
|
|
withKVPtrs' (MDB_val 8 (castPtr pIdx)) nullVal $ \pKey pVal ->
|
|
|
|
rwith (readTxn env) $ \txn ->
|
|
|
|
rwith (cursor txn dbi) $ \cur ->
|
|
|
|
io (mdb_cursor_get MDB_SET_RANGE cur pKey pVal) >>= \case
|
|
|
|
False -> pure mempty
|
2019-08-30 02:48:46 +03:00
|
|
|
True -> V.unfoldrM (fetchBatch cur pKey pVal) 1000
|
2019-08-29 03:26:59 +03:00
|
|
|
|
2019-08-30 02:48:46 +03:00
|
|
|
fetchBatch :: Cur -> Ptr Val -> Ptr Val -> Word
|
2019-08-29 03:26:59 +03:00
|
|
|
-> RIO e (Maybe ((Word64, ByteString), Word))
|
2019-08-30 02:48:46 +03:00
|
|
|
fetchBatch cur pKey pVal 0 = pure Nothing
|
|
|
|
fetchBatch cur pKey pVal n = do
|
2019-08-29 03:26:59 +03:00
|
|
|
key <- io $ peek pKey >>= mdbValToWord64
|
|
|
|
val <- io $ peek pVal >>= mdbValToBytes
|
|
|
|
io $ mdb_cursor_get MDB_NEXT cur pKey pVal >>= \case
|
2019-08-21 02:32:46 +03:00
|
|
|
False -> pure $ Just ((key, val), 0)
|
|
|
|
True -> pure $ Just ((key, val), pred n)
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
withKVPtrs' :: (MonadIO m, MonadUnliftIO m)
|
|
|
|
=> Val -> Val -> (Ptr Val -> Ptr Val -> m a) -> m a
|
|
|
|
withKVPtrs' k v cb =
|
|
|
|
withRunInIO $ \run ->
|
|
|
|
withKVPtrs k v $ \x y -> run (cb x y)
|
|
|
|
|
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-08-29 03:26:59 +03:00
|
|
|
withWord64AsMDBval :: (MonadIO m, MonadUnliftIO m)
|
|
|
|
=> Word64 -> (MDB_val -> m a) -> m a
|
2019-06-25 04:10:41 +03:00
|
|
|
withWord64AsMDBval w cb = do
|
|
|
|
withWordPtr w $ \p ->
|
|
|
|
cb (MDB_val (fromIntegral (sizeOf w)) (castPtr p))
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
withWordPtr :: (MonadIO m, MonadUnliftIO m)
|
|
|
|
=> Word64 -> (Ptr Word64 -> m a) -> m a
|
|
|
|
withWordPtr w cb =
|
|
|
|
withRunInIO $ \run ->
|
|
|
|
allocaBytes (sizeOf w) (\p -> poke p w >> run (cb p))
|
2019-06-25 04:10:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Lower-Level Operations ------------------------------------------------------
|
|
|
|
|
2019-08-29 03:26:59 +03:00
|
|
|
getMb :: MonadIO m => Txn -> Dbi -> ByteString -> m (Maybe Noun)
|
2019-07-19 03:52:53 +03:00
|
|
|
getMb txn db key =
|
2019-08-29 03:26:59 +03:00
|
|
|
io $
|
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-08-29 03:26:59 +03:00
|
|
|
putNoun :: MonadIO m
|
|
|
|
=> MDB_WriteFlags -> Txn -> Dbi -> ByteString -> Noun -> m Bool
|
2019-06-25 04:10:41 +03:00
|
|
|
putNoun flags txn db key val =
|
2019-08-29 03:26:59 +03:00
|
|
|
io $
|
2019-06-25 04:10:41 +03:00
|
|
|
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-08-29 03:26:59 +03:00
|
|
|
putBytes :: MonadIO m
|
|
|
|
=> MDB_WriteFlags -> Txn -> Dbi -> Word64 -> ByteString -> m Bool
|
|
|
|
putBytes flags txn db id bs =
|
|
|
|
io $
|
|
|
|
withWord64AsMDBval id $ \idVal ->
|
|
|
|
byteStringAsMdbVal bs $ \mVal ->
|
|
|
|
mdb_put flags txn db idVal mVal
|