Skip to content

Commit b24ff44

Browse files
committed
glean merge: clean up and remove all the #ifdefery
* Generally speaking, don't use #ifdef. Most platform-specific APIs have platform-independent abstractions. * Don't enable `CPP` everywhere in the open-source build. It impacts copmile time and encourages use of `#ifdef`, which we want to keep to a minimum. * Don't import `Configerator` directly, use it via `ConfigProvider` * Don't hardwire the location of the schema, use schemaLocation We do this in a few places, see e.g. the implementation of `glean write-serialized-inventory` in `GleanCLI.hs`. And various genreal nits. This code needs some more work but I'll do this as a first pass.
1 parent a42509f commit b24ff44

4 files changed

Lines changed: 26 additions & 119 deletions

File tree

glean.cabal.in

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ common fb-haskell
2525
default-extensions:
2626
BangPatterns
2727
BinaryLiterals
28-
CPP
2928
DataKinds
3029
DeriveDataTypeable
3130
DeriveGeneric
@@ -134,10 +133,6 @@ common folly
134133
build-depends: fb-util < 0.2
135134
pkgconfig-depends: libfolly
136135

137-
-- Facebook-specific features
138-
flag facebook
139-
default: False
140-
141136
common deps
142137
build-depends:
143138
fb-util,
@@ -1129,10 +1124,7 @@ executable glean
11291124
GleanCLI.Restore
11301125
GleanCLI.Write
11311126
ghc-options: -main-is GleanCLI -with-rtsopts=-I0
1132-
default-extensions: CPP
11331127
extra-libraries: stdc++
1134-
if flag(facebook)
1135-
ghc-options: -DGLEAN_FACEBOOK
11361128
build-depends:
11371129
glean:cli-types,
11381130
glean:client-hs,

glean/db/Glean/Database/Schema.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ module Glean.Database.Schema
1616
, newDbSchema
1717
, newMergedDbSchema
1818
, mkDbSchemaFromSource
19-
, mkDbSchema
20-
, findSchemaInIndex
2119
, lookupPid
2220
, compareSchemaPredicates
2321
, validateNewSchema

glean/db/Glean/Database/Util.hs

Lines changed: 0 additions & 48 deletions
This file was deleted.

glean/tools/gleancli/GleanCLI/Merge.hs

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
This source code is licensed under the BSD-style license found in the
66
LICENSE file in the root directory of this source tree.
77
-}
8-
{-# LANGUAGE CPP, ApplicativeDo #-}
8+
{-# LANGUAGE ApplicativeDo #-}
99
module GleanCLI.Merge (MergeCommand) where
1010

1111
import Control.Exception
1212
import qualified Data.ByteString as B
1313
import qualified Data.ByteString.Lazy.Char8 as L
1414
import qualified Data.Aeson as Aeson
15+
import Data.Default
1516
import Data.IORef
1617
import Options.Applicative
1718
import System.Directory
19+
import System.Directory.Extra (listFiles)
1820
import System.FilePath
1921
import System.IO
2022
import System.Process
21-
import Data.Text(unpack)
2223
import Control.Concurrent.Stream
2324
import Util.OptParse
2425
import Util.Log ( logInfo )
25-
import Glean.Util.ThriftSource (load)
2626
import Thrift.Protocol.Compact
2727

2828
import Glean.LocalOrRemote (loadDbSchema)
@@ -34,23 +34,16 @@ import qualified Glean.RTS.Foreign.FactSet as FactSet
3434
import Glean.RTS.Foreign.Define (DefineFlags(..), defineBatch)
3535
import qualified Glean.RTS.Foreign.Inventory as Inventory
3636
import Glean.RTS.Foreign.Ownership
37+
import qualified Glean.Database.Config as GleanDB
3738
import Glean.Database.Schema.Types
38-
import Glean.Database.Util (getDbSchemaFromId)
39+
import qualified Glean.Util.ThriftSource as ThriftSource
40+
import Glean.Database.Config (cfgServerConfig)
41+
import Glean.Database.Schema
3942

4043
import GleanCLI.Types
4144
import GleanCLI.Common (dbOpts, fileFormatOpt, FileFormat (..))
4245
import Glean.Write (fileToBatches, schemaIdToOpts)
4346
import Glean.Write.JSON (buildJsonBatch)
44-
import Glean.Database.Config (schemaSourceIndexConfig)
45-
import Glean.DefaultConfigs(schemaConfigPath)
46-
import System.Directory.Extra (listFiles)
47-
48-
#if GLEAN_FACEBOOK
49-
import Configerator (
50-
withConfigeratorAPI,
51-
defaultConfigeratorOptions,
52-
)
53-
#endif
5447

5548

5649
data MergeCommand = MergeCommand
@@ -88,11 +81,11 @@ instance Plugin MergeCommand where
8881
fileFormat <- fileFormatOpt BinaryFormat
8982
return MergeCommand{..}
9083

91-
withService _evb _cfgAPI _svc MergeCommand{..} = do
84+
withService evb cfgAPI svc MergeCommand{..} = do
9285
(inventory, dbSchema) <- case inventorySource of
9386
Left repo -> do
9487
dbSchema <- Glean.withBackendWithDefaultOptions
95-
_evb _cfgAPI _svc Nothing $ \backend -> do
88+
evb cfgAPI svc Nothing $ \backend -> do
9689
loadDbSchema backend repo
9790
logInfo("db's schema ID is: " <> show(schemaId dbSchema))
9891
return (schemaInventory dbSchema, Just dbSchema)
@@ -170,52 +163,24 @@ instance Plugin MergeCommand where
170163

171164
batch <- case fileFormat of
172165
JsonFormat -> do
166+
(batches, schema_id_file) <- fileToBatches file
173167
case dbSchema of
174-
Nothing ->
175-
#if GLEAN_FACEBOOK
176-
withConfigeratorAPI defaultConfigeratorOptions $ \configAPI ->
177-
-- Load the schema index from the schema directory
178-
let schemaIndexResult =
179-
Right $ schemaSourceIndexConfig schemaConfigPath in do
180-
hPutStrLn stderr $
181-
"Reading current schema index from default "
182-
<> "configerator with path " <> unpack schemaConfigPath
183-
184-
-- Get the schema_id from the JSON file
185-
(batches, Just schema_id) <- fileToBatches file
186-
logInfo("Schema_ID from JSON file " <> file <> " is "
187-
<> show schema_id)
188-
189-
-- Try to get the DbSchema from the schema ID string
190-
case schemaIndexResult of
191-
Left err -> do
192-
throwIO $ ErrorCall err
193-
Right schemaIndex -> do
194-
logInfo $ "Looking up schema with ID: " <>
195-
show schema_id
196-
-- Materialise the schema index from its Thrift source
197-
concreteIndex <- load configAPI schemaIndex
198-
dbSchemaResult <-
199-
(Right <$>
200-
getDbSchemaFromId (Just concreteIndex) schema_id
201-
)
202-
`catch` \(e :: SomeException) ->
203-
return $ Left $ "Failed to get schema: " <> show e
204-
205-
case dbSchemaResult of
206-
Left err -> do
207-
throwIO $ ErrorCall("Error getting dbSchemaResult"
208-
<> " - exiting. Error is:\n" <> err)
209-
Right dbSchema -> do
210-
logInfo $ "Successfully loaded schema with ID: "
211-
<> show schema_id
212-
buildJsonBatch dbSchema
213-
(schemaIdToOpts $ Just(schemaId dbSchema)) batches
214-
#else
215-
return $ Left $ "Failed to get schema: "
216-
#endif
168+
Nothing -> do
169+
schema_id <- case schema_id_file of
170+
Nothing -> throwIO $ ErrorCall "missing schema ID"
171+
Just id -> return id
172+
let cfg = case svc of
173+
Glean.Local cfg _ -> cfg
174+
Glean.Remote{} -> def
175+
serverConfig <- ThriftSource.load cfgAPI (cfgServerConfig cfg)
176+
loc <- GleanDB.schemaLocation cfg serverConfig
177+
let (schemaSource, _) = GleanDB.cfgSchemaHook cfg loc
178+
index <- ThriftSource.load cfgAPI schemaSource
179+
dbSchema <- newDbSchema Nothing index
180+
(SpecificSchemaId schema_id) readWriteContent def
181+
buildJsonBatch dbSchema
182+
(schemaIdToOpts $ Just (schemaId dbSchema)) batches
217183
Just schema -> do
218-
(batches, schema_id_file) <- fileToBatches file
219184
if Just(schemaId schema) == schema_id_file then
220185
logInfo(
221186
"Schema matches with db schema. Merging data from "
@@ -224,7 +189,7 @@ instance Plugin MergeCommand where
224189
else
225190
throwIO $ ErrorCall $
226191
"ERROR - ABORTING MERGE\nSchema ID mismatch:\ndb: "
227-
<> show(schemaId schema) <> "\nvs\nFile: "
192+
<> show (schemaId schema) <> "\nvs\nFile: "
228193
<> file <> " has " <> show schema_id_file
229194
let getSchemaId theschema = Just(schemaId theschema) in
230195
buildJsonBatch schema

0 commit comments

Comments
 (0)