on master help sign in
Hide the "array stream" Handle IO APIs
6225379Harendra Kumar
examples/FromFileClient.hs+2−2
@@ -7,7 +7,7 @@ import System.Environment (getArgs)
7
7
8import Streamly
8import Streamly
9import qualified Streamly.Prelude as S
9import qualified Streamly.Prelude as S
10−import qualified Streamly.FileSystem.Handle as FH
10+import qualified Streamly.Internal.FileSystem.Handle as IFH
11import qualified Streamly.Network.Client as Client
11import qualified Streamly.Network.Client as Client
12
12
13import System.IO (withFile, IOMode(..))
13import System.IO (withFile, IOMode(..))
@@ -17,5 +17,5 @@ main =
17 let sendFile file =
17 let sendFile file =
18 withFile file ReadMode $ \src ->
18 withFile file ReadMode $ \src ->
19 S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
19 S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
20− $ FH.readArrays src
20+ $ IFH.readArrays src
21 in getArgs >>= S.drain . parallely . S.mapM sendFile . S.fromList
21 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
2import qualified Streamly.Data.Fold as FL
2import qualified Streamly.Data.Fold as FL
3-- import qualified Streamly.Memory.Array as A
3-- import qualified Streamly.Memory.Array as A
4import qualified Streamly.Internal.Memory.ArrayStream as AS
4import qualified Streamly.Internal.Memory.ArrayStream as AS
5+import qualified Streamly.Internal.FileSystem.Handle as IFH
5import qualified Streamly.FileSystem.Handle as FH
6import qualified Streamly.FileSystem.Handle as FH
6import qualified System.IO as FH
7import qualified System.IO as FH
7-- import qualified Streamly.FileSystem.FD as FH
8-- import qualified Streamly.FileSystem.FD as FH
@@ -12,12 +13,12 @@ import System.Environment (getArgs)
12import System.IO (IOMode(..), hSeek, SeekMode(..))
13import System.IO (IOMode(..), hSeek, SeekMode(..))
13
14
14cat :: FH.Handle -> IO ()
15cat :: 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
16-- byte stream version
17-- byte stream version
17-- cat src = S.fold (FH.write FH.stdout) $ FH.read src
18-- cat src = S.fold (FH.write FH.stdout) $ FH.read src
18
19
19cp :: FH.Handle -> FH.Handle -> IO ()
20cp :: 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
21-- byte stream version
22-- byte stream version
22-- cp src dst = S.fold (FH.write dst) $ FH.read src
23-- cp src dst = S.fold (FH.write dst) $ FH.read src
23
24
@@ -27,7 +28,7 @@ ord' = (fromIntegral . ord)
27wcl :: FH.Handle -> IO ()
28wcl :: FH.Handle -> IO ()
28wcl src = print =<< (S.length
29wcl src = print =<< (S.length
29 $ AS.splitOn 10
30 $ AS.splitOn 10
30− $ FH.readArrays src)
31+ $ IFH.readArrays src)
31{-
32{-
32-- Char stream version
33-- Char stream version
33wcl src = print =<< (S.length
34wcl src = print =<< (S.length
src/Streamly/Benchmark/FileIO/Array.hs+16−14
@@ -43,6 +43,8 @@ import qualified Streamly.FileSystem.Handle as FH
43import qualified Streamly.Memory.Array as A
43import qualified Streamly.Memory.Array as A
44import qualified Streamly.Prelude as S
44import qualified Streamly.Prelude as S
45import qualified Streamly.Data.String as SS
45import qualified Streamly.Data.String as SS
46+
47+import qualified Streamly.Internal.FileSystem.Handle as IFH
46import qualified Streamly.Internal.Prelude as Internal
48import qualified Streamly.Internal.Prelude as Internal
47import qualified Streamly.Internal.Memory.Array as IA
49import qualified Streamly.Internal.Memory.Array as IA
48import qualified Streamly.Internal.Memory.ArrayStream as AS
50import qualified Streamly.Internal.Memory.ArrayStream as AS
@@ -57,7 +59,7 @@ import Test.Inspection
57{-# INLINE last #-}
59{-# INLINE last #-}
58last :: Handle -> IO (Maybe Word8)
60last :: Handle -> IO (Maybe Word8)
59last inh = do
61last inh = do
60− let s = FH.readArrays inh
62+ let s = IFH.readArrays inh
61 larr <- S.last s
63 larr <- S.last s
62 return $ case larr of
64 return $ case larr of
63 Nothing -> Nothing
65 Nothing -> Nothing
@@ -72,7 +74,7 @@ inspect $ 'last `hasNoType` ''Step
72{-# INLINE countBytes #-}
74{-# INLINE countBytes #-}
73countBytes :: Handle -> IO Int
75countBytes :: Handle -> IO Int
74countBytes inh =
76countBytes inh =
75− let s = FH.readArrays inh
77+ let s = IFH.readArrays inh
76 in S.sum (S.map A.length s)
78 in S.sum (S.map A.length s)
77
79
78#ifdef INSPECTION
80#ifdef INSPECTION
@@ -83,7 +85,7 @@ inspect $ 'countBytes `hasNoType` ''Step
83-- | Count the number of lines in a file.
85-- | Count the number of lines in a file.
84{-# INLINE countLines #-}
86{-# INLINE countLines #-}
85countLines :: Handle -> IO Int
87countLines :: Handle -> IO Int
86−countLines = S.length . AS.splitOnSuffix 10 . FH.readArrays
88+countLines = S.length . AS.splitOnSuffix 10 . IFH.readArrays
87
89
88#ifdef INSPECTION
90#ifdef INSPECTION
89inspect $ hasNoTypeClasses 'countLines
91inspect $ hasNoTypeClasses 'countLines
@@ -94,7 +96,7 @@ inspect $ 'countLines `hasNoType` ''Step
94-- | Count the number of lines in a file.
96-- | Count the number of lines in a file.
95{-# INLINE countWords #-}
97{-# INLINE countWords #-}
96countWords :: Handle -> IO Int
98countWords :: Handle -> IO Int
97−countWords = S.length . AS.splitOn 32 . FH.readArrays
99+countWords = S.length . AS.splitOn 32 . IFH.readArrays
98
100
99#ifdef INSPECTION
101#ifdef INSPECTION
100inspect $ hasNoTypeClasses 'countWords
102inspect $ hasNoTypeClasses 'countWords
@@ -106,7 +108,7 @@ inspect $ 'countWords `hasNoType` ''Step
106sumBytes :: Handle -> IO Word8
108sumBytes :: Handle -> IO Word8
107sumBytes inh = do
109sumBytes inh = do
108 let foldlArr' f z = runIdentity . S.foldl' f z . IA.toStream
110 let foldlArr' f z = runIdentity . S.foldl' f z . IA.toStream
109− let s = FH.readArrays inh
111+ let s = IFH.readArrays inh
110 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
112 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
111
113
112#ifdef INSPECTION
114#ifdef INSPECTION
@@ -118,7 +120,7 @@ inspect $ 'sumBytes `hasNoType` ''Step
118{-# INLINE cat #-}
120{-# INLINE cat #-}
119cat :: Handle -> Handle -> IO ()
121cat :: Handle -> Handle -> IO ()
120cat devNull inh =
122cat devNull inh =
121− S.fold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
123+ S.fold (IFH.writeArrays devNull) $ IFH.readArraysOf (256*1024) inh
122
124
123#ifdef INSPECTION
125#ifdef INSPECTION
124inspect $ hasNoTypeClasses 'cat
126inspect $ hasNoTypeClasses 'cat
@@ -129,8 +131,8 @@ inspect $ 'cat `hasNoType` ''Step
129{-# INLINE copy #-}
131{-# INLINE copy #-}
130copy :: Handle -> Handle -> IO ()
132copy :: Handle -> Handle -> IO ()
131copy inh outh =
133copy inh outh =
132− let s = FH.readArrays inh
134+ let s = IFH.readArrays inh
133− in S.fold (FH.writeArrays outh) s
135+ in S.fold (IFH.writeArrays outh) s
134
136
135#ifdef INSPECTION
137#ifdef INSPECTION
136inspect $ hasNoTypeClasses 'copy
138inspect $ hasNoTypeClasses 'copy
@@ -141,10 +143,10 @@ inspect $ 'copy `hasNoType` ''Step
141{-# INLINE linesUnlinesCopy #-}
143{-# INLINE linesUnlinesCopy #-}
142linesUnlinesCopy :: Handle -> Handle -> IO ()
144linesUnlinesCopy :: Handle -> Handle -> IO ()
143linesUnlinesCopy inh outh =
145linesUnlinesCopy inh outh =
144− S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
146+ S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
145 $ Internal.insertAfterEach (return $ A.fromList [10])
147 $ Internal.insertAfterEach (return $ A.fromList [10])
146 $ AS.splitOnSuffix 10
148 $ AS.splitOnSuffix 10
147− $ FH.readArraysOf (1024*1024) inh
149+ $ IFH.readArraysOf (1024*1024) inh
148
150
149#ifdef INSPECTION
151#ifdef INSPECTION
150inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
152inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
@@ -155,11 +157,11 @@ inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
155{-# INLINE wordsUnwordsCopy #-}
157{-# INLINE wordsUnwordsCopy #-}
156wordsUnwordsCopy :: Handle -> Handle -> IO ()
158wordsUnwordsCopy :: Handle -> Handle -> IO ()
157wordsUnwordsCopy inh outh =
159wordsUnwordsCopy inh outh =
158− S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
160+ S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
159 $ S.intersperse (A.fromList [32])
161 $ S.intersperse (A.fromList [32])
160 -- XXX use a word splitting combinator
162 -- XXX use a word splitting combinator
161 $ AS.splitOn 32
163 $ AS.splitOn 32
162− $ FH.readArraysOf (1024*1024) inh
164+ $ IFH.readArraysOf (1024*1024) inh
163
165
164#ifdef INSPECTION
166#ifdef INSPECTION
165inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
167inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
@@ -171,7 +173,7 @@ decodeUtf8Lenient :: Handle -> IO ()
171decodeUtf8Lenient inh =
173decodeUtf8Lenient inh =
172 S.drain
174 S.drain
173 $ SS.decodeUtf8ArraysLenient
175 $ SS.decodeUtf8ArraysLenient
174− $ FH.readArraysOf (1024*1024) inh
176+ $ IFH.readArraysOf (1024*1024) inh
175
177
176#ifdef INSPECTION
178#ifdef INSPECTION
177inspect $ hasNoTypeClasses 'decodeUtf8Lenient
179inspect $ hasNoTypeClasses 'decodeUtf8Lenient
@@ -187,7 +189,7 @@ copyCodecUtf8Lenient inh outh =
187 S.fold (FH.write outh)
189 S.fold (FH.write outh)
188 $ SS.encodeUtf8
190 $ SS.encodeUtf8
189 $ SS.decodeUtf8ArraysLenient
191 $ SS.decodeUtf8ArraysLenient
190− $ FH.readArraysOf (1024*1024) inh
192+ $ IFH.readArraysOf (1024*1024) inh
191
193
192#ifdef INSPECTION
194#ifdef INSPECTION
193inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient
195inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient
src/Streamly/FileSystem/Handle.hs+6−15
@@ -8,10 +8,10 @@
8
8
9-- |
9-- |
10-- Module : Streamly.FileSystem.Handle
10-- Module : Streamly.FileSystem.Handle
11−-- Copyright : (c) 2018 Harendra Kumar
11+-- Copyright : (c) 2018 Composewell Technologies
12--
12--
13-- License : BSD3
13-- License : BSD3
14−-- Maintainer : harendra.kumar@gmail.com
14+-- Maintainer : streamly@composewell.com
15-- Stability : experimental
15-- Stability : experimental
16-- Portability : GHC
16-- Portability : GHC
17--
17--
@@ -57,20 +57,14 @@ module Streamly.FileSystem.Handle
57 -- position of the file handle. The stream ends as soon as EOF is
57 -- position of the file handle. The stream ends as soon as EOF is
58 -- encountered.
58 -- encountered.
59
59
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.
60 read
62 read
61 -- , readUtf8
63 -- , readUtf8
62 -- , readLines
64 -- , readLines
63 -- , readFrames
65 -- , readFrames
64 , readInChunksOf
66 , readInChunksOf
65
67
66− -- -- * Array Read
67− -- , readArrayUpto
68− -- , readArrayOf
69−
70− , readArraysOf
71− -- , readArraysOf
72− , readArrays
73−
74 -- ** Write to Handle
68 -- ** Write to Handle
75 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
69 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
76 -- underlying handle are ignored. The write occurs from the current seek
70 -- underlying handle are ignored. The write occurs from the current seek
@@ -83,11 +77,6 @@ module Streamly.FileSystem.Handle
83 -- , writeByFrames
77 -- , writeByFrames
84 , writeInChunksOf
78 , writeInChunksOf
85
79
86− -- -- * Array Write
87− , writeArray
88− , writeArrays
89− , writeArraysInChunksOf
90−
91 -- -- * Random Access (Seek)
80 -- -- * Random Access (Seek)
92 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
81 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
93 -- files that have random access or seek capability. This type of devices
82 -- files that have random access or seek capability. This type of devices
@@ -95,6 +84,8 @@ module Streamly.FileSystem.Handle
95 -- sockets and fifos.
84 -- sockets and fifos.
96 --
85 --
97 -- , readIndex
86 -- , readIndex
87+ -- XXX we can make the names consistent with the enumerate APIs. For
88+ -- example, readFromTo, readFromToUp, readFromToDn, readFrom etc.
98 -- , readSlice
89 -- , readSlice
99 -- , readSliceRev
90 -- , readSliceRev
100 -- , readAt -- read from a given position to th end of file
91 -- , 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)
122-- import Streamly.Data.Fold (Fold)
122-- import Streamly.Data.Fold (Fold)
123-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
123-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
124
124
125−import qualified Streamly.FileSystem.Handle as FH
125+import qualified Streamly.Internal.FileSystem.Handle as FH
126import qualified Streamly.Internal.Memory.ArrayStream as AS
126import qualified Streamly.Internal.Memory.ArrayStream as AS
127import qualified Streamly.Prelude as S
127import qualified Streamly.Prelude as S
128
128