on master help sign in
Merge pull request #249 from composewell/misc-refactorings

Renaming of read / write apis

commit72177d07bba8f01fdcf8d0f8f1e129b12846c411 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 7 Aug 2019, 03:38 (7 years ago) parent5d5d16a6ffd5bc
8 files changed+85−82
benchmark/FileIO.hs+2−2 examples/FileIOExamples.hs+3−3 examples/HandleIO.hs+2−2 src/Streamly/FileSystem/FD.hs+11−11 src/Streamly/FileSystem/File.hs+19−18 src/Streamly/FileSystem/Handle.hs+25−23 src/Streamly/Network/Client.hs+5−5 src/Streamly/Network/Socket.hs+18−18
benchmark/FileIO.hs+2−2
@@ -117,7 +117,7 @@ main = do
117117 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
118118 , mkBench "cat" href $ do
119119 Handles inh _ <- readIORef href
120− FH.writeArrays devNull $ FH.readArraysOfUpto (256*1024) inh
120+ FH.writeArrays devNull $ FH.readArraysOf (256*1024) inh
121121 ]
122122 , bgroup "readStream"
123123 [ mkBench "last" href $ do
@@ -198,7 +198,7 @@ main = do
198198 FH.writeArraysPackedUpto (1024*1024) outh
199199 $ S.insertAfterEach (return $ A.fromList [10])
200200 $ AS.splitOn 10
201− $ FH.readArraysOfUpto (1024*1024) inh
201+ $ FH.readArraysOf (1024*1024) inh
202202 -}
203203 , mkBench "words-unwords" href $ do
204204 Handles inh outh <- readIORef href
examples/FileIOExamples.hs+3−3
@@ -7,13 +7,13 @@ import Data.Char (ord)
77import System.Environment (getArgs)
88
99cat :: FilePath -> IO ()
10−cat src = File.writeArrays "/dev/stdout" $ File.readArraysOfUpto (256*1024) src
10+cat src = File.writeArrays "/dev/stdout" $ File.readArraysOf (256*1024) src
1111
1212cp :: FilePath -> FilePath -> IO ()
13−cp src dst = File.writeArrays dst $ File.readArraysOfUpto (256*1024) src
13+cp src dst = File.writeArrays dst $ File.readArraysOf (256*1024) src
1414
1515append :: FilePath -> FilePath -> IO ()
16−append src dst = File.appendArrays dst $ File.readArraysOfUpto (256*1024) src
16+append src dst = File.appendArrays dst $ File.readArraysOf (256*1024) src
1717
1818ord' :: Num a => Char -> a
1919ord' = (fromIntegral . ord)
examples/HandleIO.hs+2−2
@@ -12,12 +12,12 @@ import System.Environment (getArgs)
1212import System.IO (IOMode(..), hSeek, SeekMode(..))
1313
1414cat :: FH.Handle -> IO ()
15−cat src = FH.writeArrays FH.stdout $ FH.readArraysOfUpto (256*1024) src
15+cat src = FH.writeArrays FH.stdout $ FH.readArraysOf (256*1024) src
1616-- byte stream version
1717-- cat src = FH.write FH.stdout $ FH.read src
1818
1919cp :: FH.Handle -> FH.Handle -> IO ()
20−cp src dst = FH.writeArrays dst $ FH.readArraysOfUpto (256*1024) src
20+cp src dst = FH.writeArrays dst $ FH.readArraysOf (256*1024) src
2121-- byte stream version
2222-- cp src dst = FH.write dst $ FH.read src
2323
src/Streamly/FileSystem/FD.hs+11−11
@@ -75,7 +75,7 @@ module Streamly.FileSystem.FD
7575 -- , readUtf8
7676 -- , readLines
7777 -- , readFrames
78− , readByChunksUpto
78+ , readInChunksOf
7979
8080 -- -- * Array Read
8181 -- , readArrayUpto
@@ -90,7 +90,7 @@ module Streamly.FileSystem.FD
9090 -- , writeUtf8
9191 -- , writeUtf8Lines
9292 -- , writeFrames
93− , writeByChunksOf
93+ , writeInChunksOf
9494
9595 -- -- * Array Write
9696 -- , writeArray
@@ -321,13 +321,13 @@ readArrays = readArraysOfUpto defaultChunkSize
321321-- read requests at the same time. For serial case we can use async IO. We can
322322-- also control the read throughput in mbps or IOPS.
323323
324−-- | @readByChunksUpto chunkSize handle@ reads a byte stream from a file handle,
324+-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file handle,
325325-- reads are performed in chunks of up to @chunkSize@. The stream ends as soon
326326-- as EOF is encountered.
327327--
328−{-# INLINE readByChunksUpto #-}
329−readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
330−readByChunksUpto chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
328+{-# INLINE readInChunksOf #-}
329+readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
330+readInChunksOf chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
331331
332332-- TODO
333333-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
@@ -399,11 +399,11 @@ _writevArraysPackedUpto n h xs =
399399-- input elements.
400400--
401401-- @since 0.7.0
402−{-# INLINE writeByChunksOf #-}
403−writeByChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
404−writeByChunksOf n h m = writeArrays h $ AS.arraysOf n m
402+{-# INLINE writeInChunksOf #-}
403+writeInChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
404+writeInChunksOf n h m = writeArrays h $ AS.arraysOf n m
405405
406−-- > write = 'writeByChunks' A.defaultChunkSize
406+-- > write = 'writeInChunksOf' A.defaultChunkSize
407407--
408408-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
409409-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
@@ -412,7 +412,7 @@ writeByChunksOf n h m = writeArrays h $ AS.arraysOf n m
412412-- @since 0.7.0
413413{-# INLINE write #-}
414414write :: MonadIO m => Handle -> SerialT m Word8 -> m ()
415−write = writeByChunksOf defaultChunkSize
415+write = writeInChunksOf defaultChunkSize
416416
417417{-
418418{-# INLINE write #-}
src/Streamly/FileSystem/File.hs+19−18
@@ -63,7 +63,7 @@ module Streamly.FileSystem.File
6363 -- -- * Array Read
6464 -- , readArrayOf
6565
66− , readArraysOfUpto
66+ , readArraysOf
6767 -- , readArraysOf
6868 , readArrays
6969
@@ -72,7 +72,7 @@ module Streamly.FileSystem.File
7272 -- , writeUtf8
7373 -- , writeUtf8ByLines
7474 -- , writeByFrames
75− , writeByChunks
75+ , writeInChunksOf
7676
7777 -- -- * Array Write
7878 , writeArray
@@ -183,26 +183,27 @@ appendArray file arr = SIO.withFile file AppendMode (\h -> FH.writeArray h arr)
183183-- Stream of Arrays IO
184184-------------------------------------------------------------------------------
185185
186−-- | @readArraysOfUpto size file@ reads a stream of arrays from file @file@.
186+-- | @readArraysOf size file@ reads a stream of arrays from file @file@.
187187-- The maximum size of a single array is specified by @size@. The actual size
188188-- read may be less than or equal to @size@.
189−{-# INLINABLE readArraysOfUpto #-}
190−readArraysOfUpto :: (IsStream t, MonadCatch m, MonadIO m)
189+{-# INLINABLE readArraysOf #-}
190+readArraysOf :: (IsStream t, MonadCatch m, MonadIO m)
191191 => Int -> FilePath -> t m (Array Word8)
192−readArraysOfUpto size file = withFile file ReadMode (FH.readArraysOfUpto size)
192+readArraysOf size file = withFile file ReadMode (FH.readArraysOf size)
193193
194194-- XXX read 'Array a' instead of Word8
195195--
196196-- | @readArrays file@ reads a stream of arrays from file @file@.
197−-- The maximum size of a single array is limited to @defaultChunkSize@.
197+-- The maximum size of a single array is limited to @defaultChunkSize@. The
198+-- actual size read may be less than @defaultChunkSize@.
198199--
199−-- > readArrays = readArraysOfUpto defaultChunkSize
200+-- > readArrays = readArraysOf defaultChunkSize
200201--
201202-- @since 0.7.0
202203{-# INLINE readArrays #-}
203204readArrays :: (IsStream t, MonadCatch m, MonadIO m)
204205 => FilePath -> t m (Array Word8)
205−readArrays = readArraysOfUpto A.defaultChunkSize
206+readArrays = readArraysOf A.defaultChunkSize
206207
207208-------------------------------------------------------------------------------
208209-- Read File to Stream
@@ -213,13 +214,13 @@ readArrays = readArraysOfUpto A.defaultChunkSize
213214-- also control the read throughput in mbps or IOPS.
214215
215216{-
216−-- | @readByChunksUpto chunkSize handle@ reads a byte stream from a file
217+-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
217218-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
218219-- as soon as EOF is encountered.
219220--
220−{-# INLINE readByChunksUpto #-}
221−readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
222−readByChunksUpto chunkSize h = A.flattenArrays $ readArraysOfUpto chunkSize h
221+{-# INLINE readInChunksOf #-}
222+readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
223+readInChunksOf chunkSize h = A.flattenArrays $ readArraysOf chunkSize h
223224-}
224225
225226-- TODO
@@ -286,12 +287,12 @@ writeArrays = writeArraysMode WriteMode
286287-- input elements.
287288--
288289-- @since 0.7.0
289−{-# INLINE writeByChunks #-}
290−writeByChunks :: (MonadAsync m, MonadCatch m)
290+{-# INLINE writeInChunksOf #-}
291+writeInChunksOf :: (MonadAsync m, MonadCatch m)
291292 => Int -> FilePath -> SerialT m Word8 -> m ()
292−writeByChunks n file xs = writeArrays file $ AS.arraysOf n xs
293+writeInChunksOf n file xs = writeArrays file $ AS.arraysOf n xs
293294
294−-- > write = 'writeByChunks' A.defaultChunkSize
295+-- > write = 'writeInChunksOf' A.defaultChunkSize
295296--
296297-- | Write a byte stream to a file. Combines the bytes in chunks of size
297298-- up to 'A.defaultChunkSize' before writing. If the file exists it is
@@ -301,7 +302,7 @@ writeByChunks n file xs = writeArrays file $ AS.arraysOf n xs
301302-- @since 0.7.0
302303{-# INLINE write #-}
303304write :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
304−write = writeByChunks A.defaultChunkSize
305+write = writeInChunksOf A.defaultChunkSize
305306
306307{-
307308{-# INLINE write #-}
src/Streamly/FileSystem/Handle.hs+25−23
@@ -61,13 +61,13 @@ module Streamly.FileSystem.Handle
6161 -- , readUtf8
6262 -- , readLines
6363 -- , readFrames
64− , readByChunksUpto
64+ , readInChunksOf
6565
6666 -- -- * Array Read
6767 -- , readArrayUpto
6868 -- , readArrayOf
6969
70− , readArraysOfUpto
70+ , readArraysOf
7171 -- , readArraysOf
7272 , readArrays
7373
@@ -81,7 +81,7 @@ module Streamly.FileSystem.Handle
8181 -- , writeUtf8
8282 -- , writeUtf8ByLines
8383 -- , writeByFrames
84− , writeByChunksOf
84+ , writeInChunksOf
8585
8686 -- -- * Array Write
8787 , writeArray
@@ -187,13 +187,13 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
187187-- Stream of Arrays IO
188188-------------------------------------------------------------------------------
189189
190−-- | @readArraysOfUpto size h@ reads a stream of arrays from file handle @h@.
190+-- | @readArraysOf size h@ reads a stream of arrays from file handle @h@.
191191-- The maximum size of a single array is specified by @size@. The actual size
192192-- read may be less than or equal to @size@.
193−{-# INLINABLE _readArraysOfUpto #-}
194−_readArraysOfUpto :: (IsStream t, MonadIO m)
193+{-# INLINABLE _readArraysOf #-}
194+_readArraysOf :: (IsStream t, MonadIO m)
195195 => Int -> Handle -> t m (Array Word8)
196−_readArraysOfUpto size h = go
196+_readArraysOf size h = go
197197 where
198198 -- XXX use cons/nil instead
199199 go = mkStream $ \_ yld _ stp -> do
@@ -202,13 +202,14 @@ _readArraysOfUpto size h = go
202202 then stp
203203 else yld arr go
204204
205−-- | @readArraysOfUpto size handle@ reads a stream of arrays from the file
205+-- | @readArraysOf size handle@ reads a stream of arrays from the file
206206-- handle @handle@. The maximum size of a single array is limited to @size@.
207+-- The actual size read may be less than or equal to @size@.
207208--
208209-- @since 0.7.0
209−{-# INLINE_NORMAL readArraysOfUpto #-}
210−readArraysOfUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
211−readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
210+{-# INLINE_NORMAL readArraysOf #-}
211+readArraysOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
212+readArraysOf size h = D.fromStreamD (D.Stream step ())
212213 where
213214 {-# INLINE_LATE step #-}
214215 step _ _ = do
@@ -222,14 +223,15 @@ readArraysOfUpto size h = D.fromStreamD (D.Stream step ())
222223--
223224-- | @readArrays handle@ reads a stream of arrays from the specified file
224225-- handle. The maximum size of a single array is limited to
226+-- @defaultChunkSize@. The actual size read may be less than or equal to
225227-- @defaultChunkSize@.
226228--
227−-- > readArrays = readArraysOfUpto defaultChunkSize
229+-- > readArrays = readArraysOf defaultChunkSize
228230--
229231-- @since 0.7.0
230232{-# INLINE readArrays #-}
231233readArrays :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
232−readArrays = readArraysOfUpto defaultChunkSize
234+readArrays = readArraysOf defaultChunkSize
233235
234236-------------------------------------------------------------------------------
235237-- Read File to Stream
@@ -239,13 +241,13 @@ readArrays = readArraysOfUpto defaultChunkSize
239241-- read requests at the same time. For serial case we can use async IO. We can
240242-- also control the read throughput in mbps or IOPS.
241243
242−-- | @readByChunksUpto chunkSize handle@ reads a byte stream from a file
244+-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
243245-- handle, reads are performed in chunks of up to @chunkSize@.
244246--
245247-- @since 0.7.0
246−{-# INLINE readByChunksUpto #-}
247−readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
248−readByChunksUpto chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
248+{-# INLINE readInChunksOf #-}
249+readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
250+readInChunksOf chunkSize h = AS.flatten $ readArraysOf chunkSize h
249251
250252-- TODO
251253-- Generate a stream of elements of the given type from a file 'Handle'.
@@ -289,16 +291,16 @@ writeArraysPackedUpto n h xs = writeArrays h $ AS.compact n xs
289291-- do not want buffering to occur at GHC level as well. Same thing applies to
290292-- writes as well.
291293
292−-- | @writeByChunksOf chunkSize handle stream@ writes @stream@ to @handle@ in
294+-- | @writeInChunksOf chunkSize handle stream@ writes @stream@ to @handle@ in
293295-- chunks of @chunkSize@. A write is performed to the IO device as soon as we
294296-- collect the required input size.
295297--
296298-- @since 0.7.0
297−{-# INLINE writeByChunksOf #-}
298−writeByChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
299−writeByChunksOf n h m = writeArrays h $ AS.arraysOf n m
299+{-# INLINE writeInChunksOf #-}
300+writeInChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
301+writeInChunksOf n h m = writeArrays h $ AS.arraysOf n m
300302
301−-- > write = 'writeByChunksOf' A.defaultChunkSize
303+-- > write = 'writeInChunksOf' A.defaultChunkSize
302304--
303305-- | Write a byte stream to a file handle. Combines the bytes in chunks of
304306-- up to 'A.defaultChunkSize' before writing.
@@ -306,7 +308,7 @@ writeByChunksOf n h m = writeArrays h $ AS.arraysOf n m
306308-- @since 0.7.0
307309{-# INLINE write #-}
308310write :: MonadIO m => Handle -> SerialT m Word8 -> m ()
309−write = writeByChunksOf defaultChunkSize
311+write = writeInChunksOf defaultChunkSize
310312
311313{-
312314{-# INLINE write #-}
src/Streamly/Network/Client.hs+5−5
@@ -47,7 +47,7 @@ module Streamly.Network.Client
4747 -- , writeUtf8
4848 -- , writeUtf8ByLines
4949 -- , writeByFrames
50− -- , writeByChunks
50+ -- , writeInChunksOf
5151
5252 -- -- * Array Write
5353 -- , writeArray
@@ -133,15 +133,15 @@ writeArrays addr port xs =
133133-- input elements.
134134--
135135-- @since 0.7.0
136−{-# INLINE writeByChunks #-}
137−writeByChunks
136+{-# INLINE writeInChunksOf #-}
137+writeInChunksOf
138138 :: (MonadCatch m, MonadAsync m)
139139 => Int
140140 -> (Word8, Word8, Word8, Word8)
141141 -> PortNumber
142142 -> SerialT m Word8
143143 -> m ()
144−writeByChunks n addr port m = writeArrays addr port $ AS.arraysOf n m
144+writeInChunksOf n addr port m = writeArrays addr port $ AS.arraysOf n m
145145
146146-- | Write a stream to the supplied IPv4 host address and port number.
147147--
@@ -149,4 +149,4 @@ writeByChunks n addr port m = writeArrays addr port $ AS.arraysOf n m
149149{-# INLINE write #-}
150150write :: (MonadCatch m, MonadAsync m)
151151 => (Word8, Word8, Word8, Word8) -> PortNumber -> SerialT m Word8 -> m ()
152−write = writeByChunks defaultChunkSize
152+write = writeInChunksOf defaultChunkSize
src/Streamly/Network/Socket.hs+18−18
@@ -56,7 +56,7 @@ module Streamly.Network.Socket
5656 -- , writeUtf8
5757 -- , writeUtf8ByLines
5858 -- , writeByFrames
59− , writeByChunks
59+ , writeInChunksOf
6060
6161 -- -- * Array Write
6262 , writeArray
@@ -154,9 +154,9 @@ readArrayUptoWith f size h = do
154154-- handle it blocks until some data becomes available. If data is available
155155-- then it immediately returns that data without blocking. It reads a maximum
156156-- of up to the size requested.
157−{-# INLINABLE readArrayUpto #-}
158−readArrayUpto :: Int -> Socket -> IO (Array Word8)
159−readArrayUpto = readArrayUptoWith recvBuf
157+{-# INLINABLE readArrayOf #-}
158+readArrayOf :: Int -> Socket -> IO (Array Word8)
159+readArrayOf = readArrayUptoWith recvBuf
160160
161161-------------------------------------------------------------------------------
162162-- Array IO (output)
@@ -217,14 +217,14 @@ readArraysUptoWith f size h = go
217217 then stp
218218 else yld arr go
219219
220−-- | @readArraysUpto size h@ reads a stream of arrays from file handle @h@.
220+-- | @readArraysOf size h@ reads a stream of arrays from file handle @h@.
221221-- The maximum size of a single array is limited to @size@.
222222-- 'fromHandleArraysUpto' ignores the prevailing 'TextEncoding' and 'NewlineMode'
223223-- on the 'Handle'.
224−{-# INLINABLE readArraysUpto #-}
225−readArraysUpto :: (IsStream t, MonadIO m)
224+{-# INLINABLE readArraysOf #-}
225+readArraysOf :: (IsStream t, MonadIO m)
226226 => Int -> Socket -> t m (Array Word8)
227−readArraysUpto = readArraysUptoWith readArrayUpto
227+readArraysOf = readArraysUptoWith readArrayOf
228228
229229-- XXX read 'Array a' instead of Word8
230230--
@@ -236,7 +236,7 @@ readArraysUpto = readArraysUptoWith readArrayUpto
236236-- @since 0.7.0
237237{-# INLINE readArrays #-}
238238readArrays :: (IsStream t, MonadIO m) => Socket -> t m (Array Word8)
239−readArrays = readArraysUpto A.defaultChunkSize
239+readArrays = readArraysOf A.defaultChunkSize
240240
241241-------------------------------------------------------------------------------
242242-- Read File to Stream
@@ -247,13 +247,13 @@ readArrays = readArraysUpto A.defaultChunkSize
247247-- also control the read throughput in mbps or IOPS.
248248
249249{-
250−-- | @readByChunksUpto chunkSize handle@ reads a byte stream from a file
250+-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
251251-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
252252-- as soon as EOF is encountered.
253253--
254−{-# INLINE readByChunksUpto #-}
255−readByChunksUpto :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
256−readByChunksUpto chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
254+{-# INLINE readInChunksOf #-}
255+readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
256+readInChunksOf chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
257257-}
258258
259259-- TODO
@@ -292,11 +292,11 @@ writeArrays h m = S.mapM_ (liftIO . writeArray h) m
292292-- input elements.
293293--
294294-- @since 0.7.0
295−{-# INLINE writeByChunks #-}
296−writeByChunks :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
297−writeByChunks n h m = writeArrays h $ AS.arraysOf n m
295+{-# INLINE writeInChunksOf #-}
296+writeInChunksOf :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
297+writeInChunksOf n h m = writeArrays h $ AS.arraysOf n m
298298
299−-- > write = 'writeByChunks' A.defaultChunkSize
299+-- > write = 'writeInChunksOf' A.defaultChunkSize
300300--
301301-- | Write a byte stream to a file handle. Combines the bytes in chunks of size
302302-- up to 'A.defaultChunkSize' before writing. Note that the write behavior
@@ -305,7 +305,7 @@ writeByChunks n h m = writeArrays h $ AS.arraysOf n m
305305-- @since 0.7.0
306306{-# INLINE write #-}
307307write :: MonadIO m => Socket -> SerialT m Word8 -> m ()
308−write = writeByChunks A.defaultChunkSize
308+write = writeInChunksOf A.defaultChunkSize
309309
310310{-
311311{-# INLINE write #-}