Skip to content

Commit 5627a56

Browse files
committed
yieldCborMapS: support definite length maps
1 parent 131e12b commit 5627a56

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/StreamingLedgerTables.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
module Ouroboros.Consensus.Util.StreamingLedgerTables where
99

1010
import Cardano.Slotting.Slot
11-
import Codec.CBOR.Decoding (Decoder, decodeBreakOr, decodeMapLenIndef)
11+
import Codec.CBOR.Decoding (Decoder, decodeBreakOr, decodeMapLenOrIndef)
1212
import Codec.CBOR.Encoding (Encoding, encodeBreak, encodeMapLenIndef)
1313
import Codec.CBOR.Read
1414
import Codec.CBOR.Write
1515
import Control.Concurrent.Class.MonadMVar
16-
import Control.Monad (unless)
16+
import Control.Monad (replicateM_, unless)
1717
import Control.Monad.Class.MonadAsync
1818
import Control.Monad.Class.MonadST
1919
import Control.Monad.Class.MonadSTM
@@ -87,15 +87,17 @@ yieldCborMapS ::
8787
Stream (Of ByteString) m () ->
8888
Stream (Of (a, b)) m (Stream (Of ByteString) m ())
8989
yieldCborMapS decK decV = execStateT $ do
90-
hoist lift $ decodeCbor decodeMapLenIndef
91-
go
90+
hoist lift (decodeCbor decodeMapLenOrIndef) >>= \case
91+
Nothing -> go
92+
Just n -> replicateM_ n yieldKV
9293
where
94+
yieldKV = do
95+
kv <- hoist lift $ decodeCbor $ (,) <$> decK <*> decV
96+
lift $ S.yield kv
97+
9398
go = do
9499
doBreak <- hoist lift $ decodeCbor decodeBreakOr
95-
unless doBreak $ do
96-
kv <- hoist lift $ decodeCbor $ (,) <$> decK <*> decV
97-
lift $ S.yield kv
98-
go
100+
unless doBreak $ yieldKV *> go
99101

100102
maybeToEither :: a -> Maybe b -> Either a b
101103
maybeToEither _ (Just b) = Right b

0 commit comments

Comments
 (0)