@@ -45,7 +45,6 @@ import qualified Data.Attoparsec.ByteString.Char8 as A
4545import Data.Bitraversable (bimapM )
4646import Data.ByteString.Char8 (ByteString )
4747import qualified Data.ByteString.Char8 as B
48- import qualified Data.ByteString.Lazy.Char8 as LB
4948import Data.Functor (($>) )
5049import Data.Int (Int64 )
5150import Data.List (intercalate )
@@ -85,7 +84,8 @@ data JournalStoreConfig = JournalStoreConfig
8584 -- When this limit is reached, the file will be changed.
8685 -- This number should be set bigger than queue quota.
8786 maxMsgCount :: Int ,
88- maxStateLines :: Int
87+ maxStateLines :: Int ,
88+ stateTailSize :: Int
8989 }
9090
9191data JMQueue = JMQueue
@@ -530,7 +530,7 @@ readWriteQueueState JournalMsgStore {random, config} statePath =
530530 where
531531 tempBackup = statePath <> " .bak"
532532 readQueueState = do
533- ls <- LB .lines <$> LB. readFile statePath
533+ ls <- B .lines <$> readFileTail
534534 case ls of
535535 [] -> writeNewQueueState
536536 _ -> do
@@ -541,7 +541,7 @@ readWriteQueueState JournalMsgStore {random, config} statePath =
541541 logWarn $ " STORE: readWriteQueueState, empty queue state - initialized, " <> T. pack statePath
542542 st <- newMsgQueueState <$> newJournalId random
543543 writeQueueState st
544- useLastLine len isLastLine ls = case strDecode $ LB. toStrict $ last ls of
544+ useLastLine len isLastLine ls = case strDecode $ last ls of
545545 Right st
546546 | len > maxStateLines config || not isLastLine ->
547547 backupWriteQueueState st
@@ -571,6 +571,14 @@ readWriteQueueState JournalMsgStore {random, config} statePath =
571571 sh <- openFile statePath AppendMode
572572 closeOnException sh $ appendState sh st
573573 pure (st, sh)
574+ readFileTail =
575+ IO. withFile statePath ReadMode $ \ h -> do
576+ size <- IO. hFileSize h
577+ let sz = stateTailSize config
578+ sz' = fromIntegral sz
579+ if size > sz'
580+ then IO. hSeek h AbsoluteSeek (size - sz') >> B. hGet h sz
581+ else B. hGet h (fromIntegral size)
574582
575583validQueueState :: MsgQueueState -> Bool
576584validQueueState MsgQueueState {readState = rs, writeState = ws, size}
0 commit comments