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.
This commit is contained in:
Harendra Kumar 2019-09-30 01:37:46 +05:30
parent 6f517f7ae5
commit 6225379eb6
5 changed files with 29 additions and 35 deletions

View File

@ -7,7 +7,7 @@ import System.Environment (getArgs)
import Streamly
import qualified Streamly.Prelude as S
import qualified Streamly.FileSystem.Handle as FH
import qualified Streamly.Internal.FileSystem.Handle as IFH
import qualified Streamly.Network.Client as Client
import System.IO (withFile, IOMode(..))
@ -17,5 +17,5 @@ main =
let sendFile file =
withFile file ReadMode $ \src ->
S.fold (Client.writeArrays (127, 0, 0, 1) 8090)
$ FH.readArrays src
$ IFH.readArrays src
in getArgs >>= S.drain . parallely . S.mapM sendFile . S.fromList

View File

@ -2,6 +2,7 @@ import qualified Streamly.Prelude as S
import qualified Streamly.Data.Fold as FL
-- import qualified Streamly.Memory.Array as A
import qualified Streamly.Internal.Memory.ArrayStream as AS
import qualified Streamly.Internal.FileSystem.Handle as IFH
import qualified Streamly.FileSystem.Handle as FH
import qualified System.IO as FH
-- import qualified Streamly.FileSystem.FD as FH
@ -12,12 +13,12 @@ import System.Environment (getArgs)
import System.IO (IOMode(..), hSeek, SeekMode(..))
cat :: FH.Handle -> IO ()
cat src = S.fold (FH.writeArrays FH.stdout) $ FH.readArraysOf (256*1024) src
cat src = S.fold (IFH.writeArrays FH.stdout) $ IFH.readArraysOf (256*1024) src
-- byte stream version
-- cat src = S.fold (FH.write FH.stdout) $ FH.read src
cp :: FH.Handle -> FH.Handle -> IO ()
cp src dst = S.fold (FH.writeArrays dst) $ FH.readArraysOf (256*1024) src
cp src dst = S.fold (IFH.writeArrays dst) $ IFH.readArraysOf (256*1024) src
-- byte stream version
-- cp src dst = S.fold (FH.write dst) $ FH.read src
@ -27,7 +28,7 @@ ord' = (fromIntegral . ord)
wcl :: FH.Handle -> IO ()
wcl src = print =<< (S.length
$ AS.splitOn 10
$ FH.readArrays src)
$ IFH.readArrays src)
{-
-- Char stream version
wcl src = print =<< (S.length

View File

@ -43,6 +43,8 @@ import qualified Streamly.FileSystem.Handle as FH
import qualified Streamly.Memory.Array as A
import qualified Streamly.Prelude as S
import qualified Streamly.Data.String as SS
import qualified Streamly.Internal.FileSystem.Handle as IFH
import qualified Streamly.Internal.Prelude as Internal
import qualified Streamly.Internal.Memory.Array as IA
import qualified Streamly.Internal.Memory.ArrayStream as AS
@ -57,7 +59,7 @@ import Test.Inspection
{-# INLINE last #-}
last :: Handle -> IO (Maybe Word8)
last inh = do
let s = FH.readArrays inh
let s = IFH.readArrays inh
larr <- S.last s
return $ case larr of
Nothing -> Nothing
@ -72,7 +74,7 @@ inspect $ 'last `hasNoType` ''Step
{-# INLINE countBytes #-}
countBytes :: Handle -> IO Int
countBytes inh =
let s = FH.readArrays inh
let s = IFH.readArrays inh
in S.sum (S.map A.length s)
#ifdef INSPECTION
@ -83,7 +85,7 @@ inspect $ 'countBytes `hasNoType` ''Step
-- | Count the number of lines in a file.
{-# INLINE countLines #-}
countLines :: Handle -> IO Int
countLines = S.length . AS.splitOnSuffix 10 . FH.readArrays
countLines = S.length . AS.splitOnSuffix 10 . IFH.readArrays
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'countLines
@ -94,7 +96,7 @@ inspect $ 'countLines `hasNoType` ''Step
-- | Count the number of lines in a file.
{-# INLINE countWords #-}
countWords :: Handle -> IO Int
countWords = S.length . AS.splitOn 32 . FH.readArrays
countWords = S.length . AS.splitOn 32 . IFH.readArrays
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'countWords
@ -106,7 +108,7 @@ inspect $ 'countWords `hasNoType` ''Step
sumBytes :: Handle -> IO Word8
sumBytes inh = do
let foldlArr' f z = runIdentity . S.foldl' f z . IA.toStream
let s = FH.readArrays inh
let s = IFH.readArrays inh
S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
#ifdef INSPECTION
@ -118,7 +120,7 @@ inspect $ 'sumBytes `hasNoType` ''Step
{-# INLINE cat #-}
cat :: Handle -> Handle -> IO ()
cat devNull inh =
S.fold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
S.fold (IFH.writeArrays devNull) $ IFH.readArraysOf (256*1024) inh
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'cat
@ -129,8 +131,8 @@ inspect $ 'cat `hasNoType` ''Step
{-# INLINE copy #-}
copy :: Handle -> Handle -> IO ()
copy inh outh =
let s = FH.readArrays inh
in S.fold (FH.writeArrays outh) s
let s = IFH.readArrays inh
in S.fold (IFH.writeArrays outh) s
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'copy
@ -141,10 +143,10 @@ inspect $ 'copy `hasNoType` ''Step
{-# INLINE linesUnlinesCopy #-}
linesUnlinesCopy :: Handle -> Handle -> IO ()
linesUnlinesCopy inh outh =
S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
$ Internal.insertAfterEach (return $ A.fromList [10])
$ AS.splitOnSuffix 10
$ FH.readArraysOf (1024*1024) inh
$ IFH.readArraysOf (1024*1024) inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
@ -155,11 +157,11 @@ inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
{-# INLINE wordsUnwordsCopy #-}
wordsUnwordsCopy :: Handle -> Handle -> IO ()
wordsUnwordsCopy inh outh =
S.fold (FH.writeArraysInChunksOf (1024*1024) outh)
S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
$ S.intersperse (A.fromList [32])
-- XXX use a word splitting combinator
$ AS.splitOn 32
$ FH.readArraysOf (1024*1024) inh
$ IFH.readArraysOf (1024*1024) inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
@ -171,7 +173,7 @@ decodeUtf8Lenient :: Handle -> IO ()
decodeUtf8Lenient inh =
S.drain
$ SS.decodeUtf8ArraysLenient
$ FH.readArraysOf (1024*1024) inh
$ IFH.readArraysOf (1024*1024) inh
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'decodeUtf8Lenient
@ -187,7 +189,7 @@ copyCodecUtf8Lenient inh outh =
S.fold (FH.write outh)
$ SS.encodeUtf8
$ SS.decodeUtf8ArraysLenient
$ FH.readArraysOf (1024*1024) inh
$ IFH.readArraysOf (1024*1024) inh
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient

View File

@ -8,10 +8,10 @@
-- |
-- Module : Streamly.FileSystem.Handle
-- Copyright : (c) 2018 Harendra Kumar
-- Copyright : (c) 2018 Composewell Technologies
--
-- License : BSD3
-- Maintainer : harendra.kumar@gmail.com
-- Maintainer : streamly@composewell.com
-- Stability : experimental
-- Portability : GHC
--
@ -57,20 +57,14 @@ module Streamly.FileSystem.Handle
-- position of the file handle. The stream ends as soon as EOF is
-- encountered.
-- XXX once we have APIs to read any type 'a' the array stream
-- reading/writing APIs can be expressed in terms of the polymorphic API.
read
-- , readUtf8
-- , readLines
-- , readFrames
, readInChunksOf
-- -- * Array Read
-- , readArrayUpto
-- , readArrayOf
, readArraysOf
-- , readArraysOf
, readArrays
-- ** Write to Handle
-- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
-- underlying handle are ignored. The write occurs from the current seek
@ -83,11 +77,6 @@ module Streamly.FileSystem.Handle
-- , writeByFrames
, writeInChunksOf
-- -- * Array Write
, writeArray
, writeArrays
, writeArraysInChunksOf
-- -- * Random Access (Seek)
-- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
-- files that have random access or seek capability. This type of devices
@ -95,6 +84,8 @@ module Streamly.FileSystem.Handle
-- sockets and fifos.
--
-- , readIndex
-- XXX we can make the names consistent with the enumerate APIs. For
-- example, readFromTo, readFromToUp, readFromToDn, readFrom etc.
-- , readSlice
-- , readSliceRev
-- , readAt -- read from a given position to th end of file

View File

@ -122,7 +122,7 @@ import Streamly.Internal.Data.SVar (MonadAsync)
-- import Streamly.Data.Fold (Fold)
-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
import qualified Streamly.FileSystem.Handle as FH
import qualified Streamly.Internal.FileSystem.Handle as FH
import qualified Streamly.Internal.Memory.ArrayStream as AS
import qualified Streamly.Prelude as S