Skip to content

Commit

Permalink
move catMaybes for vector compat shim to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhtz committed Mar 1, 2021
1 parent 466c5cf commit 54b25ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/Chainweb/Payload/RestAPI/Server.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -32,9 +31,6 @@ import Control.Monad.Trans.Maybe

import Data.Aeson
import Data.Function
#if ! MIN_VERSION_vector(0,12,2)
import qualified Data.Maybe as M
#endif
import Data.Proxy
import qualified Data.Text.IO as T
import qualified Data.Vector as V
Expand All @@ -52,6 +48,7 @@ import Chainweb.Payload.PayloadStore
import Chainweb.Payload.RestAPI
import Chainweb.RestAPI.Orphans ()
import Chainweb.RestAPI.Utils
import Chainweb.Utils (catMaybesVector)
import Chainweb.Version

import Data.CAS
Expand All @@ -67,13 +64,6 @@ payloadBatchLimit = 100
err404Msg :: ToJSON msg => msg -> ServerError
err404Msg msg = err404 { errBody = encode msg }

catMaybes :: V.Vector (Maybe a) -> V.Vector a
#if MIN_VERSION_vector(0,12,2)
catMaybes = V.catMaybes
#else
catMaybes = V.fromList . M.catMaybes . V.toList
#endif

-- -------------------------------------------------------------------------- --
-- GET Payload Handler

Expand Down Expand Up @@ -111,12 +101,12 @@ payloadBatchHandler
-> [BlockPayloadHash]
-> Handler [PayloadData]
payloadBatchHandler db ks = liftIO $ do
payloads <- catMaybes
payloads <- catMaybesVector
<$> casLookupBatch payloadsDb (V.fromList $ take payloadBatchLimit ks)
txs <- V.zipWith (\a b -> payloadData <$> a <*> pure b)
<$> casLookupBatch txsDb (_blockPayloadTransactionsHash <$> payloads)
<*> pure payloads
return $ V.toList $ catMaybes txs
return $ V.toList $ catMaybesVector txs
where
payloadsDb = _transactionDbBlockPayloads $ _transactionDb db
txsDb = _transactionDbBlockTransactions $ _transactionDb db
Expand Down Expand Up @@ -149,7 +139,7 @@ outputsBatchHandler
-> [BlockPayloadHash]
-> Handler [PayloadWithOutputs]
outputsBatchHandler db ks = liftIO
$ fmap (V.toList . catMaybes)
$ fmap (V.toList . catMaybesVector)
$ casLookupBatch db
$ V.fromList ks

Expand Down
10 changes: 10 additions & 0 deletions src/Chainweb/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module Chainweb.Utils
, alignWithV
, (&)
, IxedGet(..)
, catMaybesVector

-- * Encoding and Serialization
, EncodingException(..)
Expand Down Expand Up @@ -400,6 +401,15 @@ alignWithV f a b = V.zipWith (\a' -> f . These a') a b <> case (V.length a,V.len
| la > lb -> V.map (f . This) $ V.drop lb a
| otherwise -> V.map (f . That) $ V.drop la b

-- | Backward compatibility for 'V.catMaybes'
--
catMaybesVector :: V.Vector (Maybe a) -> V.Vector a
#if MIN_VERSION_vector(0,12,2)
catMaybesVector = V.catMaybes
#else
catMaybesVector = V.fromList . M.catMaybes . V.toList
#endif

-- -------------------------------------------------------------------------- --
-- * Read only Ixed

Expand Down

0 comments on commit 54b25ae

Please sign in to comment.