on master help sign in
Add some chunked file read APIs
944f57dHarendra Kumar
src/Streamly/Internal/FileSystem/File.hs+42−32
@@ -44,19 +44,18 @@ module Streamly.Internal.FileSystem.File
44 withFile
44 withFile
45
45
46 -- ** Read From File
46 -- ** Read From File
47+ , readWithBufferOf
47 , read
48 , read
48 -- , readShared
49 -- , readShared
49− -- , readTailForever
50−
51 -- , readUtf8
50 -- , readUtf8
52 -- , readLines
51 -- , readLines
53 -- , readFrames
52 -- , readFrames
54− -- , readChunks
55
53
56 , toBytes
54 , toBytes
57
55
58 -- -- * Array Read
56 -- -- * Array Read
59− -- , readArrayOf
57+ , readChunksWithBufferOf
58+ , readChunks
60
59
61 , toChunksWithBufferOf
60 , toChunksWithBufferOf
62 , toChunks
61 , toChunks
@@ -158,6 +157,19 @@ usingFile =
158 UF.bracket (\file -> liftIO $ openFile file ReadMode)
157 UF.bracket (\file -> liftIO $ openFile file ReadMode)
159 (liftIO . hClose)
158 (liftIO . hClose)
160
159
160+{-# INLINABLE usingFile2 #-}
161+usingFile2 :: (MonadCatch m, MonadIO m)
162+ => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
163+usingFile2 = UF.bracket before after
164+
165+ where
166+
167+ before (x, file) = do
168+ h <- liftIO $ openFile file ReadMode
169+ return (x, h)
170+
171+ after (_, h) = liftIO $ hClose h
172+
161-------------------------------------------------------------------------------
173-------------------------------------------------------------------------------
162-- Array IO (Input)
174-- Array IO (Input)
163-------------------------------------------------------------------------------
175-------------------------------------------------------------------------------
@@ -217,15 +229,36 @@ toChunks = toChunksWithBufferOf defaultChunkSize
217-- read requests at the same time. For serial case we can use async IO. We can
229-- read requests at the same time. For serial case we can use async IO. We can
218-- also control the read throughput in mbps or IOPS.
230-- also control the read throughput in mbps or IOPS.
219
231
220−{-
232+-- | Unfold the tuple @(bufsize, filepath)@ into a stream of 'Word8' arrays.
233+-- Read requests to the IO device are performed using a buffer of size
234+-- @bufsize@. The size of an array in the resulting stream is always less than
235+-- or equal to @bufsize@.
236+--
237+-- /Internal/
238+--
239+{-# INLINE readChunksWithBufferOf #-}
240+readChunksWithBufferOf :: (MonadCatch m, MonadIO m)
241+ => Unfold m (Int, FilePath) (Array Word8)
242+readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
243+
244+-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
245+-- device are performed using a buffer of size
246+-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
247+-- size of arrays in the resulting stream are therefore less than or equal to
248+-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
249+--
250+-- /Internal/
251+{-# INLINE readChunks #-}
252+readChunks :: (MonadCatch m, MonadIO m) => Unfold m FilePath (Array Word8)
253+readChunks = usingFile FH.readChunks
254+
221-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
255-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
222-- to the IO device are performed using buffers of @bufsize@.
256-- to the IO device are performed using buffers of @bufsize@.
223--
257--
224−-- @since 0.7.0
258+-- /Internal/
225{-# INLINE readWithBufferOf #-}
259{-# INLINE readWithBufferOf #-}
226−readWithBufferOf :: MonadIO m => Unfold m (Int, FilePath) Word8
260+readWithBufferOf :: (MonadCatch m, MonadIO m) => Unfold m (Int, FilePath) Word8
227−readWithBufferOf = UF.concat (usingFilexxx FH.readChunksWithBufferOf) A.read
261+readWithBufferOf = usingFile2 FH.readWithBufferOf
228−-}
229
262
230-- | Unfolds a file path into a byte stream. IO requests to the device are
263-- | Unfolds a file path into a byte stream. IO requests to the device are
231-- performed in sizes of
264-- performed in sizes of
@@ -236,20 +269,6 @@ readWithBufferOf = UF.concat (usingFilexxx FH.readChunksWithBufferOf) A.read
236read :: (MonadCatch m, MonadIO m) => Unfold m FilePath Word8
269read :: (MonadCatch m, MonadIO m) => Unfold m FilePath Word8
237read = UF.concat (usingFile FH.readChunks) A.read
270read = UF.concat (usingFile FH.readChunks) A.read
238
271
239−{-
240−-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
241−-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
242−-- as soon as EOF is encountered.
243−--
244−{-# INLINE readInChunksOf #-}
245−readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
246−readInChunksOf chunkSize h = A.flattenArrays $ toChunksWithBufferOf chunkSize h
247−-}
248−
249−-- TODO
250−-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
251−--
252−-- > read = 'readByChunks' defaultChunkSize
253-- | Generate a stream of bytes from a file specified by path. The stream ends
272-- | Generate a stream of bytes from a file specified by path. The stream ends
254-- when EOF is encountered. File is locked using multiple reader and single
273-- when EOF is encountered. File is locked using multiple reader and single
255-- writer locking mode.
274-- writer locking mode.
@@ -269,15 +288,6 @@ toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
269{-# INLINE readShared #-}
288{-# INLINE readShared #-}
270readShared :: (IsStream t, MonadIO m) => Handle -> t m Word8
289readShared :: (IsStream t, MonadIO m) => Handle -> t m Word8
271readShared = undefined
290readShared = undefined
272−
273−-- | Read a stream from a given file path. When end of file (EOF) is reached
274−-- this API waits for more data to be written to the file and keeps reading it
275−-- as it is written.
276−--
277−-- @since 0.7.0
278−{-# INLINE readTailForever #-}
279−readTailForever :: (IsStream t, MonadIO m) => Handle -> t m Word8
280−readTailForever = undefined
281-}
291-}
282
292
283-------------------------------------------------------------------------------
293-------------------------------------------------------------------------------