适应流媒体库的存储解码

时间:2018-03-13 01:07:49

标签: haskell haskell-streaming

我正在尝试为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的逻辑错误提出一些指示。

1 个答案:

答案 0 :(得分:0)

发生PeekException因为Store在以流媒体模式保存邮件时使用不同的格式,与Binary不同。在使用Message函数时,它期望在Store数据周围使用decodeMessageBS类型的包装器。 decodeIOPortionWith不期望Message包装器,因此可以正常使用已保存的Store数据。修复序列化以将数据保存为Message编码后,decodeMessageBS在该数据上运行良好。