hmemdb- In-memory relational database

Safe HaskellNone




This library allows one to create mutable (but thread-safe) lists of values and access them quickly with certain automatically updated indices (foreign keys).



type SP = (STM :. PutM) ()Source


data Table a Source

This type represent tables. Each table is the set of values. Individual values can be accessed with TableVars or ForeignKeys. Tables are never created manually, they should be generated by createTable or loaded by getTable. Both functions require the structure of the table to be described as the FullSpec.

foldTable_ :: (a -> MS b) -> Table a -> STM ()Source

This function traverses through all values in the table, applying the same action to all of them. Errors are silently ignored.

type TableVar = TableVarS IdSource

This type represents references to individual values in the table. It is returned by insert and select functions.

data TableVarS s a Source

This is a more generic type, which represents a set of values in the same table.

tableVarTarget :: TableVarS s a -> Table aSource

This function returns the Table that the TableVar is from.

forTV :: (Foldable s, Applicative f) => (TableVar a -> f b) -> TableVarS s a -> f ()Source

This function iterates through all elements of the set.

deleteTV :: TableVar a -> MS aSource

This function removes the value from whatever table it's in. It returnes the original value, provided that it wasn't removed before or invalidated by removing some other value this one references with ForeignKey.

modifyTV :: a -> TableVar a -> MS aSource

This function overrides the value with another one. All indices referencing the original value would be referencing the new one. It fails if the original value was removed before or became invalid.

readTV :: TableVar a -> MS aSource

This function reads the value from the table. It fails if the value was removed before or became invalid.

selectAll :: Table a -> STM (TableVarS Set a)Source

This function gives the set of all values in the table.

data ForeignKey s i a Source

keyTarget :: ForeignKey s i a -> Table aSource

This function returns the table that the key points to.


data TableVarId a Source

This type can be used for columns in a table. It's sort of a ForeignKey accompanied with a specific index.


Eq (TableVarId a)

TableVarIds pointing to different Tables may accidentally appear equal.

Ord (TableVarId a) 

idToVar :: Table a -> TableVarId a -> MS (TableVar a)Source

This function looks up the specific Id in the Table.

varToId :: TableVar a -> TableVarId aSource

This function gets an Id of the variable.


data FullSpec a u Source

This is the full specification, of both table and set of keys, which should be fed to createTable and getTable functions.




tabSpec :: TableSpec a
keySpec :: u a KeySpec

Table specs

type TableSpec a = ColSpec a aSource

This type represents the table structure. It can be generated using the Applicative interface of the ColSpec like this:

 data MyData = {myField1 :: Integer, myField2 :: String}
 tabSpec = TableSpec (MyData <$> val myField1 <*> val myField2)

data ColSpec input output Source

This is the internal of the TableSpec type.


Functor (ColSpec input) 
Applicative (ColSpec input) 

val :: Binary col => (input -> col) -> ColSpec input colSource

This function specifies one column in the table. It instructs the library to store one part of the value.

key :: Ord i => ForeignKey Id i col -> (input -> i) -> ColSpec input colSource

This function specifies one column in the table. Unlike val, it doesn't store some part of the value; instead it stores the reference to some other table.

tref :: Table col -> (input -> TableVarId col) -> ColSpec input (TableVarId col)Source

This function eases the use of TableVarIds as columns of the table.

Key specs

data KeySpec s i a Source

This is the specification of one ForeignKey. It could be unique or non-unique.


(Ord i, RefContainer s) => IsKeySpec (KeySpec s i) 

unique :: (a -> i) -> KeySpec Id i aSource

This function specifies a unique key.

nonunique :: (a -> i) -> KeySpec Set i aSource

This function specifies a non-unique key.

Creating tables

createTable :: CreateTable u => FullSpec a u -> STM (Table a, u a ForeignKey)Source

This function creates an empty table, given the table structure (TableSpec) and the set of keys (KeySpec). It returns the table itself, accompanied with the same set of foreign keys, allowing one to search through the table quickly, or to make a new TableSpec.

data Keys a h Source

This type represents an empty set of KeySpecs or ForeignKeys.




class Foldable s => RefContainer s Source

This class is a closed one; the user is not supposed to create new instances. It allows treating unique and non-unique keys in the same way.

class IsKeySpec ks Source

This class is here for technical reasons; it has just one instance.


(Ord i, RefContainer s) => IsKeySpec (KeySpec s i) 

data (u :+: ks) a h whereSource

This type operator adds one more KeySpec to the set, or allows to get a ForeignKey back with pattern-matching. Use it like this:

 do (table, ... :+: foreignKey)
      <- createTable $ FullSpec {..., keySpec = ... :+: unique myKey}


:+: :: u a h -> h s i a -> (u :+: KeySpec s i) a h 


(CreateTable u, IsKeySpec ks) => CreateTable (:+: u ks) 

class CreateTable u Source

This is a class of sets of KeySpecs and ForeignKeys


Basic operations

insert :: a -> Table a -> MS (TableVar a)Source

This function inserts a new value into the table and gives a TableVar back. Failure indicates that one of the unique indices for this value coincides with the same index of another value already present in the table. It won't happen for non-unique indices.

select :: Ord i => ForeignKey s i a -> i -> MS (TableVarS s a)Source

This function searches for some particular index in the table. It fails if there is no value with that index. Empty set of values is never returned. For non-unique indices it returns the set of TableVars.

delete :: (Foldable s, Ord i) => ForeignKey s i a -> i -> STM ()Source

This function deletes the value from the table. It won't be accessible anymore. It never fails; nonexistent values are silently skipped.

update :: Ord i => ForeignKey Id i a -> i -> a -> MS aSource

This function overrides the existing value in the table with the new one. All foreign keys pointing to the original value become pointing to the new value. It returns the original value, which is no longer in the table. Failure means that there was no such value.


fillTable :: Table a -> GS ()Source

This function reads the table from the ByteString.

putTable :: Table a -> SPSource

This function saves the table to the ByteString. Note that it doesn't really matter if the type of values in the table are serializable.

NB: if any index used to access the values in this table depended on any foreign keys, and targets of these keys have changed, the index could be different after storing and restoring the table.