-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathDirIO.hs
More file actions
301 lines (260 loc) · 10.9 KB
/
DirIO.hs
File metadata and controls
301 lines (260 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
-- |
-- Module : BenchTestLib.DirIO
-- Copyright : (c) 2019 Composewell Technologies
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
-- Stability : experimental
-- Portability : GHC
{-# LANGUAGE QuasiQuotes #-}
module BenchTestLib.DirIO
( createDirStucture
, listDirUnfoldDfs
, listDirUnfoldBfs
, listDirUnfoldBfsRev
, listDirConcatDfs
, listDirConcatBfs
, listDirConcatBfsRev
, listDirAppend
, listDirInterleave
, listDirPar
, listDirParInterleaved
, listDirParOrdered
, listDirChunkDfs
, listDirChunkBfs
, listDirChunkBfsRev
, listDirChunkAppend
, listDirChunkInterleave
, listDirChunkPar
, listDirChunkParInterleaved
, listDirChunkParOrdered
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
, listDirByteChunked
#endif
) where
--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------
import Data.IORef (newIORef, modifyIORef', readIORef)
import Data.Maybe (fromJust)
import Data.Word (Word8)
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
import Streamly.Data.Array (Array)
#endif
import Streamly.Data.Stream (Stream)
import Streamly.Data.Unfold (Unfold)
import Streamly.FileSystem.Path (Path)
import Streamly.Unicode.String (str)
import System.Directory (createDirectoryIfMissing)
import Data.Foldable (for_)
import qualified Streamly.Data.Stream.Prelude as Stream
import qualified Streamly.Data.Array as Array
import qualified Streamly.Internal.Data.Stream as Stream
import qualified Streamly.Data.StreamK as StreamK
import qualified Streamly.Internal.Data.StreamK as StreamK
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.Internal.Data.Unfold as Unfold
import qualified Streamly.Internal.FileSystem.DirIO as Dir
import qualified Streamly.FileSystem.Path as Path
import qualified Streamly.Internal.FileSystem.Path as Path (toChunk)
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
import qualified Streamly.Internal.FileSystem.Posix.ReadDir as Dir
#endif
--------------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------------
concatIterateWith :: Monad m =>
(a -> Stream m a)
-> (StreamK.StreamK m a
-> StreamK.StreamK m a -> StreamK.StreamK m a)
-> Stream m a
-> Stream m a
concatIterateWith nxt f =
StreamK.toStream
. StreamK.concatIterateWith f (StreamK.fromStream . nxt)
. StreamK.fromStream
mergeIterateWith :: Monad m =>
(a -> Stream m a)
-> (StreamK.StreamK m a
-> StreamK.StreamK m a -> StreamK.StreamK m a)
-> Stream m a
-> Stream m a
mergeIterateWith nxt f =
StreamK.toStream
. StreamK.mergeIterateWith f (StreamK.fromStream . nxt)
. StreamK.fromStream
streamDir
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either Path b -> Stream IO (Either Path Path)
streamDir f = either (Dir.readEitherPaths f) (const Stream.nil)
unfoldDir
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Unfold IO (Either Path b) (Either Path Path)
unfoldDir f = Unfold.either (Dir.eitherReaderPaths f) Unfold.nil
streamDirMaybe
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either Path b -> Maybe (Stream IO (Either Path Path))
streamDirMaybe f = either (Just . Dir.readEitherPaths f) (const Nothing)
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
_streamDirByteChunked
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either [Path] b -> Stream IO (Either [Path] (Array Word8))
_streamDirByteChunked f = either (Dir.readEitherByteChunks f) (const Stream.nil)
streamDirByteChunkedMaybe
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either [Path] b -> Maybe (Stream IO (Either [Path] (Array Word8)))
streamDirByteChunkedMaybe f =
either (Just . Dir.readEitherByteChunks f) (const Nothing)
#endif
streamDirChunkedMaybe
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either [Path] b -> Maybe (Stream IO (Either [Path] [Path]))
streamDirChunkedMaybe f = either (Just . Dir.readEitherChunks f) (const Nothing)
streamDirChunked
:: (Dir.ReadOptions -> Dir.ReadOptions)
-> Either [Path] b -> Stream IO (Either [Path] [Path])
streamDirChunked f = either (Dir.readEitherChunks f) (const Stream.nil)
--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
createDirStucture :: FilePath -> Int -> Int -> IO [FilePath]
createDirStucture root d w = do
ref <- newIORef [root]
createDirStucture_ ref root d w
readIORef ref
where
createDirStucture_ _ _ depth _ | depth <= 0 = pure ()
createDirStucture_ ref parentDir depth width = do
for_ [1..width] $ \i -> do
let iStr = show i
subDir = [str|#{parentDir}/dir_#{iStr}|]
createDirectoryIfMissing True subDir
modifyIORef' ref (subDir:)
createDirStucture_ ref subDir (depth - 1) width
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
-- Fastest implementation, only works for posix as of now.
{-# INLINE listDirByteChunked #-}
listDirByteChunked :: FilePath -> Stream IO (Array Word8)
listDirByteChunked inp = do
Stream.catRights
$ Stream.concatIterateDfs (streamDirByteChunkedMaybe id)
$ Stream.fromPure (Left [fromJust $ Path.fromString inp])
#endif
-- Faster than the listDir implementation below
{-# INLINE listDirChunkedWith #-}
listDirChunkedWith
:: (Stream IO (Either [Path] b) -> Stream IO (Either [Path] [Path]))
-> [Char] -> Stream IO Word8
listDirChunkedWith act inp = do
Stream.unfoldEachEndBy 10 Array.reader
$ fmap (Array.asBytes . Path.toChunk)
$ Stream.unfoldEach Unfold.fromList
$ fmap (either id id)
$ act
$ Stream.fromPure (Left [fromJust $ Path.fromString inp])
{-# INLINE listDirWith #-}
listDirWith
:: (Stream IO (Either Path Path) -> Stream IO (Either Path Path))
-> [Char] -> Stream IO Word8
listDirWith act inp = do
Stream.unfoldEachEndBy 10 Array.reader
$ fmap (Array.asBytes . Path.toChunk . either id id)
$ act
$ Stream.fromPure (Left (fromJust $ Path.fromString inp))
--------------------------------------------------------------------------------
-- Non chunked
--------------------------------------------------------------------------------
{-# INLINE listDirUnfoldDfs #-}
listDirUnfoldDfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirUnfoldDfs f = listDirWith (Stream.unfoldIterateDfs (unfoldDir f))
{-# INLINE listDirUnfoldBfs #-}
listDirUnfoldBfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirUnfoldBfs f = listDirWith (Stream.unfoldIterateBfs (unfoldDir f))
{-# INLINE listDirUnfoldBfsRev #-}
listDirUnfoldBfsRev
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirUnfoldBfsRev f = listDirWith (Stream.unfoldIterateBfsRev (unfoldDir f))
{-# INLINE listDirConcatDfs #-}
listDirConcatDfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirConcatDfs f = listDirWith (Stream.concatIterateDfs (streamDirMaybe f))
{-# INLINE listDirConcatBfs #-}
listDirConcatBfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirConcatBfs f = listDirWith (Stream.concatIterateBfs (streamDirMaybe f))
{-# INLINE listDirConcatBfsRev #-}
listDirConcatBfsRev
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirConcatBfsRev f =
listDirWith (Stream.concatIterateBfsRev (streamDirMaybe f))
{-# INLINE listDirAppend #-}
listDirAppend
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirAppend f = listDirWith (concatIterateWith (streamDir f) StreamK.append)
{-# INLINE listDirInterleave #-}
listDirInterleave
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirInterleave f =
listDirWith (mergeIterateWith (streamDir f) StreamK.interleave)
{-# INLINE listDirPar #-}
listDirPar
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirPar f = listDirWith (Stream.parConcatIterate id (streamDir f))
{-# INLINE listDirParInterleaved #-}
listDirParInterleaved
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirParInterleaved f =
listDirWith
(Stream.parConcatIterate (Stream.interleaved True) (streamDir f))
{-# INLINE listDirParOrdered #-}
listDirParOrdered
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirParOrdered f =
listDirWith (Stream.parConcatIterate (Stream.ordered True) (streamDir f))
--------------------------------------------------------------------------------
-- Chunked
--------------------------------------------------------------------------------
{-# INLINE listDirChunkDfs #-}
listDirChunkDfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkDfs f =
listDirChunkedWith (Stream.concatIterateDfs (streamDirChunkedMaybe f))
{-# INLINE listDirChunkBfs #-}
listDirChunkBfs
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkBfs f =
listDirChunkedWith (Stream.concatIterateBfs (streamDirChunkedMaybe f))
{-# INLINE listDirChunkBfsRev #-}
listDirChunkBfsRev
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkBfsRev f =
listDirChunkedWith (Stream.concatIterateBfsRev (streamDirChunkedMaybe f))
{-# INLINE listDirChunkAppend #-}
listDirChunkAppend
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkAppend f =
listDirChunkedWith (concatIterateWith (streamDirChunked f) StreamK.append)
{-# INLINE listDirChunkInterleave #-}
listDirChunkInterleave
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkInterleave f =
listDirChunkedWith
(mergeIterateWith (streamDirChunked f) StreamK.interleave)
{-# INLINE listDirChunkPar #-}
listDirChunkPar
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkPar f =
listDirChunkedWith (Stream.parConcatIterate id (streamDirChunked f))
{-# INLINE listDirChunkParInterleaved #-}
listDirChunkParInterleaved
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkParInterleaved f =
listDirChunkedWith
(Stream.parConcatIterate (Stream.interleaved True) (streamDirChunked f))
{-# INLINE listDirChunkParOrdered #-}
listDirChunkParOrdered
:: (Dir.ReadOptions -> Dir.ReadOptions) -> [Char] -> Stream IO Word8
listDirChunkParOrdered f =
listDirChunkedWith
(Stream.parConcatIterate (Stream.ordered True) (streamDirChunked f))