Skip to content

Commit be4552d

Browse files
committed
Add some missing NFData instances
We'll use these instances in the new tests that are added by the next commit
1 parent 26bfefc commit be4552d

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Database/LSMTree/Internal/Merge.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Database.LSMTree.Internal.Merge (
1515
, steps
1616
) where
1717

18+
import Control.DeepSeq (NFData (..))
1819
import Control.Exception (assert)
1920
import Control.Monad (when)
2021
import Control.Monad.Class.MonadST (MonadST)
@@ -74,6 +75,10 @@ data MergeState =
7475
data Level = MidLevel | LastLevel
7576
deriving stock (Eq, Show)
7677

78+
instance NFData Level where
79+
rnf MidLevel = ()
80+
rnf LastLevel = ()
81+
7782
type Mappend = SerialisedValue -> SerialisedValue -> SerialisedValue
7883

7984
{-# SPECIALISE new ::

src/Database/LSMTree/Internal/MergeSchedule.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module Database.LSMTree.Internal.MergeSchedule (
4545
) where
4646

4747
import Control.Concurrent.Class.MonadMVar.Strict
48+
import Control.DeepSeq (NFData (..))
4849
import Control.Monad (void, when, (<$!>))
4950
import Control.Monad.Class.MonadST (MonadST)
5051
import Control.Monad.Class.MonadSTM (MonadSTM (..))
@@ -399,6 +400,10 @@ duplicateMergingRunRuns reg (DeRef mr) =
399400
data MergePolicyForLevel = LevelTiering | LevelLevelling
400401
deriving stock (Show, Eq)
401402

403+
instance NFData MergePolicyForLevel where
404+
rnf LevelTiering = ()
405+
rnf LevelLevelling = ()
406+
402407
mergePolicyForLevel :: MergePolicy -> LevelNo -> Levels m h -> MergePolicyForLevel
403408
mergePolicyForLevel MergePolicyLazyLevelling (LevelNo n) nextLevels
404409
| n == 1
@@ -409,6 +414,7 @@ mergePolicyForLevel MergePolicyLazyLevelling (LevelNo n) nextLevels
409414

410415
newtype NumRuns = NumRuns { unNumRuns :: Int }
411416
deriving stock (Show, Eq)
417+
deriving newtype NFData
412418

413419
newtype UnspentCreditsVar s = UnspentCreditsVar { getUnspentCreditsVar :: PrimVar s Int }
414420

@@ -430,6 +436,10 @@ newtype SpentCreditsVar s = SpentCreditsVar { getSpentCreditsVar :: PrimVar s In
430436
data MergeKnownCompleted = MergeKnownCompleted | MergeMaybeCompleted
431437
deriving stock (Show, Eq, Read)
432438

439+
instance NFData MergeKnownCompleted where
440+
rnf MergeKnownCompleted = ()
441+
rnf MergeMaybeCompleted = ()
442+
433443
{-# SPECIALISE duplicateLevels :: TempRegistry IO -> Levels IO h -> IO (Levels IO h) #-}
434444
duplicateLevels ::
435445
(PrimMonad m, MonadMVar m, MonadMask m)

src/Database/LSMTree/Internal/Snapshot.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Database.LSMTree.Internal.Snapshot (
2424

2525
import Control.Concurrent.Class.MonadMVar.Strict
2626
import Control.Concurrent.Class.MonadSTM (MonadSTM)
27+
import Control.DeepSeq (NFData (..))
2728
import Control.Monad (when)
2829
import Control.Monad.Class.MonadST (MonadST)
2930
import Control.Monad.Class.MonadThrow (MonadMask)
@@ -66,11 +67,17 @@ import System.FS.BlockIO.API (HasBlockIO)
6667
-- is opened at the correct key\/value\/blob type.
6768
newtype SnapshotLabel = SnapshotLabel Text
6869
deriving stock (Show, Eq)
70+
deriving newtype NFData
6971

7072
-- TODO: revisit if we need three table types.
7173
data SnapshotTableType = SnapNormalTable | SnapMonoidalTable | SnapFullTable
7274
deriving stock (Show, Eq)
7375

76+
instance NFData SnapshotTableType where
77+
rnf SnapNormalTable = ()
78+
rnf SnapMonoidalTable = ()
79+
rnf SnapFullTable = ()
80+
7481
data SnapshotMetaData = SnapshotMetaData {
7582
-- | See 'SnapshotLabel'.
7683
--
@@ -94,40 +101,58 @@ data SnapshotMetaData = SnapshotMetaData {
94101
}
95102
deriving stock (Show, Eq)
96103

104+
instance NFData SnapshotMetaData where
105+
rnf (SnapshotMetaData a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d
106+
97107
{-------------------------------------------------------------------------------
98108
Levels snapshot format
99109
-------------------------------------------------------------------------------}
100110

101111
newtype SnapLevels r = SnapLevels { getSnapLevels :: V.Vector (SnapLevel r) }
102112
deriving stock (Show, Eq, Functor, Foldable, Traversable)
113+
deriving newtype NFData
103114

104115
data SnapLevel r = SnapLevel {
105116
snapIncoming :: !(SnapIncomingRun r)
106117
, snapResidentRuns :: !(V.Vector r)
107118
}
108119
deriving stock (Show, Eq, Functor, Foldable, Traversable)
109120

121+
instance NFData r => NFData (SnapLevel r) where
122+
rnf (SnapLevel a b) = rnf a `seq` rnf b
123+
110124
data SnapIncomingRun r =
111125
SnapMergingRun !MergePolicyForLevel !NumRuns !NumEntries !UnspentCredits !MergeKnownCompleted !(SnapMergingRunState r)
112126
| SnapSingleRun !r
113127
deriving stock (Show, Eq, Functor, Foldable, Traversable)
114128

129+
instance NFData r => NFData (SnapIncomingRun r) where
130+
rnf (SnapMergingRun a b c d e f) =
131+
rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq` rnf e `seq` rnf f
132+
rnf (SnapSingleRun a) = rnf a
133+
115134
-- | The total number of unspent credits. This total is used in combination with
116135
-- 'SpentCredits' on snapshot load to restore merging work that was lost when
117136
-- the snapshot was created.
118137
newtype UnspentCredits = UnspentCredits { getUnspentCredits :: Int }
119138
deriving stock (Show, Eq, Read)
139+
deriving newtype NFData
120140

121141
data SnapMergingRunState r =
122142
SnapCompletedMerge !r
123143
| SnapOngoingMerge !(V.Vector r) !SpentCredits !Merge.Level
124144
deriving stock (Show, Eq, Functor, Foldable, Traversable)
125145

146+
instance NFData r => NFData (SnapMergingRunState r) where
147+
rnf (SnapCompletedMerge a) = rnf a
148+
rnf (SnapOngoingMerge a b c) = rnf a `seq` rnf b `seq` rnf c
149+
126150
-- | The total number of spent credits. This total is used in combination with
127151
-- 'UnspentCedits' on snapshot load to restore merging work that was lost when
128152
-- the snapshot was created.
129153
newtype SpentCredits = SpentCredits { getSpentCredits :: Int }
130154
deriving stock (Show, Eq, Read)
155+
deriving newtype NFData
131156

132157
{-------------------------------------------------------------------------------
133158
Conversion to levels snapshot format

0 commit comments

Comments
 (0)