Skip to content

add method to snapshot TBQueue without flushing #54

Open
@epoberezkin

Description

@epoberezkin

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions