|
| 1 | +-- Copyright (c) 2014, Facebook, Inc. |
| 2 | +-- All rights reserved. |
| 3 | +-- |
| 4 | +-- This source code is distributed under the terms of a BSD license, |
| 5 | +-- found in the LICENSE file. An additional grant of patent rights can |
| 6 | +-- be found in the PATENTS file. |
| 7 | + |
| 8 | +{-# LANGUAGE ExistentialQuantification #-} |
| 9 | +{-# LANGUAGE RankNTypes #-} |
| 10 | +{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 11 | + |
| 12 | +-- | A cache mapping data requests to their results. |
| 13 | +module Haxl.Core.DataCache |
| 14 | + ( DataCache |
| 15 | + , empty |
| 16 | + , insert |
| 17 | + , lookup |
| 18 | + , showCache |
| 19 | + ) where |
| 20 | + |
| 21 | +import Data.HashMap.Strict (HashMap) |
| 22 | +import Data.Hashable |
| 23 | +import Prelude hiding (lookup) |
| 24 | +import Unsafe.Coerce |
| 25 | +import qualified Data.HashMap.Strict as HashMap |
| 26 | +import Data.Typeable.Internal |
| 27 | +import Data.Maybe |
| 28 | +import Control.Applicative hiding (empty) |
| 29 | +import Control.Exception |
| 30 | + |
| 31 | +import Haxl.Core.Types |
| 32 | + |
| 33 | +-- | The 'DataCache' maps things of type @f a@ to @'ResultVar' a@, for |
| 34 | +-- any @f@ and @a@ provided @f a@ is an instance of 'Typeable'. In |
| 35 | +-- practice @f a@ will be a request type parameterised by its result. |
| 36 | +-- |
| 37 | +-- See the definition of 'ResultVar' for more details. |
| 38 | + |
| 39 | +newtype DataCache = DataCache (HashMap TypeRep SubCache) |
| 40 | + |
| 41 | +-- | The implementation is a two-level map: the outer level maps the |
| 42 | +-- types of requests to 'SubCache', which maps actual requests to their |
| 43 | +-- results. So each 'SubCache' contains requests of the same type. |
| 44 | +-- This works well because we only have to store the dictionaries for |
| 45 | +-- 'Hashable' and 'Eq' once per request type. |
| 46 | +data SubCache = |
| 47 | + forall req a . (Hashable (req a), Eq (req a), Show (req a), Show a) => |
| 48 | + SubCache ! (HashMap (req a) (ResultVar a)) |
| 49 | + -- NB. the inner HashMap is strict, to avoid building up |
| 50 | + -- a chain of thunks during repeated insertions. |
| 51 | + |
| 52 | +-- | A new, empty 'DataCache'. |
| 53 | +empty :: DataCache |
| 54 | +empty = DataCache HashMap.empty |
| 55 | + |
| 56 | +-- | Inserts a request-result pair into the 'DataCache'. |
| 57 | +insert |
| 58 | + :: (Hashable (r a), Typeable (r a), Eq (r a), Show (r a), Show a) |
| 59 | + => r a |
| 60 | + -- ^ Request |
| 61 | + -> ResultVar a |
| 62 | + -- ^ Result |
| 63 | + -> DataCache |
| 64 | + -> DataCache |
| 65 | + |
| 66 | +insert req result (DataCache m) = |
| 67 | + DataCache $ |
| 68 | + HashMap.insertWith fn (typeOf req) |
| 69 | + (SubCache (HashMap.singleton req result)) m |
| 70 | + where |
| 71 | + fn (SubCache new) (SubCache old) = |
| 72 | + SubCache (unsafeCoerce new `HashMap.union` old) |
| 73 | + |
| 74 | +-- | Looks up the cached result of a request. |
| 75 | +lookup |
| 76 | + :: Typeable (r a) |
| 77 | + => r a |
| 78 | + -- ^ Request |
| 79 | + -> DataCache |
| 80 | + -> Maybe (ResultVar a) |
| 81 | + |
| 82 | +lookup req (DataCache m) = |
| 83 | + case HashMap.lookup (typeOf req) m of |
| 84 | + Nothing -> Nothing |
| 85 | + Just (SubCache sc) -> |
| 86 | + unsafeCoerce (HashMap.lookup (unsafeCoerce req) sc) |
| 87 | + |
| 88 | +-- | Dumps the contents of the cache, with requests and responses |
| 89 | +-- converted to 'String's using 'show'. The entries are grouped by |
| 90 | +-- 'TypeRep'. |
| 91 | +-- |
| 92 | +showCache |
| 93 | + :: DataCache |
| 94 | + -> IO [(TypeRep, [(String, Either SomeException String)])] |
| 95 | + |
| 96 | +showCache (DataCache cache) = mapM goSubCache (HashMap.toList cache) |
| 97 | + where |
| 98 | + goSubCache |
| 99 | + :: (TypeRep,SubCache) |
| 100 | + -> IO (TypeRep,[(String, Either SomeException String)]) |
| 101 | + goSubCache (ty, SubCache hmap) = do |
| 102 | + elems <- catMaybes <$> mapM go (HashMap.toList hmap) |
| 103 | + return (ty, elems) |
| 104 | + |
| 105 | + go :: (Show (req a), Show a) |
| 106 | + => (req a, ResultVar a) |
| 107 | + -> IO (Maybe (String, Either SomeException String)) |
| 108 | + go (req, rvar) = do |
| 109 | + maybe_r <- tryReadResult rvar |
| 110 | + case maybe_r of |
| 111 | + Nothing -> return Nothing |
| 112 | + Just (Left e) -> return (Just (show req, Left e)) |
| 113 | + Just (Right result) -> return (Just (show req, Right (show result))) |
0 commit comments