Skip to content

Commit 16e3f7e

Browse files
committed
New create-ib-index command
1 parent 3b04169 commit 16e3f7e

File tree

6 files changed

+82
-24
lines changed

6 files changed

+82
-24
lines changed

app/App/Commands.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module App.Commands where
22

33
import App.Commands.Count
4+
import App.Commands.CreateIbIndex
45
import App.Commands.CreateIndex
56
import App.Commands.Demo
6-
import Data.Semigroup ((<>))
7+
import Data.Semigroup ((<>))
78
import Options.Applicative
89

910
commands :: Parser (IO ())
@@ -14,4 +15,5 @@ commandsGeneral = subparser $ mempty
1415
<> commandGroup "Commands:"
1516
<> cmdCount
1617
<> cmdCreateIndex
18+
<> cmdCreateIbIndex
1719
<> cmdDemo

app/App/Commands/CreateIbIndex.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE FlexibleInstances #-}
4+
{-# LANGUAGE OverloadedStrings #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE TypeApplications #-}
7+
{-# LANGUAGE TypeSynonymInstances #-}
8+
9+
module App.Commands.CreateIbIndex
10+
( cmdCreateIbIndex
11+
) where
12+
13+
import Control.Lens
14+
import Data.Generics.Product.Any
15+
import Data.Semigroup ((<>))
16+
import HaskellWorks.Data.Xml.Internal.ToIbBp64
17+
import HaskellWorks.Data.Xml.Succinct.Cursor.BlankedXml
18+
import Options.Applicative hiding (columns)
19+
20+
import qualified App.Commands.Types as Z
21+
import qualified Data.ByteString.Lazy as LBS
22+
23+
runCreateIbIndex :: Z.CreateIbIndexOptions -> IO ()
24+
runCreateIbIndex opt = do
25+
let input = opt ^. the @"input"
26+
let ibOutput = opt ^. the @"ibOutput"
27+
28+
lbs <- LBS.readFile input
29+
let blankedXml = lbsToBlankedXml lbs
30+
let ib = toInterestBits64' blankedXml
31+
LBS.writeFile ibOutput (LBS.fromChunks ib)
32+
33+
return ()
34+
35+
optsCreateIbIndex :: Parser Z.CreateIbIndexOptions
36+
optsCreateIbIndex = Z.CreateIbIndexOptions
37+
<$> strOption
38+
( long "input"
39+
<> help "Input file"
40+
<> metavar "FILE"
41+
)
42+
<*> strOption
43+
( long "ib-output"
44+
<> help "Interest Bits output"
45+
<> metavar "FILE"
46+
)
47+
48+
cmdCreateIbIndex :: Mod CommandFields (IO ())
49+
cmdCreateIbIndex = command "create-ib-index" $ flip info idm $ runCreateIbIndex <$> optsCreateIbIndex

app/App/Commands/CreateIndex.hs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module App.Commands.CreateIndex
1111
) where
1212

1313
import Control.Lens
14-
import Control.Monad
1514
import Data.Generics.Product.Any
1615
import Data.Semigroup ((<>))
1716
import HaskellWorks.Data.Xml.Succinct.Cursor.MMap
@@ -23,14 +22,14 @@ import qualified HaskellWorks.Data.ByteString.Lazy as LBS
2322

2423
runCreateIndex :: Z.CreateIndexOptions -> IO ()
2524
runCreateIndex opt = do
26-
let input = opt ^. the @"input"
27-
let maybeIbOutput = opt ^. the @"ibOutput"
28-
let maybeBpOutput = opt ^. the @"bpOutput"
25+
let input = opt ^. the @"input"
26+
let ibOutput = opt ^. the @"ibOutput"
27+
let bpOutput = opt ^. the @"bpOutput"
2928

3029
cursor <- mmapSlowCursor input
3130

32-
forM_ maybeIbOutput $ flip LBS.writeFile (LBS.toLazyByteString (cursor ^. the @"interests" . the @1))
33-
forM_ maybeBpOutput $ flip LBS.writeFile (LBS.toLazyByteString (cursor ^. the @"balancedParens" . the @1))
31+
LBS.writeFile ibOutput (LBS.toLazyByteString (cursor ^. the @"interests" . the @1))
32+
LBS.writeFile bpOutput (LBS.toLazyByteString (cursor ^. the @"balancedParens" . the @1))
3433

3534
return ()
3635

@@ -41,19 +40,15 @@ optsCreateIndex = Z.CreateIndexOptions
4140
<> help "Input file"
4241
<> metavar "FILE"
4342
)
44-
<*> optional
45-
( strOption
46-
( long "ib-output"
47-
<> help "Interest Bits output"
48-
<> metavar "FILE"
49-
)
43+
<*> strOption
44+
( long "ib-output"
45+
<> help "Interest Bits output"
46+
<> metavar "FILE"
5047
)
51-
<*> optional
52-
( strOption
53-
( long "bp-output"
54-
<> help "Balanced Parens output"
55-
<> metavar "FILE"
56-
)
48+
<*> strOption
49+
( long "bp-output"
50+
<> help "Balanced Parens output"
51+
<> metavar "FILE"
5752
)
5853

5954
cmdCreateIndex :: Mod CommandFields (IO ())

app/App/Commands/Types.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module App.Commands.Types
55
( CountOptions(..)
66
, CreateIndexOptions(..)
7+
, CreateIbIndexOptions(..)
78
, DemoOptions(..)
89
) where
910

@@ -21,6 +22,11 @@ data CountOptions = CountOptions
2122

2223
data CreateIndexOptions = CreateIndexOptions
2324
{ input :: FilePath
24-
, ibOutput :: Maybe FilePath
25-
, bpOutput :: Maybe FilePath
25+
, ibOutput :: FilePath
26+
, bpOutput :: FilePath
27+
} deriving (Eq, Show, Generic)
28+
29+
data CreateIbIndexOptions = CreateIbIndexOptions
30+
{ input :: FilePath
31+
, ibOutput :: FilePath
2632
} deriving (Eq, Show, Generic)

hw-xml.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ executable hw-xml
139139
other-modules: Paths_hw_xml
140140
App.Commands
141141
App.Commands.Count
142+
App.Commands.CreateIbIndex
142143
App.Commands.CreateIndex
143144
App.Commands.Demo
144145
App.Commands.Types

src/HaskellWorks/Data/Xml/Succinct/Cursor/BlankedXml.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ module HaskellWorks.Data.Xml.Succinct.Cursor.BlankedXml
44
, FromBlankedXml(..)
55
, getBlankedXml
66
, bsToBlankedXml
7+
, lbsToBlankedXml
78
) where
89

9-
import HaskellWorks.Data.ByteString
1010
import HaskellWorks.Data.Xml.Conduit.Blank
1111

12-
import qualified Data.ByteString as BS
12+
import qualified Data.ByteString as BS
13+
import qualified Data.ByteString.Lazy as LBS
14+
import qualified HaskellWorks.Data.ByteString as BS
1315

1416
newtype BlankedXml = BlankedXml [BS.ByteString] deriving (Eq, Show)
1517

@@ -20,4 +22,7 @@ class FromBlankedXml a where
2022
fromBlankedXml :: BlankedXml -> a
2123

2224
bsToBlankedXml :: BS.ByteString -> BlankedXml
23-
bsToBlankedXml bs = BlankedXml (blankXml (chunkedBy 4064 bs))
25+
bsToBlankedXml bs = BlankedXml (blankXml (BS.chunkedBy 4064 bs))
26+
27+
lbsToBlankedXml :: LBS.ByteString -> BlankedXml
28+
lbsToBlankedXml lbs = BlankedXml (blankXml (BS.resegmentPadded 4096 (LBS.toChunks lbs)))

0 commit comments

Comments
 (0)