| 1 |
{-# LANGUAGE FlexibleInstances #-} |
| 2 |
|
| 3 |
{- Copyright (c) 2004-5 Thomas Jaeger, Don Stewart |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
02111-1307, USA. -} |
| 19 |
|
| 20 |
-- | Serialisation |
| 21 |
module Lambdabot.Util.Serial |
| 22 |
( Serial(..) |
| 23 |
|
| 24 |
, stdSerial |
| 25 |
, mapSerial |
| 26 |
, mapPackedSerial |
| 27 |
, assocListPackedSerial |
| 28 |
, mapListPackedSerial |
| 29 |
, readM |
| 30 |
, Packable(..) {- instances of Packable -} |
| 31 |
, readOnly |
| 32 |
) where |
| 33 |
|
| 34 |
import Control.Monad.Fail (MonadFail) |
| 35 |
|
| 36 |
import Data.Maybe (mapMaybe) |
| 37 |
|
| 38 |
import Data.Map (Map) |
| 39 |
import qualified Data.Map as M |
| 40 |
|
| 41 |
import qualified Data.ByteString.Char8 as P |
| 42 |
import Data.ByteString.Char8 (ByteString) |
| 43 |
|
| 44 |
import Data.ByteString.Lazy (fromChunks,toChunks) |
| 45 |
|
| 46 |
import Codec.Compression.GZip |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
|
| 50 |
-- A flexible (moreso than a typeclass) way to define introduction and |
| 51 |
-- elimination for persistent state on a per-module basis. |
| 52 |
-- |
| 53 |
data Serial s = Serial { |
| 54 |
serialize :: s -> Maybe ByteString, |
| 55 |
deserialize :: ByteString -> Maybe s |
| 56 |
} |
| 57 |
|
| 58 |
gzip :: ByteString -> ByteString |
| 59 |
gzip = P.concat . toChunks . compress . fromChunks . (:[]) |
| 60 |
|
| 61 |
gunzip :: ByteString -> ByteString |
| 62 |
gunzip = P.concat . toChunks . decompress . fromChunks . (:[]) |
| 63 |
|
| 64 |
-- |
| 65 |
-- read-only serialisation |
| 66 |
-- |
| 67 |
readOnly :: (ByteString -> b) -> Serial b |
| 68 |
readOnly f = Serial (const Nothing) (Just . f) |
| 69 |
|
| 70 |
-- | Default `instance' for a Serial |
| 71 |
stdSerial :: (Show s, Read s) => Serial s |
| 72 |
stdSerial = Serial (Just. P.pack.show) (readM.P.unpack) |
| 73 |
|
| 74 |
-- | Serializes a 'Map' type if both the key and the value are instances |
| 75 |
-- of Read and Show. The serialization is done by converting the map to |
| 76 |
-- and from lists. Results are saved line-wise, for better editing and |
| 77 |
-- revision control. |
| 78 |
-- |
| 79 |
mapSerial :: (Ord k, Show k, Show v, Read k, Read v) => Serial (Map k v) |
| 80 |
mapSerial = Serial { |
| 81 |
serialize = Just . P.pack . unlines . map show . M.toList, |
| 82 |
deserialize = Just . M.fromList . mapMaybe (readM . P.unpack) . P.lines |
| 83 |
} |
| 84 |
|
| 85 |
------------------------------------------------------------------------ |
| 86 |
|
| 87 |
-- | 'readM' behaves like read, but catches failure in a monad. |
| 88 |
-- this allocates a 20-30 M on startup... |
| 89 |
readM :: (MonadFail m, Read a) => String -> m a |
| 90 |
readM s = case [x | (x,t) <- {-# SCC "Serial.readM.reads" #-} reads s -- bad! |
| 91 |
, ("","") <- lex t] of |
| 92 |
[x] -> return x |
| 93 |
[] -> fail "Serial.readM: no parse" |
| 94 |
_ -> fail "Serial.readM: ambiguous parse" |
| 95 |
|
| 96 |
class Packable t where |
| 97 |
readPacked :: ByteString -> t |
| 98 |
showPacked :: t -> ByteString |
| 99 |
|
| 100 |
-- | An instance for Map Packed [Packed] |
| 101 |
-- uses gzip compression |
| 102 |
instance Packable (Map ByteString [ByteString]) where |
| 103 |
readPacked ps = M.fromList (readKV ( P.lines . gunzip $ ps)) |
| 104 |
where |
| 105 |
readKV :: [ByteString] -> [(ByteString,[ByteString])] |
| 106 |
readKV [] = [] |
| 107 |
readKV (k:rest) = let (vs, rest') = break (== P.empty) rest |
| 108 |
in (k,vs) : readKV (drop 1 rest') |
| 109 |
|
| 110 |
showPacked m = gzip |
| 111 |
. P.unlines |
| 112 |
. concatMap (\(k,vs) -> k : vs ++ [P.empty]) $ M.toList m |
| 113 |
|
| 114 |
-- assumes single line second strings |
| 115 |
instance Packable (Map ByteString ByteString) where |
| 116 |
readPacked ps = M.fromList (readKV (P.lines . gunzip $ ps)) |
| 117 |
where |
| 118 |
readKV :: [ByteString] -> [(ByteString,ByteString)] |
| 119 |
readKV [] = [] |
| 120 |
readKV (k:v:rest) = (k,v) : readKV rest |
| 121 |
readKV _ = error "Serial.readPacked: parse failed" |
| 122 |
|
| 123 |
showPacked m = gzip. P.unlines . concatMap (\(k,v) -> [k,v]) $ M.toList m |
| 124 |
|
| 125 |
instance Packable ([(ByteString,ByteString)]) where |
| 126 |
readPacked ps = readKV (P.lines . gunzip $ ps) |
| 127 |
where |
| 128 |
readKV :: [ByteString] -> [(ByteString,ByteString)] |
| 129 |
readKV [] = [] |
| 130 |
readKV (k:v:rest) = (k,v) : readKV rest |
| 131 |
readKV _ = error "Serial.readPacked: parse failed" |
| 132 |
|
| 133 |
showPacked = gzip . P.unlines . concatMap (\(k,v) -> [k,v]) |
| 134 |
|
| 135 |
-- The following instance is used by the `poll` plugin. |
| 136 |
-- The `read` and `show` are there for backward compatibility. |
| 137 |
instance Packable (M.Map P.ByteString (Bool, [(P.ByteString, Int)])) where |
| 138 |
readPacked = M.fromList . readKV . P.lines |
| 139 |
where |
| 140 |
readKV :: [P.ByteString] -> [(P.ByteString,(Bool, [(P.ByteString, Int)]))] |
| 141 |
readKV [] = [] |
| 142 |
readKV (k:v:rest) = (k, (read . P.unpack) v) : readKV rest |
| 143 |
readKV _ = error "Vote.readPacked: parse failed" |
| 144 |
|
| 145 |
showPacked m = P.unlines . concatMap (\(k,v) -> [k,P.pack . show $ v]) $ M.toList m |
| 146 |
|
| 147 |
-- And for packed string maps |
| 148 |
mapPackedSerial :: Serial (Map ByteString ByteString) |
| 149 |
mapPackedSerial = Serial (Just . showPacked) (Just . readPacked) |
| 150 |
|
| 151 |
-- And for list of packed string maps |
| 152 |
mapListPackedSerial :: Serial (Map ByteString [ByteString]) |
| 153 |
mapListPackedSerial = Serial (Just . showPacked) (Just . readPacked) |
| 154 |
|
| 155 |
-- And for association list |
| 156 |
assocListPackedSerial :: Serial ([(ByteString,ByteString)]) |
| 157 |
assocListPackedSerial = Serial (Just . showPacked) (Just . readPacked) |
| 158 |
|
| 159 |
------------------------------------------------------------------------ |