Skip to content

Commit 0570e60

Browse files
committed
Runtime config for RepoMapping
1 parent 8f419e3 commit 0570e60

5 files changed

Lines changed: 54 additions & 11 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ THRIFT_GLEAN= \
267267
glean/config/server/server_config.thrift \
268268
glean/config/service.thrift \
269269
glean/config/client/client_config.thrift \
270+
glean/config/glass/repomapping.thrift \
270271
thrift/annotation/cpp.thrift \
271272
thrift/annotation/hack.thrift \
272273
thrift/annotation/rust.thrift \

glean.cabal.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,9 @@ library glass-lib
12841284
import: fb-haskell, fb-cpp, deps, thrift-server
12851285
visibility: public
12861286
default-extensions: CPP
1287-
hs-source-dirs: glean/glass
1287+
hs-source-dirs:
1288+
glean/glass
1289+
glean/config/glass/gen-hs2
12881290
exposed-modules:
12891291
Glean.Glass.Annotations
12901292
Glean.Glass.Attributes
@@ -1320,6 +1322,7 @@ library glass-lib
13201322
Glean.Glass.Query.Cxx
13211323
Glean.Glass.Range
13221324
Glean.Glass.RepoMapping
1325+
Glean.Glass.Repomapping.Types
13231326
Glean.Glass.Repos
13241327
Glean.Glass.Search
13251328
Glean.Glass.Search.Angle

glean/glass/Glean/Glass/Base.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Glean.Glass.Base
1515
, GleanDBSelector(..)
1616
) where
1717

18+
import Data.Default
1819
import Data.Function
1920
import Data.Hashable
2021
import Data.List.NonEmpty(NonEmpty(..))
@@ -100,3 +101,6 @@ data RepoMapping = RepoMapping
100101
-- This pairs attribute Glean dbs with a key type to index the ToAttribute
101102
-- class, that in turns knowns how to query and marshal the attributes
102103
}
104+
105+
instance Default RepoMapping where
106+
def = RepoMapping { gleanIndices = def, gleanAttrIndices = def }

glean/glass/Glean/Glass/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ withEnv Glass.Config{..} gleanDB f =
103103
withLatestRepos backend scm (Just logger)
104104
(if isRemote gleanService then listDatabasesRetry else Nothing) refreshFreq
105105
$ \latestGleanRepos -> do
106-
repoMapping <- getRepoMapping
106+
repoMapping <- getRepoMapping cfgapi
107107
f Glass.Env
108108
{ gleanBackend = Some backend
109109
, gleanDB = gleanDB
@@ -148,7 +148,7 @@ assignHeaders _ _ = []
148148
-- | Perform an operation with the latest RepoMapping
149149
withCurrentRepoMapping :: Glass.Env -> (Glass.Env -> IO a) -> IO a
150150
withCurrentRepoMapping env0 fn = do
151-
current <- getRepoMapping
151+
current <- getRepoMapping (Glass.cfgapi env0)
152152
fn (env0 { Glass.repoMapping = current })
153153

154154
withRequestTracing :: Env' GlassTraceWithId -> (Env -> IO b) -> IO b

glean/glass/Glean/Glass/RepoMapping.hs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
LICENSE file in the root directory of this source tree.
77
-}
88

9+
{-# LANGUAGE OverloadedRecordDot #-}
910
module Glean.Glass.RepoMapping
1011
( getRepoMapping
1112
, fixedRepoMapping
@@ -16,23 +17,57 @@ module Glean.Glass.RepoMapping
1617
, Mirror(..)
1718
) where
1819

20+
import qualified Data.Map.Strict as Map
21+
import Data.Maybe
1922
import Data.Set (Set)
2023
import qualified Data.Set as Set
21-
import qualified Data.Map.Strict as Map
24+
import Data.Text(Text)
25+
26+
import Thrift.Protocol.JSON (deserializeJSON)
27+
28+
import qualified Glean.Util.ThriftSource as ThriftSource
29+
import Glean.Util.ConfigProvider
2230

2331
import Glean.Glass.Base
2432
( GleanDBName(..)
2533
, RepoMapping(..)
2634
, GleanDBSelector(..)
2735
)
28-
import Glean.Glass.Types ( Language(..), RepoName(..) )
29-
import Data.Text(Text)
36+
import qualified Glean.Glass.Repomapping.Types as Config
37+
import Glean.Glass.SymbolId ( fromShortCode )
38+
import Glean.Glass.Types
3039

31-
getRepoMapping :: IO RepoMapping
32-
getRepoMapping = return RepoMapping
33-
{ gleanIndices = gleanIndices_
34-
, gleanAttrIndices = Map.empty
35-
}
40+
getRepoMapping :: ConfigProvider cfg => cfg -> IO RepoMapping
41+
getRepoMapping cfg = ThriftSource.load cfg repoMappingSource
42+
43+
toRepoMapping :: Config.RepoMapping -> RepoMapping
44+
toRepoMapping rm =
45+
RepoMapping
46+
{ gleanIndices = Map.fromList
47+
[ (RepoName repo, mapMaybe toSelector selectors)
48+
| (repo, selectors) <- Map.toList rm.indices
49+
]
50+
, gleanAttrIndices = Map.empty
51+
-- can do this later if necessary. We would have to pattern-match
52+
-- on a string and map to the ToAttributes key type.
53+
}
54+
where
55+
toSelector :: Config.DbSelector -> Maybe GleanDBSelector
56+
toSelector dbsel = do
57+
lang <- fromShortCode dbsel.language
58+
return GleanDBSelector
59+
{ dbName = GleanDBName dbsel.name
60+
, language = lang
61+
, branchName = dbsel.branch
62+
}
63+
64+
repoMappingSource :: ThriftSource.ThriftSource RepoMapping
65+
repoMappingSource =
66+
ThriftSource.configWithDeserializerDefault "glass/repomapping" $
67+
\bytes -> toRepoMapping <$> deserializeJSON bytes
68+
-- doing the translation to RepoMapping in the deserializer ensures
69+
-- that this is cached, we don't have to worry about the translation
70+
-- happening on every request.
3671

3772
fixedRepoMapping :: RepoMapping
3873
fixedRepoMapping = RepoMapping

0 commit comments

Comments
 (0)