diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-01-11 03:20:56 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-01-11 03:26:52 +0100 |
commit | 7bdf16be84b368655ce2ee3d9ab6bf185dfb59b5 (patch) | |
tree | 3ee48fc98f98ab7ac7ad19e24334e07b8b147dd6 /app/Store.hs | |
parent | 673c59d9be8b62106ffbba96d805680f0b5e7e3f (diff) |
chore: make computing `probabilityMap` more performance
Diffstat (limited to 'app/Store.hs')
-rw-r--r-- | app/Store.hs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/app/Store.hs b/app/Store.hs new file mode 100644 index 0000000..7a4637f --- /dev/null +++ b/app/Store.hs @@ -0,0 +1,60 @@ +{-# LANGUAGE BlockArguments #-} +{-# LANGUAGE OverloadedRecordDot #-} + +module Store + ( commitDocument, + replaceDocument, + ) +where + +import Control.Exception (Exception, throwIO) +import Data.Aeson qualified as J +import Data.ByteString.Lazy qualified as LB +import Data.Default (Default (def)) +import Data.List (intercalate) +import Data.String (IsString (fromString)) +import Document qualified as D +import ProbabilityMap qualified as C +import System.Directory (copyFile) +import System.Exit (ExitCode (ExitFailure, ExitSuccess)) +import System.IO.LockFile (withLockFile) +import System.Process.Typed (readProcessStderr) +import Text.Printf (printf) + +replaceDocument :: String -> D.Document -> IO () +replaceDocument message doc = do + withGit do + J.encodeFile doc.iFilePath doc.index + oldDoc <- D.readDocument doc.iFileName + _ <- C.modifyProbabilityCache (C.replaceDocument oldDoc doc) + commitAll [doc.iFilePath, C.fileName] message + +commitDocument :: String -> FilePath -> D.Document -> IO () +commitDocument message original doc = do + withGit do + copyFile original doc.oFilePath + J.encodeFile doc.iFilePath doc.index + _ <- C.modifyProbabilityCache (C.addDocument doc) + commitAll [doc.iFilePath, doc.oFilePath, C.fileName] message + +withGit :: IO a -> IO a +withGit = withLockFile def ".gitlock" + +commitAll :: [FilePath] -> String -> IO () +commitAll fps m = do + sh_ (">/dev/null git add -- " ++ intercalate " " (map (printf "'%s'") fps)) + sh_ (printf ">/dev/null git commit -m '%s' || :" m) + +-- TODO Refacotor library `sh` +sh_ :: String -> IO () +sh_ cmd = do + -- printf "+ %s\n" cmd + (exitCode, err) <- readProcessStderr (fromString cmd) + case exitCode of + ExitSuccess -> return () + ExitFailure exitCode' -> throwIO $ ProcessException exitCode' err + +data ProcessException = ProcessException Int LB.ByteString + deriving (Show) + +instance Exception ProcessException |