|
4 | 4 | module Share.BackgroundJobs.Search.DefinitionSync (worker) where |
5 | 5 |
|
6 | 6 | import Control.Lens |
7 | | -import Control.Monad.Except |
8 | 7 | import Data.Either (isRight) |
9 | 8 | import Data.Generics.Product (HasField (..)) |
10 | 9 | import Data.List qualified as List |
@@ -384,47 +383,56 @@ syncTypes :: |
384 | 383 | m ([DefnIndexingFailure], [Text]) |
385 | 384 | syncTypes codebase namesPerspective rootBranchHashId typesCursor = do |
386 | 385 | 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 -> |
403 | 413 | 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 | + } |
415 | 423 | -- 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 |
417 | 425 | pped <- PPEPostgres.ppedForReferences namesPerspective allDeps |
418 | 426 | let ppe = PPED.unsuffixifiedPPE pped |
419 | | - let namedDocs :: [DefinitionDocument Name (Name, ShortHash)] |
| 427 | + let namedDocs :: V.Vector (DefinitionDocument Name (Name, ShortHash)) |
420 | 428 | namedDocs = |
421 | | - refDocs |
| 429 | + defDocuments |
422 | 430 | & traversed . field @"tokens" %~ Set.mapMaybe \token -> do |
423 | 431 | for token \ref -> do |
424 | 432 | name <- PPE.types ppe ref |
425 | 433 | 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) |
428 | 436 |
|
429 | 437 | -- | Compute the search tokens for a type declaration. |
430 | 438 | -- Note that constructors are handled separately when syncing terms. |
|
0 commit comments