@@ -10,28 +10,57 @@ module App.Commands.CreateIndex
1010 ( cmdCreateIndex
1111 ) where
1212
13+ import App.Options
1314import Control.Lens
15+ import Control.Monad
1416import 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
1620import HaskellWorks.Data.Xml.Succinct.Cursor.MMap
17- import Options.Applicative hiding (columns )
21+ import Options.Applicative hiding (columns )
1822
1923import qualified App.Commands.Types as Z
24+ import qualified Data.ByteString as BS
2025import qualified Data.ByteString.Lazy as LBS
26+ import qualified Data.Text.IO as TIO
2127import 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
2332runCreateIndex :: Z. CreateIndexOptions -> IO ()
2433runCreateIndex 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
3665optsCreateIndex :: Parser Z. CreateIndexOptions
3766optsCreateIndex = 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
5489cmdCreateIndex :: Mod CommandFields (IO () )
5590cmdCreateIndex = command " create-index" $ flip info idm $ runCreateIndex <$> optsCreateIndex
0 commit comments