diff --git a/python-client/src/main/scala/raw/client/python/PythonCompilerService.scala b/python-client/src/main/scala/raw/client/python/PythonCompilerService.scala index 5baa8be03..4e8bbea5b 100644 --- a/python-client/src/main/scala/raw/client/python/PythonCompilerService.scala +++ b/python-client/src/main/scala/raw/client/python/PythonCompilerService.scala @@ -261,7 +261,7 @@ class PythonCompilerService(engineDefinition: (Engine, Boolean))(implicit protec private def buildTruffleContext( environment: ProgramEnvironment, - maybeOutputStream: Option[OutputStream] = None + maybeOutputStream: Option[OutputStream] ): Context = { // Add environment settings as hardcoded environment variables. val ctxBuilder = Context diff --git a/snapi-frontend/src/main/java/module-info.java b/snapi-frontend/src/main/java/module-info.java index 3fc34f720..8f44366d5 100644 --- a/snapi-frontend/src/main/java/module-info.java +++ b/snapi-frontend/src/main/java/module-info.java @@ -60,5 +60,4 @@ opens raw.inferrer.api to com.fasterxml.jackson.databind; - } diff --git a/snapi-truffle/src/main/java/raw/runtime/truffle/RawLanguageCache.java b/snapi-truffle/src/main/java/raw/runtime/truffle/RawLanguageCache.java index 330eb4a2b..1aff4f202 100644 --- a/snapi-truffle/src/main/java/raw/runtime/truffle/RawLanguageCache.java +++ b/snapi-truffle/src/main/java/raw/runtime/truffle/RawLanguageCache.java @@ -13,11 +13,9 @@ package raw.runtime.truffle; import com.oracle.truffle.api.CompilerDirectives; - import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; - import raw.compiler.base.CompilerContext; import raw.creds.api.CredentialsService; import raw.creds.api.CredentialsServiceProvider; @@ -27,122 +25,116 @@ import raw.utils.AuthenticatedUser; import raw.utils.RawSettings; import raw.utils.RawUtils; -import scala.Some; import scala.runtime.BoxedUnit; public class RawLanguageCache { - private final ClassLoader classLoader = RawLanguage.class.getClassLoader(); - - private final Object activeContextsLock = new Object(); - private final Set activeContexts = new HashSet(); - - private final ConcurrentHashMap credentialsCache = - new ConcurrentHashMap<>(); - - private final ConcurrentHashMap map = new ConcurrentHashMap<>(); - - private static class Value { - private final CompilerContext compilerContext; - private final SourceContext sourceContext; - private final InferrerService inferrer; + private final ClassLoader classLoader = RawLanguage.class.getClassLoader(); - Value(CompilerContext compilerContext, SourceContext sourceContext, InferrerService inferrer) { - this.compilerContext = compilerContext; - this.sourceContext = sourceContext; - this.inferrer = inferrer; - } + private final Object activeContextsLock = new Object(); + private final Set activeContexts = new HashSet(); - public CompilerContext getCompilerContext() { - return compilerContext; - } + private final ConcurrentHashMap credentialsCache = + new ConcurrentHashMap<>(); - public SourceContext getSourceContext() { - return sourceContext; - } + private final ConcurrentHashMap map = new ConcurrentHashMap<>(); - public InferrerService getInferrer() { - return inferrer; - } - } + private static class Value { + private final CompilerContext compilerContext; + private final SourceContext sourceContext; + private final InferrerService inferrer; - @CompilerDirectives.TruffleBoundary - private Value get(AuthenticatedUser user, RawSettings rawSettings) { - // Create services on-demand. - CredentialsService credentialsService = - credentialsCache.computeIfAbsent( - rawSettings, k -> CredentialsServiceProvider.apply(rawSettings)); - return map.computeIfAbsent( - user, - k -> { - SourceContext sourceContext = new SourceContext(user, credentialsService, rawSettings); - InferrerService inferrer = InferrerServiceProvider.apply(sourceContext); - CompilerContext compilerContext = - new CompilerContext( - "rql2-truffle", - user, - inferrer, - sourceContext, - rawSettings); - return new Value(compilerContext, sourceContext, inferrer); - }); + Value(CompilerContext compilerContext, SourceContext sourceContext, InferrerService inferrer) { + this.compilerContext = compilerContext; + this.sourceContext = sourceContext; + this.inferrer = inferrer; } - public SourceContext getSourceContext(AuthenticatedUser user, RawSettings rawSettings) { - return get(user, rawSettings).getSourceContext(); + public CompilerContext getCompilerContext() { + return compilerContext; } - public CompilerContext getCompilerContext(AuthenticatedUser user, RawSettings rawSettings) { - return get(user, rawSettings).getCompilerContext(); + public SourceContext getSourceContext() { + return sourceContext; } - public InferrerService getInferrer(AuthenticatedUser user, RawSettings rawSettings) { - return get(user, rawSettings).getInferrer(); + public InferrerService getInferrer() { + return inferrer; } - - @CompilerDirectives.TruffleBoundary - public void incrementContext(RawContext context) { - synchronized (activeContextsLock) { - activeContexts.add(context); - } + } + + @CompilerDirectives.TruffleBoundary + private Value get(AuthenticatedUser user, RawSettings rawSettings) { + // Create services on-demand. + CredentialsService credentialsService = + credentialsCache.computeIfAbsent( + rawSettings, k -> CredentialsServiceProvider.apply(rawSettings)); + return map.computeIfAbsent( + user, + k -> { + SourceContext sourceContext = new SourceContext(user, credentialsService, rawSettings); + InferrerService inferrer = InferrerServiceProvider.apply(sourceContext); + CompilerContext compilerContext = + new CompilerContext("rql2-truffle", user, inferrer, sourceContext, rawSettings); + return new Value(compilerContext, sourceContext, inferrer); + }); + } + + public SourceContext getSourceContext(AuthenticatedUser user, RawSettings rawSettings) { + return get(user, rawSettings).getSourceContext(); + } + + public CompilerContext getCompilerContext(AuthenticatedUser user, RawSettings rawSettings) { + return get(user, rawSettings).getCompilerContext(); + } + + public InferrerService getInferrer(AuthenticatedUser user, RawSettings rawSettings) { + return get(user, rawSettings).getInferrer(); + } + + @CompilerDirectives.TruffleBoundary + public void incrementContext(RawContext context) { + synchronized (activeContextsLock) { + activeContexts.add(context); } - - @CompilerDirectives.TruffleBoundary - public void releaseContext(RawContext context) { - synchronized (activeContextsLock) { - activeContexts.remove(context); - if (activeContexts.isEmpty()) { - // Close all inferrer services and credential services. - map.values() - .forEach( - v -> { - RawUtils.withSuppressNonFatalException( - () -> { - v.getInferrer().stop(); - return BoxedUnit.UNIT; - }, - true); - RawUtils.withSuppressNonFatalException( - () -> { - v.getSourceContext().credentialsService().stop(); - return BoxedUnit.UNIT; - }, - true); - }); - map.clear(); - credentialsCache - .values() - .forEach( - v -> { - RawUtils.withSuppressNonFatalException( - () -> { - v.stop(); - return BoxedUnit.UNIT; - }, - true); - }); - credentialsCache.clear(); - } - } + } + + @CompilerDirectives.TruffleBoundary + public void releaseContext(RawContext context) { + synchronized (activeContextsLock) { + activeContexts.remove(context); + if (activeContexts.isEmpty()) { + // Close all inferrer services and credential services. + map.values() + .forEach( + v -> { + RawUtils.withSuppressNonFatalException( + () -> { + v.getInferrer().stop(); + return BoxedUnit.UNIT; + }, + true); + RawUtils.withSuppressNonFatalException( + () -> { + v.getSourceContext().credentialsService().stop(); + return BoxedUnit.UNIT; + }, + true); + }); + map.clear(); + credentialsCache + .values() + .forEach( + v -> { + RawUtils.withSuppressNonFatalException( + () -> { + v.stop(); + return BoxedUnit.UNIT; + }, + true); + }); + credentialsCache.clear(); + } } + } }