-----------------------------------------------------------------------------
-- |
-- 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 backends. To avoid data
-- inconsistencies, these functions should not be used directly but only 
-- via the TMap interface. 
--
-----------------------------------------------------------------------------

module Data.TMap.Backend( Backend(..) )
where
import Prelude hiding(lookup)

-- | 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 Ord k => Backend k a b | a -> k where

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

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

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

    -- | Called in 'newTMapIO' 
    initialize :: b k a -> IO ()

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

{-
instance Backend k a b IO where
  insert b k a = liftIO (insert b k a)
  adjust b f k = liftIO (adjust b f k)
  delete b k   = liftIO (delete b k)
  lookup b k   = liftIO (lookup b k)
  flush        = liftIO . flush
  initialize   = liftIO . initialize
-}