Skip to content

Commit 27c76b1

Browse files
committed
Improve OffChain loading queries
1 parent bb1c820 commit 27c76b1

File tree

1 file changed

+35
-21
lines changed
  • cardano-db-sync/src/Cardano/DbSync/OffChain

1 file changed

+35
-21
lines changed

cardano-db-sync/src/Cardano/DbSync/OffChain/Query.hs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Cardano.Db (
1414
OffChainPoolFetchErrorId,
1515
OffChainVoteData,
1616
OffChainVoteFetchError,
17+
OffChainVoteFetchErrorId,
1718
PoolHash,
1819
PoolHashId,
1920
PoolMetaHash (PoolMetaHash),
@@ -36,12 +37,13 @@ import Database.Esqueleto.Experimental (
3637
SqlExpr,
3738
Value (..),
3839
ValueList,
39-
desc,
40+
asc,
4041
from,
4142
groupBy,
4243
in_,
4344
innerJoin,
4445
just,
46+
limit,
4547
max_,
4648
notExists,
4749
on,
@@ -63,16 +65,16 @@ import System.Random.Shuffle (shuffleM)
6365
---------------------------------------------------------------------------------------------------------------------------------
6466
getOffChainVoteData :: MonadIO m => POSIXTime -> Int -> ReaderT SqlBackend m [OffChainVoteWorkQueue]
6567
getOffChainVoteData now maxCount = do
66-
xs <- queryNewVoteWorkQueue now
68+
xs <- queryNewVoteWorkQueue now maxCount
6769
if length xs >= maxCount
6870
then take maxCount <$> liftIO (shuffleM xs)
6971
else do
70-
ys <- queryOffChainVoteWorkQueue (Time.posixSecondsToUTCTime now)
72+
ys <- queryOffChainVoteWorkQueue (Time.posixSecondsToUTCTime now) maxCount
7173
take maxCount . (xs ++) <$> liftIO (shuffleM ys)
7274

7375
-- get all the voting anchors that don't already exist in OffChainVoteData or OffChainVoteFetchError
74-
queryNewVoteWorkQueue :: MonadIO m => POSIXTime -> ReaderT SqlBackend m [OffChainVoteWorkQueue]
75-
queryNewVoteWorkQueue now = do
76+
queryNewVoteWorkQueue :: MonadIO m => POSIXTime -> Int -> ReaderT SqlBackend m [OffChainVoteWorkQueue]
77+
queryNewVoteWorkQueue now maxCount = do
7678
res <- select $ do
7779
va <- from $ table @VotingAnchor
7880
where_
@@ -85,6 +87,7 @@ queryNewVoteWorkQueue now = do
8587
from (table @OffChainVoteFetchError) >>= \ocvfe ->
8688
where_ (ocvfe ^. OffChainVoteFetchErrorVotingAnchorId ==. va ^. VotingAnchorId)
8789
)
90+
limit $ fromIntegral maxCount
8891
pure
8992
( va ^. VotingAnchorId
9093
, va ^. VotingAnchorDataHash
@@ -101,20 +104,17 @@ queryNewVoteWorkQueue now = do
101104
, oVoteWqUrl = url
102105
}
103106

104-
queryOffChainVoteWorkQueue :: MonadIO m => UTCTime -> ReaderT SqlBackend m [OffChainVoteWorkQueue]
105-
queryOffChainVoteWorkQueue _now = do
107+
queryOffChainVoteWorkQueue :: MonadIO m => UTCTime -> Int -> ReaderT SqlBackend m [OffChainVoteWorkQueue]
108+
queryOffChainVoteWorkQueue _now maxCount = do
106109
res <- select $ do
107110
(va :& ocpfe) <-
108111
from
109112
$ table @VotingAnchor
110113
`innerJoin` table @OffChainVoteFetchError
111114
`on` (\(va :& ocpfe) -> ocpfe ^. OffChainVoteFetchErrorVotingAnchorId ==. va ^. VotingAnchorId)
112-
where_
113-
( notExists $
114-
from (table @OffChainVoteData) >>= \ocvd ->
115-
where_ (ocvd ^. OffChainVoteDataVotingAnchorId ==. ocpfe ^. OffChainVoteFetchErrorVotingAnchorId)
116-
)
117-
orderBy [desc (ocpfe ^. OffChainVoteFetchErrorFetchTime)]
115+
orderBy [asc (ocpfe ^. OffChainVoteFetchErrorId)]
116+
where_ (just (ocpfe ^. OffChainVoteFetchErrorId) `in_` latestRefs)
117+
limit $ fromIntegral maxCount
118118
pure
119119
( ocpfe ^. OffChainVoteFetchErrorFetchTime
120120
, va ^. VotingAnchorId
@@ -133,23 +133,35 @@ queryOffChainVoteWorkQueue _now = do
133133
, oVoteWqUrl = url
134134
}
135135

136+
latestRefs :: SqlExpr (ValueList (Maybe OffChainVoteFetchErrorId))
137+
latestRefs =
138+
subList_select $ do
139+
ocvfe <- from (table @OffChainVoteFetchError)
140+
groupBy (ocvfe ^. OffChainVoteFetchErrorVotingAnchorId)
141+
where_
142+
( notExists $
143+
from (table @OffChainVoteData) >>= \ocvd ->
144+
where_ (ocvd ^. OffChainVoteDataVotingAnchorId ==. ocvfe ^. OffChainVoteFetchErrorVotingAnchorId)
145+
)
146+
pure $ max_ (ocvfe ^. OffChainVoteFetchErrorId)
147+
136148
---------------------------------------------------------------------------------------------------------------------------------
137149
-- Query OffChain PoolData
138150
---------------------------------------------------------------------------------------------------------------------------------
139151
getOffChainPoolData :: MonadIO m => POSIXTime -> Int -> ReaderT SqlBackend m [OffChainPoolWorkQueue]
140152
getOffChainPoolData now maxCount = do
141153
-- Results from the query are shuffles so we don't continuously get the same entries.
142-
xs <- queryNewPoolWorkQueue now
154+
xs <- queryNewPoolWorkQueue now maxCount
143155
if length xs >= maxCount
144156
then take maxCount <$> liftIO (shuffleM xs)
145157
else do
146-
ys <- queryOffChainPoolWorkQueue (Time.posixSecondsToUTCTime now)
158+
ys <- queryOffChainPoolWorkQueue (Time.posixSecondsToUTCTime now) maxCount
147159
take maxCount . (xs ++) <$> liftIO (shuffleM ys)
148160

149161
-- Get pool work queue data for new pools (ie pools that had OffChainPoolData entry and no
150162
-- OffChainPoolFetchError).
151-
queryNewPoolWorkQueue :: MonadIO m => POSIXTime -> ReaderT SqlBackend m [OffChainPoolWorkQueue]
152-
queryNewPoolWorkQueue now = do
163+
queryNewPoolWorkQueue :: MonadIO m => POSIXTime -> Int -> ReaderT SqlBackend m [OffChainPoolWorkQueue]
164+
queryNewPoolWorkQueue now maxCount = do
153165
res <- select $ do
154166
(ph :& pmr) <-
155167
from
@@ -167,6 +179,7 @@ queryNewPoolWorkQueue now = do
167179
from (table @OffChainPoolFetchError) >>= \pofe ->
168180
where_ (pofe ^. OffChainPoolFetchErrorPmrId ==. pmr ^. PoolMetadataRefId)
169181
)
182+
limit $ fromIntegral maxCount
170183
pure
171184
( ph ^. PoolHashId
172185
, pmr ^. PoolMetadataRefId
@@ -198,8 +211,8 @@ queryNewPoolWorkQueue now = do
198211
}
199212

200213
-- Get pool fetch data for pools that have previously errored.
201-
queryOffChainPoolWorkQueue :: MonadIO m => UTCTime -> ReaderT SqlBackend m [OffChainPoolWorkQueue]
202-
queryOffChainPoolWorkQueue _now = do
214+
queryOffChainPoolWorkQueue :: MonadIO m => UTCTime -> Int -> ReaderT SqlBackend m [OffChainPoolWorkQueue]
215+
queryOffChainPoolWorkQueue _now maxCount = do
203216
res <- select $ do
204217
(ph :& pmr :& pofe) <-
205218
from
@@ -209,8 +222,8 @@ queryOffChainPoolWorkQueue _now = do
209222
`innerJoin` table @OffChainPoolFetchError
210223
`on` (\(_ph :& pmr :& pofe) -> pofe ^. OffChainPoolFetchErrorPmrId ==. pmr ^. PoolMetadataRefId)
211224
where_ (just (pofe ^. OffChainPoolFetchErrorId) `in_` latestRefs)
212-
where_ (notExists $ from (table @OffChainPoolData) >>= \pod -> where_ (pod ^. OffChainPoolDataPmrId ==. pofe ^. OffChainPoolFetchErrorPmrId))
213-
orderBy [desc (pofe ^. OffChainPoolFetchErrorFetchTime)]
225+
orderBy [asc (pofe ^. OffChainPoolFetchErrorId)]
226+
limit $ fromIntegral maxCount
214227
pure
215228
( pofe ^. OffChainPoolFetchErrorFetchTime
216229
, pofe ^. OffChainPoolFetchErrorPmrId
@@ -228,6 +241,7 @@ queryOffChainPoolWorkQueue _now = do
228241
latestRefs =
229242
subList_select $ do
230243
pofe <- from (table @OffChainPoolFetchError)
244+
where_ (notExists $ from (table @OffChainPoolData) >>= \pod -> where_ (pod ^. OffChainPoolDataPmrId ==. pofe ^. OffChainPoolFetchErrorPmrId))
231245
groupBy (pofe ^. OffChainPoolFetchErrorPoolId)
232246
pure $ max_ (pofe ^. OffChainPoolFetchErrorId)
233247

0 commit comments

Comments
 (0)