hyperion-0.1.0.0
Safe HaskellNone
LanguageHaskell2010

Hyperion.Database.KeyValMap

Synopsis

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

Constructors

KeyValMap 

Fields

Instances

Instances details
Eq (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

Methods

(==) :: KeyValMap a b -> KeyValMap a b -> Bool #

(/=) :: KeyValMap a b -> KeyValMap a b -> Bool #

Ord (KeyValMap a b) Source # 
Instance details

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 #

max :: KeyValMap a b -> KeyValMap a b -> KeyValMap a b #

min :: KeyValMap a b -> KeyValMap a b -> KeyValMap a b #

Show (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

Methods

showsPrec :: Int -> KeyValMap a b -> ShowS #

show :: KeyValMap a b -> String #

showList :: [KeyValMap a b] -> ShowS #

Generic (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

Associated Types

type Rep (KeyValMap a b) :: Type -> Type #

Methods

from :: KeyValMap a b -> Rep (KeyValMap a b) x #

to :: Rep (KeyValMap a b) x -> KeyValMap a b #

ToJSON (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

FromJSON (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

Binary (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

Methods

put :: KeyValMap a b -> Put #

get :: Get (KeyValMap a b) #

putList :: [KeyValMap a b] -> Put #

ToField (KeyValMap a b) Source #

KeyValMap is an instance of ToField in order to use with Sqlite

Instance details

Defined in Hyperion.Database.KeyValMap

Methods

toField :: KeyValMap a b -> SQLData

type Rep (KeyValMap a b) Source # 
Instance details

Defined in Hyperion.Database.KeyValMap

type Rep (KeyValMap a b) = D1 ('MetaData "KeyValMap" "Hyperion.Database.KeyValMap" "hyperion-0.1.0.0-BChDBJtiU1m4GBpewNuAxw" 'True) (C1 ('MetaCons "KeyValMap" 'PrefixI 'True) (S1 ('MetaSel ('Just "kvMapName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

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

newtype JsonField a Source #

Constructors

JsonField a 

Instances

Instances details
ToJSON a => ToField (JsonField a) Source #

Make JsonField an instance of ToField.

Instance details

Defined in Hyperion.Database.KeyValMap

Methods

toField :: JsonField a -> SQLData

(Typeable a, FromJSON a) => FromField (JsonField a) Source #

Make JsonField an instance of FromField. First turns the field into Text and then tries to decode JSON. Returns Errors or Ok with the result.

Instance details

Defined in Hyperion.Database.KeyValMap

Methods

fromField :: FieldParser (JsonField a)

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

memoizeWithMap Source #

Arguments

:: (MonadIO m, MonadReader env m, HasDB env, MonadCatch m, ToJSON a, ToJSON b, Typeable b, FromJSON b) 
=> KeyValMap a b

The KeyValMap in which to memoize

-> (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