[ 
https://issues.apache.org/jira/browse/OAK-9332?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17293729#comment-17293729
 ] 

Miroslav Smiljanic commented on OAK-9332:
-----------------------------------------

Hi [~angela], thanks for your suggestions! Regarding tree traversal, do you 
think section below can be added to "Does and Don'ts"?

*Tree traversal*

As explained in [Understanding the node state 
model|https://jackrabbit.apache.org/oak/docs/architecture/nodestate.html], Oak 
stores content in a tree hierarchy. Considering that, when traversing the path 
to access parent or child nodes, even though being equivalent operations, it is 
preferable to use JCR Node API instead of Session API. The reason being is that 
session API uses an absolute path, and to get to the desired parent or child 
node, all ancestor nodes will have to be traversed before reaching the target 
node. Traversal for each ancestor node includes building the node state and 
associating it with TreePermission (more details is [Permission Evaluation in 
Detail|https://jackrabbit.apache.org/oak/docs/security/permission/evaluation.html]),
 where this is not needed when using Node API and relative paths. 
{code:java}
Node c = session.getNode("/a/b/c"); 
Node d = null; 

// get child node 
d = session.getNode("/a/b/c/d"); 
d = c.getNode("d");                             // preferred way to fetch the 
child node

// get parent node 
c = session.getNode("/a/b/c");
c = d.getParent();                              // preferred way to fetch the 
parent node
{code}

> Document best practices and anti-patterns in repository tree traversal  
> ------------------------------------------------------------------------
>
>                 Key: OAK-9332
>                 URL: https://issues.apache.org/jira/browse/OAK-9332
>             Project: Jackrabbit Oak
>          Issue Type: Improvement
>          Components: doc
>            Reporter: Miroslav Smiljanic
>            Priority: Major
>
> When using JCR API, there is more than one way to perform tree/path traversal:
> {code:java}
> Node c = session.getNode("/a/b/c");
> Node d = null;
> //get child node
> d = session.getNode("/a/b/c/d");
> d = c.getNode("d");
> // get parent node
> c = d.getParent();
> c = session.getNode("/a/b/c");
> {code}
> To traverse a path using Node API with  performs better compared to Session 
> API. 
> {noformat}
> > java -jar target/oak-benchmarks-*-SNAPSHOT.jar benchmark  
> > GetParentNodeWithNodeAPI  GetParentNodeWithSessionAPI  Oak-Segment-Tar
> Apache Jackrabbit Oak 1.37-SNAPSHOT
> # GetParentNodeWithNodeAPI         C     min     10%     50%     90%     max  
>    N       mean 
> Oak-Segment-Tar                    1       2       2       2       3      5   
> 25891       2
> # GetParentNodeWithSessionAP       C     min     10%     50%     90%     max  
>    N       mean 
> Oak-Segment-Tar                    1      26      27      29      32     40   
>  2069      29{noformat}
> Example where Session API is used: 
> https://issues.apache.org/jira/browse/SLING-10011
> Considering Oak implementation details (tree repository structure, ACL 
> evaluation, ...) the best practice for path traversal should be explicitly 
> documented.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to