Skip to content

Commit

Permalink
Merge pull request #20 from enigma-commits/EulerHS/feat/newFindallFun…
Browse files Browse the repository at this point in the history
…ction

EulerHS/feat/newFindallFunction
  • Loading branch information
aravindgopall authored Oct 10, 2023
2 parents cefc430 + f113f35 commit 3c35933
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions src/EulerHS/KVConnector/Flow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module EulerHS.KVConnector.Flow
getFieldsAndValuesFromClause,
updateAllReturningWithKVConnector,
findAllWithOptionsKVConnector,
findAllWithOptionsKVConnector',
deleteWithKVConnector,
deleteReturningWithKVConnector,
deleteAllReturningWithKVConnector
Expand Down Expand Up @@ -50,6 +51,7 @@ import Named (defaults, (!))
import qualified Data.Serialize as Serialize
import qualified EulerHS.KVConnector.Encoding as Encoding
import System.CPUTime (getCPUTime)
import qualified Data.Maybe as DMaybe

createWoReturingKVConnector :: forall (table :: (Type -> Type) -> Type) be m beM.
( HasCallStack,
Expand Down Expand Up @@ -770,8 +772,8 @@ findOneFromDB dbConf whereClause = do
mapLeft MDBError <$> runQuery dbConf findQuery


-- Need to recheck offset implementation
findAllWithOptionsKVConnector :: forall be table beM m.

findAllWithOptionsHelper :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Model be table,
Expand All @@ -785,11 +787,11 @@ findAllWithOptionsKVConnector :: forall be table beM m.
DBConfig beM ->
MeshConfig ->
Where be table ->
OrderBy table ->
Maybe (OrderBy table) ->
Maybe Int ->
Maybe Int ->
m (MeshResult [table Identity])
findAllWithOptionsKVConnector dbConf meshCfg whereClause orderBy mbLimit mbOffset = do
findAllWithOptionsHelper dbConf meshCfg whereClause orderBy mbLimit mbOffset = do
let isDisabled = meshCfg.kvHardKilled
res <- if not isDisabled
then do
Expand All @@ -803,7 +805,7 @@ findAllWithOptionsKVConnector dbConf meshCfg whereClause orderBy mbLimit mbOffse
updatedOffset = if offset - shift >= 0 then offset - shift else 0
findAllQueryUpdated = DB.findRows (sqlSelect'
! #where_ whereClause
! #orderBy (Just [orderBy])
! #orderBy (if isJust orderBy then Just [DMaybe.fromJust orderBy] else Nothing)
! #limit ((shift +) <$> mbLimit)
! #offset (Just updatedOffset) -- Offset is 0 in case mbOffset Nothing
! defaults)
Expand All @@ -822,36 +824,81 @@ findAllWithOptionsKVConnector dbConf meshCfg whereClause orderBy mbLimit mbOffse
else do
let findAllQuery = DB.findRows (sqlSelect'
! #where_ whereClause
! #orderBy (Just [orderBy])
! #orderBy (if isJust orderBy then Just [DMaybe.fromJust orderBy] else Nothing)
! #limit mbLimit
! #offset mbOffset
! defaults)
mapLeft MDBError <$> runQuery dbConf findAllQuery
diffRes <- whereClauseDiffCheck whereClause
let source = if not isDisabled then KV_AND_SQL else SQL
pure res

where
applyOptions :: Int -> [table Identity] -> [table Identity]
applyOptions shift rows = do
let cmp = case orderBy of
(Asc col) -> compareCols (fromColumnar' . col . columnize) True
(Desc col) -> compareCols (fromColumnar' . col . columnize) False
let resWithoutLimit = (drop shift . sortBy cmp) rows
let resWithoutLimit = case orderBy of
Nothing -> drop shift rows
Just res -> do
let cmp = case res of
Asc col -> compareCols (fromColumnar' . col . columnize) True
Desc col -> compareCols (fromColumnar' . col . columnize) False
(drop shift . sortBy cmp) rows
maybe resWithoutLimit (`take` resWithoutLimit) mbLimit

compareCols :: (Ord value) => (table Identity -> value) -> Bool -> table Identity -> table Identity -> Ordering
compareCols col isAsc r1 r2 = if isAsc then compare (col r1) (col r2) else compare (col r2) (col r1)

calculateLeftFelledRedisEntries :: [table Identity] -> [table Identity] -> Int
calculateLeftFelledRedisEntries kvRows dbRows = do
case orderBy of
(Asc col) -> do
Just (Asc col) -> do
let dbMn = maximum $ map (fromColumnar' . col . columnize) dbRows
length $ filter (\r -> dbMn > fromColumnar' (col $ columnize r)) kvRows
(Desc col) -> do
Just (Desc col) -> do
let dbMx = maximum $ map (fromColumnar' . col . columnize) dbRows
length $ filter (\r -> dbMx < fromColumnar' (col $ columnize r)) kvRows
Nothing -> 0

-- Need to recheck offset implementation
findAllWithOptionsKVConnector :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Model be table,
MeshMeta be table,
KVConnector (table Identity),
Serialize.Serialize (table Identity),
Show (table Identity),
ToJSON (table Identity),
FromJSON (table Identity),
L.MonadFlow m, B.HasQBuilder be, BeamRunner beM) =>
DBConfig beM ->
MeshConfig ->
Where be table ->
OrderBy table ->
Maybe Int ->
Maybe Int ->
m (MeshResult [table Identity])
findAllWithOptionsKVConnector dbConf meshCfg whereClause orderBy mbLimit mbOffset = do
findAllWithOptionsHelper dbConf meshCfg whereClause (Just orderBy) mbLimit mbOffset

-- Need to recheck offset implementation
findAllWithOptionsKVConnector' :: forall be table beM m.
( HasCallStack,
BeamRuntime be beM,
Model be table,
MeshMeta be table,
KVConnector (table Identity),
Serialize.Serialize (table Identity),
Show (table Identity),
ToJSON (table Identity),
FromJSON (table Identity),
L.MonadFlow m, B.HasQBuilder be, BeamRunner beM) =>
DBConfig beM ->
MeshConfig ->
Where be table ->
Maybe Int ->
Maybe Int ->
m (MeshResult [table Identity])
findAllWithOptionsKVConnector' dbConf meshCfg whereClause mbLimit mbOffset =
findAllWithOptionsHelper dbConf meshCfg whereClause Nothing mbLimit mbOffset

findAllWithKVConnector :: forall be table beM m.
( HasCallStack,
Expand Down

0 comments on commit 3c35933

Please sign in to comment.