Skip to content

Commit 47f4d00

Browse files
committed
Compiling but messy!
1 parent 52c3f28 commit 47f4d00

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

src/Share/BackgroundJobs/Search/DefinitionSync.hs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
module Share.BackgroundJobs.Search.DefinitionSync (worker) where
55

66
import Control.Lens
7-
import Control.Monad.Except
87
import Data.Either (isRight)
98
import Data.Generics.Product (HasField (..))
109
import Data.List qualified as List
@@ -384,47 +383,56 @@ syncTypes ::
384383
m ([DefnIndexingFailure], [Text])
385384
syncTypes codebase namesPerspective rootBranchHashId typesCursor = do
386385
Cursors.foldBatched typesCursor defnBatchSize \types -> do
387-
(errs, refDocs) <-
388-
types
389-
-- Most lib names are already filtered out by using the name lookup; but sometimes
390-
-- when libs aren't at the project root some can slip through, so we remove them.
391-
& V.filter
392-
( \(fqn, _) -> not (libSegment `elem` (NEL.toList $ Name.reverseSegments fqn))
393-
)
394-
& foldMapM \(fqn, ref) -> fmap (either (\err -> ([err], [])) (\doc -> ([], [doc]))) . runExceptT $ do
395-
(declTokens, declArity) <- case ref of
396-
Reference.Builtin _ -> pure (mempty, Arity 0)
397-
Reference.DerivedId refId -> do
398-
-- TODO: batchify this
399-
decl <- lift (Codebase.loadV1TypeDeclarationsByRefIdsOf codebase id refId) `whenNothingM` throwError (NoDeclForType fqn ref)
400-
pure $ (tokensForDecl refId decl, Arity . fromIntegral . length . DD.bound $ DD.asDataDecl decl)
401-
let basicTokens = Set.fromList [NameToken fqn, HashToken $ Reference.toShortHash ref]
402-
typeSummary <- lift $ Summary.typeSummariesForReferencesOf codebase Nothing id (ref, Just fqn)
386+
let nonLibTypes =
387+
types
388+
-- Most lib names are already filtered out by using the name lookup; but sometimes
389+
-- when libs aren't at the project root some can slip through, so we remove them.
390+
& V.filter
391+
( \(fqn, _) -> not (libSegment `elem` (NEL.toList $ Name.reverseSegments fqn))
392+
)
393+
let (fqns, refs) = V.unzip nonLibTypes
394+
let partitioned :: V.Vector (Either ([DefnIndexingFailure], Set (DefnSearchToken TypeReference), Arity) (Reference.Id, Reference.Id))
395+
partitioned =
396+
refs <&> \case
397+
Reference.Builtin _ -> Left (mempty, mempty, Arity 0)
398+
Reference.DerivedId refId -> Right (refId, refId)
399+
(errs, declTokens :: (V.Vector (Set (DefnSearchToken typeRef0))), arities :: V.Vector Arity) <- do
400+
Codebase.loadV1TypeDeclarationsByRefIdsOf codebase (traverse . _Right . _2) partitioned
401+
<&> itraversed <. _Right
402+
%@~ ( \i ->
403+
\case
404+
(refId, Just decl) -> ([], tokensForDecl refId decl, Arity . fromIntegral . length . DD.bound $ DD.asDataDecl decl)
405+
(refId, Nothing) -> ([(NoDeclForType (fqns V.! i) $ Reference.DerivedId refId)], mempty, Arity 0)
406+
)
407+
<&> fmap (either id id)
408+
<&> V.unzip3
409+
let basicTokens = Data.zipWith2 fqns refs \fqn ref -> Set.fromList [NameToken fqn, HashToken $ Reference.toShortHash ref]
410+
let allTokens = Data.zipWith2 declTokens basicTokens Set.union
411+
typeSummaries <- Summary.typeSummariesForReferencesOf codebase Nothing traversed (Data.zip2 refs (Just <$> fqns))
412+
let defDocuments = Data.zipWith5 refs fqns typeSummaries allTokens arities $ \ref fqn typeSummary tokens arity ->
403413
let sh = Reference.toShortHash ref
404-
let dd =
405-
DefinitionDocument
406-
{ rootBranchHashId,
407-
fqn,
408-
hash = sh,
409-
tokens = declTokens <> basicTokens,
410-
arity = declArity,
411-
tag = ToTTypeTag (typeSummary.tag),
412-
metadata = ToTTypeSummary typeSummary
413-
}
414-
pure dd
414+
in DefinitionDocument
415+
{ rootBranchHashId,
416+
fqn,
417+
hash = sh,
418+
tokens,
419+
arity,
420+
tag = ToTTypeTag (typeSummary.tag),
421+
metadata = ToTTypeSummary typeSummary
422+
}
415423
-- It's much more efficient to build only one PPE per batch.
416-
let allDeps = setOf (folded . folding tokens . folded . to LD.TypeReference) refDocs
424+
let allDeps = setOf (folded . folding tokens . folded . to LD.TypeReference) defDocuments
417425
pped <- PPEPostgres.ppedForReferences namesPerspective allDeps
418426
let ppe = PPED.unsuffixifiedPPE pped
419-
let namedDocs :: [DefinitionDocument Name (Name, ShortHash)]
427+
let namedDocs :: V.Vector (DefinitionDocument Name (Name, ShortHash))
420428
namedDocs =
421-
refDocs
429+
defDocuments
422430
& traversed . field @"tokens" %~ Set.mapMaybe \token -> do
423431
for token \ref -> do
424432
name <- PPE.types ppe ref
425433
pure $ (HQ'.toName name, Reference.toShortHash ref)
426-
badNames <- DDQ.insertDefinitionDocuments namedDocs
427-
pure (errs, badNames)
434+
badNames <- DDQ.insertDefinitionDocuments $ V.toList namedDocs
435+
pure (fold errs, badNames)
428436

429437
-- | Compute the search tokens for a type declaration.
430438
-- Note that constructors are handled separately when syncing terms.

src/Share/Utils/Data.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Share.Utils.Data
55
zipWith3,
66
zip4,
77
zipWith4,
8+
zip5,
9+
zipWith5,
810
mapFromSelf,
911
)
1012
where
@@ -47,6 +49,15 @@ zipWith4 as bs cs ds f =
4749
zip4 as bs cs ds
4850
<&> (\(a, b, c, d) -> f a b c d)
4951

52+
zip5 :: (Zip.Zip t) => t a -> t b -> t c -> t d -> t e -> t (a, b, c, d, e)
53+
zip5 as bs cs ds es =
54+
Zip.zipWith (\(a, b) (c, d, e) -> (a, b, c, d, e)) (Zip.zip as bs) (zip3 cs ds es)
55+
56+
zipWith5 :: (Zip.Zip t) => t a -> t b -> t c -> t d -> t e -> (a -> b -> c -> d -> e -> f) -> t f
57+
zipWith5 as bs cs ds es f =
58+
zip5 as bs cs ds es
59+
<&> (\(a, b, c, d, e) -> f a b c d e)
60+
5061
mapFromSelf :: (Ord k) => [k] -> Map k k
5162
mapFromSelf ks =
5263
Map.fromList (ks <&> \k -> (k, k))

0 commit comments

Comments
 (0)