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/09/04 12:18:29 UTC

(jackrabbit-filevault) branch master updated: JCRVLT-813 read node and property definitions lazy (#380)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 84c91249 JCRVLT-813 read node and property definitions lazy (#380)
84c91249 is described below

commit 84c912494a8efcfec5d8175a5045e4976be80ebb
Author: Jörg Hoh <jo...@users.noreply.github.com>
AuthorDate: Thu Sep 4 14:18:24 2025 +0200

    JCRVLT-813 read node and property definitions lazy (#380)
    
    * JCRVLT-813 read node and property definitions lazy
    
    Co-authored-by: Konrad Windszus <kw...@apache.org>
---
 .../jackrabbit/vault/util/CachingSupplier.java     | 54 ++++++++++++++++++++
 .../jackrabbit/vault/util/EffectiveNodeType.java   | 16 +++---
 .../apache/jackrabbit/vault/util/package-info.java |  2 +-
 .../jackrabbit/vault/util/CachingSupplierTest.java | 58 ++++++++++++++++++++++
 4 files changed, 121 insertions(+), 9 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/CachingSupplier.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/CachingSupplier.java
new file mode 100644
index 00000000..6f3fe053
--- /dev/null
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/CachingSupplier.java
@@ -0,0 +1,54 @@
+/*
+ * 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.function.Supplier;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+A supplier wrapper which caches the supplied values. This class is not threadsafe!
+ * @param <T>
+ * @since FileVault 4.0.2
+ */
+
+public class CachingSupplier<T> implements Supplier<T>{
+
+    T resolvedValue = null;
+    Supplier<T> delegate;
+    
+    private CachingSupplier (Supplier<T> supplier) {
+        delegate = supplier;
+    }
+    
+    public static<T> CachingSupplier<T> of(@NotNull Supplier<T> supplier) {
+        return new CachingSupplier<T>(supplier);
+    }
+
+    /**
+     * Uses the cached value from previous get() calls instead of contacting the underlying delegate
+     * @return the resolved value (can be null);
+     */
+    public @Nullable T get() {
+        if (resolvedValue == null) {
+            resolvedValue = delegate.get();
+        }
+        return resolvedValue;
+    }
+
+}
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..9d2b23be 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;
+    CachingSupplier<List<PropertyDefinition>> propertyDefinitions;
+    CachingSupplier<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 = CachingSupplier.of(() -> nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getPropertyDefinitions())).collect(Collectors.toList()));
+        nodeDefinitions = CachingSupplier.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/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/CachingSupplierTest.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/CachingSupplierTest.java
new file mode 100644
index 00000000..68380408
--- /dev/null
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/CachingSupplierTest.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 CachingSupplierTest {
+
+    @Test
+    public void testLazyResolution() {
+        Supplier<String> s = spyLambda(() -> "test");
+        CachingSupplier<String> lazy = CachingSupplier.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);
+        CachingSupplier<Object> lazy = CachingSupplier.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));
+    }
+}