on 7d00819 help sign in
streamly/examples/Split.hs
38 lines 1.3 kB history raw
1import qualified Streamly.Internal.Data.Stream.IsStream as S
2import qualified Streamly.Internal.FileSystem.Handle as IFH
3import qualified Streamly.FileSystem.Handle as FH
4import qualified System.IO as FH
6import Control.Monad.IO.Class (liftIO)
7import Control.Monad.State.Strict (StateT(..), get, put)
8import System.Environment (getArgs)
9import System.IO (IOMode(..))
10import Data.Function ((&))
12newHandle :: StateT (Maybe (FH.Handle, Int)) IO FH.Handle
13newHandle = do
14 old <- get
15 idx <- case old of
16 Nothing -> return 0
17 Just (h, i) -> liftIO (FH.hClose h) >> return (i + 1)
18 h <- liftIO $ FH.openFile ("dst-xyz-" ++ show idx ++ ".txt") WriteMode
19 put (Just (h, idx))
20 return h
22-- XXX reduce the input stream to a stream of file names
23-- The fold can return the file name/handle after it is done.
24-- similarly the files can written to directories and we can generate a stream
25-- of directory names.
26splitFile :: FH.Handle -> IO ()
27splitFile inHandle =
28 S.unfold FH.read inHandle
29 & S.liftInner
30 & S.chunksOf2 (180 * 1024 * 1024) newHandle IFH.write2
31 & S.evalStateT (return Nothing) -- generate new handle for each iteration
32 & S.drain
34main :: IO ()
35main = do
36 name <- fmap head getArgs
37 src <- FH.openFile name ReadMode
38 splitFile src