2015-12-23 18:59:58 +00:00
|
|
|
{- Persistent sqlite database queues
|
|
|
|
-
|
2022-06-06 16:16:55 +00:00
|
|
|
- Copyright 2015-2022 Joey Hess <id@joeyh.name>
|
2015-12-23 18:59:58 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-12-23 18:59:58 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
|
|
|
module Database.Queue (
|
|
|
|
DbQueue,
|
|
|
|
openDbQueue,
|
|
|
|
queryDbQueue,
|
|
|
|
closeDbQueue,
|
2015-12-23 20:36:08 +00:00
|
|
|
flushDbQueue,
|
2015-12-23 18:59:58 +00:00
|
|
|
QueueSize,
|
2024-08-12 15:19:58 +00:00
|
|
|
LastCommitTime,
|
2015-12-23 18:59:58 +00:00
|
|
|
queueDb,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Utility.Monad
|
2020-11-05 22:45:37 +00:00
|
|
|
import Utility.RawFilePath
|
2022-06-03 18:10:24 +00:00
|
|
|
import Utility.DebugLocks
|
2022-06-06 16:16:55 +00:00
|
|
|
import Utility.Exception
|
2015-12-23 18:59:58 +00:00
|
|
|
import Database.Handle
|
|
|
|
|
|
|
|
import Database.Persist.Sqlite
|
|
|
|
import Control.Concurrent
|
|
|
|
import Data.Time.Clock
|
2015-12-28 16:41:47 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2015-12-23 18:59:58 +00:00
|
|
|
|
|
|
|
{- A DbQueue wraps a DbHandle, adding a queue of writes to perform.
|
|
|
|
-
|
|
|
|
- This is efficient when there are frequent writes, but
|
|
|
|
- reads will not immediately have access to queued writes. -}
|
|
|
|
data DbQueue = DQ DbHandle (MVar Queue)
|
|
|
|
|
|
|
|
{- Opens the database queue, but does not perform any migrations. Only use
|
|
|
|
- if the database is known to exist and have the right tables; ie after
|
|
|
|
- running initDb. -}
|
2021-10-20 16:24:40 +00:00
|
|
|
openDbQueue :: RawFilePath -> TableName -> IO DbQueue
|
|
|
|
openDbQueue db tablename = DQ
|
|
|
|
<$> openDb db tablename
|
2015-12-23 18:59:58 +00:00
|
|
|
<*> (newMVar =<< emptyQueue)
|
|
|
|
|
2015-12-23 20:36:08 +00:00
|
|
|
{- This or flushDbQueue must be called, eg at program exit to ensure
|
|
|
|
- queued changes get written to the database. -}
|
2015-12-23 18:59:58 +00:00
|
|
|
closeDbQueue :: DbQueue -> IO ()
|
|
|
|
closeDbQueue h@(DQ hdl _) = do
|
|
|
|
flushDbQueue h
|
|
|
|
closeDb hdl
|
|
|
|
|
2015-12-23 20:36:08 +00:00
|
|
|
{- Blocks until all queued changes have been written to the database. -}
|
|
|
|
flushDbQueue :: DbQueue -> IO ()
|
|
|
|
flushDbQueue (DQ hdl qvar) = do
|
2022-06-03 18:10:24 +00:00
|
|
|
q@(Queue sz _ qa) <- debugLocks $ takeMVar qvar
|
2015-12-23 20:36:08 +00:00
|
|
|
if sz > 0
|
2022-06-06 16:16:55 +00:00
|
|
|
then tryNonAsync (commitDb hdl qa) >>= \case
|
|
|
|
Right () -> debugLocks $ putMVar qvar =<< emptyQueue
|
|
|
|
Left e -> do
|
|
|
|
debugLocks $ putMVar qvar q
|
|
|
|
throwM e
|
2022-06-03 18:10:24 +00:00
|
|
|
else debugLocks $ putMVar qvar q
|
2015-12-23 20:36:08 +00:00
|
|
|
|
|
|
|
{- Makes a query using the DbQueue's database connection.
|
|
|
|
- This should not be used to make changes to the database!
|
2015-12-23 18:59:58 +00:00
|
|
|
-
|
2017-09-06 21:07:49 +00:00
|
|
|
- Queries will not see changes that have been recently queued,
|
2015-12-23 18:59:58 +00:00
|
|
|
- so use with care.
|
|
|
|
-}
|
|
|
|
queryDbQueue :: DbQueue -> SqlPersistM a -> IO a
|
|
|
|
queryDbQueue (DQ hdl _) = queryDb hdl
|
|
|
|
|
|
|
|
{- A queue of actions to perform, with a count of the number of actions
|
|
|
|
- queued, and a last commit time. -}
|
|
|
|
data Queue = Queue QueueSize LastCommitTime (SqlPersistM ())
|
|
|
|
|
|
|
|
type QueueSize = Int
|
|
|
|
|
|
|
|
type LastCommitTime = UTCTime
|
|
|
|
|
|
|
|
emptyQueue :: IO Queue
|
|
|
|
emptyQueue = do
|
|
|
|
now <- getCurrentTime
|
|
|
|
return $ Queue 0 now (return ())
|
|
|
|
|
|
|
|
{- Queues a change to be made to the database. It will be queued
|
|
|
|
- to be committed later, unless the commitchecker action returns true,
|
|
|
|
- in which case any previously queued changes are also committed.
|
|
|
|
-
|
|
|
|
- Transactions built up by queueDb are sent to sqlite all at once.
|
|
|
|
- If sqlite fails due to another change being made concurrently by another
|
|
|
|
- process, the transaction is put back in the queue. This avoids
|
|
|
|
- the sqlite multiple writer problem.
|
|
|
|
-}
|
|
|
|
queueDb
|
|
|
|
:: DbQueue
|
|
|
|
-> (QueueSize -> LastCommitTime -> IO Bool)
|
|
|
|
-> SqlPersistM ()
|
|
|
|
-> IO ()
|
|
|
|
queueDb (DQ hdl qvar) commitchecker a = do
|
2022-06-03 18:10:24 +00:00
|
|
|
Queue sz lastcommittime qa <- debugLocks $ takeMVar qvar
|
2015-12-23 18:59:58 +00:00
|
|
|
let !sz' = sz + 1
|
|
|
|
let qa' = qa >> a
|
2022-06-03 18:10:24 +00:00
|
|
|
let enqueue = debugLocks . putMVar qvar
|
2015-12-23 18:59:58 +00:00
|
|
|
ifM (commitchecker sz' lastcommittime)
|
|
|
|
( do
|
|
|
|
r <- commitDb' hdl qa'
|
|
|
|
case r of
|
|
|
|
Left _ -> enqueue $ Queue sz' lastcommittime qa'
|
2015-12-23 20:36:08 +00:00
|
|
|
Right _ -> enqueue =<< emptyQueue
|
2015-12-23 18:59:58 +00:00
|
|
|
, enqueue $ Queue sz' lastcommittime qa'
|
|
|
|
)
|