stm
stm copied to clipboard
add method to snapshot TBQueue without flushing
Currently there is no way to efficiently snapshot the state of TBQueue without flushing it and rewriting (and neither there is a way to efficiently create TBQueue from the list). What is required to do to a snapshot now:
snapshotTBQueue :: TBQueue a -> STM [a]
snapshotTBQueue q = do
xs <- flushTBQueue q
mapM_ (writeTBQueue q) xs
pure xs
But snapshot is a part of flushTBQueue, and if TBQueue constructor was exported it could have been implemented outside in this way:
snapshotTBQueue :: TBQueue a -> STM [a]
snapshotTBQueue (TBQueue _ read _ write _) = do
xs <- readTVar read
ys <- readTVar write
return $ if null xs && null is then [] else xs ++ reverse ys
The use case for snapshot is dumping a state of the queue(s) to the hard-drive, so it can be efficiently snapshotted before dump starts (so that the state is consistent).
Could we add snapshot to STM? Alternatively, could we export TBQueue constructor?
I have no objection to adding this.
cool, I will make a PR if that's ok
in #56