module Codec.Compression.SnappyC.Framed
(
compress
, EncodeParams(..)
, FrameSize
, Threshold(..)
, compressWithParams
, defaultFrameSize
, customFrameSize
, unFrameSize
, DecodeFailure(..)
, decompress
, decompress'
, DecodeParams(..)
, decompressWithParams
, decompressWithParams'
, Encoder
, initializeEncoder
, finalizeEncoder
, compressStep
, Decoder
, initializeDecoder
, finalizeDecoder
, decompressStep
, decompressStep'
) where
import Codec.Compression.SnappyC.Internal.Buffer qualified as Buffer
import Codec.Compression.SnappyC.Internal.FrameFormat
import Codec.Compression.SnappyC.Internal.Util
import Data.ByteString.Internal qualified as Strict (ByteString)
import Data.ByteString.Lazy qualified as Lazy (ByteString)
import Data.ByteString.Lazy qualified as BS.Lazy
import Data.Default
import GHC.Stack
compress :: BS.Lazy.ByteString -> BS.Lazy.ByteString
compress :: ByteString -> ByteString
compress = EncodeParams -> ByteString -> ByteString
compressWithParams EncodeParams
forall a. Default a => a
def
compressWithParams :: EncodeParams -> Lazy.ByteString -> Lazy.ByteString
compressWithParams :: EncodeParams -> ByteString -> ByteString
compressWithParams EncodeParams
ps =
[ByteString] -> ByteString
BS.Lazy.fromChunks
([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString
streamId ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:)
([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoder -> [ByteString] -> [ByteString]
go Encoder
initialEncoder
([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BS.Lazy.toChunks
where
streamId :: Strict.ByteString
initialEncoder :: Encoder
(ByteString
streamId, Encoder
initialEncoder) = (ByteString, Encoder)
initializeEncoder
go ::
Encoder
-> [Strict.ByteString]
-> [Strict.ByteString]
go :: Encoder -> [ByteString] -> [ByteString]
go Encoder
encoder =
\case
[] ->
EncodeParams -> Encoder -> [ByteString]
finalizeEncoder EncodeParams
ps Encoder
encoder
(ByteString
c:[ByteString]
cs) ->
case EncodeParams -> Encoder -> ByteString -> ([ByteString], Encoder)
compressStep EncodeParams
ps Encoder
encoder ByteString
c of
([ByteString]
encodedChunks, Encoder
encoder') ->
[ByteString]
encodedChunks [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ Encoder -> [ByteString] -> [ByteString]
go Encoder
encoder' [ByteString]
cs
compressStep ::
EncodeParams
-> Encoder
-> Strict.ByteString
-> ([Strict.ByteString], Encoder)
compressStep :: EncodeParams -> Encoder -> ByteString -> ([ByteString], Encoder)
compressStep EncodeParams
ps (Encoder Buffer
b) ByteString
bs =
let
EncodeResult{[ByteString]
Encoder
encodeResultEncoded :: [ByteString]
encodeResultEncoder :: Encoder
encodeResultEncoder :: EncodeResult -> Encoder
encodeResultEncoded :: EncodeResult -> [ByteString]
..} = EncodeParams -> Encoder -> EncodeResult
encodeBuffered EncodeParams
ps (Buffer -> Encoder
Encoder (Buffer -> Encoder) -> Buffer -> Encoder
forall a b. (a -> b) -> a -> b
$ Buffer
b Buffer -> ByteString -> Buffer
`Buffer.append` ByteString
bs)
in
([ByteString]
encodeResultEncoded, Encoder
encodeResultEncoder)
decompress :: HasCallStack => Lazy.ByteString -> Lazy.ByteString
decompress :: HasCallStack => ByteString -> ByteString
decompress = HasCallStack => DecodeParams -> ByteString -> ByteString
DecodeParams -> ByteString -> ByteString
decompressWithParams DecodeParams
forall a. Default a => a
def
decompressWithParams ::
HasCallStack
=> DecodeParams
-> Lazy.ByteString
-> Lazy.ByteString
decompressWithParams :: HasCallStack => DecodeParams -> ByteString -> ByteString
decompressWithParams DecodeParams
dps =
[ByteString] -> ByteString
BS.Lazy.fromChunks
([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Decoder -> [ByteString] -> [ByteString]
go Decoder
initializeDecoder
([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BS.Lazy.toChunks
where
go ::
Decoder
-> [Strict.ByteString]
-> [Strict.ByteString]
go :: Decoder -> [ByteString] -> [ByteString]
go Decoder
decoder =
\case
[] ->
Either DecodeFailure () -> ()
forall e a. Exception e => Either e a -> a
throwLeft (Decoder -> Either DecodeFailure ()
finalizeDecoder Decoder
decoder) () -> [ByteString] -> [ByteString]
forall a b. a -> b -> b
`seq` []
(ByteString
c:[ByteString]
cs) ->
let
([ByteString]
decompressed, Decoder
decoder') = HasCallStack =>
DecodeParams -> Decoder -> ByteString -> ([ByteString], Decoder)
DecodeParams -> Decoder -> ByteString -> ([ByteString], Decoder)
decompressStep DecodeParams
dps Decoder
decoder ByteString
c
in
[ByteString]
decompressed [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ Decoder -> [ByteString] -> [ByteString]
go Decoder
decoder' [ByteString]
cs
decompressStep ::
HasCallStack
=> DecodeParams
-> Decoder
-> Strict.ByteString
-> ([Strict.ByteString], Decoder)
decompressStep :: HasCallStack =>
DecodeParams -> Decoder -> ByteString -> ([ByteString], Decoder)
decompressStep DecodeParams
dps d :: Decoder
d@Decoder{Buffer
DecodeState
decoderBuffer :: Buffer
decoderState :: DecodeState
decoderState :: Decoder -> DecodeState
decoderBuffer :: Decoder -> Buffer
..} ByteString
bs =
let
DecodeResult{[ByteString]
Decoder
decodeResultDecoded :: [ByteString]
decodeResultDecoder :: Decoder
decodeResultDecoder :: DecodeResult -> Decoder
decodeResultDecoded :: DecodeResult -> [ByteString]
..} =
Either DecodeFailure DecodeResult -> DecodeResult
forall e a. Exception e => Either e a -> a
throwLeft (Either DecodeFailure DecodeResult -> DecodeResult)
-> Either DecodeFailure DecodeResult -> DecodeResult
forall a b. (a -> b) -> a -> b
$
DecodeParams -> Decoder -> Either DecodeFailure DecodeResult
decodeBuffered
DecodeParams
dps
Decoder
d { decoderBuffer = decoderBuffer `Buffer.append` bs }
in
( [ByteString]
decodeResultDecoded
, Decoder
decodeResultDecoder
)
{-# DEPRECATED decompress' "Consider using decompress or decompressStep' instead" #-}
decompress' :: Lazy.ByteString -> Either DecodeFailure Lazy.ByteString
decompress' :: ByteString -> Either DecodeFailure ByteString
decompress' = DecodeParams -> ByteString -> Either DecodeFailure ByteString
decompressWithParams' DecodeParams
forall a. Default a => a
def
{-# DEPRECATED decompressWithParams' "Consider using decompressWithParams or decompressStep' instead" #-}
decompressWithParams' ::
DecodeParams
-> Lazy.ByteString
-> Either DecodeFailure Lazy.ByteString
decompressWithParams' :: DecodeParams -> ByteString -> Either DecodeFailure ByteString
decompressWithParams' DecodeParams
dps ByteString
compressed = do
[ByteString]
decompressedChunks <- Decoder -> [ByteString] -> Either DecodeFailure [ByteString]
go Decoder
initializeDecoder ([ByteString] -> Either DecodeFailure [ByteString])
-> [ByteString] -> Either DecodeFailure [ByteString]
forall a b. (a -> b) -> a -> b
$ ByteString -> [ByteString]
BS.Lazy.toChunks ByteString
compressed
ByteString -> Either DecodeFailure ByteString
forall a. a -> Either DecodeFailure a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either DecodeFailure ByteString)
-> ByteString -> Either DecodeFailure ByteString
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
BS.Lazy.fromChunks [ByteString]
decompressedChunks
where
go ::
Decoder
-> [Strict.ByteString]
-> Either DecodeFailure [Strict.ByteString]
go :: Decoder -> [ByteString] -> Either DecodeFailure [ByteString]
go Decoder
decoder =
\case
[] -> do
Decoder -> Either DecodeFailure ()
finalizeDecoder Decoder
decoder
[ByteString] -> Either DecodeFailure [ByteString]
forall a. a -> Either DecodeFailure a
forall (m :: * -> *) a. Monad m => a -> m a
return []
(ByteString
c:[ByteString]
cs) -> do
([ByteString]
decompressed, Decoder
decompressor') <- DecodeParams
-> Decoder
-> ByteString
-> Either DecodeFailure ([ByteString], Decoder)
decompressStep' DecodeParams
dps Decoder
decoder ByteString
c
([ByteString]
decompressed [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++) ([ByteString] -> [ByteString])
-> Either DecodeFailure [ByteString]
-> Either DecodeFailure [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder -> [ByteString] -> Either DecodeFailure [ByteString]
go Decoder
decompressor' [ByteString]
cs
decompressStep' ::
DecodeParams
-> Decoder
-> Strict.ByteString
-> Either DecodeFailure ([Strict.ByteString], Decoder)
decompressStep' :: DecodeParams
-> Decoder
-> ByteString
-> Either DecodeFailure ([ByteString], Decoder)
decompressStep' DecodeParams
dps d :: Decoder
d@Decoder{Buffer
DecodeState
decoderState :: Decoder -> DecodeState
decoderBuffer :: Decoder -> Buffer
decoderBuffer :: Buffer
decoderState :: DecodeState
..} ByteString
bs = do
DecodeResult{[ByteString]
Decoder
decodeResultDecoder :: DecodeResult -> Decoder
decodeResultDecoded :: DecodeResult -> [ByteString]
decodeResultDecoded :: [ByteString]
decodeResultDecoder :: Decoder
..} <-
DecodeParams -> Decoder -> Either DecodeFailure DecodeResult
decodeBuffered DecodeParams
dps Decoder
d { decoderBuffer = decoderBuffer `Buffer.append` bs }
([ByteString], Decoder)
-> Either DecodeFailure ([ByteString], Decoder)
forall a. a -> Either DecodeFailure a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString]
decodeResultDecoded, Decoder
decodeResultDecoder)