Skip to content

Commit

Permalink
Swapping IO for abstract IO type-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
recursion-ninja authored and jorisdral committed Oct 3, 2024
1 parent 869575f commit e7cb644
Show file tree
Hide file tree
Showing 17 changed files with 1,302 additions and 411 deletions.
16 changes: 13 additions & 3 deletions src-extras/Database/LSMTree/Extras/NoThunks.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}
Expand Down Expand Up @@ -496,11 +497,20 @@ instance NoThunksIOLike' IO RealWorld

type NoThunksIOLike m = NoThunksIOLike' m (PrimState m)

-- TODO: on ghc-9.4, a check on StrictTVar IO (RWState (TableContent IO h))
-- fails, but we have not yet found out why so we simply disable NoThunks checks
-- for StrictTVars on ghc-9.4
instance NoThunks a => NoThunks (StrictTVar IO a) where
showTypeOf (_ :: Proxy (StrictTVar IO a)) = "StrictTVar IO"
wNoThunks ctx var = do
x <- readTVarIO var
noThunks ctx x
wNoThunks _ctx _var = do
#if defined(MIN_VERSION_GLASGOW_HASKELL)
#if MIN_VERSION_GLASGOW_HASKELL(9,4,0,0) && !MIN_VERSION_GLASGOW_HASKELL(9,6,0,0)
pure Nothing
#else
x <- readTVarIO _var
noThunks _ctx x
#endif
#endif

instance NoThunks a => NoThunks (StrictMVar IO a) where
showTypeOf (_ :: Proxy (StrictMVar IO a)) = "StrictMVar IO"
Expand Down
43 changes: 33 additions & 10 deletions src/Database/LSMTree/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ module Database.LSMTree.Common (

import Control.Concurrent.Class.MonadMVar.Strict
import Control.Concurrent.Class.MonadSTM (MonadSTM, STM)
import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadThrow
import Control.Monad.Fix (MonadFix)
import Control.Monad.Primitive (PrimMonad)
import Control.Tracer (Tracer)
import Data.Kind (Type)
import Data.Typeable (Proxy, Typeable)
Expand All @@ -70,10 +73,7 @@ import System.FS.IO (HandleIO)

-- | Utility class for grouping @io-classes@ constraints.
class ( MonadMVar m, MonadSTM m, MonadThrow (STM m), MonadThrow m, MonadCatch m
, m ~ IO -- TODO: temporary constraint until we add I/O fault testing.
-- Don't forget to specialise your functions! @m ~ IO@ in a
-- function constraint will not produce to an IO-specialised
-- function.
, MonadMask m, PrimMonad m, MonadFix m , MonadST m
) => IOLike m

instance IOLike IO
Expand Down Expand Up @@ -112,7 +112,13 @@ instance IOLike IO
type Session :: (Type -> Type) -> Type
type Session = Internal.Session'

{-# SPECIALISE withSession :: Tracer IO Internal.LSMTreeTrace -> HasFS IO HandleIO -> HasBlockIO IO HandleIO -> FsPath -> (Session IO -> IO a) -> IO a #-}
{-# SPECIALISE withSession ::
Tracer IO Internal.LSMTreeTrace
-> HasFS IO HandleIO
-> HasBlockIO IO HandleIO
-> FsPath
-> (Session IO -> IO a)
-> IO a #-}
-- | (Asynchronous) exception-safe, bracketed opening and closing of a session.
--
-- If possible, it is recommended to use this function instead of 'openSession'
Expand All @@ -127,7 +133,12 @@ withSession ::
-> m a
withSession tr hfs hbio dir action = Internal.withSession tr hfs hbio dir (action . Internal.Session')

{-# SPECIALISE openSession :: Tracer IO Internal.LSMTreeTrace -> HasFS IO HandleIO -> HasBlockIO IO HandleIO -> FsPath -> IO (Session IO) #-}
{-# SPECIALISE openSession ::
Tracer IO Internal.LSMTreeTrace
-> HasFS IO HandleIO
-> HasBlockIO IO HandleIO
-> FsPath
-> IO (Session IO) #-}
-- | Create either a new empty table session or open an existing table session,
-- given the path to the session directory.
--
Expand Down Expand Up @@ -183,7 +194,10 @@ closeSession (Internal.Session' sesh) = Internal.closeSession sesh
class Labellable a where
makeSnapshotLabel :: Proxy a -> Internal.SnapshotLabel

{-# SPECIALISE deleteSnapshot :: Session IO -> Internal.SnapshotName -> IO () #-}
{-# SPECIALISE deleteSnapshot ::
Session IO
-> Internal.SnapshotName
-> IO () #-}
-- | Delete a named snapshot.
--
-- NOTE: has similar behaviour to 'removeDirectory'.
Expand All @@ -194,15 +208,24 @@ class Labellable a where
--
-- TODO: this function currently has a temporary implementation until we have
-- proper snapshots.
deleteSnapshot :: IOLike m => Session m -> Internal.SnapshotName -> m ()
deleteSnapshot ::
IOLike m
=> Session m
-> Internal.SnapshotName
-> m ()
deleteSnapshot (Internal.Session' sesh) = Internal.deleteSnapshot sesh

{-# SPECIALISE listSnapshots :: Session IO -> IO [Internal.SnapshotName] #-}
{-# SPECIALISE listSnapshots ::
Session IO
-> IO [Internal.SnapshotName] #-}
-- | List snapshots by name.
--
-- TODO: this function currently has a temporary implementation until we have
-- proper snapshots.
listSnapshots :: IOLike m => Session m -> m [Internal.SnapshotName]
listSnapshots ::
IOLike m
=> Session m
-> m [Internal.SnapshotName]
listSnapshots (Internal.Session' sesh) = Internal.listSnapshots sesh

{-------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit e7cb644

Please sign in to comment.