Skip to content

Commit

Permalink
Allow compressed (.ztd) files for glean write
Browse files Browse the repository at this point in the history
Summary:
Accept compressed (zstd, .zst) files to ```glean write```

Goal: allow compressed files to be passed through workflows
https://www.internalfb.com/code/fbsource/[b5249951f60baf828a88b7089556ee62ae3f099b]/tools/skycastle/lib2/glean/glean.sky?lines=657

Reviewed By: malanka

Differential Revision: D63698716

fbshipit-source-id: 6e1236e75717f477b0edc1743aee335351f17ec4
  • Loading branch information
Julia Molin authored and facebook-github-bot committed Oct 4, 2024
1 parent 73a22a7 commit 8ca0536
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion glean.cabal.in
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ library client-hs
glean:haxl-datasource,
glean:stubs,
prettyprinter-ansi-terminal,
thrift-haxl
thrift-haxl,
process-extras

library client-hs-local
import: fb-haskell, fb-cpp, deps
Expand Down
10 changes: 9 additions & 1 deletion glean/client/hs/Glean/Write.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import Util.String.Quasi

import Glean.Types hiding (Value)
import Glean.Schema.Util
import System.FilePath (takeExtension)
import qualified System.Process.ByteString as BS
import System.Exit (ExitCode(ExitSuccess))

$(mangle
[s|
Expand All @@ -53,7 +56,12 @@ instance FromJSON ParseJsonFactBatchForWriteServer where

fileToBatches :: FilePath -> IO [JsonFactBatch]
fileToBatches file = do
bs <- B.readFile file
bs <- if takeExtension file == ".zst" then do
(exit, bs, err) <- BS.readProcessWithExitCode "zstd" [file,"-d","-c"] ""
when (exit /= ExitSuccess) $
throwIO $ ErrorCall $ file ++ ": " ++ show err
return bs
else B.readFile file
r <- Foreign.CPP.Dynamic.callJSONParserFFI c_parseJsonFacts bs
case r of
Right val -> case Aeson.parse parseJSON val of
Expand Down
3 changes: 2 additions & 1 deletion glean/tools/gleancli/GleanCLI/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ fileFormatOpt defaultFormat = option (eitherReader parseFileFormat)
<> value defaultFormat
<> showDefault
<> metavar "(json|binary)"
<> help "Format of the files with facts (see FILE for more details)"
<> help ("Format of the files with facts (see FILE for more details). "
<> "json also accepts zstd compressed json")
)
where
parseFileFormat :: String -> Either String FileFormat
Expand Down
2 changes: 1 addition & 1 deletion glean/tools/gleancli/GleanCLI/Write.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fileArg :: Parser [FilePath]
fileArg = many $ strArgument
( metavar "FILE..."
<> help ("File(s) of facts to add to the DB. "
<> "You can specify the format of the file with --file-format")
<> "You can specify the format of the file with --file-format. ")
)

repoTimeOpt :: Parser UTCTime
Expand Down
8 changes: 4 additions & 4 deletions glean/website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ using this option, creation will fail if the current schema has
a different definition for any predicate in the base DB schema;
therefore predicates may only be added or removed relative to the base DB.
* `FILE..`<br />
File(s) of facts to write into the database (JSON). See [Writing data
to Glean](./write.md).
File(s) of facts to write into the database. Accepts JSON or compressed (zstd) JSONs.
See [Writing data to Glean](./write.md).

The schema for the new DB is given by:

Expand All @@ -74,8 +74,8 @@ Write facts to a database.
* `--db NAME/INSTANCE` or `--db-name NAME --db-instance INSTANCE`<br />
Specifies the name and instance of the database
* `FILE..`<br />
File(s) of facts to write into the database (JSON). See [Writing data
to Glean](./write.md).
File(s) of facts to write into the database. Accepts JSON or compressed (zstd) JSONs.
See [Writing data to Glean](./write.md).
* `--finish`<br />
Also mark the DB as complete

Expand Down

0 comments on commit 8ca0536

Please sign in to comment.