rishabhdaim commented on code in PR #3253:
URL: https://github.com/apache/jackrabbit-oak/pull/3253#discussion_r4494493631


##########
oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java:
##########
@@ -1718,26 +1721,43 @@ private int getOPV(Tree nodeTree, PropertyState 
property)
 
     }
 
-    private static class PropertyIteratorDelegate {
+    /**
+     * Shares lazy size calculation between filtered and unfiltered property 
iterators.
+     * A {@code null} predicate selects unfiltered iteration.
+     */
+    private class PropertyIteratorDelegate {
         private final NodeDelegate node;
         private final Predicate<PropertyDelegate> predicate;
+        private long size = -1;
+        private long updateCount = -1;
 
         PropertyIteratorDelegate(NodeDelegate node, 
Predicate<PropertyDelegate> predicate) {
             this.node = node;
             this.predicate = predicate;
         }
 
         public Iterator<PropertyDelegate> iterator() throws 
InvalidItemStateException {
-            return IteratorUtils.filter(node.getProperties(), predicate::test);
+            Iterator<PropertyDelegate> properties = node.getProperties();
+            return predicate == null ? properties : 
IteratorUtils.filter(properties, predicate::test);
         }
 
         public long getSize() {
-            try {
-                return IteratorUtils.size(iterator());
-            } catch (InvalidItemStateException e) {
-                throw new IllegalStateException(
-                        "This iterator is no longer valid", e);
-            }
+            return sessionDelegate.safePerform(new 
SessionOperation<Long>("getPropertyIteratorSize") {

Review Comment:
   ๐ŸŸก **P3** ยท โšก Performance โ€” optional allocation improvement
   
   **๐Ÿ” Issue:** Every `getSize()` call constructs a new `SessionOperation`, 
including calls that return the cached size.
   
   **๐Ÿ› ๏ธ Fix:** Reuse one operation per `PropertyIteratorDelegate`, keeping 
every invocation inside `safePerform` and retaining the update-count check 
inside `perform()` so refresh and synchronization still run.



##########
oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java:
##########
@@ -1718,26 +1721,43 @@ private int getOPV(Tree nodeTree, PropertyState 
property)
 
     }
 
-    private static class PropertyIteratorDelegate {
+    /**
+     * Shares lazy size calculation between filtered and unfiltered property 
iterators.
+     * A {@code null} predicate selects unfiltered iteration.
+     */
+    private class PropertyIteratorDelegate {
         private final NodeDelegate node;
         private final Predicate<PropertyDelegate> predicate;
+        private long size = -1;
+        private long updateCount = -1;
 
         PropertyIteratorDelegate(NodeDelegate node, 
Predicate<PropertyDelegate> predicate) {
             this.node = node;
             this.predicate = predicate;
         }
 
         public Iterator<PropertyDelegate> iterator() throws 
InvalidItemStateException {
-            return IteratorUtils.filter(node.getProperties(), predicate::test);
+            Iterator<PropertyDelegate> properties = node.getProperties();
+            return predicate == null ? properties : 
IteratorUtils.filter(properties, predicate::test);
         }
 
         public long getSize() {
-            try {
-                return IteratorUtils.size(iterator());
-            } catch (InvalidItemStateException e) {
-                throw new IllegalStateException(
-                        "This iterator is no longer valid", e);
-            }
+            return sessionDelegate.safePerform(new 
SessionOperation<Long>("getPropertyIteratorSize") {
+                @Override
+                public Long perform() {
+                    try {
+                        long currentUpdateCount = 
sessionDelegate.getUpdateCount();
+                        if (size == -1 || updateCount != currentUpdateCount) {

Review Comment:
   ๐ŸŸก **P3** ยท ๐ŸŽฏ Correctness โ€” refresh regression coverage
   
   **๐Ÿ” Issue:** The new cache is tested after local writes, but no size 
assertion retains a populated iterator cache across explicit or automatic 
reader refresh; the ACL test checks only `hasProperties()`.
   
   **๐Ÿ› ๏ธ Fix:** Add these two cases to `PropertyIterationTest` to check the same 
unfiltered and filtered iterators after permission/content changes:
   
   ```java
       /** Recounts cached sizes after an explicit refresh changes readable 
properties. */
       @Test
       public void cachedSizesAfterExplicitRefresh() throws Exception {
           Session reader = getAnonymousSession();
           NodeImpl<?> readable = (NodeImpl<?>) reader.getNode("/test");
           PropertyIterator[] iterators = {readable.getProperties(), 
readable.getProperties("tit*"),
                   readable.getProperties(new String[] {"tit*"})};
           for (int i = 0; i < iterators.length; i++) {
               assertEquals(i == 0 ? 2 : 1, iterators[i].getSize());
           }
   
           AccessControlUtils.addAccessControlEntry(session, "/test", 
EveryonePrincipal.getInstance(),
                   new String[] {PrivilegeConstants.REP_READ_PROPERTIES}, 
false);
           session.save();
           reader.refresh(false);
   
           for (PropertyIterator properties : iterators) {
               assertEquals(0, properties.getSize());
               assertEquals(0, properties.getSize());
               assertEquals(0, properties.getPosition());
           }
       }
   
       /** Recounts cached sizes when getSize performs a scheduled session 
refresh. */
       @Test
       public void cachedSizesAfterAutomaticRefresh() throws Exception {
           Session reader = getAnonymousSession();
           NodeImpl<?> readable = (NodeImpl<?>) reader.getNode("/test");
           PropertyIterator[] iterators = {readable.getProperties(), 
readable.getProperties("tit*"),
                   readable.getProperties(new String[] {"tit*"})};
           for (int i = 0; i < iterators.length; i++) {
               assertEquals(i == 0 ? 2 : 1, iterators[i].getSize());
           }
   
           source.setProperty("title2", "value");
           session.save();
           readable.sessionDelegate.refreshAtNextAccess();
   
           for (int i = 0; i < iterators.length; i++) {
               assertEquals(i == 0 ? 3 : 2, iterators[i].getSize());
               assertEquals(i == 0 ? 3 : 2, iterators[i].getSize());
               assertEquals(0, iterators[i].getPosition());
           }
       }
   ```
   
   Validated locally against `afeb5cd550aa6b75ad65d79084cd39930af05a0d` on Java 
17 with `mvn -B -pl oak-jcr -Dtest=PropertyIterationTest 
-Dnsfixtures=SEGMENT_TAR test`: **8 tests passed, 0 failures/errors/skips**, 
including these two added cases and the six PR tests.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to