hmemdb 0.3.0.0 → 0.3.0.1
raw patch · 2 files changed
+38/−9 lines, 2 filesdep ~basedep ~binarydep ~containersPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
Dependency ranges changed: base, binary, containers, stm, transformers
API changes (from Hackage documentation)
+ Data.HMemDb: instance Eq Multiple
+ Data.HMemDb: instance Eq Single
+ Data.HMemDb: instance Eq u => Eq (TableVarU t a u)
+ Data.HMemDb: instance Ord Single
+ Data.HMemDb: instance Ord u => Ord (TableVarU t a u)
+ Data.HMemDb: selectBetween :: (Multitude u, Ord i) => Key t a i u -> i -> Bool -> i -> Bool -> STM [TableVar t a]
Files
- hmemdb.cabal +2/−2
- src/Data/HMemDb.hs +36/−7
hmemdb.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: hmemdb -version: 0.3.0.0 +version: 0.3.0.1 synopsis: In-memory relational database description: Library that provides a sort of relational database in memory (which could be saved to the disk, however). Very untested. license: BSD3 @@ -16,5 +16,5 @@ library exposed-modules: Data.HMemDb - build-depends: base ==4.6.*, transformers ==0.3.*, stm ==2.4.*, containers ==0.5.*, binary ==0.5.*, monad-stm + build-depends: base <= 10, transformers >= 0.2, stm >= 2.3, containers, binary >= 0.3, monad-stm hs-source-dirs: src
src/Data/HMemDb.hs view
@@ -6,15 +6,22 @@ ( MS, Multitude, Single, Multiple, +-- * Main structures Table, Key, +-- * Value references TableVarU, TableVar, TableVars, fromList, toList, readVar, readRefs, +-- * Specifications + Spec (Spec, sRefs, sKeys), +-- ** Foreign table references TableRef, only, some, - KeySpec, single, multiple, single_, multiple_, RefsC, Refs (Refs), RefsComponent, Ref, (:&:)((:&:)), +-- ** Keys + KeySpec, single, multiple, single_, multiple_, KeysC, Keys (Keys), KeysComponent, KeyRef, (:+:)((:+:)), - Spec (Spec, sRefs, sKeys), +-- * Table manipulation Created (Created, cTable, cKeys), - createTable, select, select_, insert, update, update_, delete, + createTable, select, select_, selectBetween, insert, update, update_, delete, +-- * Persistence getTable, getTable_, getTable__, putTable, putTable_, putTable__ ) where @@ -38,9 +45,9 @@ -- Note that it doesn't revert the transaction on failure. type MS = MaybeT STM -- | This type specifies that we want a single value. -newtype Single = Single {sVal :: Integer} +newtype Single = Single {sVal :: Integer} deriving (Eq, Ord) -- | This type specifies that we want multiple values. -newtype Multiple = Multiple {mVal :: S.Set Integer} +newtype Multiple = Multiple {mVal :: S.Set Integer} deriving Eq -- | Closed class. -- It's instances allow us to choose whether we want to get a single value -- or multiple ones. @@ -70,7 +77,7 @@ -- Type 't' is an abstract type, same as in the 'Table'. -- Type 'a' is a type of value, which can be obtained with 'unVar', -- also same as in the 'Table'. -data TableVarU t a u = TableVar {tvVal :: u} +data TableVarU t a u = TableVar {tvVal :: u} deriving (Eq, Ord) -- | Reference to a single value in some table. type TableVar t a = TableVarU t a Single -- | Reference to multiple values in a single table. @@ -289,6 +296,10 @@ -- It can also select values with indices that are smaller or greater to the provided one, -- depending on the third argument, which could be anything like @(>)@, @(<=)@, @(/=)@, -- or even @return True@. +-- +-- @ +-- select_ k i (==) ~~ [select k i] +-- @ select_ :: (Multitude u, Ord i) => Key t a i u @@ -311,6 +322,24 @@ guard $ i `c` gi return $ M.elems g >>= mToList return $ map (TableVar . Single) $ [lvs, evs, gvs] >>= listUnMaybe +-- | A variant of 'select_', which allows to choose two bounds for the index. +-- Additional boolean arguments show whether to include bounds themselves or not. +selectBetween + :: (Multitude u, Ord i) => + Key t a i u + -> i -- ^ lower bound + -> Bool -- ^ including lower bound? + -> i -- ^ upper bound + -> Bool -- ^ including upper bound? + -> STM [TableVar t a] +selectBetween k il bl ig bg = + do kv <- readTVar $ kVal k + let ~(_, l, mgu) = M.splitLookup il kv + ~(m, g, _) = M.splitLookup ig mgu + lvs = if bl then fmap mToList l else Nothing + mvs = return $ M.elems m >>= mToList + gvs = if bg then fmap mToList g else Nothing + return $ map (TableVar . Single) $ [lvs, mvs, gvs] >>= listUnMaybe -- | Function that lets one to insert a new value to the 'Table'. -- Of course, we have to provide accompanying references as well. -- This function can fail if some key clashes with an already existing one. @@ -447,4 +476,4 @@ putTable_ p = putTable__ $ return . p -- | Function that makes it possible to write the table to the file or other storage. putTable :: Binary a => Table t r a -> STM Put -putTable = putTable_ put+putTable = putTable_ put