You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by jo...@apache.org on 2025/08/02 15:19:12 UTC

(jackrabbit-filevault) 01/01: JCRVLT-813 read node and property definitions lazy

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch JCRVLT-813
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit 77b701efa96c6c600748ac46da7f2395c19b6eda
Author: Joerg Hoh <jh...@adobe.com>
AuthorDate: Sat Aug 2 17:18:48 2025 +0200

    JCRVLT-813 read node and property definitions lazy
---
 .../jackrabbit/vault/util/EffectiveNodeType.java   | 16 +++---
 .../org/apache/jackrabbit/vault/util/Lazy.java     | 55 ++++++++++++++++++++
 .../apache/jackrabbit/vault/util/package-info.java |  2 +-
 .../org/apache/jackrabbit/vault/util/LazyTest.java | 58 ++++++++++++++++++++++
 4 files changed, 122 insertions(+), 9 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
index a130ca03..5c850907 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
@@ -51,8 +51,8 @@ import org.jetbrains.annotations.Nullable;
  */
 public final class EffectiveNodeType {
 
-    List<PropertyDefinition> propertyDefinitions;
-    List<NodeDefinition> nodeDefinitions;
+    Lazy<List<PropertyDefinition>> propertyDefinitions;
+    Lazy<List<NodeDefinition>> nodeDefinitions;
 
     public static @NotNull EffectiveNodeType ofNode(@NotNull Node node) throws RepositoryException {
         return ofPrimaryTypeAndMixins(node.getPrimaryNodeType(), node.getMixinNodeTypes());
@@ -71,8 +71,8 @@ public final class EffectiveNodeType {
 
     private EffectiveNodeType(@NotNull List<NodeType> nodeTypes) {
         this.nodeTypes = nodeTypes;
-        propertyDefinitions = nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getPropertyDefinitions())).collect(Collectors.toList());
-        nodeDefinitions = nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getChildNodeDefinitions())).collect(Collectors.toList());
+        propertyDefinitions = Lazy.of(() -> nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getPropertyDefinitions())).collect(Collectors.toList()));
+        nodeDefinitions = Lazy.of(() -> nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getChildNodeDefinitions())).collect(Collectors.toList()));
     }
 
     @Override
@@ -114,10 +114,10 @@ public final class EffectiveNodeType {
 
     public Optional<PropertyDefinition> getApplicablePropertyDefinition(Predicate<PropertyDefinition> predicate, @NotNull String name) {
         // first named then unnamed
-        Optional<PropertyDefinition> namedPropertyDef = EffectiveNodeType.<PropertyDefinition>getApplicableItemDefinition(propertyDefinitions, predicate, name);
+        Optional<PropertyDefinition> namedPropertyDef = EffectiveNodeType.<PropertyDefinition>getApplicableItemDefinition(propertyDefinitions.get(), predicate, name);
         if (!namedPropertyDef.isPresent()) {
             // then unnamed
-            return EffectiveNodeType.<PropertyDefinition>getApplicableItemDefinition(propertyDefinitions, predicate, null);
+            return EffectiveNodeType.<PropertyDefinition>getApplicableItemDefinition(propertyDefinitions.get(), predicate, null);
         } else {
             return namedPropertyDef;
         }
@@ -136,10 +136,10 @@ public final class EffectiveNodeType {
 
     public Optional<NodeDefinition> getApplicableChildNodeDefinition(@NotNull Predicate<NodeDefinition> predicate, @NotNull String name) {
         // first named then unnamed
-        Optional<NodeDefinition> namedNodeDef = EffectiveNodeType.<NodeDefinition>getApplicableItemDefinition(nodeDefinitions, predicate, name);
+        Optional<NodeDefinition> namedNodeDef = EffectiveNodeType.<NodeDefinition>getApplicableItemDefinition(nodeDefinitions.get(), predicate, name);
         if (!namedNodeDef.isPresent()) {
             // then unnamed
-            return EffectiveNodeType.<NodeDefinition>getApplicableItemDefinition(nodeDefinitions, predicate, null);
+            return EffectiveNodeType.<NodeDefinition>getApplicableItemDefinition(nodeDefinitions.get(), predicate, null);
         } else {
             return namedNodeDef;
         }
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/Lazy.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/Lazy.java
new file mode 100644
index 00000000..ce395a40
--- /dev/null
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/Lazy.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.vault.util;
+
+import java.util.Optional;
+import java.util.function.Supplier;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * A simple utility to resolve values lazy
+ * @param <T>
+ */
+
+public class Lazy<T> {
+
+    T resolvedValue = null;
+    Supplier<T> resolver;
+    
+    private Lazy (Supplier<T> supplier) {
+        resolver = supplier;
+    }
+    
+    public static<T> Lazy<T> of(@NotNull Supplier<T> supplier) {
+        return new Lazy<T>(supplier);
+    }
+
+    /**
+     * Resolve the value if necessary and return it; if the resolved value is non-null, it
+     * won't be resolved twice, but just reused.
+     * @return the resolved value (can be null);
+     */
+    public @Nullable T get() {
+        if (resolvedValue == null) {
+            resolvedValue = resolver.get();
+        }
+        return resolvedValue;
+    }
+
+}
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/package-info.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/package-info.java
index d387b718..4bf90e5d 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/package-info.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/package-info.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-@Version("2.11.0")
+@Version("2.12.0")
 package org.apache.jackrabbit.vault.util;
 
 import org.osgi.annotation.versioning.Version;
\ No newline at end of file
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/util/LazyTest.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/LazyTest.java
new file mode 100644
index 00000000..f820807e
--- /dev/null
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/LazyTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.vault.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.util.function.Supplier;
+
+import org.junit.Test;
+import org.mockito.AdditionalAnswers;
+import org.mockito.Mockito;
+
+public class LazyTest {
+
+    @Test
+    public void testLazyResolution() {
+        Supplier<String> s = spyLambda(() -> "test");
+        Lazy<String> lazy = Lazy.of(s);
+        verify(s, times(0)).get();
+        assertEquals("test",lazy.get());
+        // ensure that it was resolved just once
+        assertEquals("test",lazy.get());
+        verify(s,times(1)).get();
+    }
+
+    @Test
+    public void testLazyResolutionWithNull() {
+        Supplier<Object> s = spyLambda(() -> null);
+        Lazy<Object> lazy = Lazy.of(s);
+        assertEquals(null,lazy.get());
+        assertEquals(null,lazy.get());
+        // resolution is done twice, as null is returned
+        verify(s,times(2)).get(); 
+    }
+
+    // Spying on lambdas is not possible; see https://stackoverflow.com/questions/54328867/spying-a-lambda-with-mockito
+    @SuppressWarnings("unchecked")
+    static <T, P extends T> P spyLambda (P lambda) {
+        Class<?>[] interfaces = lambda.getClass().getInterfaces();
+        return (P) Mockito.mock((Class<T>) interfaces[0], AdditionalAnswers.delegatesTo(lambda));
+    }
+}