Skip to content

Commit 398c7e3

Browse files
batch assoc functions
1 parent 462ed7c commit 398c7e3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/Simplex/Messaging/Server/MsgStore/Journal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ instance QueueStoreClass (JournalQueue s) (QStore s) where
353353
{-# INLINE getCreateService #-}
354354
setQueueService = withQS setQueueService
355355
{-# INLINE setQueueService #-}
356-
setQueueServices st = withQS (\qs -> setQueueServices qs) st
356+
setQueueServices = withQS setQueueServices
357357
{-# INLINE setQueueServices #-}
358358
getQueueNtfServices = withQS (getQueueNtfServices @(JournalQueue s))
359359
{-# INLINE getQueueNtfServices #-}

src/Simplex/Messaging/Server/QueueStore/Postgres.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,29 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
505505
withLog "setQueueService" st $ \sl -> logQueueService sl rId party serviceId
506506

507507
setQueueServices _ _ _ [] = pure $ Right M.empty
508-
setQueueServices _ _ _ _ = pure $ Right M.empty -- TODO batch implementation
508+
setQueueServices st party serviceId qs = E.uninterruptibleMask_ $ runExceptT $ do
509+
updated <- S.fromList <$> withDB' "setQueueServices" st (\db ->
510+
map fromOnly <$> DB.query db updateQuery (serviceId, In (map recipientId qs)))
511+
results <- liftIO $ forM qs $ \sq -> do
512+
let rId = recipientId sq
513+
(rId,) <$> if S.member rId updated
514+
then readQueueRecIO (queueRec sq) $>>= \q -> do
515+
atomically $ writeTVar (queueRec sq) $ Just $ updateRec q
516+
withLog "setQueueServices" st $ \sl -> logQueueService sl rId party serviceId
517+
pure $ Right ()
518+
else pure $ Left AUTH
519+
pure $ M.fromList results
520+
where
521+
updateQuery = case party of
522+
SRecipientService ->
523+
"UPDATE msg_queues SET rcv_service_id = ? WHERE recipient_id IN ? AND deleted_at IS NULL RETURNING recipient_id"
524+
SNotifierService ->
525+
"UPDATE msg_queues SET ntf_service_id = ? WHERE recipient_id IN ? AND notifier_id IS NOT NULL AND deleted_at IS NULL RETURNING recipient_id"
526+
updateRec q = case party of
527+
SRecipientService -> q {rcvServiceId = serviceId}
528+
SNotifierService -> case notifier q of
529+
Just nc -> q {notifier = Just nc {ntfServiceId = serviceId}}
530+
Nothing -> q
509531

510532
getQueueNtfServices :: PostgresQueueStore q -> [(NotifierId, a)] -> IO (Either ErrorType ([(Maybe ServiceId, [(NotifierId, a)])], [(NotifierId, a)]))
511533
getQueueNtfServices st ntfs = E.uninterruptibleMask_ $ runExceptT $ do

src/Simplex/Messaging/Server/QueueStore/STM.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ instance StoreQueueClass q => QueueStoreClass q (STMQueueStore q) where
337337
mapM_ (removeServiceQueue st serviceSel qId) prevSrvId
338338
mapM_ (addServiceQueue st serviceSel qId) serviceId
339339

340-
setQueueServices _ _ _ _ = pure $ Right M.empty -- TODO loop implementation
340+
setQueueServices st party serviceId qs = Right . M.fromList <$> mapM setOne qs
341+
where
342+
setOne sq = (recipientId sq,) <$> setQueueService st sq party serviceId
341343

342344
getQueueNtfServices :: STMQueueStore q -> [(NotifierId, a)] -> IO (Either ErrorType ([(Maybe ServiceId, [(NotifierId, a)])], [(NotifierId, a)]))
343345
getQueueNtfServices st ntfs = do

0 commit comments

Comments
 (0)