Skip to content

Commit 07e2b41

Browse files
authored
Merge pull request #76 from haskell-works/new-stream-method-for-create-index
New stream method for create-index
2 parents 92e5216 + 685ce2a commit 07e2b41

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

app/App/Commands/CreateBlankedXml.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module App.Commands.CreateBlankedXml
1313
import Control.Lens
1414
import Data.Generics.Product.Any
1515
import Data.Semigroup ((<>))
16-
import HaskellWorks.Data.Xml.Internal.ToIbBp64
1716
import HaskellWorks.Data.Xml.Succinct.Cursor.BlankedXml
1817
import Options.Applicative hiding (columns)
1918

app/App/Commands/CreateIndex.hs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,57 @@ module App.Commands.CreateIndex
1010
( cmdCreateIndex
1111
) where
1212

13+
import App.Options
1314
import Control.Lens
15+
import Control.Monad
1416
import Data.Generics.Product.Any
15-
import Data.Semigroup ((<>))
17+
import Data.Semigroup ((<>))
18+
import HaskellWorks.Data.Xml.Internal.ToIbBp64
19+
import HaskellWorks.Data.Xml.Succinct.Cursor.BlankedXml
1620
import HaskellWorks.Data.Xml.Succinct.Cursor.MMap
17-
import Options.Applicative hiding (columns)
21+
import Options.Applicative hiding (columns)
1822

1923
import qualified App.Commands.Types as Z
24+
import qualified Data.ByteString as BS
2025
import qualified Data.ByteString.Lazy as LBS
26+
import qualified Data.Text.IO as TIO
2127
import qualified HaskellWorks.Data.ByteString.Lazy as LBS
28+
import qualified Options.Applicative as OA
29+
import qualified System.Exit as IO
30+
import qualified System.IO as IO
2231

2332
runCreateIndex :: Z.CreateIndexOptions -> IO ()
2433
runCreateIndex opt = do
2534
let input = opt ^. the @"input"
2635
let ibOutput = opt ^. the @"ibOutput"
2736
let bpOutput = opt ^. the @"bpOutput"
37+
let method = opt ^. the @"method"
2838

29-
cursor <- mmapSlowCursor input
39+
case method of
40+
"memory" -> do
41+
cursor <- mmapSlowCursor input
3042

31-
LBS.writeFile ibOutput (LBS.toLazyByteString (cursor ^. the @"interests" . the @1))
32-
LBS.writeFile bpOutput (LBS.toLazyByteString (cursor ^. the @"balancedParens" . the @1))
43+
LBS.writeFile ibOutput (LBS.toLazyByteString (cursor ^. the @"interests" . the @1))
44+
LBS.writeFile bpOutput (LBS.toLazyByteString (cursor ^. the @"balancedParens" . the @1))
45+
"stream" -> do
46+
lbs <- LBS.readFile input
47+
let blankedXml = lbsToBlankedXml lbs
48+
let ibBp = toIbBp64 blankedXml
3349

34-
return ()
50+
hIbOutput <- IO.openFile ibOutput IO.WriteMode
51+
hBpOutput <- IO.openFile bpOutput IO.WriteMode
52+
53+
forM_ ibBp $ \(ib, bp) -> do
54+
BS.hPut hIbOutput ib
55+
BS.hPut hBpOutput bp
56+
57+
IO.hClose hIbOutput
58+
IO.hClose hBpOutput
59+
60+
return ()
61+
unknown -> do
62+
TIO.hPutStrLn IO.stderr $ "Unsupported method: " <> unknown
63+
IO.exitFailure
3564

3665
optsCreateIndex :: Parser Z.CreateIndexOptions
3766
optsCreateIndex = Z.CreateIndexOptions
@@ -50,6 +79,12 @@ optsCreateIndex = Z.CreateIndexOptions
5079
<> help "Balanced Parens output"
5180
<> metavar "FILE"
5281
)
82+
<*> textOption
83+
( long "method"
84+
<> help "Method"
85+
<> metavar "METHOD"
86+
<> OA.value "memory"
87+
)
5388

5489
cmdCreateIndex :: Mod CommandFields (IO ())
5590
cmdCreateIndex = command "create-index" $ flip info idm $ runCreateIndex <$> optsCreateIndex

app/App/Commands/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data CreateIndexOptions = CreateIndexOptions
2626
{ input :: FilePath
2727
, ibOutput :: FilePath
2828
, bpOutput :: FilePath
29+
, method :: Text
2930
} deriving (Eq, Show, Generic)
3031

3132
data CreateBlankedXmlOptions = CreateBlankedXmlOptions

0 commit comments

Comments
 (0)