You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2021/01/22 15:19:19 UTC

[GitHub] [sling-org-apache-sling-graphql-core] raducotescu commented on a change in pull request #17: SLING-10085 - Cache the GraphQL schemas in the DefaultQueryExecutor

raducotescu commented on a change in pull request #17:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/17#discussion_r562703132



##########
File path: src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java
##########
@@ -289,4 +332,71 @@ private String getDirectiveArgumentValue(Directive d, String name) {
             throw up;
         }
     }
+
+    GraphQLSchema getSchema(@NotNull String sdl, @NotNull Resource currentResource, @NotNull String[] selectors) {
+        readLock.lock();
+        String newHash = SHA256Hasher.getHash(sdl);
+        String resourceToHashMapKey = getCacheKey(currentResource, selectors);
+        String oldHash = resourceToHashMap.get(resourceToHashMapKey);
+        if (!newHash.equals(oldHash)) {
+            readLock.unlock();
+            writeLock.lock();
+            try {
+                oldHash = resourceToHashMap.get(resourceToHashMapKey);
+                if (!newHash.equals(oldHash)) {
+                    resourceToHashMap.put(resourceToHashMapKey, newHash);
+                    TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
+                    Iterable<GraphQLScalarType> scalars = scalarsProvider.getCustomScalars(typeRegistry.scalars());
+                    RuntimeWiring runtimeWiring = buildWiring(typeRegistry, scalars, currentResource);
+                    SchemaGenerator schemaGenerator = new SchemaGenerator();
+                    hashToSchemaMap.put(newHash, schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring));
+                }
+                readLock.lock();
+            } finally {
+                writeLock.unlock();
+            }
+        }
+        try {
+            return hashToSchemaMap.get(newHash);
+        } finally {

Review comment:
       There's no clean way to unlock both of them in a single `finally`, or I haven't found the way to correctly write this. You need to make sure that no matter the failures, both locks are unlocked, but it has to happen in the correct sequence and the locks have to be unlocked by the same thread that locked them.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org