Skip to content

Commit 85690a5

Browse files
batch assoc functions
1 parent 462ed7c commit 85690a5

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,28 @@ 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+
forM_ qs $ \sq -> when (S.member (recipientId sq) updated) $ do
512+
ExceptT $ readQueueRecIO (queueRec sq) >>= \case
513+
Left e -> pure $ Left e
514+
Right q -> runExceptT $ do
515+
let q' = updateRec q
516+
atomically $ writeTVar (queueRec sq) $ Just q'
517+
withLog "setQueueServices" st $ \sl -> logQueueService sl (recipientId sq) party serviceId
518+
pure $ M.fromList [(recipientId sq, if S.member (recipientId sq) updated then Right () else Left AUTH) | sq <- qs]
519+
where
520+
updateQuery = case party of
521+
SRecipientService ->
522+
"UPDATE msg_queues SET rcv_service_id = ? WHERE recipient_id IN ? AND deleted_at IS NULL RETURNING recipient_id"
523+
SNotifierService ->
524+
"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"
525+
updateRec q = case party of
526+
SRecipientService -> q {rcvServiceId = serviceId}
527+
SNotifierService -> case notifier q of
528+
Just nc -> q {notifier = Just nc {ntfServiceId = serviceId}}
529+
Nothing -> q
509530

510531
getQueueNtfServices :: PostgresQueueStore q -> [(NotifierId, a)] -> IO (Either ErrorType ([(Maybe ServiceId, [(NotifierId, a)])], [(NotifierId, a)]))
511532
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)