From 2ba1c155d7549d9461d8deba3f91abb433237685 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Mon, 16 Dec 2019 19:36:02 +0100 Subject: [PATCH] actually create one MVar per DB context ... --- lib/core/src/Cardano/Wallet/DB/Sqlite.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs index 6fc72584a8c..63e8c05b458 100644 --- a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs +++ b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs @@ -92,7 +92,7 @@ import Cardano.Wallet.Primitive.AddressDiscovery import Control.Arrow ( (***) ) import Control.Concurrent.MVar - ( newEmptyMVar, putMVar, readMVar ) + ( modifyMVar_, newMVar ) import Control.DeepSeq ( NFData ) import Control.Exception @@ -111,6 +111,8 @@ import Data.Coerce ( coerce ) import Data.Either ( isRight ) +import Data.Functor + ( ($>) ) import Data.Generics.Internal.VL.Lens ( (^.) ) import Data.List.Split @@ -220,7 +222,7 @@ newDBFactory -- ^ Path to database directory, or Nothing for in-memory database -> IO (DBFactory IO s k) newDBFactory cfg tr mDatabaseDir = do - mvar <- newEmptyMVar + mvar <- newMVar mempty case mDatabaseDir of Nothing -> pure DBFactory { withDatabase = \_ action -> @@ -229,17 +231,20 @@ newDBFactory cfg tr mDatabaseDir = do pure () } Just databaseDir -> pure DBFactory - { withDatabase = \wid action -> - withDBLayer cfg tracerDB (Just $ databaseFile wid) $ \(ctx,db) -> do - putMVar mvar ctx - action db + { withDatabase = \wid action -> do + withDBLayer cfg tracerDB (Just $ databaseFile wid) + $ \(ctx, db) -> do + modifyMVar_ mvar (pure . Map.insert wid ctx) + action db , removeDatabase = \wid -> do let files = [ databaseFile wid , databaseFile wid <> "-wal" , databaseFile wid <> "-shm" ] - readMVar mvar >>= destroyDBLayer + modifyMVar_ mvar $ \m -> case Map.lookup wid m of + Nothing -> pure m + Just ctx -> destroyDBLayer ctx $> Map.delete wid m mapM_ removePathForcibly files } where