-----------------------------------------------------------------------------
-- |
-- Module      :  Data.TMap.Backend
-- Copyright   :  Peter Robinson 2009
-- License     :  LGPL
--
-- Maintainer  :  Peter Robinson <thaldyron@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable (requires STM)
--
-- Provides a type class for backend instantiations. To avoid data
-- inconsistencies, these functions should not be used directly but only by 
-- means of the TMap interface.
--
-----------------------------------------------------------------------------

module Data.TMap.Backend( Backend(..) )
where
import Control.Monad.Trans( MonadIO )

-- | This class needs to be instantiated when developing a new
-- backend. The backend is expected to be able to handle concurrent
-- (non-conflicting) requests.
class Backend k a b | a -> k where

    -- | Called when a new element was inserted.
    insert     :: MonadIO m => b k a -> k -> a -> m ()

    -- | Called when an element was updated.
    adjust     :: MonadIO m => b k a-> (a -> a) -> k -> m ()
    
    -- | Called when an element was deleted from the map.
    delete     :: MonadIO m => b k a-> k -> m ()
    
    -- | Called when an element is not found in the map. 
    lookup     :: MonadIO m => b k a -> k -> m (Maybe a)

    -- | Called by /flushBackend/ and /purgeTMap/.
    flush      :: MonadIO m => b k a -> m ()

    -- | Called in 'newTMapIO' 
    initialize :: MonadIO m => b k a -> m ()

    flush      _ = return ()
    initialize _ = return ()