@@ -9,24 +9,22 @@ module Share.Codebase
9
9
codebaseOwner ,
10
10
CodebaseRuntime (.. ),
11
11
codebaseEnv ,
12
- withCodebaseRuntime ,
13
- codebaseRuntimeTransaction ,
14
12
codebaseForProjectBranch ,
15
13
codebaseLocationForUserCodebase ,
16
14
codebaseLocationForProjectBranchCodebase ,
17
15
codebaseLocationForProjectRelease ,
18
16
CodebaseLocation (.. ),
19
17
20
18
-- * Definitions
21
- loadTermAndTypeByRefIdsOf ,
19
+ loadV1TermAndTypeByRefIdsOf ,
22
20
expectTermsByRefIdsOf ,
23
21
loadTypesOfTermsOf ,
24
22
expectTypesOfTermsOf ,
25
23
expectTypesOfReferentsOf ,
26
24
expectTypesOfConstructorsOf ,
27
25
loadTypesOfConstructorsOf ,
28
26
loadTypesOfReferentsOf ,
29
- loadTypeDeclarationsByRefIdsOf ,
27
+ loadV1TypeDeclarationsByRefIdsOf ,
30
28
expectTypeDeclarationsByRefIdsOf ,
31
29
loadDeclKindsOf ,
32
30
expectDeclKindsOf ,
@@ -38,7 +36,6 @@ module Share.Codebase
38
36
-- * Eval
39
37
loadCachedEvalResult ,
40
38
saveCachedEvalResult ,
41
- codeLookupForUser ,
42
39
43
40
-- * Causals
44
41
CausalQ. loadCausalNamespace ,
@@ -62,7 +59,6 @@ module Share.Codebase
62
59
)
63
60
where
64
61
65
- import Control.Concurrent.STM (TVar , atomically , modifyTVar' , newTVarIO , readTVarIO )
66
62
import Control.Lens
67
63
import Data.ByteString.Lazy.Char8 qualified as BL
68
64
import Data.Map qualified as Map
@@ -105,8 +101,6 @@ import U.Codebase.Term qualified as TermV2
105
101
import U.Codebase.Term qualified as V2.Term
106
102
import Unison.Builtin qualified as Builtin
107
103
import Unison.Builtin qualified as Builtins
108
- import Unison.Codebase.CodeLookup qualified as CL
109
- import Unison.Codebase.Runtime (Runtime )
110
104
import Unison.Codebase.SqliteCodebase.Conversions qualified as Cv
111
105
import Unison.ConstructorType qualified as CT
112
106
import Unison.DataDeclaration qualified as DD
@@ -119,14 +113,11 @@ import Unison.Reference qualified as Reference
119
113
import Unison.Reference qualified as V1
120
114
import Unison.Referent qualified as V1
121
115
import Unison.Referent qualified as V1Referent
122
- import Unison.Runtime.IOSource qualified as IOSource
123
116
import Unison.ShortHash
124
117
import Unison.ShortHash qualified as ShortHash
125
118
import Unison.Symbol (Symbol )
126
- import Unison.Term qualified as Term
127
119
import Unison.Term qualified as V1
128
120
import Unison.Type qualified as V1
129
- import UnliftIO qualified
130
121
131
122
data CodebaseError
132
123
= MissingTypeForTerm Reference. Reference
@@ -170,30 +161,6 @@ codebaseEnv !_authZReceipt codebaseLoc = do
170
161
let codebaseOwner = Codebase. codebaseOwnerUserId codebaseLoc
171
162
in CodebaseEnv {codebaseOwner}
172
163
173
- -- | Construct a Runtime linked to a specific codebase and transaction.
174
- -- Don't use the runtime for one codebase with another codebase.
175
- -- Don't use this runtime in any transaction other than the one where it's created.
176
- withCodebaseRuntime :: (Exception e ) => CodebaseEnv -> Runtime Symbol -> (forall s . CodebaseRuntime s IO -> PG. Transaction e r ) -> PG. Transaction e r
177
- withCodebaseRuntime codebaseEnv sandboxedRuntime f = do
178
- rt <- PG. transactionUnsafeIO (codebaseRuntimeTransaction sandboxedRuntime codebaseEnv)
179
- PG. asUnliftIOTransaction $ do
180
- UnliftIO. withRunInIO \ toIO -> do
181
- toIO . PG. UnliftIOTransaction $ f $ hoistCodebaseRuntime (toIO . PG. UnliftIOTransaction ) rt
182
-
183
- -- | Ideally, we'd use this – a runtime with lookup actions in transaction, not IO. But that will require refactoring to
184
- -- the runtime interface in ucm, so we can't use it for now. That's bad: we end up unsafely running separate
185
- -- transactions for inner calls to 'codeLookup' / 'cachedEvalResult', which can lead to deadlock due to a starved
186
- -- connection pool.
187
- codebaseRuntimeTransaction :: Runtime Symbol -> CodebaseEnv -> IO (CodebaseRuntime s (PG. Transaction e ))
188
- codebaseRuntimeTransaction unisonRuntime codebase = do
189
- cacheVar <- newTVarIO (CodeLookupCache mempty mempty )
190
- pure
191
- CodebaseRuntime
192
- { codeLookup = codeLookupForUser cacheVar codebase,
193
- cachedEvalResult = (fmap . fmap ) Term. unannotate . loadCachedEvalResult codebase,
194
- unisonRuntime
195
- }
196
-
197
164
-- | Wrap a response in caching.
198
165
-- This combinator respects the cachability stored on the provided auth receipt.
199
166
cachedCodebaseResponse ::
@@ -218,8 +185,8 @@ cachedCodebaseResponse authzReceipt codebaseOwner endpointName providedCachePara
218
185
codebaseViewCacheKey = IDs. toText (codebaseOwnerUserId codebaseOwner)
219
186
220
187
-- | Load terms and type.
221
- loadTermAndTypeByRefIdsOf :: (QueryM m ) => CodebaseEnv -> Traversal s t TermReferenceId (Maybe (V1. Term Symbol Ann , V1. Type Symbol Ann )) -> s -> m t
222
- loadTermAndTypeByRefIdsOf codebase trav s = do
188
+ loadV1TermAndTypeByRefIdsOf :: (QueryM m ) => CodebaseEnv -> Traversal s t TermReferenceId (Maybe (V1. Term Symbol Ann , V1. Type Symbol Ann )) -> s -> m t
189
+ loadV1TermAndTypeByRefIdsOf codebase trav s = do
223
190
s
224
191
& asListOf trav %%~ \ refs -> do
225
192
let hashes = refs <&> \ (Reference. Id h _) -> h
@@ -257,7 +224,7 @@ convertTerms2to1Of trav s = do
257
224
expectTermsByRefIdsOf :: (QueryM m ) => CodebaseEnv -> Traversal s t TermReferenceId (V1. Term Symbol Ann , V1. Type Symbol Ann ) -> s -> m t
258
225
expectTermsByRefIdsOf codebase trav s = do
259
226
s & asListOfDeduped trav \ refIds -> do
260
- termsAndTypes <- loadTermAndTypeByRefIdsOf codebase traversed refIds
227
+ termsAndTypes <- loadV1TermAndTypeByRefIdsOf codebase traversed refIds
261
228
for (zip refIds termsAndTypes) \ case
262
229
(refId, Nothing ) -> unrecoverableError (MissingTerm refId)
263
230
(_, Just (term, typ)) -> pure (term, typ)
@@ -277,7 +244,7 @@ loadTypesOfTermsOf codebase trav s =
277
244
<&> \ typ ->
278
245
(typ $> builtinAnnotation)
279
246
in Left builtinType
280
- results <- loadTermAndTypeByRefIdsOf codebase (traversed . _Right) partitionedRefs
247
+ results <- loadV1TermAndTypeByRefIdsOf codebase (traversed . _Right) partitionedRefs
281
248
pure $
282
249
results <&> \ case
283
250
Left builtin -> builtin
@@ -329,8 +296,8 @@ loadDeclKindsOf trav s =
329
296
& DefnQ. loadDeclKindsOf (traversed . _Right)
330
297
pure (fmap (either id id ) xs)
331
298
332
- loadTypeDeclarationsByRefIdsOf :: (QueryM m ) => CodebaseEnv -> Traversal s t Reference. Id (Maybe (V1. Decl Symbol Ann )) -> s -> m t
333
- loadTypeDeclarationsByRefIdsOf codebase trav s =
299
+ loadV1TypeDeclarationsByRefIdsOf :: (QueryM m ) => CodebaseEnv -> Traversal s t Reference. Id (Maybe (V1. Decl Symbol Ann )) -> s -> m t
300
+ loadV1TypeDeclarationsByRefIdsOf codebase trav s =
334
301
s
335
302
& asListOf trav %%~ \ refIds -> do
336
303
DefnQ. loadDeclsByRefIdsOf codebase traversed refIds
@@ -343,7 +310,7 @@ expectTypeDeclarationsByRefIdsOf :: (QueryM m) => CodebaseEnv -> Traversal s t R
343
310
expectTypeDeclarationsByRefIdsOf codebase trav s = do
344
311
s
345
312
& asListOf trav %%~ \ refIds -> do
346
- results <- loadTypeDeclarationsByRefIdsOf codebase traversed refIds
313
+ results <- loadV1TypeDeclarationsByRefIdsOf codebase traversed refIds
347
314
for (zip refIds results) \ case
348
315
(refId, Nothing ) -> unrecoverableError (MissingDecl refId)
349
316
(_, Just decl) -> pure decl
@@ -398,7 +365,7 @@ loadTypesOfConstructorsOf codebase trav s =
398
365
refs <&> \ case
399
366
(Reference. Builtin _t, _conId) -> Left Nothing
400
367
(Reference. DerivedId refId, conId) -> Right ((refId, conId), refId)
401
- results <- loadTypeDeclarationsByRefIdsOf codebase (traversed . _Right . _2) partitionedRefs
368
+ results <- loadV1TypeDeclarationsByRefIdsOf codebase (traversed . _Right . _2) partitionedRefs
402
369
for results \ case
403
370
(Right ((refId, _conId), Nothing )) -> (unrecoverableError (MissingDecl refId))
404
371
(Right ((_refId, conId), (Just decl))) -> do
@@ -414,49 +381,6 @@ expectTypesOfConstructorsOf codebase trav s =
414
381
((ref, conId), Nothing ) -> unrecoverableError (MissingTypeForConstructor ref conId)
415
382
(_, Just r) -> pure r
416
383
417
- data CodeLookupCache = CodeLookupCache
418
- { termCache :: Map Reference. Id (V1. Term Symbol Ann , V1. Type Symbol Ann ),
419
- typeCache :: Map Reference. Id (V1. Decl Symbol Ann )
420
- }
421
-
422
- -- | TODO: The code lookup should either be batchified or we should preload the cache with
423
- -- everything we think we'll need.
424
- codeLookupForUser :: TVar CodeLookupCache -> CodebaseEnv -> CL. CodeLookup Symbol (PG. Transaction e ) Ann
425
- codeLookupForUser cacheVar codebaseOwner = do
426
- CL. CodeLookup (fmap (fmap fst ) . getTermAndType) (fmap (fmap snd ) . getTermAndType) getTypeDecl
427
- <> Builtin. codeLookup
428
- <> IOSource. codeLookupM
429
- where
430
- getTermAndType ::
431
- Reference. Id ->
432
- PG. Transaction e (Maybe (V1. Term Symbol Ann , V1. Type Symbol Ann ))
433
- getTermAndType r = do
434
- CodeLookupCache {termCache} <- PG. transactionUnsafeIO (readTVarIO cacheVar)
435
- case Map. lookup r termCache of
436
- Just termAndType -> pure (Just termAndType)
437
- Nothing -> do
438
- maybeTermAndType <- loadTermAndTypeByRefIdsOf codebaseOwner id r
439
- whenJust maybeTermAndType \ termAndType -> do
440
- PG. transactionUnsafeIO do
441
- atomically do
442
- modifyTVar' cacheVar \ CodeLookupCache {termCache, .. } ->
443
- CodeLookupCache {termCache = Map. insert r termAndType termCache, .. }
444
- pure maybeTermAndType
445
-
446
- getTypeDecl :: Reference. Id -> PG. Transaction e (Maybe (V1. Decl Symbol Ann ))
447
- getTypeDecl r = do
448
- CodeLookupCache {typeCache} <- PG. transactionUnsafeIO (readTVarIO cacheVar)
449
- case Map. lookup r typeCache of
450
- Just typ -> pure (Just typ)
451
- Nothing -> do
452
- maybeType <- loadTypeDeclarationsByRefIdsOf codebaseOwner id r
453
- whenJust maybeType \ typ ->
454
- PG. transactionUnsafeIO do
455
- atomically do
456
- modifyTVar' cacheVar \ CodeLookupCache {typeCache, .. } ->
457
- CodeLookupCache {typeCache = Map. insert r typ typeCache, .. }
458
- pure maybeType
459
-
460
384
-- | Look up the result of evaluating a term if we have it cached.
461
385
--
462
386
-- This is intentionally not in CodebaseM because it's used to build the CodebaseEnv.
0 commit comments