Skip to content

Commit

Permalink
Merge pull request #21 from enigma-commits/EulerHd/New-function-for-f…
Browse files Browse the repository at this point in the history
…etching-entries-from-kv-only-with-conditional-db

Added a new function for conditional access to db
  • Loading branch information
aravindgopall authored Oct 17, 2023
2 parents 3c35933 + ec345c8 commit a3ea4d2
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/EulerHS/KVConnector/Flow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module EulerHS.KVConnector.Flow
findAllWithOptionsKVConnector',
deleteWithKVConnector,
deleteReturningWithKVConnector,
deleteAllReturningWithKVConnector
deleteAllReturningWithKVConnector,
findAllWithKVAndConditionalDBInternal
)
where

Expand Down Expand Up @@ -897,8 +898,8 @@ findAllWithOptionsKVConnector' :: forall be table beM m.
Maybe Int ->
Maybe Int ->
m (MeshResult [table Identity])
findAllWithOptionsKVConnector' dbConf meshCfg whereClause mbLimit mbOffset =
findAllWithOptionsHelper dbConf meshCfg whereClause Nothing mbLimit mbOffset
findAllWithOptionsKVConnector' dbConf meshCfg whereClause =
findAllWithOptionsHelper dbConf meshCfg whereClause Nothing

findAllWithKVConnector :: forall be table beM m.
( HasCallStack,
Expand All @@ -925,7 +926,7 @@ findAllWithKVConnector dbConf meshCfg whereClause = do
let matchedKVLiveRows = findAllMatching whereClause (fst kvRows)
dbRes <- runQuery dbConf findAllQuery
case dbRes of
Right dbRows -> pure $ Right $ matchedKVLiveRows ++ (getUniqueDBRes dbRows (fst kvRows ++ snd kvRows))
Right dbRows -> pure $ Right $ matchedKVLiveRows ++ getUniqueDBRes dbRows (fst kvRows ++ snd kvRows)
Left err -> return $ Left $ MDBError err
Left err -> return $ Left err
else do
Expand All @@ -934,6 +935,41 @@ findAllWithKVConnector dbConf meshCfg whereClause = do
let source = if not isDisabled then KV_AND_SQL else SQL
pure res

findAllWithKVAndConditionalDBInternal :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Model be table,
MeshMeta be table,
KVConnector (table Identity),
ToJSON (table Identity),
FromJSON (table Identity),
Serialize.Serialize (table Identity),
L.MonadFlow m, B.HasQBuilder be, BeamRunner beM) =>
DBConfig beM ->
MeshConfig ->
Where be table ->
m (MeshResult [table Identity])
findAllWithKVAndConditionalDBInternal dbConf meshCfg whereClause = do
let isDisabled = meshCfg.kvHardKilled
if not isDisabled
then do
kvRes <- redisFindAll meshCfg whereClause
case kvRes of
Right kvRows -> do
let matchedKVLiveRows = findAllMatching whereClause (fst kvRows)
if not (null matchedKVLiveRows)
then pure $ Right matchedKVLiveRows
else do
let findAllQueryUpdated = DB.findRows (sqlSelect ! #where_ whereClause ! defaults)
dbRes <- runQuery dbConf findAllQueryUpdated
case dbRes of
Right dbRows -> pure $ Right $ getUniqueDBRes dbRows (snd kvRows)
Left err -> return $ Left $ MDBError err
Left err -> return $ Left err
else do
let findAllQuery = DB.findRows (sqlSelect ! #where_ whereClause ! defaults)
mapLeft MDBError <$> runQuery dbConf findAllQuery

redisFindAll :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Expand Down Expand Up @@ -977,7 +1013,7 @@ deleteObjectRedis :: forall table be beM m.
-- Show (table Identity), --debugging purpose
L.MonadFlow m
) =>
MeshConfig -> Bool -> Where be table -> (table Identity) -> m (MeshResult (table Identity))
MeshConfig -> Bool -> Where be table -> table Identity -> m (MeshResult (table Identity))
deleteObjectRedis meshCfg addPrimaryKeyToWhereClause whereClause obj = do
time <- fromIntegral <$> L.getCurrentDateInMillis
let pKeyText = getLookupKeyByPKey obj
Expand Down

0 comments on commit a3ea4d2

Please sign in to comment.