Skip to content

Commit

Permalink
optimise(sut): Make dumblog with journal use a better in-memory repre…
Browse files Browse the repository at this point in the history
…sentation
  • Loading branch information
symbiont-daniel-gustafsson committed Mar 2, 2022
1 parent 3cb9bef commit 1b0e43e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/sut/dumblog/src/Dumblog/Journal/StateMachine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import Data.Binary (Binary)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBS8
import Data.Sequence
import GHC.Generics (Generic)

import Dumblog.Journal.Types

-- This is the main state of Dumblog, which is the result of applying all commands in the log
data InMemoryDumblog = InMemoryDumblog
{ theLog :: [ByteString] -- not very memory efficient, but not the point
{ theLog :: Seq ByteString -- not very memory efficient, but not the point
, nextIx :: Int
} deriving Generic

instance Binary InMemoryDumblog where

initState :: InMemoryDumblog
initState = InMemoryDumblog [] 0
initState = InMemoryDumblog empty 0

-- this could be pure?
runCommand :: InMemoryDumblog -> Command -> IO (InMemoryDumblog, Response)
runCommand state@(InMemoryDumblog appLog ix) cmd = case cmd of
Write bs -> pure (InMemoryDumblog (bs:appLog) (ix+1), LBS8.pack (show ix))
Write bs -> pure (InMemoryDumblog (appLog |> bs) (ix+1), LBS8.pack (show ix))
Read i
| i < ix -> pure (state, LBS.fromStrict $ appLog !! (ix - 1 - i))
| i < ix -> pure (state, LBS.fromStrict $ index appLog i)
| otherwise -> pure (state, "Transaction not in the store!") -- we probably should really signal failure

0 comments on commit 1b0e43e

Please sign in to comment.