Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite: Fix deletion for larger numbers of checkpoints/transactions #360

Merged
merged 2 commits into from
Jun 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import Data.Text
import Data.Typeable
( Typeable )
import Database.Persist.Class
( PersistField, PersistRecordBackend )
( DeleteCascade, PersistField, PersistRecordBackend )
import Database.Persist.Sql
( Entity (..)
, Filter
Expand All @@ -112,7 +112,6 @@ import Database.Persist.Sql
, selectKeysList
, selectList
, updateWhere
, (/<-.)
, (<-.)
, (=.)
, (==.)
Expand Down Expand Up @@ -590,15 +589,21 @@ chunkedM n f = mapM_ f . chunksOf n
-- any wallet.
deleteLooseTransactions :: SqlPersistM ()
deleteLooseTransactions = do
pendingTxId <- fmap (pendingTxTableId2 . entityVal) <$> selectList [] []
metaTxId <- fmap (txMetaTableTxId . entityVal) <$> selectList [] []
deleteWhere [ TxInputTableTxId /<-. pendingTxId
, TxInputTableTxId /<-. metaTxId ]
deleteWhere [ TxOutputTableTxId /<-. pendingTxId
, TxOutputTableTxId /<-. metaTxId ]
deleteLoose "tx_in"
deleteLoose "tx_out"
where
-- Deletes all TxIn/TxOuts returned by the sub-select.
-- The sub-select outer joins PendingTx and TxMeta with TxIn/TxOut.
-- All rows of the join table with both PendingTx and TxMeta as NULL are
-- loose (unreferenced) transactions.
deleteLoose t = rawExecute ("DELETE FROM "<>t<>" where tx_id IN (SELECT "<>t<>".tx_id FROM "<>t<>" LEFT OUTER JOIN tx_meta ON tx_meta.tx_id = "<>t<>".tx_id LEFT OUTER JOIN pending_tx ON pending_tx.tx_id = "<>t<>".tx_id WHERE (tx_meta.tx_id IS NULL) AND (pending_tx.tx_id IS NULL))") []


deleteMany
::forall typ record. (PersistField typ, PersistRecordBackend record SqlBackend)
:: forall typ record.
( PersistField typ
, DeleteCascade record SqlBackend
, PersistRecordBackend record SqlBackend )
=> [Filter record]
-> EntityField record typ
-> [typ]
Expand All @@ -608,14 +613,13 @@ deleteMany filters entity types
-- we arbitrarily pick 500 which is way below 999. This should prevent the
-- infamous: too many SQL variables
| length types < sz =
deleteWhere ((entity <-. types):filters)
deleteCascadeWhere ((entity <-. types):filters)
| otherwise = do
deleteWhere ((entity <-. take sz types):filters)
deleteCascadeWhere ((entity <-. take sz types):filters)
deleteMany filters entity (drop sz types)
where
sz = 500


selectLatestCheckpoint
:: W.WalletId
-> SqlPersistM (Maybe Checkpoint)
Expand Down Expand Up @@ -703,8 +707,8 @@ instance W.KeyToAddress t => PersistState (W.SeqState t) where
selectList [ SeqStateInternalPoolSeqStateId <-. ssid ] []
extApId <- fmap (seqStateExternalPoolAddressPool . entityVal) <$>
selectList [ SeqStateExternalPoolSeqStateId <-. ssid ] []
deleteCascadeWhere [AddressPoolId <-. intApId]
deleteCascadeWhere [AddressPoolId <-. extApId]
deleteMany [] AddressPoolId intApId
deleteMany [] AddressPoolId extApId
deleteCascadeWhere [SeqStateTableWalletId ==. wid]

insertAddressPool
Expand Down