[temp file] fix tests

parent 2796048c
Pipeline #7325 passed with stages
in 61 minutes and 26 seconds
...@@ -23,11 +23,9 @@ module Gargantext.API.Node.Corpus.New ...@@ -23,11 +23,9 @@ module Gargantext.API.Node.Corpus.New
import Conduit ((.|), yieldMany, mapMC, mapC, transPipe) import Conduit ((.|), yieldMany, mapMC, mapC, transPipe)
import Control.Exception.Safe (MonadMask) import Control.Exception.Safe (MonadMask)
import Control.Lens ( view, non ) import Control.Lens ( view, non )
import Data.ByteString.Lazy qualified as BSL
import Data.Conduit.Internal (zipSources) import Data.Conduit.Internal (zipSources)
import Data.Swagger ( ToSchema(..) ) import Data.Swagger ( ToSchema(..) )
import Data.Text qualified as T import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Database.PostgreSQL.Simple.LargeObjects qualified as PSQL import Database.PostgreSQL.Simple.LargeObjects qualified as PSQL
import Gargantext.API.Admin.Orchestrator.Types qualified as API import Gargantext.API.Admin.Orchestrator.Types qualified as API
import Gargantext.API.Ngrams (commitStatePatch, Versioned(..)) import Gargantext.API.Ngrams (commitStatePatch, Versioned(..))
...@@ -53,7 +51,7 @@ import Gargantext.Database.Admin.Types.Hyperdata.Document ( ToHyperdataDocument( ...@@ -53,7 +51,7 @@ import Gargantext.Database.Admin.Types.Hyperdata.Document ( ToHyperdataDocument(
import Gargantext.Database.Admin.Types.Hyperdata.File ( HyperdataFile(..) ) import Gargantext.Database.Admin.Types.Hyperdata.File ( HyperdataFile(..) )
import Gargantext.Database.Admin.Types.Node (CorpusId, NodeType(..), ParentId) import Gargantext.Database.Admin.Types.Node (CorpusId, NodeType(..), ParentId)
import Gargantext.Database.GargDB qualified as GargDB import Gargantext.Database.GargDB qualified as GargDB
import Gargantext.Database.Prelude (readLargeObjectViaTempFile) import Gargantext.Database.Prelude (readLargeObject)
import Gargantext.Database.Query.Table.Node (getNodeWith, getOrMkList) import Gargantext.Database.Query.Table.Node (getNodeWith, getOrMkList)
import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata) import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
import Gargantext.Database.Query.Tree.Root (MkCorpusUser(MkCorpusUserNormalCorpusIds)) import Gargantext.Database.Query.Tree.Root (MkCorpusUser(MkCorpusUserNormalCorpusIds))
...@@ -248,10 +246,9 @@ addToCorpusWithTempFile user cid nwtf jobHandle = do ...@@ -248,10 +246,9 @@ addToCorpusWithTempFile user cid nwtf jobHandle = do
-- TODO granularity of the logStatus -- TODO granularity of the logStatus
let oId = PSQL.Oid $ fromIntegral $ nwtf ^. wtf_file_oid let oId = PSQL.Oid $ fromIntegral $ nwtf ^. wtf_file_oid
(data'', size) <- readLargeObjectViaTempFile oId data' <- readLargeObject oId
let data' = BSL.toStrict data'' -- $(logLocM) DEBUG $ "[addToCorpusWithTempFile] size: " <> show size
$(logLocM) DEBUG $ "[addToCorpusWithTempFile] size: " <> show size -- $(logLocM) DEBUG $ "[addToCorpusWithTempFile] data': " <> TE.decodeUtf8 data'
$(logLocM) DEBUG $ "[addToCorpusWithTempFile] data': " <> TE.decodeUtf8 data'
eDocsC <- liftBase $ parseC (nwtf ^. wtf_fileformat) data' eDocsC <- liftBase $ parseC (nwtf ^. wtf_fileformat) data'
case eDocsC of case eDocsC of
Right (count, docsC) -> do Right (count, docsC) -> do
...@@ -297,7 +294,7 @@ addToCorpusWithTempFile user cid nwtf jobHandle = do ...@@ -297,7 +294,7 @@ addToCorpusWithTempFile user cid nwtf jobHandle = do
markComplete jobHandle markComplete jobHandle
Left parseErr -> do Left parseErr -> do
$(logLocM) ERROR $ "[addToCorpusWithTempFile] parse error: " <> (Parser._ParseFormatError parseErr) $(logLocM) ERROR $ "[addToCorpusWithTempFile] parse error: " <> Parser._ParseFormatError parseErr
markFailed (Just parseErr) jobHandle markFailed (Just parseErr) jobHandle
{- {-
......
...@@ -75,7 +75,6 @@ addCorpusWithQuery user = ...@@ -75,7 +75,6 @@ addCorpusWithQuery user =
-- | Uses temporary file stored in postgres to add that file to a corpus -- | Uses temporary file stored in postgres to add that file to a corpus
addWithTempFileApi :: AuthenticatedUser addWithTempFileApi :: AuthenticatedUser
-> Named.AddWithTempFile (AsServerT (GargM Env BackendInternalError)) -> Named.AddWithTempFile (AsServerT (GargM Env BackendInternalError))
-- -> WorkerAPI '[FormUrlEncoded] NewWithForm (AsServerT m)
addWithTempFileApi authenticatedUser = addWithTempFileApi authenticatedUser =
Named.AddWithTempFile { Named.AddWithTempFile {
addWithTempFileEp = \cId -> addWithTempFileEp = \cId ->
......
...@@ -9,6 +9,7 @@ Portability : POSIX ...@@ -9,6 +9,7 @@ Portability : POSIX
-} -}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE LambdaCase #-} {-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
...@@ -285,31 +286,34 @@ createLargeObject bs = mkCmd $ \c -> PGS.withTransaction c $ do ...@@ -285,31 +286,34 @@ createLargeObject bs = mkCmd $ \c -> PGS.withTransaction c $ do
PSQL.loClose c loFd PSQL.loClose c loFd
pure oId pure oId
readLargeObject :: PSQL.Oid -> DBCmd err (BSL.ByteString, Int) readLargeObject :: PSQL.Oid -> DBCmd err BS.ByteString
readLargeObject oId = mkCmd $ \c -> PGS.withTransaction c $ do readLargeObject oId = mkCmd $ \c -> PGS.withTransaction c $ do
loFd <- PSQL.loOpen c oId PSQL.ReadMode loFd <- PSQL.loOpen c oId PSQL.ReadMode
let chunkSize = 1024 let chunkSize = 1024
let readChunks tell = do let readChunks tell = do
c' <- PSQL.loRead c loFd chunkSize c' <- PSQL.loRead c loFd chunkSize
tell' <- PSQL.loTell c loFd tell' <- PSQL.loTell c loFd
putText $ "[readLargeObject] tell': " <> show tell'
if tell == tell' then if tell == tell' then
pure ([c'], tell) pure ([c'], tell)
else do else do
(cs', tell'') <- readChunks tell' (cs', tell'') <- readChunks tell'
pure (c':cs', tell'') pure (c':cs', tell'')
(chunks, size) <- readChunks 0 (chunks, _size) <- readChunks 0
pure (BSL.fromChunks chunks, size) let s = force BSL.toStrict $ BSL.fromChunks chunks
PSQL.loClose c loFd
pure s
readLargeObjectViaTempFile :: (CES.MonadMask m, IsDBCmd env err m) readLargeObjectViaTempFile :: (CES.MonadMask m, IsDBCmd env err m)
=> PSQL.Oid -> m (BSL.ByteString, Int) => PSQL.Oid -> m BS.ByteString
readLargeObjectViaTempFile oId = readLargeObjectViaTempFile oId = do
CES.bracket (liftBase $ emptySystemTempFile "large-object") CES.bracket (liftBase $ emptySystemTempFile "large-object")
(liftBase . removeFile) (liftBase . removeFile)
(\fp -> do (\fp -> do
mkCmd $ \c -> PSQL.loExport c oId fp mkCmd $ \c -> withTransaction c $ \_ -> PSQL.loExport c oId fp
c <- liftBase $ BSL.readFile fp !contents <- liftBase $ BS.readFile fp
pure (c, fromIntegral $ BSL.length c)) pure contents)
where
withTransaction c = CES.bracket (PGS.begin c) (\_ -> PGS.rollback c)
removeLargeObject :: Int -> DBCmd err () removeLargeObject :: Int -> DBCmd err ()
removeLargeObject oId = mkCmd $ \c -> do removeLargeObject oId = mkCmd $ \c -> do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment