Skip to content

Commit

Permalink
Do actual Sqlite sorting and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Jul 29, 2019
1 parent c443596 commit 68e0db7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -108,6 +108,8 @@ import Data.List.Split
( chunksOf )
import Data.Map.Strict
( Map )
import Data.Maybe
( catMaybes )
import Data.Maybe
( fromMaybe )
import Data.Quantity
Expand Down Expand Up @@ -137,8 +139,10 @@ import Database.Persist.Sql
, selectList
, updateWhere
, (<-.)
, (<=.)
, (=.)
, (==.)
, (>=.)
)
import Database.Persist.Sqlite
( SqlBackend, SqlPersistT, mkSqliteConnectionInfo, wrapConnectionInfo )
Expand Down Expand Up @@ -306,7 +310,7 @@ newDBLayer logConfig trace fp = do
selectLatestCheckpoint wid >>= \case
Just cp -> do
utxo <- selectUTxO cp
txs <- selectTxHistory @t wid [TxMetaStatus ==. W.Pending]
txs <- selectTxHistory @t wid W.Descending [TxMetaStatus ==. W.Pending]
s <- selectState (checkpointId cp)
pure (checkpointFromEntity @s @t cp utxo txs <$> s)
Nothing -> pure Nothing
Expand Down Expand Up @@ -347,9 +351,12 @@ newDBLayer logConfig trace fp = do
pure $ Right ()
Nothing -> pure $ Left $ ErrNoSuchWallet wid

, readTxHistory = \(PrimaryKey wid) _sortDir _filt ->
, readTxHistory = \(PrimaryKey wid) sortDir filt ->
runQuery $
selectTxHistory @t wid []
selectTxHistory @t wid sortDir $ catMaybes
[ (TxMetaSlotId >=.) <$> W.filterStart filt
, (TxMetaSlotId <=.) <$> W.filterStart filt
]

{-----------------------------------------------------------------------
Keystore
Expand Down Expand Up @@ -739,18 +746,22 @@ selectTxs txids = do
selectTxHistory
:: forall t. PersistTx t
=> W.WalletId
-> W.SortDirection
-> [Filter TxMeta]
-> SqlPersistT IO [(W.Hash "Tx", (W.Tx t, W.TxMeta))]
selectTxHistory wid conditions = do
selectTxHistory wid dir conditions = do
-- Note: there are sorted indices on these columns.
-- The secondary sort by TxId is to make the ordering stable
-- so that testing with random data always works.
let sortOpt = [ Desc TxMetaSlotId, Asc TxMetaTxId ]
metas <- fmap entityVal <$> selectList
((TxMetaWalletId ==. wid) : conditions) sortOpt
let txids = map txMetaTxId metas
(ins, outs) <- selectTxs txids
pure $ txHistoryFromEntity @t metas ins outs
where
sortOpt = case dir of
W.Ascending -> [Asc TxMetaSlotId, Desc TxMetaTxId]
W.Descending -> [Desc TxMetaSlotId, Asc TxMetaTxId]

---------------------------------------------------------------------------
-- DB queries for address discovery state
Expand Down
4 changes: 2 additions & 2 deletions lib/core/test/unit/Cardano/Wallet/DBSpec.hs
Expand Up @@ -423,8 +423,8 @@ readTxHistoryF
-> PrimaryKey WalletId
-> m (Identity GenTxHistory)
readTxHistoryF db wid =
fmap (Identity . GenTxHistory)
$ readTxHistory db wid Descending noFilter
(Identity . GenTxHistory)
<$> readTxHistory db wid Descending noFilter

putTxHistoryF
:: DBLayer m s DummyTarget
Expand Down

0 comments on commit 68e0db7

Please sign in to comment.