on master help sign in
Hide the "array stream" Handle IO APIs

These can possibly be expressed in terms of the generic polymorphic IO APIs once we have those. For now we can use these from the Internal module.

commit6225379eb630e278b909cb3148693e94b0fcb2c7 authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 30 Sep 2019, 01:37 (7 years ago) parent6f517f7
5 files changed+29−35
examples/FromFileClient.hs+2−2 examples/HandleIO.hs+4−3 src/Streamly/Benchmark/FileIO/Array.hs+16−14 src/Streamly/FileSystem/Handle.hs+6−15 src/Streamly/Internal/FileSystem/File.hs+1−1
examples/FromFileClient.hs+2−2
@@ -7,7 +7,7 @@ import System.Environment (getArgs)
77
88import Streamly
99import qualified Streamly.Prelude as S
10−import qualified Streamly.FileSystem.Handle as FH
10+import qualified Streamly.Internal.FileSystem.Handle as IFH
1111import qualified Streamly.Network.Client as Client
1212
1313import System.IO (withFile, IOMode(..))
@@ -17,5 +17,5 @@ main =
1717 let sendFile file =
1818 withFile file ReadMode $ \src ->
1919 S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
20− $ FH.readArrays src
20+ $ IFH.readArrays src
2121 in getArgs >>= S.drain . parallely . S.mapM sendFile . S.fromList
examples/HandleIO.hs+4−3
@@ -2,6 +2,7 @@ import qualified Streamly.Prelude as S
22import qualified Streamly.Data.Fold as FL
33-- import qualified Streamly.Memory.Array as A
44import qualified Streamly.Internal.Memory.ArrayStream as AS
5+import qualified Streamly.Internal.FileSystem.Handle as IFH
56import qualified Streamly.FileSystem.Handle as FH
67import qualified System.IO as FH
78-- import qualified Streamly.FileSystem.FD as FH
@@ -12,12 +13,12 @@ import System.Environment (getArgs)
1213import System.IO (IOMode(..), hSeek, SeekMode(..))
1314
1415cat :: FH.Handle -> IO ()
15−cat src = S.fold (FH.writeArrays FH.stdout) $ FH.readArraysOf (256*1024) src
16+cat src = S.fold (IFH.writeArrays FH.stdout) $ IFH.readArraysOf (256*1024) src
1617-- byte stream version
1718-- cat src = S.fold (FH.write FH.stdout) $ FH.read src
1819
1920cp :: FH.Handle -> FH.Handle -> IO ()
20−cp src dst = S.fold (FH.writeArrays dst) $ FH.readArraysOf (256*1024) src
21+cp src dst = S.fold (IFH.writeArrays dst) $ IFH.readArraysOf (256*1024) src
2122-- byte stream version
2223-- cp src dst = S.fold (FH.write dst) $ FH.read src
2324
@@ -27,7 +28,7 @@ ord' = (fromIntegral . ord)
2728wcl :: FH.Handle -> IO ()
2829wcl src = print =<< (S.length
2930 $ AS.splitOn 10
30− $ FH.readArrays src)
31+ $ IFH.readArrays src)
3132{-
3233-- Char stream version
3334wcl src = print =<< (S.length
src/Streamly/Benchmark/FileIO/Array.hs+16−14
@@ -43,6 +43,8 @@ import qualified Streamly.FileSystem.Handle as FH
4343import qualified Streamly.Memory.Array as A
4444import qualified Streamly.Prelude as S
4545import qualified Streamly.Data.String as SS
46+
47+import qualified Streamly.Internal.FileSystem.Handle as IFH
4648import qualified Streamly.Internal.Prelude as Internal
4749import qualified Streamly.Internal.Memory.Array as IA
4850import qualified Streamly.Internal.Memory.ArrayStream as AS
@@ -57,7 +59,7 @@ import Test.Inspection
5759{-# INLINE last #-}
5860last :: Handle -> IO (Maybe Word8)
5961last inh = do
60− let s = FH.readArrays inh
62+ let s = IFH.readArrays inh
6163 larr <- S.last s
6264 return $ case larr of
6365 Nothing -> Nothing
@@ -72,7 +74,7 @@ inspect $ 'last `hasNoType` ''Step
7274{-# INLINE countBytes #-}
7375countBytes :: Handle -> IO Int
7476countBytes inh =
75− let s = FH.readArrays inh
77+ let s = IFH.readArrays inh
7678 in S.sum (S.map A.length s)
7779
7880#ifdef INSPECTION
@@ -83,7 +85,7 @@ inspect $ 'countBytes `hasNoType` ''Step
8385-- | Count the number of lines in a file.
8486{-# INLINE countLines #-}
8587countLines :: Handle -> IO Int
86−countLines = S.length . AS.splitOnSuffix 10 . FH.readArrays
88+countLines = S.length . AS.splitOnSuffix 10 . IFH.readArrays
8789
8890#ifdef INSPECTION
8991inspect $ hasNoTypeClasses 'countLines
@@ -94,7 +96,7 @@ inspect $ 'countLines `hasNoType` ''Step
9496-- | Count the number of lines in a file.
9597{-# INLINE countWords #-}
9698countWords :: Handle -> IO Int
97−countWords = S.length . AS.splitOn 32 . FH.readArrays
99+countWords = S.length . AS.splitOn 32 . IFH.readArrays
98100
99101#ifdef INSPECTION
100102inspect $ hasNoTypeClasses 'countWords
@@ -106,7 +108,7 @@ inspect $ 'countWords `hasNoType` ''Step
106108sumBytes :: Handle -> IO Word8
107109sumBytes inh = do
108110 let foldlArr' f z = runIdentity . S.foldl' f z . IA.toStream
109− let s = FH.readArrays inh
111+ let s = IFH.readArrays inh
110112 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
111113
112114#ifdef INSPECTION
@@ -118,7 +120,7 @@ inspect $ 'sumBytes `hasNoType` ''Step
118120{-# INLINE cat #-}
119121cat :: Handle -> Handle -> IO ()
120122cat devNull inh =
121− S.fold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
123+ S.fold (IFH.writeArrays devNull) $ IFH.readArraysOf (256*1024) inh
122124
123125#ifdef INSPECTION
124126inspect $ hasNoTypeClasses 'cat
@@ -129,8 +131,8 @@ inspect $ 'cat `hasNoType` ''Step
129131{-# INLINE copy #-}
130132copy :: Handle -> Handle -> IO ()
131133copy inh outh =
132− let s = FH.readArrays inh
133− in S.fold (FH.writeArrays outh) s
134+ let s = IFH.readArrays inh
135+ in S.fold (IFH.writeArrays outh) s
134136
135137#ifdef INSPECTION
136138inspect $ hasNoTypeClasses 'copy
@@ -141,10 +143,10 @@ inspect $ 'copy `hasNoType` ''Step
141143{-# INLINE linesUnlinesCopy #-}
142144linesUnlinesCopy :: Handle -> Handle -> IO ()
143145linesUnlinesCopy inh outh =
144− S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
146+ S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
145147 $ Internal.insertAfterEach (return $ A.fromList [10])
146148 $ AS.splitOnSuffix 10
147− $ FH.readArraysOf (1024*1024) inh
149+ $ IFH.readArraysOf (1024*1024) inh
148150
149151#ifdef INSPECTION
150152inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
@@ -155,11 +157,11 @@ inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
155157{-# INLINE wordsUnwordsCopy #-}
156158wordsUnwordsCopy :: Handle -> Handle -> IO ()
157159wordsUnwordsCopy inh outh =
158− S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
160+ S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
159161 $ S.intersperse (A.fromList [32])
160162 -- XXX use a word splitting combinator
161163 $ AS.splitOn 32
162− $ FH.readArraysOf (1024*1024) inh
164+ $ IFH.readArraysOf (1024*1024) inh
163165
164166#ifdef INSPECTION
165167inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
@@ -171,7 +173,7 @@ decodeUtf8Lenient :: Handle -> IO ()
171173decodeUtf8Lenient inh =
172174 S.drain
173175 $ SS.decodeUtf8ArraysLenient
174− $ FH.readArraysOf (1024*1024) inh
176+ $ IFH.readArraysOf (1024*1024) inh
175177
176178#ifdef INSPECTION
177179inspect $ hasNoTypeClasses 'decodeUtf8Lenient
@@ -187,7 +189,7 @@ copyCodecUtf8Lenient inh outh =
187189 S.fold (FH.write outh)
188190 $ SS.encodeUtf8
189191 $ SS.decodeUtf8ArraysLenient
190− $ FH.readArraysOf (1024*1024) inh
192+ $ IFH.readArraysOf (1024*1024) inh
191193
192194#ifdef INSPECTION
193195inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient
src/Streamly/FileSystem/Handle.hs+6−15
@@ -8,10 +8,10 @@
88
99-- |
1010-- Module : Streamly.FileSystem.Handle
11−-- Copyright : (c) 2018 Harendra Kumar
11+-- Copyright : (c) 2018 Composewell Technologies
1212--
1313-- License : BSD3
14−-- Maintainer : harendra.kumar@gmail.com
14+-- Maintainer : streamly@composewell.com
1515-- Stability : experimental
1616-- Portability : GHC
1717--
@@ -57,20 +57,14 @@ module Streamly.FileSystem.Handle
5757 -- position of the file handle. The stream ends as soon as EOF is
5858 -- encountered.
5959
60+ -- XXX once we have APIs to read any type 'a' the array stream
61+ -- reading/writing APIs can be expressed in terms of the polymorphic API.
6062 read
6163 -- , readUtf8
6264 -- , readLines
6365 -- , readFrames
6466 , readInChunksOf
6567
66− -- -- * Array Read
67− -- , readArrayUpto
68− -- , readArrayOf
69−
70− , readArraysOf
71− -- , readArraysOf
72− , readArrays
73−
7468 -- ** Write to Handle
7569 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
7670 -- underlying handle are ignored. The write occurs from the current seek
@@ -83,11 +77,6 @@ module Streamly.FileSystem.Handle
8377 -- , writeByFrames
8478 , writeInChunksOf
8579
86− -- -- * Array Write
87− , writeArray
88− , writeArrays
89− , writeArraysInChunksOf
90−
9180 -- -- * Random Access (Seek)
9281 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
9382 -- files that have random access or seek capability. This type of devices
@@ -95,6 +84,8 @@ module Streamly.FileSystem.Handle
9584 -- sockets and fifos.
9685 --
9786 -- , readIndex
87+ -- XXX we can make the names consistent with the enumerate APIs. For
88+ -- example, readFromTo, readFromToUp, readFromToDn, readFrom etc.
9889 -- , readSlice
9990 -- , readSliceRev
10091 -- , readAt -- read from a given position to th end of file
src/Streamly/Internal/FileSystem/File.hs+1−1
@@ -122,7 +122,7 @@ import Streamly.Internal.Data.SVar (MonadAsync)
122122-- import Streamly.Data.Fold (Fold)
123123-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
124124
125−import qualified Streamly.FileSystem.Handle as FH
125+import qualified Streamly.Internal.FileSystem.Handle as FH
126126import qualified Streamly.Internal.Memory.ArrayStream as AS
127127import qualified Streamly.Prelude as S
128128