jcrjackrabbitjcr-sql2

how to find size of a jcr node including its childnodes?


I am using Jackrabbit 2.8.0 and JCR 2.0, I am developing a project for a organization so that it's users can store their documents and directories in digital format using their user account. I want to give size quota limit to all users. For keeping track of size, I am trying to get size of any JCR Node including its child nodes. Any kind of help would be highly appreciable. Thanks in advance

//jcr session after login to repository with user credentials            
Session jcrsession = sessions.getSession();
Node root = jcrsession.getRootNode();

//added a file type node to root            
file = root.addNode(fileName, Config.EDMS_DOCUMENT);
file.setProperty(Config.EDMS_SIZE, filesize);
InputStream isss= new ByteArrayInputStream(decodedBaseData);
Binary myBinary = valueFactory.createBinary(isss);
file.addMixin("mix:referenceable");

//added a node which will contain binary file           
Node resNode = file.addNode(Config.EDMS_CONTENT,"edms:resource");
    try {
          //added binary data to node
          resNode.setProperty("jcr:data",myBinary);
        } catch (IOException e) {
                e.printStackTrace();
        }finally{
                myBinary.dispose();
                isss.close();
        }
jcrsession.save();

this is how I add document type nodes and now I want to get size (in KB) of a node which may have several child nodes either of folder type or file type.


Solution

  • The JCR Property.getLength() method is specified at [1] to return the length of a binary property in bytes, so if you have your resNode as above, resNode.getProperty("jcr:data").getLength() should return the binary size.

    You can then walk the tree with Node.getNodes() to recursively compute the total size.

    Note that getLength() can return -1 if the implementation cannot determine the length, so you'll have to check for any possible constraints with the JCR implementation and file store implementation that you are using.

    [1] https://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/Property.html#getLength%28%29