Copyright | (c) Adam Conner-Sax 2019 |
---|---|
License | BSD-3-Clause |
Maintainer | adam_conner_sax@yahoo.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
This module provides the types and supporting functions to use various serializers with the caching framework in knit-haskell. A Cereal implementation is provided, but using a different implementation is straightforward. See CacheExample2 for an example.
The cache framework expects an explicit dictionary (i.e., record-of-functions) rather than a Typeclass for serialization. Since various serializers use slightly different Typeclass structures and function names, we can't write code which is polynorphic in the serialization type-class. But with an explicit dictionary we get that flexibility. Once a dictionary for encoding and decoding objects of arbitrary type is provided, along with the typeclass constraint required to use it, the code in this module can convert that into all the functions the caching effect requires to cache that data type or streams of it.
This could be implemented as a more straightforward effect but at the cost of complicating the use for streams. Making the explicit dictionary available balances the flexibility of being able to change serializers with the relative ease of bootstrapping the single item serializer into a serializer for streams, etc.
Synopsis
- data SerializeDict c ct = SerializeDict {}
- data Serialize e r a ct where
- type SerializeEnv c ct = Reader (SerializeDict c ct)
- serializeOne :: (c a, MemberWithError (Error SerializationError) r) => SerializeDict c ct -> Serialize SerializationError r a ct
- serializeStreamlyViaList :: (MemberWithError (Error SerializationError) r, Member (Embed IO) r, c [a]) => SerializeDict c ct -> Serialize SerializationError r (SerialT (Sem r) a) ct
- type DefaultCacheData = Array Word8
- type DefaultSerializer = Serialize
- cerealStreamlyDict :: SerializeDict DefaultSerializer DefaultCacheData
- getSerializeDict :: Member (SerializeEnv c ct) r => Sem r (SerializeDict c ct)
- runSerializeEnv :: SerializeDict c ct -> InterpreterFor (SerializeEnv c ct) r
- data SerializationError = SerializationError Text
Types
data SerializeDict c ct Source #
Encoding/decoding functions for Serializing data, made explicit here so we can pass them around as part of a configuration. Allows for different Serializers as well as Serializing to different types of in-memory store.
NB: The first parameter is of kind Type -> Constraint
, e.g., S.Serialize
,
which must be satisfied by anything serializable by
the implementation.
This should be straightforward to write for any serializer, and is all that's required to use
a non-default serializer as long as it serializes to ByteStream
(or, less likely, Streamly.Memory.Array.Array
)
data Serialize e r a ct where Source #
Record-of-functions type to carry encoding/decoding functions for Serializing data.
Allows for different Serializers as well as
Serializing to different types of in memory store.
encode
returns the encoded value *and* a (possibly buffered) copy of its input.
This is designed around serialization of streams, where the original (effectful) stream may be expensive to run. But once run,
we can return a "buffered" stream which just unfolds from a memory buffer.
In many cases, we will just return the input in that slot.
type SerializeEnv c ct = Reader (SerializeDict c ct) Source #
Make the dictionary available within effect stacks
Deploy Implementations
serializeOne :: (c a, MemberWithError (Error SerializationError) r) => SerializeDict c ct -> Serialize SerializationError r a ct Source #
Given a
and SerializeDict
c cta
satisfying c a
,
produce the (trivial) Serialize
record-of-functions to encode/decode a single a
.
serializeStreamlyViaList :: (MemberWithError (Error SerializationError) r, Member (Embed IO) r, c [a]) => SerializeDict c ct -> Serialize SerializationError r (SerialT (Sem r) a) ct Source #
Given a
and SerializeDict
c cta
satisfying c [a]
--usually
true as long as a
satisfies c a
--produce
the Serialize
record-of-functions to encode/decode Streamly.SerialT (P.Sem r) a
,
by mapping the stream to a (lazy) list, and encoding that and
decoding as a list and creating the stream from that.
Implementations
type DefaultCacheData = Array Word8 Source #
type-alias for default in-memory storage type.
type DefaultSerializer = Serialize Source #
type-alias for default Serializer
cerealStreamlyDict :: SerializeDict DefaultSerializer DefaultCacheData Source #
Implementation of SerializeDict
for the cereal serializer,
encoding to/decoding from Array
Reader for Serializer Dictionary
getSerializeDict :: Member (SerializeEnv c ct) r => Sem r (SerializeDict c ct) Source #
access the dictionary
runSerializeEnv :: SerializeDict c ct -> InterpreterFor (SerializeEnv c ct) r Source #
run the SerializeEnv effect by giving it the dictionary for use by the cache functions
Errors
data SerializationError Source #
Error Type for Serialization errors. Simplifies catching and reporting them.
Instances
Show SerializationError Source # | |
Defined in Knit.Effect.Serialize showsPrec :: Int -> SerializationError -> ShowS # show :: SerializationError -> String # showList :: [SerializationError] -> ShowS # |