This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bdf628
commit 5983151
Showing
10 changed files
with
218 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
module Cardano.Metadata.Store.KeyValue.Map where |
9 changes: 9 additions & 0 deletions
9
metadata-lib/src/Cardano/Metadata/Store/KeyValue/LockedSchema/Properties.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Cardano.Metadata.Store.KeyValue.LockedSchema.Properties where | ||
|
||
import Data.Map.Strict | ||
import qualified Data.Map.Strict as M | ||
|
||
data KeyValueStore k v = KeyValueStore (Map k v) | ||
|
||
keys :: KeyValueStore k v -> [k] | ||
keys = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
module Cardano.Metadata.Store.KeyValue.Map where | ||
|
||
import Data.Map.Strict (Map) | ||
import Control.Concurrent.MVar | ||
import qualified Data.Map.Strict as Map | ||
|
||
newtype KeyValue k v = KeyValue (MVar (Map k v)) | ||
|
||
init :: Map k v -> IO (KeyValue k v) | ||
init state = do | ||
mVar <- newMVar state | ||
pure (KeyValue mVar) | ||
|
||
read :: Ord k => k -> KeyValue k v -> IO (Maybe v) | ||
read k (KeyValue mVar) = do | ||
m <- readMVar mVar | ||
pure $ Map.lookup k m | ||
|
||
write :: Ord k => k -> v -> KeyValue k v -> IO (KeyValue k v) | ||
write k v = modifyKeyValue (Map.insert k v) | ||
|
||
delete :: Ord k => k -> KeyValue k v -> IO (KeyValue k v) | ||
delete k = modifyKeyValue (Map.delete k) | ||
|
||
toList :: KeyValue k v -> IO [(k, v)] | ||
toList (KeyValue mVar) = do | ||
m <- readMVar mVar | ||
pure $ Map.toList m | ||
|
||
-- update :: k -> (v -> v) -> KeyValue k v -> IO (KeyValue k v) | ||
|
||
modifyKeyValue :: (Map k v -> Map k v) -> KeyValue k v -> IO (KeyValue k v) | ||
modifyKeyValue f (KeyValue mVar) = do | ||
-- Take the MVar, then place an unevaluated thunk inside of it, so | ||
-- we don't hold onto the MVar for long | ||
m <- takeMVar mVar | ||
let m' = f m | ||
putMVar mVar m' | ||
-- Force evaluation of the thunk so that we don't build up a large | ||
-- chain of thunks | ||
seq m' (pure (KeyValue mVar)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
module Cardano.Metadata.Store.Simple where | ||
|
||
import qualified Data.Map.Strict as M | ||
import qualified Data.Aeson as Aeson | ||
import qualified Data.HashMap.Strict as HM | ||
import Data.Text (Text) | ||
|
||
import Cardano.Metadata.Server.Types | ||
import Cardano.Metadata.Store.Types | ||
|
||
readFns :: ReadFns | ||
readFns = | ||
ReadFns | ||
(pure . getEntryForSubject) | ||
(\subj -> pure . getPartialEntryForProperty subj) | ||
(pure . getBatch) | ||
|
||
getEntryForSubject :: Subject -> Either ReadError Entry | ||
getEntryForSubject subj = case M.lookup subj dat of | ||
Nothing -> Left $ NoSubject subj | ||
Just e -> Right e | ||
|
||
getPartialEntryForProperty :: Subject -> Text -> Either ReadError PartialEntry | ||
getPartialEntryForProperty subj prop = do | ||
entry <- getEntryForSubject subj | ||
getProperty subj prop entry | ||
|
||
getProperty :: Subject -> Text -> Entry -> Either ReadError PartialEntry | ||
getProperty subj prop entry = | ||
case Aeson.toJSON entry of | ||
(Aeson.Object obj) -> case HM.lookup prop obj of | ||
Nothing -> Left $ NoProperty subj prop | ||
Just p -> case Aeson.fromJSON (Aeson.Object $ HM.fromList [("subject", Aeson.String subj), (prop, p)]) of | ||
Aeson.Error str -> error $ "JSON parsing error: " <> str | ||
Aeson.Success x -> Right x | ||
otherwise -> error "Entry isn't a JSON Object but should be." | ||
|
||
getBatch :: BatchRequest -> BatchResponse | ||
getBatch (BatchRequest subjs props) = | ||
BatchResponse $ | ||
flip foldMap subjs $ \subj -> | ||
flip foldMap props $ \prop -> | ||
case getPartialEntryForProperty subj prop of | ||
Left _err -> [] | ||
Right x -> [x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Cardano.Metadata.Store.Types where | ||
|
||
import Data.Text (Text) | ||
|
||
import Cardano.Metadata.Server.Types | ||
|
||
-- | A set of functions that allows the user of this library to | ||
-- determine how metadata entries are retrieved. E.g. with postgres or | ||
-- with dynamo-db. | ||
data ReadFns | ||
= ReadFns { readEntry :: Subject -> IO (Either ReadError Entry) | ||
-- ^ Given a subject, return an Entry | ||
, readProperty :: Subject -> Text -> IO (Either ReadError PartialEntry) | ||
-- ^ Return the given property for the given subject | ||
, readBatch :: BatchRequest -> IO BatchResponse | ||
-- ^ Service a batch request | ||
} | ||
|
||
data ReadError = NoSubject Subject | ||
| NoProperty Subject Text | ||
|
||
data WriteFns | ||
= WriteFns { writeEntry :: Entry -> IO () | ||
-- ^ Given an entry, write it to the data store | ||
, removeEntry :: Subject -> IO () | ||
-- ^ Given a subject, remove it's entry from the data store | ||
, writeBatch :: [Entry] -> IO () | ||
, removeAll :: IO () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.