1

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.

Rohit Tiwari
  • 58
  • 1
  • 10
  • Please add the stacktrace and code. You may also have a look at [How to Ask](http://stackoverflow.com/help/how-to-ask) to improve the question. Welcome to SO! – Debosmit Ray Mar 12 '16 at 06:05
  • Size as file size or number of documents inside? – d33t Mar 12 '16 at 15:54
  • Size as file size not number of documents. Sorry for less description. @DebosmitRay thanks for advice, I'll put the stack trace also. – Rohit Tiwari Mar 14 '16 at 06:24

1 Answers1

3

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

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
  • Thanks for reply. Definetly it will help in finding size of single node. But I want a way to find out total size of a node if that node contains several other node with binary property. for eg. there is node A which is not jcr:content type. A have a child node B which is also not jcr:content type. B have 3 nodes X, Y, Z which have binary data of different size. So is there any way to find Node A size including its child nodes also. I don't want to iterate and sum up sizes of all files one by one. Thanks. – Rohit Tiwari Mar 14 '16 at 07:40
  • 2
    You have to iterate, the node will not aggregate the values by itself. Iterating is usually very efficient with JCR repositories. – Bertrand Delacretaz Mar 14 '16 at 12:08