[英]Adapting Store decoding for streaming library
我正在嘗試將Store
編碼和解碼用於Streaming
。 Store
已經使用稱為decodeMessageBS
的功能實現了流式decode
。
我嘗試按照以下方式對流進行store
反序列化的基本實現(現在為了簡化Streaming
,沒有bracket
)。 但是, popper
的邏輯似乎有問題,因為decodeMessageBS
不斷拋出PeekException
:
{-# LANGUAGE RankNTypes #-}
import Streaming.Prelude as S hiding (print,show)
import Data.IORef
import Streaming as S
import qualified Data.ByteString as BS (ByteString,empty,length)
import System.IO.ByteBuffer
import Data.Store
import Data.Store.Streaming
streamDecode :: forall a. (Store a) => ByteBuffer -> Stream (Of BS.ByteString) IO () -> Stream (Of a) IO ()
streamDecode bb inp = do
ref <- lift $ newIORef inp
let popper = do
r <- S.uncons =<< readIORef ref
case r of
Nothing -> return Nothing
Just (a,rest) -> writeIORef ref rest >> return (Just a)
let go = do
r <- lift $ decodeMessageBS bb $ popper
lift $ print "Decoding"
case r of
Nothing -> return ()
Just msg -> (lift $ print "Message found") >> (S.yield . fromMessage $ msg) >> go
go
我可以使用decodeIOPortionWith
很好地解碼我的測試文件,因此,問題似乎出在提供decodeMessageBS
所需的邏輯中。 在這里,將感謝您指出popper
邏輯到底出了什么問題。
PeekException
會發生PeekException
,是因為在以流模式保存消息時, Store
使用不同的格式,這與Binary
不同。 當使用decodeMessageBS
函數時,它期望在Store
數據周圍使用Message
類型的包裝器。 decodeIOPortionWith
不需要Message
包裝器,因此可以與保存的向下Store
數據一起正常工作。 在修復序列化以將數據保存為Message
編碼后, decodeMessageBS
在該數據上工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.