Skip to content

Commit cb1e750

Browse files
committedMay 2, 2024
made directory searching concurrent
1 parent 3327288 commit cb1e750

File tree

2 files changed

+13
-90
lines changed

2 files changed

+13
-90
lines changed
 

‎my-grep.cabal

+1-85
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,52 @@
11
cabal-version: 3.4
2-
-- The cabal-version field refers to the version of the .cabal specification,
3-
-- and can be different from the cabal-install (the tool) version and the
4-
-- Cabal (the library) version you are using. As such, the Cabal (the library)
5-
-- version used must be equal or greater than the version stated in this field.
6-
-- Starting from the specification version 2.2, the cabal-version field must be
7-
-- the first thing in the cabal file.
8-
9-
-- Initial package description 'my-grep' generated by
10-
-- 'cabal init'. For further documentation, see:
11-
-- http://haskell.org/cabal/users-guide/
12-
--
13-
-- The name of the package.
142
name: my-grep
15-
16-
-- The package version.
17-
-- See the Haskell package versioning policy (PVP) for standards
18-
-- guiding when and how versions should be incremented.
19-
-- https://pvp.haskell.org
20-
-- PVP summary: +-+------- breaking API changes
21-
-- | | +----- non-breaking API additions
22-
-- | | | +--- code changes with no API change
233
version: 0.1.0.0
24-
25-
-- A short (one-line) description of the package.
26-
-- synopsis:
27-
28-
-- A longer description of the package.
29-
-- description:
30-
31-
-- The license under which the package is released.
324
license: BSD-2-Clause
33-
34-
-- The file containing the license text.
355
license-file: LICENSE
36-
37-
-- The package author(s).
386
author: tusharad
39-
40-
-- An email address to which users can send suggestions, bug reports, and patches.
417
maintainer: tusharadhatrao@gmail.com
42-
43-
-- A copyright notice.
44-
-- copyright:
458
category: Text
469
build-type: Simple
47-
48-
-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README.
4910
extra-doc-files: CHANGELOG.md
50-
51-
-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
52-
-- extra-source-files:
53-
5411
common warnings
55-
ghc-options: -Wall
12+
ghc-options: -Wall -threaded -rtsopts
5613

5714
library
58-
-- Import common warning flags.
5915
import: warnings
60-
61-
-- Modules exported by the library.
6216
exposed-modules: MyLib
6317
, MyGrep.Common.Types
6418
, MyGrep.Common.Utils
6519
, MyGrep.Core
6620
, MyGrep.Search
6721

68-
-- Modules included in this library but not exported.
69-
-- other-modules:
70-
71-
-- LANGUAGE extensions used by modules in this package.
72-
-- other-extensions:
73-
74-
-- Other library packages from which modules are imported.
7522
build-depends: base ^>=4.17.2.1
7623
, text
7724
, directory
7825
, filepath
7926
, safe-coloured-text
8027

81-
-- Directories containing source files.
8228
hs-source-dirs: src
8329

84-
-- Base language which the package is written in.
8530
default-language: GHC2021
8631

8732
executable my-grep
88-
-- Import common warning flags.
8933
import: warnings
9034

91-
-- .hs or .lhs file containing the Main module.
9235
main-is: Main.hs
9336

94-
-- Modules included in this executable, other than Main.
95-
-- other-modules:
96-
97-
-- LANGUAGE extensions used by modules in this package.
98-
-- other-extensions:
99-
100-
-- Other library packages from which modules are imported.
10137
build-depends:
10238
base ^>=4.17.2.1,
10339
my-grep
104-
105-
-- Directories containing source files.
10640
hs-source-dirs: app
107-
108-
-- Base language which the package is written in.
10941
default-language: GHC2021
11042

11143
test-suite my-grep-test
112-
-- Import common warning flags.
11344
import: warnings
114-
115-
-- Base language which the package is written in.
11645
default-language: GHC2021
117-
118-
-- Modules included in this executable, other than Main.
119-
-- other-modules:
120-
121-
-- LANGUAGE extensions used by modules in this package.
122-
-- other-extensions:
123-
124-
-- The interface type and version of the test suite.
12546
type: exitcode-stdio-1.0
126-
127-
-- Directories containing source files.
12847
hs-source-dirs: test
12948

130-
-- The entrypoint to the test suite.
13149
main-is: Main.hs
132-
133-
-- Test dependencies.
13450
build-depends:
13551
base ^>=4.17.2.1,
13652
my-grep

‎src/MyGrep/Search.hs

+12-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import Data.Text.Internal.Search
1010
import Data.Maybe (fromMaybe)
1111
import Control.Exception
1212
import System.FilePath
13+
import Control.Concurrent
14+
15+
16+
addInMVar :: MVar () -> IO ()
17+
addInMVar mVar1 = putMVar mVar1 ()
1318

1419
search :: Args -> IO ()
1520
search Args{..} = do
@@ -22,13 +27,15 @@ search Args{..} = do
2227
searchInDirectory searchLocation' = do
2328
files_ <- listDirectory searchLocation'
2429
let files = filter (\x -> head x /= '.') files_ -- filtering out hidden directories
25-
mapM_ go files
30+
isSearchCompleteVar <- newEmptyMVar
31+
mapM_ (\file -> forkIO $ go isSearchCompleteVar file) files
32+
mapM_ (\_ -> takeMVar isSearchCompleteVar) files -- wait for all threads to finish
2633
where
27-
go :: FilePath -> IO ()
28-
go fp = do
34+
go :: MVar () -> FilePath -> IO ()
35+
go mVar1 fp = do
2936
let fullPath = searchLocation' </> fp
30-
doesDirectoryExist fullPath >>= (\isDir -> if isDir then searchInDirectory fullPath
31-
else searchInFile fullPath)
37+
doesDirectoryExist fullPath >>= (\isDir -> if isDir then searchInDirectory fullPath >> addInMVar mVar1
38+
else searchInFile fullPath >> addInMVar mVar1)
3239

3340
searchInFile :: FilePath -> IO ()
3441
searchInFile fileName = do

0 commit comments

Comments
 (0)
Please sign in to comment.