on 7bf60ff help sign in
675 lines 25 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.FileSystem.Handle
5-- Copyright : (c) 2018 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12-- The fundamental singleton IO APIs are 'getChunk' and 'putChunk' and the
13-- fundamental stream IO APIs built on top of those are
14-- 'readChunksWithBufferOf' and 'writeChunks'. Rest of this module is just
15-- combinatorial programming using these.
17-- TODO: Need a separate module for pread/pwrite based reading writing for
18-- seekable devices. Stateless read/write can be helpful in multithreaded
19-- applications.
20--
21module Streamly.Internal.FileSystem.Handle
22 (
23 -- * Singleton APIs
24 getChunk
25 , getChunkOf
26 , putChunk
28 -- * Byte Stream Read
29 , read
30 -- , readUtf8
31 -- , readLines
32 -- , readFrames
33 , readWithBufferOf
35 , toBytes
36 , toBytesWithBufferOf
38 -- * Chunked Stream Read
39 , readChunks
40 , readChunksWithBufferOf
42 , toChunksWithBufferOf
43 , toChunks
45 -- * Byte Stream Write
46 -- Byte stream write (Folds)
47 , write
48 , write2
49 -- , writeUtf8
50 -- , writeUtf8ByLines
51 -- , writeByFrames
52 -- , writeLines
53 , writeWithBufferOf
55 , putBytes
56 , putBytesWithBufferOf
58 -- * Chunked Stream Write
59 , writeChunks
60 , writeChunksWithBufferOf
62 , putChunksWithBufferOf
63 , putChunks
65 -- * Random Access (Seek)
66 -- | Unlike the streaming APIs listed above, these APIs apply to devices or
67 -- files that have random access or seek capability. This type of devices
68 -- include disks, files, memory devices and exclude terminals, pipes,
69 -- sockets and fifos.
71 -- We can also generate the request pattern using a funciton.
72 --
73 -- , readIndex
74 -- , readFrom -- read from a given position to the end of file
75 -- , readFromRev -- read from a given position the beginning of file
76 -- , readTo -- read from beginning up to the given position
77 -- , readToRev -- read from end to the given position in file
78 -- , readFromTo
79 -- , readFromThenTo
81 -- , readChunksFrom
82 -- , readChunksFromTo
83 , readChunksFromToWith
84 -- , readChunksFromThenToWith
86 -- , writeIndex
87 -- , writeFrom -- start writing at the given position
88 -- , writeFromRev
89 -- , writeTo -- write from beginning up to the given position
90 -- , writeToRev
91 -- , writeFromTo
92 -- , writeFromThenTo
93 --
94 -- , writeChunksFrom
95 -- , writeChunksFromTo
96 -- , writeChunksFromToWith
97 -- , writeChunksFromThenToWith
98 )
99where
101import Control.Monad.IO.Class (MonadIO(..))
102import Data.Word (Word8)
103import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
104import Foreign.ForeignPtr (withForeignPtr)
105import Foreign.Ptr (minusPtr, plusPtr)
106import Foreign.Storable (Storable(..))
107import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
108import System.IO (Handle, hGetBufSome, hPutBuf)
109import Prelude hiding (read)
111import Streamly.Internal.BaseCompat
112import Streamly.Data.Fold (Fold)
113import Streamly.Internal.Data.Fold.Type (Fold2(..))
114import Streamly.Internal.Data.Unfold.Type (Unfold(..))
115import Streamly.Internal.Data.Array.Foreign.Type
116 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
117import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
118import Streamly.Internal.Data.Stream.Serial (SerialT)
119import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
120import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
121-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
122import Streamly.Internal.System.IO (defaultChunkSize)
124import qualified Streamly.Data.Fold as FL
125import qualified Streamly.Internal.Data.Fold.Type as FL
126import qualified Streamly.Internal.Data.Unfold as UF
127import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
128import qualified Streamly.Internal.Data.Stream.IsStream as S
129import qualified Streamly.Data.Array.Foreign as A
130import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
132-- $setup
133-- >>> import qualified Streamly.Data.Array.Foreign as Array
134-- >>> import qualified Streamly.Data.Fold as Fold
135-- >>> import qualified Streamly.Data.Unfold as Unfold
136-- >>> import qualified Streamly.FileSystem.Handle as Handle
137-- >>> import qualified Streamly.Prelude as Stream
138--
139-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
140-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (supplyFirst)
141-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
142-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
144-------------------------------------------------------------------------------
145-- References
146-------------------------------------------------------------------------------
147--
148-- The following references may be useful to build an understanding about the
149-- file API design:
150--
151-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
152-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
153-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
154-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
155-- https://www.w3.org/TR/FileAPI/ for http file API.
157-------------------------------------------------------------------------------
158-- Array IO (Input)
159-------------------------------------------------------------------------------
161-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
162-- no data is available on the handle it blocks until at least one byte becomes
163-- available. If any data is available then it immediately returns that data
164-- without blocking. As a result of this behavior, it may read less than or
165-- equal to the size requested.
166--
167-- @since 0.8.1
168{-# INLINABLE getChunk #-}
169getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
170getChunk size h = liftIO $ do
171 ptr <- mallocPlainForeignPtrBytes size
172 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
173 withForeignPtr ptr $ \p -> do
174 n <- hGetBufSome h p size
175 -- XXX shrink only if the diff is significant
176 return $
177 unsafeFreezeWithShrink $
178 mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
180-- This could be useful in implementing the "reverse" read APIs or if you want
181-- to read arrays of exact size instead of compacting them later. Compacting
182-- later requires more copying.
183--
184-- | Read a 'ByteArray' consisting of exactly the specified number of bytes
185-- from a file handle.
186--
187-- /Unimplemented/
188{-# INLINABLE getChunkOf #-}
189getChunkOf :: Int -> Handle -> IO (Array Word8)
190getChunkOf = undefined
192-------------------------------------------------------------------------------
193-- Stream of Arrays IO
194-------------------------------------------------------------------------------
196-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
197-- The maximum size of a single array is specified by @size@. The actual size
198-- read may be less than or equal to @size@.
199{-# INLINABLE _toChunksWithBufferOf #-}
200_toChunksWithBufferOf :: (IsStream t, MonadIO m)
201 => Int -> Handle -> t m (Array Word8)
202_toChunksWithBufferOf size h = go
203 where
204 -- XXX use cons/nil instead
205 go = mkStream $ \_ yld _ stp -> do
206 arr <- getChunk size h
207 if A.length arr == 0
208 then stp
209 else yld arr go
211-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
212-- handle @handle@. The maximum size of a single array is limited to @size@.
213-- The actual size read may be less than or equal to @size@.
214--
215-- >>> toChunksWithBufferOf size h = Stream.unfold Handle.readChunksWithBufferOf (size, h)
216--
217-- @since 0.7.0
218{-# INLINE_NORMAL toChunksWithBufferOf #-}
219toChunksWithBufferOf :: (IsStream t, MonadIO m) =>
220 Int -> Handle -> t m (Array Word8)
221toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
222 where
223 {-# INLINE_LATE step #-}
224 step _ _ = do
225 arr <- getChunk size h
226 return $
227 case A.length arr of
228 0 -> D.Stop
229 _ -> D.Yield arr ()
231-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
232-- Read requests to the IO device are performed using a buffer of size
233-- @bufsize@. The size of an array in the resulting stream is always less than
234-- or equal to @bufsize@.
235--
236-- @since 0.7.0
237{-# INLINE_NORMAL readChunksWithBufferOf #-}
238readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
239readChunksWithBufferOf = Unfold step return
240 where
241 {-# INLINE_LATE step #-}
242 step (size, h) = do
243 arr <- getChunk size h
244 return $
245 case A.length arr of
246 0 -> D.Stop
247 _ -> D.Yield arr (size, h)
249-- There are two ways to implement this.
250--
251-- 1. Idiomatic: use a scan on the output of readChunksWithBufferOf to total
252-- the array lengths and trim the last array to correct size.
253-- 2. Simply implement it from scratch like readChunksWithBufferOf.
254--
255-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
256-- reading from the offset `from` in the file and reads up to the offset `to`.
257--
258--
259{-# INLINE_NORMAL readChunksFromToWith #-}
260readChunksFromToWith :: -- MonadIO m =>
261 Unfold m (Int, Int, Int, Handle) (Array Word8)
262readChunksFromToWith = undefined
264-- XXX read 'Array a' instead of Word8
265--
266-- | @toChunks handle@ reads a stream of arrays from the specified file
267-- handle. The maximum size of a single array is limited to
268-- @defaultChunkSize@. The actual size read may be less than or equal to
269-- @defaultChunkSize@.
270--
271-- >>> toChunks = Handle.toChunksWithBufferOf IO.defaultChunkSize
272--
273-- @since 0.7.0
274{-# INLINE toChunks #-}
275toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
276toChunks = toChunksWithBufferOf defaultChunkSize
278-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
279-- device are performed using a buffer of size
280-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
281-- size of arrays in the resulting stream are therefore less than or equal to
282-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
283--
284-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWithBufferOf
285--
286-- @since 0.7.0
287{-# INLINE readChunks #-}
288readChunks :: MonadIO m => Unfold m Handle (Array Word8)
289readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
291-------------------------------------------------------------------------------
292-- Read File to Stream
293-------------------------------------------------------------------------------
295-- TODO for concurrent streams implement readahead IO. We can send multiple
296-- read requests at the same time. For serial case we can use async IO. We can
297-- also control the read throughput in mbps or IOPS.
299-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
300-- to the IO device are performed using buffers of @bufsize@.
301--
302-- >>> readWithBufferOf = Unfold.many Handle.readChunksWithBufferOf Array.read
303--
304-- @since 0.7.0
305{-# INLINE readWithBufferOf #-}
306readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
307readWithBufferOf = UF.many readChunksWithBufferOf A.read
309-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
310-- handle, reads are performed in chunks of up to @bufsize@.
311--
312-- >>> toBytesWithBufferOf size h = Stream.unfoldMany Array.read $ Handle.toChunksWithBufferOf size h
313--
314-- /Pre-release/
315{-# INLINE toBytesWithBufferOf #-}
316toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
317toBytesWithBufferOf size h = AS.concat $ toChunksWithBufferOf size h
319-- TODO
320-- Generate a stream of elements of the given type from a file 'Handle'.
321-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
322--
323-- | Unfolds a file handle into a byte stream. IO requests to the device are
324-- performed in sizes of
325-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
326--
327-- >>> read = Unfold.many Handle.readChunks Array.read
328--
329-- @since 0.7.0
330{-# INLINE read #-}
331read :: MonadIO m => Unfold m Handle Word8
332read = UF.many readChunks A.read
334-- | Generate a byte stream from a file 'Handle'.
335--
336-- >>> toBytes h = Stream.unfoldMany Array.read $ Handle.toChunks h
337--
338-- /Pre-release/
339{-# INLINE toBytes #-}
340toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
341toBytes = AS.concat . toChunks
343-------------------------------------------------------------------------------
344-- Writing
345-------------------------------------------------------------------------------
347-------------------------------------------------------------------------------
348-- Array IO (output)
349-------------------------------------------------------------------------------
351-- | Write an 'Array' to a file handle.
352--
353-- @since 0.8.1
354{-# INLINABLE putChunk #-}
355putChunk :: (MonadIO m, Storable a) => Handle -> Array a -> m ()
356putChunk _ arr | A.length arr == 0 = return ()
357putChunk h Array{..} =
358 liftIO $ unsafeWithForeignPtr aStart $ \p -> hPutBuf h p aLen
360 where
362 aLen =
363 let p = unsafeForeignPtrToPtr aStart
364 in aEnd `minusPtr` p
366-------------------------------------------------------------------------------
367-- Stream of Arrays IO
368-------------------------------------------------------------------------------
370-------------------------------------------------------------------------------
371-- Writing
372-------------------------------------------------------------------------------
374-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
375-- | Write a stream of arrays to a handle.
376--
377-- >>> putChunks h = Stream.mapM_ (Handle.putChunk h)
378--
379-- @since 0.7.0
380{-# INLINE putChunks #-}
381putChunks :: (MonadIO m, Storable a)
382 => Handle -> SerialT m (Array a) -> m ()
383putChunks h = S.mapM_ (putChunk h)
385-- XXX AS.compact can be written idiomatically in terms of foldMany, just like
386-- AS.concat is written in terms of foldMany. Once that is done we can write
387-- idiomatic def in the docs.
388--
389-- | @putChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
390-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
391-- The chunk size is only a maximum and the actual writes could be smaller as
392-- we do not split the arrays to fit exactly to the specified size.
393--
394-- @since 0.7.0
395{-# INLINE putChunksWithBufferOf #-}
396putChunksWithBufferOf :: (MonadIO m, Storable a)
397 => Int -> Handle -> SerialT m (Array a) -> m ()
398putChunksWithBufferOf n h xs = putChunks h $ AS.compact n xs
400-- | @putBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
401-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
402-- collect the required input size.
403--
404-- >>> putBytesWithBufferOf n h m = Handle.putChunks h $ Stream.arraysOf n m
405--
406-- @since 0.7.0
407{-# INLINE putBytesWithBufferOf #-}
408putBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
409putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
410-- putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
412-- | Write a byte stream to a file handle. Accumulates the input in chunks of
413-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing.
414--
415-- NOTE: This may perform better than the 'write' fold, you can try this if you
416-- need some extra perf boost.
417--
418-- >>> putBytes = Handle.putBytesWithBufferOf IO.defaultChunkSize
419--
420-- @since 0.7.0
421{-# INLINE putBytes #-}
422putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
423putBytes = putBytesWithBufferOf defaultChunkSize
425-- | Write a stream of arrays to a handle. Each array in the stream is written
426-- to the device as a separate IO request.
427--
428-- writeChunks h = Fold.drainBy (Handle.putChunk h)
429--
430-- @since 0.7.0
431{-# INLINE writeChunks #-}
432writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
433writeChunks h = FL.drainBy (putChunk h)
435{-# INLINE writeChunks2 #-}
436writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
437writeChunks2 =
438 Fold2 (\h arr -> liftIO $ putChunk h arr >> return h)
439 return
440 (\_ -> return ())
442-- XXX lpackArraysChunksOf should be written idiomatically
443--
444-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
445-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
446-- We never split an array, if a single array is bigger than the specified size
447-- it emitted as it is. Multiple arrays are coalesed as long as the total size
448-- remains below the specified size.
449--
450-- @since 0.7.0
451{-# INLINE writeChunksWithBufferOf #-}
452writeChunksWithBufferOf :: (MonadIO m, Storable a)
453 => Int -> Handle -> Fold m (Array a) ()
454writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
456-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
457--
458-- XXX test this
459-- Note that if you use a chunk size less than 8K (GHC's default buffer
460-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
461-- do not want buffering to occur at GHC level as well. Same thing applies to
462-- writes as well.
464-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
465--
466-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
467-- Bytes in the input stream are collected into a buffer until we have a chunk
468-- of @reqSize@ and then written to the IO device.
469--
470-- >>> writeWithBufferOf n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
471--
472-- @since 0.7.0
473{-# INLINE writeWithBufferOf #-}
474writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
475writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
477{-# INLINE writeWithBufferOf2 #-}
478writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
479writeWithBufferOf2 n = FL.chunksOf2 n (writeNUnsafe n) writeChunks2
481-- | Write a byte stream to a file handle. Accumulates the input in chunks of
482-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing
483-- to the IO device.
484--
485-- >>> write = Handle.writeWithBufferOf IO.defaultChunkSize
486--
487-- @since 0.7.0
488{-# INLINE write #-}
489write :: MonadIO m => Handle -> Fold m Word8 ()
490write = writeWithBufferOf defaultChunkSize
492{-# INLINE write2 #-}
493write2 :: MonadIO m => Fold2 m Handle Word8 ()
494write2 = writeWithBufferOf2 defaultChunkSize
496{-
497{-# INLINE write #-}
498write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
499write = toHandleWith A.defaultChunkSize
500-}
502-- XXX mmap a file into an array. This could be useful for in-place operations
503-- on a file. For example, we can quicksort the contents of a file by mmapping
504-- it.
506-------------------------------------------------------------------------------
507-- IO with encoding/decoding Unicode characters
508-------------------------------------------------------------------------------
510{-
511-- |
512-- > readUtf8 = decodeUtf8 . read
513--
514-- Read a UTF8 encoded stream of unicode characters from a file handle.
515--
516-- @since 0.7.0
517{-# INLINE readUtf8 #-}
518readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
519readUtf8 = decodeUtf8 . read
521-- |
522-- > writeUtf8 h s = write h $ encodeUtf8 s
523--
524-- Encode a stream of unicode characters to UTF8 and write it to the given file
525-- handle. Default block buffering applies to the writes.
526--
527-- @since 0.7.0
528{-# INLINE writeUtf8 #-}
529writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
530writeUtf8 h s = write h $ encodeUtf8 s
532-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
533-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
534-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
535-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
536-- option.
537--
538-- @since 0.7.0
539{-# INLINE writeUtf8ByLines #-}
540writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
541writeUtf8ByLines = undefined
543-- | Read UTF-8 lines from a file handle and apply the specified fold to each
544-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
545--
546-- @since 0.7.0
547{-# INLINE readLines #-}
548readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
549readLines h f = foldLines (readUtf8 h) f
551-------------------------------------------------------------------------------
552-- Framing on a sequence
553-------------------------------------------------------------------------------
555-- | Read a stream from a file handle and split it into frames delimited by
556-- the specified sequence of elements. The supplied fold is applied on each
557-- frame.
558--
559-- @since 0.7.0
560{-# INLINE readFrames #-}
561readFrames :: (IsStream t, MonadIO m, Storable a)
562 => Array a -> Handle -> Fold m a b -> t m b
563readFrames = undefined -- foldFrames . read
565-- | Write a stream to the given file handle buffering up to frames separated
566-- by the given sequence or up to a maximum of @defaultChunkSize@.
567--
568-- @since 0.7.0
569{-# INLINE writeByFrames #-}
570writeByFrames :: (IsStream t, MonadIO m, Storable a)
571 => Array a -> Handle -> t m a -> m ()
572writeByFrames = undefined
574-------------------------------------------------------------------------------
575-- Framing by time
576-------------------------------------------------------------------------------
578-- | Write collecting the input in sessions of n seconds or if chunkSize
579-- gets exceeded.
580{-# INLINE writeByChunksOrSessionsOf #-}
581writeByChunksOrSessionsOf :: MonadIO m
582 => Int -> Double -> Handle -> SerialT m Word8 -> m ()
583writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
585-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
586-- gets exceeded.
587{-# INLINE writeBySessionsOf #-}
588writeBySessionsOf :: MonadIO m => Double -> Handle -> SerialT m Word8 -> m ()
589writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
591-------------------------------------------------------------------------------
592-- Random Access IO (Seek)
593-------------------------------------------------------------------------------
595-- XXX handles could be shared, so we may not want to use the handle state at
596-- all for these APIs. we can use pread and pwrite instead. On windows we will
597-- need to use readFile/writeFile with an offset argument.
599-------------------------------------------------------------------------------
601-- | Read the element at the given index treating the file as an array.
602--
603-- @since 0.7.0
604{-# INLINE readIndex #-}
605readIndex :: Storable a => Handle -> Int -> Maybe a
606readIndex arr i = undefined
608-- NOTE: To represent a range to read we have chosen (start, size) instead of
609-- (start, end). This helps in removing the ambiguity of whether "end" is
610-- included in the range or not.
611--
612-- We could avoid specifying the range to be read and instead use "take size"
613-- on the stream, but it may end up reading more and then consume it partially.
615-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
616-- from @handle@ starting at the offset @pos@ from the beginning of the file.
617--
618-- Reads are performed in chunks of size @chunkSize@. For block devices, to
619-- avoid reading partial blocks @chunkSize@ must align with the block size of
620-- the underlying device. If the underlying block size is unknown, it is a good
621-- idea to keep it a multiple 4KiB. This API ensures that the start of each
622-- chunk is aligned with @chunkSize@ from second chunk onwards.
623--
624{-# INLINE readSliceWith #-}
625readSliceWith :: (IsStream t, MonadIO m, Storable a)
626 => Int -> Handle -> Int -> Int -> t m a
627readSliceWith chunkSize h pos len = undefined
629-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
630-- at index @i@ and reading up to @count@ elements in the forward direction
631-- ending at the index @i + count - 1@.
632--
633-- @since 0.7.0
634{-# INLINE readSlice #-}
635readSlice :: (IsStream t, MonadIO m, Storable a)
636 => Handle -> Int -> Int -> t m a
637readSlice = readSliceWith A.defaultChunkSize
639-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
640-- at index @i@ and reading up to @count@ elements in the reverse direction
641-- ending at the index @i - count + 1@.
642--
643-- @since 0.7.0
644{-# INLINE readSliceRev #-}
645readSliceRev :: (IsStream t, MonadIO m, Storable a)
646 => Handle -> Int -> Int -> t m a
647readSliceRev h i count = undefined
649-- | Write the given element at the given index in the file.
650--
651-- @since 0.7.0
652{-# INLINE writeIndex #-}
653writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
654writeIndex h i a = undefined
656-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
657-- starting at index @i@ and writing up to @count@ elements in the forward
658-- direction ending at the index @i + count - 1@.
659--
660-- @since 0.7.0
661{-# INLINE writeSlice #-}
662writeSlice :: (IsStream t, Monad m, Storable a)
663 => Handle -> Int -> Int -> t m a -> m ()
664writeSlice h i len s = undefined
666-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
667-- starting at index @i@ and writing up to @count@ elements in the reverse
668-- direction ending at the index @i - count + 1@.
669--
670-- @since 0.7.0
671{-# INLINE writeSliceRev #-}
672writeSliceRev :: (IsStream t, Monad m, Storable a)
673 => Handle -> Int -> Int -> t m a -> m ()
674writeSliceRev arr i len s = undefined
675-}