on dc58c59 help sign in
711 lines 26 kB history raw
1{-# LANGUAGE CPP #-}
3#include "inline.hs"
5-- |
6-- Module : Streamly.Internal.FileSystem.Handle
7-- Copyright : (c) 2018 Composewell Technologies
8--
9-- License : BSD3
10-- Maintainer : streamly@composewell.com
11-- Stability : experimental
12-- Portability : GHC
13--
14-- The fundamental singleton IO APIs are 'getChunk' and 'putChunk' and the
15-- fundamental stream IO APIs built on top of those are
16-- 'readChunksWith' and 'writeChunks'. Rest of this module is just
17-- combinatorial programming using these.
18--
19-- We can achieve line buffering by folding lines in the input stream into a
20-- stream of arrays using Stream.splitOn or Fold.takeEndBy_ and similar
21-- operations. One can wrap the input stream in 'Maybe' type and then use
22-- 'writeMaybesWith' to achieve user controlled buffering.
24-- TODO: Need a separate module for pread/pwrite based reading writing for
25-- seekable devices. Stateless read/write can be helpful in multithreaded
26-- applications.
27--
28module Streamly.Internal.FileSystem.Handle
29 (
30 -- * Setup
31 -- | To execute the code examples provided in this module in ghci, please
32 -- run the following commands first.
33 --
34 -- $setup
36 -- * Singleton APIs
37 getChunk
38 , getChunkOf
39 , putChunk
41 -- * Streams
42 , read
43 , readWith
44 , readChunksWith
45 , readChunks
47 -- * Unfolds
48 , reader
49 -- , readUtf8
50 -- , readLines
51 -- , readFrames
52 , readerWith
53 , chunkReader
54 , chunkReaderWith
56 -- * Folds
57 , write
58 -- , writeUtf8
59 -- , writeUtf8ByLines
60 -- , writeByFrames
61 -- , writeLines
62 , writeWith
63 , writeChunks
64 , writeChunksWith
65 , writeMaybesWith
67 -- * Refolds
68 , writer
69 , writerWith
70 , chunkWriter
71 -- , chunkWriterWith
73 -- * Stream writes
74 , putBytes
75 , putBytesWith
76 , putChunksWith
77 , putChunks
79 -- * Random Access (Seek)
80 -- | Unlike the streaming APIs listed above, these APIs apply to devices or
81 -- files that have random access or seek capability. This type of devices
82 -- include disks, files, memory devices and exclude terminals, pipes,
83 -- sockets and fifos.
85 -- We can also generate the request pattern using a funciton.
86 --
87 -- , readIndex
88 -- , readFrom -- read from a given position to the end of file
89 -- , readFromRev -- read from a given position the beginning of file
90 -- , readTo -- read from beginning up to the given position
91 -- , readToRev -- read from end to the given position in file
92 -- , readFromTo
93 -- , readFromThenTo
95 -- , readChunksFrom
96 -- , readChunksFromTo
97 , chunkReaderFromToWith
98 -- , readChunksFromThenToWith
100 -- , writeIndex
101 -- , writeFrom -- start writing at the given position
102 -- , writeFromRev
103 -- , writeTo -- write from beginning up to the given position
104 -- , writeToRev
105 -- , writeFromTo
106 -- , writeFromThenTo
107 --
108 -- , writeChunksFrom
109 -- , writeChunksFromTo
110 -- , writeChunksFromToWith
111 -- , writeChunksFromThenToWith
113 -- * Deprecated
114 , readChunksWithBufferOf
115 , readWithBufferOf
116 , writeChunksWithBufferOf
117 , writeWithBufferOf
118 )
119where
121import Control.Exception (assert)
122import Control.Monad.IO.Class (MonadIO(..))
123import Data.Function ((&))
124import Data.Maybe (isNothing, fromJust)
125import Data.Word (Word8)
126import Streamly.Internal.Data.Unbox (Unbox)
127import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
128import Prelude hiding (read)
130import Streamly.Internal.Data.Fold (Fold)
131import Streamly.Internal.Data.Refold.Type (Refold(..))
132import Streamly.Internal.Data.Unfold.Type (Unfold(..))
133import Streamly.Internal.Data.Array.Type
134 (Array(..), pinnedWriteNUnsafe, unsafeFreezeWithShrink, byteLength)
135import Streamly.Internal.Data.Stream.Type (Stream)
136import Streamly.Internal.Data.Array.Stream (lpackArraysChunksOf)
137-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
138import Streamly.Internal.System.IO (defaultChunkSize)
140import qualified Streamly.Data.Fold as FL
141import qualified Streamly.Data.Array as A
142import qualified Streamly.Internal.Data.Array.Type as A
143import qualified Streamly.Internal.Data.Array.Stream as AS
144import qualified Streamly.Internal.Data.MutArray.Type as MArray
145import qualified Streamly.Internal.Data.Refold.Type as Refold
146import qualified Streamly.Internal.Data.Fold.Type as FL(refoldMany)
147import qualified Streamly.Internal.Data.Stream as S
148import qualified Streamly.Internal.Data.Stream as D
149 (Stream(..), Step(..))
150import qualified Streamly.Internal.Data.Unfold as UF
151import qualified Streamly.Internal.Data.StreamK.Type as K (mkStream)
153#include "DocTestFileSystemHandle.hs"
155-------------------------------------------------------------------------------
156-- References
157-------------------------------------------------------------------------------
158--
159-- The following references may be useful to build an understanding about the
160-- file API design:
161--
162-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
163-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
164-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
165-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
166-- https://www.w3.org/TR/FileAPI/ for http file API.
168-------------------------------------------------------------------------------
169-- Array IO (Input)
170-------------------------------------------------------------------------------
172-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
173-- no data is available on the handle it blocks until at least one byte becomes
174-- available. If any data is available then it immediately returns that data
175-- without blocking. As a result of this behavior, it may read less than or
176-- equal to the size requested.
177--
178{-# INLINABLE getChunk #-}
179getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
180getChunk size h = liftIO $ do
181 arr <- MArray.pinnedNewBytes size
182 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
183 MArray.asPtrUnsafe arr $ \p -> do
184 n <- hGetBufSome h p size
185 -- XXX shrink only if the diff is significant
186 return $
187 unsafeFreezeWithShrink $
188 arr { MArray.arrEnd = n, MArray.arrBound = size }
190-- This could be useful in implementing the "reverse" read APIs or if you want
191-- to read arrays of exact size instead of compacting them later. Compacting
192-- later requires more copying.
193--
194-- | Read a 'ByteArray' consisting of exactly the specified number of bytes
195-- from a file handle.
196--
197-- /Unimplemented/
198{-# INLINABLE getChunkOf #-}
199getChunkOf :: Int -> Handle -> IO (Array Word8)
200getChunkOf = undefined
202-------------------------------------------------------------------------------
203-- Stream of Arrays IO
204-------------------------------------------------------------------------------
206-- | @getChunksWith size h@ reads a stream of arrays from file handle @h@.
207-- The maximum size of a single array is specified by @size@. The actual size
208-- read may be less than or equal to @size@.
209--
210{-# INLINE _getChunksWith #-}
211_getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
212_getChunksWith size h = S.fromStreamK go
213 where
214 -- XXX use cons/nil instead
215 go = K.mkStream $ \_ yld _ stp -> do
216 arr <- getChunk size h
217 if byteLength arr == 0
218 then stp
219 else yld arr go
221-- | @readChunksWith size handle@ reads a stream of arrays from the file
222-- handle @handle@. The maximum size of a single array is limited to @size@.
223-- The actual size read may be less than or equal to @size@.
224--
225-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
226--
227{-# INLINE_NORMAL readChunksWith #-}
228readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
229readChunksWith size h = D.Stream step ()
230 where
231 {-# INLINE_LATE step #-}
232 step _ _ = do
233 arr <- getChunk size h
234 return $
235 case byteLength arr of
236 0 -> D.Stop
237 _ -> D.Yield arr ()
239-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
240-- Read requests to the IO device are performed using a buffer of size
241-- @bufsize@. The size of an array in the resulting stream is always less than
242-- or equal to @bufsize@.
243--
244{-# INLINE_NORMAL chunkReaderWith #-}
245chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
246chunkReaderWith =
247 UF.lmap (uncurry getChunk) UF.repeatM
248 & UF.takeWhile ((/= 0) . byteLength)
250-- | Same as 'chunkReaderWith'
251--
252{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
253{-# INLINE_NORMAL readChunksWithBufferOf #-}
254readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
255readChunksWithBufferOf = chunkReaderWith
257-- There are two ways to implement this.
258--
259-- 1. Idiomatic: use a scan on the output of readChunksWith to total
260-- the array lengths and trim the last array to correct size.
261-- 2. Simply implement it from scratch like readChunksWith.
262--
263-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
265-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
266-- reading from the offset `from` in the file and reads up to the offset `to`.
267--
268{-# INLINE_NORMAL chunkReaderFromToWith #-}
269chunkReaderFromToWith :: MonadIO m =>
270 Unfold m (Int, Int, Int, Handle) (Array Word8)
271chunkReaderFromToWith = Unfold step inject
273 where
275 inject (from, to, bufSize, h) = do
276 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
277 -- XXX Use a strict Tuple?
278 return (to - from + 1, bufSize, h)
280 {-# INLINE_LATE step #-}
281 step (remaining, bufSize, h) =
282 if remaining <= 0
283 then return D.Stop
284 else do
285 arr <- getChunk (min bufSize remaining) h
286 return $
287 case byteLength arr of
288 0 -> D.Stop
289 len ->
290 assert (len <= remaining)
291 $ D.Yield arr (remaining - len, bufSize, h)
293-- | @getChunks handle@ reads a stream of arrays from the specified file
294-- handle. The maximum size of a single array is limited to
295-- @defaultChunkSize@. The actual size read may be less than or equal to
296-- @defaultChunkSize@.
297--
298-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
299--
300-- /Pre-release/
301{-# INLINE readChunks #-}
302readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
303readChunks = readChunksWith defaultChunkSize
305-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
306-- device are performed using a buffer of size
307-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'. The
308-- size of arrays in the resulting stream are therefore less than or equal to
309-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
310--
311-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
312--
313{-# INLINE chunkReader #-}
314chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
315chunkReader = UF.first defaultChunkSize chunkReaderWith
317-------------------------------------------------------------------------------
318-- Read File to Stream
319-------------------------------------------------------------------------------
321-- TODO for concurrent streams implement readahead IO. We can send multiple
322-- read requests at the same time. For serial case we can use async IO. We can
323-- also control the read throughput in mbps or IOPS.
325-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
326-- to the IO device are performed using buffers of @bufsize@.
327--
328-- >>> readerWith = Unfold.many Array.reader Handle.chunkReaderWith
329--
330{-# INLINE readerWith #-}
331readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
332readerWith = UF.many A.reader chunkReaderWith
334-- | Same as 'readerWith'
335--
336{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
337{-# INLINE readWithBufferOf #-}
338readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
339readWithBufferOf = readerWith
341{-# INLINE concatChunks #-}
342concatChunks :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
343concatChunks = S.unfoldMany A.reader
345-- | @readWith bufsize handle@ reads a byte stream from a file
346-- handle, reads are performed in chunks of up to @bufsize@.
347--
348-- >>> readWith size h = Stream.unfoldMany Array.reader $ Handle.readChunksWith size h
349--
350-- /Pre-release/
351{-# INLINE readWith #-}
352readWith :: MonadIO m => Int -> Handle -> Stream m Word8
353readWith size h = concatChunks $ readChunksWith size h
355-- | Unfolds a file handle into a byte stream. IO requests to the device are
356-- performed in sizes of
357-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
358--
359-- >>> reader = Unfold.many Array.reader Handle.chunkReader
360--
361{-# INLINE reader #-}
362reader :: MonadIO m => Unfold m Handle Word8
363reader = UF.many A.reader chunkReader
365-- | Generate a byte stream from a file 'Handle'.
366--
367-- >>> read h = Stream.unfoldMany Array.reader $ Handle.readChunks h
368--
369-- /Pre-release/
370{-# INLINE read #-}
371read :: MonadIO m => Handle -> Stream m Word8
372read = concatChunks . readChunks
374-------------------------------------------------------------------------------
375-- Writing
376-------------------------------------------------------------------------------
378-------------------------------------------------------------------------------
379-- Array IO (output)
380-------------------------------------------------------------------------------
382-- | Write an 'Array' to a file handle.
383--
384{-# INLINABLE putChunk #-}
385putChunk :: MonadIO m => Handle -> Array a -> m ()
386putChunk _ arr | byteLength arr == 0 = return ()
387putChunk h arr = A.asPtrUnsafe arr $ \ptr ->
388 liftIO $ hPutBuf h ptr aLen
390 where
392 -- XXX We should have the length passed by asPtrUnsafe itself.
393 aLen = A.byteLength arr
395-------------------------------------------------------------------------------
396-- Stream of Arrays IO
397-------------------------------------------------------------------------------
399-------------------------------------------------------------------------------
400-- Writing
401-------------------------------------------------------------------------------
403-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
404-- | Write a stream of arrays to a handle.
405--
406-- >>> putChunks h = Stream.fold (Fold.drainBy (Handle.putChunk h))
407--
408{-# INLINE putChunks #-}
409putChunks :: MonadIO m => Handle -> Stream m (Array a) -> m ()
410putChunks h = S.fold (FL.drainMapM (putChunk h))
412-- XXX AS.compact can be written idiomatically in terms of foldMany, just like
413-- AS.concat is written in terms of foldMany. Once that is done we can write
414-- idiomatic def in the docs.
415--
416-- | @putChunksWith bufsize handle stream@ writes a stream of arrays
417-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
418-- The chunk size is only a maximum and the actual writes could be smaller as
419-- we do not split the arrays to fit exactly to the specified size.
420--
421{-# INLINE putChunksWith #-}
422putChunksWith :: (MonadIO m, Unbox a)
423 => Int -> Handle -> Stream m (Array a) -> m ()
424putChunksWith n h xs = putChunks h $ AS.compact n xs
426-- > putBytesWith n h m = Handle.putChunks h $ A.pinnedChunksOf n m
428-- | @putBytesWith bufsize handle stream@ writes @stream@ to @handle@
429-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
430-- collect the required input size.
431--
432{-# INLINE putBytesWith #-}
433putBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
434putBytesWith n h m = putChunks h $ A.pinnedChunksOf n m
436-- | Write a byte stream to a file handle. Accumulates the input in chunks of
437-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing.
438--
439-- NOTE: This may perform better than the 'write' fold, you can try this if you
440-- need some extra perf boost.
441--
442-- >>> putBytes = Handle.putBytesWith IO.defaultChunkSize
443--
444{-# INLINE putBytes #-}
445putBytes :: MonadIO m => Handle -> Stream m Word8 -> m ()
446putBytes = putBytesWith defaultChunkSize
448-- | Write a stream of arrays to a handle. Each array in the stream is written
449-- to the device as a separate IO request.
450--
451-- writeChunks h = Fold.drainBy (Handle.putChunk h)
452--
453{-# INLINE writeChunks #-}
454writeChunks :: MonadIO m => Handle -> Fold m (Array a) ()
455writeChunks h = FL.drainMapM (putChunk h)
457-- | Like writeChunks but uses the experimental 'Refold' API.
458--
459-- /Internal/
460{-# INLINE chunkWriter #-}
461chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
462chunkWriter = Refold.drainBy putChunk
464-- XXX lpackArraysChunksOf should be written idiomatically
466-- | @writeChunksWith bufsize handle@ writes a stream of arrays
467-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
468-- We never split an array, if a single array is bigger than the specified size
469-- it emitted as it is. Multiple arrays are coalesed as long as the total size
470-- remains below the specified size.
471--
472{-# INLINE writeChunksWith #-}
473writeChunksWith :: (MonadIO m, Unbox a)
474 => Int -> Handle -> Fold m (Array a) ()
475writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
477-- | Same as 'writeChunksWith'
478--
479{-# DEPRECATED writeChunksWithBufferOf "Please use writeChunksWith instead." #-}
480{-# INLINE writeChunksWithBufferOf #-}
481writeChunksWithBufferOf :: (MonadIO m, Unbox a)
482 => Int -> Handle -> Fold m (Array a) ()
483writeChunksWithBufferOf = writeChunksWith
485-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
486--
487-- XXX test this
488-- Note that if you use a chunk size less than 8K (GHC's default buffer
489-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
490-- do not want buffering to occur at GHC level as well. Same thing applies to
491-- writes as well.
493-- XXX Maybe we should have a Fold.chunksOf like we have Stream.chunksOf
495-- | @writeWith reqSize handle@ writes the input stream to @handle@.
496-- Bytes in the input stream are collected into a buffer until we have a chunk
497-- of @reqSize@ and then written to the IO device.
498--
499-- >>> writeWith n h = Fold.groupsOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
500--
501{-# INLINE writeWith #-}
502writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
503writeWith n h = FL.groupsOf n (pinnedWriteNUnsafe n) (writeChunks h)
505-- | Same as 'writeWith'
506--
507{-# DEPRECATED writeWithBufferOf "Please use writeWith instead." #-}
508{-# INLINE writeWithBufferOf #-}
509writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
510writeWithBufferOf = writeWith
512-- | Write a stream of 'Maybe' values. Keep buffering the just values in an
513-- array until a 'Nothing' is encountered or the buffer size exceeds the
514-- specified limit, at that point flush the buffer to the handle.
515--
516-- /Pre-release/
517{-# INLINE writeMaybesWith #-}
518writeMaybesWith :: (MonadIO m )
519 => Int -> Handle -> Fold m (Maybe Word8) ()
520writeMaybesWith n h =
521 let writeNJusts = FL.lmap fromJust $ A.pinnedWriteN n
522 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
523 in FL.many writeOnNothing (writeChunks h)
525-- | Like 'writeWith' but uses the experimental 'Refold' API.
526--
527-- /Internal/
528{-# INLINE writerWith #-}
529writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
530writerWith n =
531 FL.refoldMany (FL.take n $ pinnedWriteNUnsafe n) chunkWriter
533-- | Write a byte stream to a file handle. Accumulates the input in chunks of
534-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing
535-- to the IO device.
536--
537-- >>> write = Handle.writeWith IO.defaultChunkSize
538--
539{-# INLINE write #-}
540write :: MonadIO m => Handle -> Fold m Word8 ()
541write = writeWith defaultChunkSize
543-- | Like 'write' but uses the experimental 'Refold' API.
544--
545-- /Internal/
546{-# INLINE writer #-}
547writer :: MonadIO m => Refold m Handle Word8 ()
548writer = writerWith defaultChunkSize
550-- XXX mmap a file into an array. This could be useful for in-place operations
551-- on a file. For example, we can quicksort the contents of a file by mmapping
552-- it.
554-------------------------------------------------------------------------------
555-- IO with encoding/decoding Unicode characters
556-------------------------------------------------------------------------------
558{-
559-- |
560-- > readUtf8 = decodeUtf8 . read
561--
562-- Read a UTF8 encoded stream of unicode characters from a file handle.
563--
564{-# INLINE readUtf8 #-}
565readUtf8 :: MonadIO m => Handle -> Stream m Char
566readUtf8 = decodeUtf8 . read
568-- |
569-- > writeUtf8 h s = write h $ encodeUtf8 s
570--
571-- Encode a stream of unicode characters to UTF8 and write it to the given file
572-- handle. Default block buffering applies to the writes.
573--
574{-# INLINE writeUtf8 #-}
575writeUtf8 :: MonadIO m => Handle -> Stream m Char -> m ()
576writeUtf8 h s = write h $ encodeUtf8 s
578-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
579-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
580-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
581-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
582-- option.
583--
584{-# INLINE writeUtf8ByLines #-}
585writeUtf8ByLines :: MonadIO m => Handle -> Stream m Char -> m ()
586writeUtf8ByLines = undefined
588-- | Read UTF-8 lines from a file handle and apply the specified fold to each
589-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
590--
591{-# INLINE readLines #-}
592readLines :: MonadIO m => Handle -> Fold m Char b -> Stream m b
593readLines h f = foldLines (readUtf8 h) f
595-------------------------------------------------------------------------------
596-- Framing on a sequence
597-------------------------------------------------------------------------------
599-- | Read a stream from a file handle and split it into frames delimited by
600-- the specified sequence of elements. The supplied fold is applied on each
601-- frame.
602--
603{-# INLINE readFrames #-}
604readFrames :: (MonadIO m, Unboxed a)
605 => Array a -> Handle -> Fold m a b -> Stream m b
606readFrames = undefined -- foldFrames . read
608-- | Write a stream to the given file handle buffering up to frames separated
609-- by the given sequence or up to a maximum of @defaultChunkSize@.
610--
611{-# INLINE writeByFrames #-}
612writeByFrames :: (MonadIO m, Unboxed a)
613 => Array a -> Handle -> Stream m a -> m ()
614writeByFrames = undefined
616-------------------------------------------------------------------------------
617-- Framing by time
618-------------------------------------------------------------------------------
620-- | Write collecting the input in sessions of n seconds or if chunkSize
621-- gets exceeded.
622{-# INLINE writeByChunksOrSessionsOf #-}
623writeByChunksOrSessionsOf :: MonadIO m
624 => Int -> Double -> Handle -> Stream m Word8 -> m ()
625writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
627-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
628-- gets exceeded.
629{-# INLINE writeBySessionsOf #-}
630writeBySessionsOf :: MonadIO m => Double -> Handle -> Stream m Word8 -> m ()
631writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
633-------------------------------------------------------------------------------
634-- Random Access IO (Seek)
635-------------------------------------------------------------------------------
637-- XXX handles could be shared, so we may not want to use the handle state at
638-- all for these APIs. we can use pread and pwrite instead. On windows we will
639-- need to use readFile/writeFile with an offset argument.
641-------------------------------------------------------------------------------
643-- | Read the element at the given index treating the file as an array.
644--
645{-# INLINE readIndex #-}
646readIndex :: Unboxed a => Handle -> Int -> Maybe a
647readIndex arr i = undefined
649-- NOTE: To represent a range to read we have chosen (start, size) instead of
650-- (start, end). This helps in removing the ambiguity of whether "end" is
651-- included in the range or not.
652--
653-- We could avoid specifying the range to be read and instead use "take size"
654-- on the stream, but it may end up reading more and then consume it partially.
656-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
657-- from @handle@ starting at the offset @pos@ from the beginning of the file.
658--
659-- Reads are performed in chunks of size @chunkSize@. For block devices, to
660-- avoid reading partial blocks @chunkSize@ must align with the block size of
661-- the underlying device. If the underlying block size is unknown, it is a good
662-- idea to keep it a multiple 4KiB. This API ensures that the start of each
663-- chunk is aligned with @chunkSize@ from second chunk onwards.
664--
665{-# INLINE readSliceWith #-}
666readSliceWith :: (MonadIO m, Unboxed a)
667 => Int -> Handle -> Int -> Int -> Stream m a
668readSliceWith chunkSize h pos len = undefined
670-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
671-- at index @i@ and reading up to @count@ elements in the forward direction
672-- ending at the index @i + count - 1@.
673--
674{-# INLINE readSlice #-}
675readSlice :: (MonadIO m, Unboxed a)
676 => Handle -> Int -> Int -> Stream m a
677readSlice = readSliceWith A.defaultChunkSize
679-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
680-- at index @i@ and reading up to @count@ elements in the reverse direction
681-- ending at the index @i - count + 1@.
682--
683{-# INLINE readSliceRev #-}
684readSliceRev :: (MonadIO m, Unboxed a)
685 => Handle -> Int -> Int -> Stream m a
686readSliceRev h i count = undefined
688-- | Write the given element at the given index in the file.
689--
690{-# INLINE writeIndex #-}
691writeIndex :: (MonadIO m, Unboxed a) => Handle -> Int -> a -> m ()
692writeIndex h i a = undefined
694-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
695-- starting at index @i@ and writing up to @count@ elements in the forward
696-- direction ending at the index @i + count - 1@.
697--
698{-# INLINE writeSlice #-}
699writeSlice :: (Monad m, Unboxed a)
700 => Handle -> Int -> Int -> Stream m a -> m ()
701writeSlice h i len s = undefined
703-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
704-- starting at index @i@ and writing up to @count@ elements in the reverse
705-- direction ending at the index @i - count + 1@.
706--
707{-# INLINE writeSliceRev #-}
708writeSliceRev :: (Monad m, Unboxed a)
709 => Handle -> Int -> Int -> Stream m a -> m ()
710writeSliceRev arr i len s = undefined
711-}