Safe Haskell | None |
---|---|
Language | Haskell2010 |
Hyperion.Database.KeyValMap
Synopsis
- newtype KeyValMap a b = KeyValMap {}
- setupKeyValTable :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m) => m ()
- newtype JsonField a = JsonField a
- insert :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, ToJSON b) => KeyValMap a b -> a -> b -> m ()
- lookup :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, Typeable b, FromJSON b) => KeyValMap a b -> a -> m (Maybe b)
- lookupAll :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, Typeable a, FromJSON a, Typeable b, FromJSON b) => KeyValMap a b -> m [(a, b)]
- lookupDefault :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, Typeable b, FromJSON b) => KeyValMap a b -> b -> a -> m b
- memoizeWithMap :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, ToJSON b, Typeable b, FromJSON b) => KeyValMap a b -> (a -> m b) -> a -> m b
General comments
Hyperion.Database.KeyValMap provides various actions with Sqlite
DB using withConnection
from Hyperion.Database.HasDB.
The DB contains entries for KeyValMap
: given kvMapName
and a
key, DB can produce value or values for the key. We think about
this as a map, i.e. there is a preferred value for each key for a
given KeyValMap
, even if DB contains the key several times. This
is described below.
The keys and values are represented by JSON encoding of Haskell
values. Data.Aeson is used to perform encoding/decoding. Thus,
the keys and the values should implement ToJSON
/FromJSON
appropriately.
Convention on DB entries with the same key
The database table for a KeyValMap
can contain multiple entries
with the same key. In this case, the newest entry is the active
one. Older entries are always ignored. Thus, an update can be
achieved by inserting a new key val pair.
The reason for this choice is that newer programs should not modify data associated with old programs. However, a new program may lookup data from old programs. This convention allows one to change the data that a new program sees without violating the above constraint.
newtype KeyValMap a b Source #
Type for KeyValMap
holds the types of key and value, but only
contains kvMapName
the name of the map
Instances
Eq (KeyValMap a b) Source # | |
Ord (KeyValMap a b) Source # | |
Defined in Hyperion.Database.KeyValMap Methods compare :: KeyValMap a b -> KeyValMap a b -> Ordering # (<) :: KeyValMap a b -> KeyValMap a b -> Bool # (<=) :: KeyValMap a b -> KeyValMap a b -> Bool # (>) :: KeyValMap a b -> KeyValMap a b -> Bool # (>=) :: KeyValMap a b -> KeyValMap a b -> Bool # | |
Show (KeyValMap a b) Source # | |
Generic (KeyValMap a b) Source # | |
ToJSON (KeyValMap a b) Source # | |
Defined in Hyperion.Database.KeyValMap | |
FromJSON (KeyValMap a b) Source # | |
Binary (KeyValMap a b) Source # | |
ToField (KeyValMap a b) Source # |
|
Defined in Hyperion.Database.KeyValMap | |
type Rep (KeyValMap a b) Source # | |
Defined in Hyperion.Database.KeyValMap |
setupKeyValTable :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m) => m () Source #
setupKeyValTable
creates the table "hyperion_key_val" if it
doesn't yet exist. This table will hold the map-key-val entries.
The entry format is program_id, kv_map, key, val, created_at
.
These are the program id, map name, key, value, and timestamp,
respectively.
program_id
is not used in lookups
Constructors
JsonField a |
Instances
ToJSON a => ToField (JsonField a) Source # | Make |
Defined in Hyperion.Database.KeyValMap | |
(Typeable a, FromJSON a) => FromField (JsonField a) Source # | Make |
Defined in Hyperion.Database.KeyValMap |
insert :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, ToJSON b) => KeyValMap a b -> a -> b -> m () Source #
Inserts an map-key-val entry into the database.
If fails, retries using withConnectionRetry
lookup :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, Typeable b, FromJSON b) => KeyValMap a b -> a -> m (Maybe b) Source #
Looks up a value in the database given the map name and the key. Takes the most recent matching entry according to the convention.
If fails, retries using withConnectionRetry
lookupAll :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, Typeable a, FromJSON a, Typeable b, FromJSON b) => KeyValMap a b -> m [(a, b)] Source #
Returns the list of all kev-value pairs for a given map. Again only keeps the latest versino of the value accroding to the convention.
If fails, retries using withConnectionRetry
lookupDefault :: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, Typeable b, FromJSON b) => KeyValMap a b -> b -> a -> m b Source #
Same as lookup
but with a default value provided
Arguments
:: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, ToJSON b, Typeable b, FromJSON b) | |
=> KeyValMap a b | The |
-> (a -> m b) | |
-> a | |
-> m b |
This implements memoization using the DB. Given a function, it first tries to look up the function result in the DB and if no result is available, runs the function and inserts the result into the DB