@@ -14,8 +14,8 @@ module U.Codebase.Sqlite.Operations where
1414
1515import Control.Lens (Lens' )
1616import qualified Control.Lens as Lens
17- import Control.Monad (MonadPlus (mzero ), join , (<=<) )
1817import Control.Monad.Except (ExceptT , MonadError , runExceptT , throwError )
18+ import Control.Monad (MonadPlus (mzero ), join , when , (<=<) )
1919import Control.Monad.State (MonadState , StateT , evalStateT )
2020import Control.Monad.Trans (MonadTrans (lift ))
2121import Control.Monad.Trans.Maybe (MaybeT (MaybeT ), runMaybeT )
@@ -113,7 +113,7 @@ import qualified U.Util.Monoid as Monoid
113113import U.Util.Serialization (Get )
114114import qualified U.Util.Serialization as S
115115import qualified U.Util.Set as Set
116- import Control.Monad ( when )
116+ import Data.Foldable ( traverse_ )
117117
118118-- * Error handling
119119
@@ -427,7 +427,13 @@ saveTermComponent h terms = do
427427 hashId <- Q. saveHashHash h
428428 let li = S.Term. LocallyIndexedComponent $ Vector. fromList sTermElements
429429 bytes = S. putBytes S. putTermFormat $ S.Term. Term li
430- Q. saveObject hashId OT. TermComponent bytes
430+ oId <- Q. saveObject hashId OT. TermComponent bytes
431+ -- populate dependents index
432+ error " todo: populate dependents index"
433+ -- populate type indexes
434+ error " todo: populate type index"
435+ error " todo: populate type-mentions index"
436+ pure oId
431437
432438-- | implementation detail of c2{s,w}Term
433439-- The Type is optional, because we don't store them for watch expression results.
@@ -688,7 +694,23 @@ saveDeclComponent h decls = do
688694 hashId <- Q. saveHashHash h
689695 let li = S.Decl. LocallyIndexedComponent $ Vector. fromList sDeclElements
690696 bytes = S. putBytes S. putDeclFormat $ S.Decl. Decl li
691- Q. saveObject hashId OT. DeclComponent bytes
697+ oId <- Q. saveObject hashId OT. DeclComponent bytes
698+ -- populate dependents index
699+ let dependencies :: Set (S.Reference. Reference , S.Reference. Id ) = foldMap unlocalizeRefs (sDeclElements `zip` [0 .. ])
700+ unlocalizeRefs :: ((LocalIds' Db. TextId Db. ObjectId , S.Decl. Decl Symbol ), Word64 ) -> Set (S.Reference. Reference , S.Reference. Id )
701+ unlocalizeRefs ((LocalIds tIds oIds, decl), i) =
702+ let self = C.Reference. Id oId i
703+ dependencies :: Set (C.Reference. Reference' LocalTextId (Maybe LocalDefnId )) = C.Decl. dependencies decl
704+ getSRef :: C.Reference. Reference' LocalTextId (Maybe LocalDefnId ) -> Maybe S.Reference. Reference
705+ getSRef (C. ReferenceBuiltin t) = Just (C. ReferenceBuiltin (tIds Vector. ! fromIntegral t))
706+ getSRef (C.Reference. Derived (Just h) i) = Just (C.Reference. Derived (oIds Vector. ! fromIntegral h) i)
707+ getSRef _selfCycleRef@ (C.Reference. Derived Nothing _) = Nothing
708+ in Set. mapMaybe (fmap (,self) . getSRef) dependencies
709+ traverse_ (uncurry Q. addToDependentsIndex) dependencies
710+ -- populate type indexes
711+ error " todo: populate type index for constructors"
712+ error " todo: populate type-mentions index for constructors"
713+ pure oId
692714
693715c2sDecl :: forall m t d . EDB m => (Text -> m t ) -> (H. Hash -> m d ) -> C. Decl Symbol -> m (LocalIds' t d , S.Decl. Decl Symbol )
694716c2sDecl saveText saveDefn (C.Decl. DataDeclaration dt m b cts) = do
@@ -735,8 +757,8 @@ loadDeclByReference (C.Reference.Id h i) = do
735757 hashes <- traverse loadHashByObjectId $ LocalIds. defnLookup localIds
736758
737759 -- substitute the text and hashes back into the term
738- let substText ( LocalTextId w) = texts Vector. ! fromIntegral w
739- substHash ( LocalDefnId w) = hashes Vector. ! fromIntegral w
760+ let substText tIdx = texts Vector. ! fromIntegral tIdx
761+ substHash hIdx = hashes Vector. ! fromIntegral hIdx
740762 substTypeRef :: S.Decl. TypeRef -> C.Decl. TypeRef
741763 substTypeRef = bimap substText (fmap substHash)
742764 pure (C.Decl. DataDeclaration dt m b (C.Type. rmap substTypeRef <$> ct)) -- lens might be nice here
0 commit comments