Skip to content

Commit

Permalink
trace-forward: Datapoint strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaro committed May 30, 2023
1 parent 79ad09d commit a4d6814
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions trace-forward/src/Trace/Forward/Utils/DataPoint.hs
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}


module Trace.Forward.Utils.DataPoint
( DataPoint (..)
, DataPointStore
Expand All @@ -14,8 +15,9 @@ module Trace.Forward.Utils.DataPoint
) where

import Control.Concurrent.STM (atomically, check, orElse)
import Control.Concurrent.STM.TVar
import Control.Concurrent.STM.TMVar
import Control.Concurrent.STM.TVar
import Control.DeepSeq (deepseq, NFData)
import Data.Aeson
import qualified Data.Map.Strict as M

Expand All @@ -31,7 +33,7 @@ import Trace.Forward.Protocol.DataPoint.Type
-- available for the acceptor application, to decode unstructured JSON.
--
data DataPoint where
DataPoint :: ToJSON v => v -> DataPoint
DataPoint :: (ToJSON v, NFData v) => v -> DataPoint

type DataPointStore = TVar (M.Map DataPointName DataPoint)

Expand All @@ -44,11 +46,11 @@ writeToStore
-> DataPointName
-> DataPoint
-> IO ()
writeToStore dpStore dpName dp = atomically $
writeToStore dpStore dpName (DataPoint obj) = atomically $
modifyTVar' dpStore $ \store ->
if dpName `M.member` store
then M.adjust (const dp) dpName store
else M.insert dpName dp store
then M.adjust (const (DataPoint (deepseq obj obj))) dpName store
else M.insert dpName (DataPoint (deepseq obj obj)) store

-- | Read 'DataPoint's from the store. Please note that we don't care what's
-- inside of 'DataPoint', we just know it can be encoded to JSON.
Expand Down
1 change: 1 addition & 0 deletions trace-forward/trace-forward.cabal
Expand Up @@ -62,6 +62,7 @@ library
, cborg
, containers
, contra-tracer
, deepseq
, extra
, io-classes
, ouroboros-network-api >= 0.3
Expand Down

0 comments on commit a4d6814

Please sign in to comment.