Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ THRIFT_GLEAN= \
glean/config/server/server_config.thrift \
glean/config/service.thrift \
glean/config/client/client_config.thrift \
glean/config/glass/repomapping.thrift \
thrift/annotation/cpp.thrift \
thrift/annotation/hack.thrift \
thrift/annotation/rust.thrift \
Expand Down
5 changes: 4 additions & 1 deletion glean.cabal.in
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,9 @@ library glass-lib
import: fb-haskell, fb-cpp, deps, thrift-server
visibility: public
default-extensions: CPP
hs-source-dirs: glean/glass
hs-source-dirs:
glean/glass
glean/config/glass/gen-hs2
exposed-modules:
Glean.Glass.Annotations
Glean.Glass.Attributes
Expand Down Expand Up @@ -1320,6 +1322,7 @@ library glass-lib
Glean.Glass.Query.Cxx
Glean.Glass.Range
Glean.Glass.RepoMapping
Glean.Glass.Repomapping.Types
Glean.Glass.Repos
Glean.Glass.Search
Glean.Glass.Search.Angle
Expand Down
15 changes: 15 additions & 0 deletions glean/config/glass/repomapping.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace hs Glean.Glass

typedef string RepoName
typedef string GleanDBName
typedef string Language

struct DbSelector {
1: GleanDBName name
2: Language language
3: optional string branch
} (hs.prefix = "")

struct RepoMapping {
1: map<RepoName,list<DbSelector>> indices
} (hs.prefix = "")
4 changes: 4 additions & 0 deletions glean/glass/Glean/Glass/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Glean.Glass.Base
, GleanDBSelector(..)
) where

import Data.Default
import Data.Function
import Data.Hashable
import Data.List.NonEmpty(NonEmpty(..))
Expand Down Expand Up @@ -100,3 +101,6 @@ data RepoMapping = RepoMapping
-- This pairs attribute Glean dbs with a key type to index the ToAttribute
-- class, that in turns knowns how to query and marshal the attributes
}

instance Default RepoMapping where
def = RepoMapping { gleanIndices = def, gleanAttrIndices = def }
4 changes: 2 additions & 2 deletions glean/glass/Glean/Glass/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ withEnv Glass.Config{..} gleanDB f =
withLatestRepos backend scm (Just logger)
(if isRemote gleanService then listDatabasesRetry else Nothing) refreshFreq
$ \latestGleanRepos -> do
repoMapping <- getRepoMapping
repoMapping <- getRepoMapping cfgapi
f Glass.Env
{ gleanBackend = Some backend
, gleanDB = gleanDB
Expand Down Expand Up @@ -148,7 +148,7 @@ assignHeaders _ _ = []
-- | Perform an operation with the latest RepoMapping
withCurrentRepoMapping :: Glass.Env -> (Glass.Env -> IO a) -> IO a
withCurrentRepoMapping env0 fn = do
current <- getRepoMapping
current <- getRepoMapping (Glass.cfgapi env0)
fn (env0 { Glass.repoMapping = current })

withRequestTracing :: Env' GlassTraceWithId -> (Env -> IO b) -> IO b
Expand Down
51 changes: 43 additions & 8 deletions glean/glass/Glean/Glass/RepoMapping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
LICENSE file in the root directory of this source tree.
-}

{-# LANGUAGE OverloadedRecordDot #-}
module Glean.Glass.RepoMapping
( getRepoMapping
, fixedRepoMapping
Expand All @@ -16,23 +17,57 @@ module Glean.Glass.RepoMapping
, Mirror(..)
) where

import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Map.Strict as Map
import Data.Text(Text)

import Thrift.Protocol.JSON (deserializeJSON)

import qualified Glean.Util.ThriftSource as ThriftSource
import Glean.Util.ConfigProvider

import Glean.Glass.Base
( GleanDBName(..)
, RepoMapping(..)
, GleanDBSelector(..)
)
import Glean.Glass.Types ( Language(..), RepoName(..) )
import Data.Text(Text)
import qualified Glean.Glass.Repomapping.Types as Config
import Glean.Glass.SymbolId ( fromShortCode )
import Glean.Glass.Types

getRepoMapping :: IO RepoMapping
getRepoMapping = return RepoMapping
{ gleanIndices = gleanIndices_
, gleanAttrIndices = Map.empty
}
getRepoMapping :: ConfigProvider cfg => cfg -> IO RepoMapping
getRepoMapping cfg = ThriftSource.load cfg repoMappingSource

toRepoMapping :: Config.RepoMapping -> RepoMapping
toRepoMapping rm =
RepoMapping
{ gleanIndices = Map.fromList
[ (RepoName repo, mapMaybe toSelector selectors)
| (repo, selectors) <- Map.toList rm.indices
]
, gleanAttrIndices = Map.empty
-- can do this later if necessary. We would have to pattern-match
-- on a string and map to the ToAttributes key type.
}
where
toSelector :: Config.DbSelector -> Maybe GleanDBSelector
toSelector dbsel = do
lang <- fromShortCode dbsel.language
return GleanDBSelector
{ dbName = GleanDBName dbsel.name
, language = lang
, branchName = dbsel.branch
}

repoMappingSource :: ThriftSource.ThriftSource RepoMapping
repoMappingSource =
ThriftSource.configWithDeserializerDefault "glass/repomapping" $
\bytes -> toRepoMapping <$> deserializeJSON bytes
-- doing the translation to RepoMapping in the deserializer ensures
-- that this is cached, we don't have to worry about the translation
-- happening on every request.

fixedRepoMapping :: RepoMapping
fixedRepoMapping = RepoMapping
Expand Down
Loading