stm icon indicating copy to clipboard operation
stm copied to clipboard

add method to snapshot TBQueue without flushing

Open epoberezkin opened this issue 2 years ago • 3 comments

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?

epoberezkin avatar Mar 03 '22 22:03 epoberezkin

I have no objection to adding this.

simonmar avatar Apr 23 '22 12:04 simonmar

cool, I will make a PR if that's ok

epoberezkin avatar Apr 23 '22 12:04 epoberezkin

in #56

epoberezkin avatar Apr 23 '22 12:04 epoberezkin