Use an acid state, such as an event log in Haskell

I use acid-state in the project, and I really like it. I like how easy it is to add persistence to simple Haskell types without a special template.

As far as I understand, acid-state stores an event log, and does not record all new state with each update. What I'm looking for is a way to view the log of recent state changes from an application in a list (read-only). (Something like git log , although I don't need a fork or the ability to revert to an older commit.)

Of course, I can write to my separate log file with detailed information about all state changes or even simulate my data as a list of differences, but I prefer what is automatic and allows me to make the most of simple data types.

Is there a library like an acid state, or perhaps some internal functions of an acid state that I could use to do this?

+7
haskell persistence acid-state
source share
1 answer

Here's the approach I came across:

I already used a wrapper around Data.Acid.update (because it works in a monad with limited IO), and I realized that the wrapper can save the event in my own log. The limitation of UpdateEvent update implies SafeCopy update with runPut . safePut as runPut . safePut runPut . safePut I can serialize this to a ByteString . However ... this is a binary representation not intended for readability, and I would like to review it. I realized that reading an acid state event log from disk would have the same problem.

So, I added Show update to the limitations of my wrapper. In every place where the state is used, I added:

 {-# LANGUAGE StandaloneDeriving #-} ... $(makeAcidic ''State ['update]) deriving instance Show Update 

(StandaloneDeriving may be a bit inconsistent, but it does not cause problems with orphans here, as it is in the same file.)

In the wrapper, I now call show in the update and write the result to my own log file. Of course, this loses the atomicity of the update: perhaps the application crashes between the update call and my own logging, but I am ready to accept this risk.

+2
source share

All Articles