persistent-map-0.3.5: A thread-safe (STM) persistency interface for finite map types.Source codeContentsIndex
Data.TMap
Portabilitynon-portable (requires STM)
Stabilityexperimental
MaintainerPeter Robinson <thaldyron@gmail.com>
Contents
TMap Types
Creating a new TMap
Finite Map Interace
Controlling the size of the TMap
Backend Communication
Exception Type
Description
Provides a thread-safe STM interface for finite map types with optional persistent storage.
Synopsis
data TMap map key val backendType cacheType
type TFiniteMap key val backendType = TMap (FM key) key val backendType LRU
newTMapIO :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => b k a -> Maybe Int -> IO (TMap map k a b c)
newTFiniteMapIO :: (Ord k, Backend k a b) => b k a -> IO (TFiniteMap k a b)
lookup :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm (Maybe a)
insert :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> a -> TMap map k a b c -> stm ()
delete :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm ()
member :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm Bool
adjust :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => (a -> a) -> k -> TMap map k a b c -> stm ()
purgeTMapIO :: (FiniteMapX map k, MonadIO io, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> io ()
getMaximumSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> stm (Maybe Int)
setMaximumSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> Int -> stm ()
getCurrentSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> stm Int
markAsDirty :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> IO ()
tryMarkAsDirty :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> IO ()
flushBackend :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> IO ()
data TMapException
= DuplicateEntry
| EntryNotFound
| TMapDefaultExc String
| BackendException String
TMap Types
data TMap map key val backendType cacheType Source
The generic transactional map type.
type TFiniteMap key val backendType = TMap (FM key) key val backendType LRUSource
The standard library type Data.Map repackaged as a TMap.
Creating a new TMap
newTMapIOSource
:: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k)
=> b k athe backend
-> Maybe Intmaximum-size: Use Nothing for unbounded size.
-> IO (TMap map k a b c)

Creates a new TMap. You will need to use an apropriate backend and specify the caching policy, e.g.,

   import Data.TMap.Backend.Binary( BinaryBackend,mkBinaryBackend )
   import Data.CacheStructure.LRU(LRU)

will use a binary-serialization backend for persistent storage and a "least recently used" caching algorithm. See newTFiniteMapIO for a less generic construction method.

Now, to create an unbounded map that uses the 'FM Int String' (see package EdisonCore) as the map type, you can write

   backend <- mkBinaryBackend "myworkdir" "mytempdir"
   tmap <- newTMapIO backend Nothing :: IO (TMap (FM.FM key) key val BinaryBackend LRU)

Note that newTFiniteMapIO provides an easier construction method. See file Sample.hs for further examples.

newTFiniteMapIOSource
:: (Ord k, Backend k a b)
=> b k athe backend
-> IO (TFiniteMap k a b)
Creates an (unbounded) TFiniteMap.
Finite Map Interace
lookup :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm (Maybe a)Source
Looks for a given key in the map and (if necessary) in the persistent storage and updates the map if necessary.
insert :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> a -> TMap map k a b c -> stm ()Source
Adds a key-value mapping to the map. Can throw a DuplicateEntry exception.
delete :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm ()Source
Removes a key from the map. Can throw an EntryNotFound exception.
member :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> stm BoolSource
Checks whether the given key is in the map.
adjust :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => (a -> a) -> k -> TMap map k a b c -> stm ()Source
Applies a function to the element identified by the key. Can throw an EntryNotFound exception.
Controlling the size of the TMap
purgeTMapIO :: (FiniteMapX map k, MonadIO io, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> io ()Source
Reduces the map to the appropriate size if the maximum size was exceeded. Calls Data.TMap.Backend.flush if the map is purged. Runs in O(1) if the map size is within bounds, otherwise O(n).
getMaximumSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> stm (Maybe Int)Source
Gets the maximum size of the map. O(1).
setMaximumSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> Int -> stm ()Source
Sets the maximum size of the map. O(1). Note that the size of the TMap needs to be reduced manually to the maximum size by calling purgeTMap.
getCurrentSize :: (FiniteMapX map k, MonadAdvSTM stm, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> stm IntSource
Gets the current size of the map. O(1).
Backend Communication
markAsDirty :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> IO ()Source
Causes the element to be reread from the backend on the next lookup. Throws an EntryNotFound exception if the entry does not exist.
tryMarkAsDirty :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => k -> TMap map k a b c -> IO ()Source
Causes the element to be reread from the backend on the next lookup. Does not throw an error when the element does not exist.
flushBackend :: (FiniteMapX map k, Ord k, Backend k a b, CacheStructure c k) => TMap map k a b c -> IO ()Source
Sends a B.flush request to the backend. Useful for asynchronous backend implementations.
Exception Type
data TMapException Source
Constructors
DuplicateEntry
EntryNotFound
TMapDefaultExc String
BackendException String
show/hide Instances
Produced by Haddock version 2.4.2