How to Calculate the Size of a Folder in JCR (Java Content Repository)?

For this specific need the goal is to calculate the size of all the documents stored inside a specific location in the content repository. This could be used for example to manage quotas, estimate the size of a shared or personal storage, … For this specific sample I will only take in consideration the size of the binary part of the document stored in the repository; this means I will not pay attention to the various attributes and meta-data that are also stored, neither the full text index created by Lucene that is embedded in eXo JCR.

How the files are stored in the JCR?

Files are stored in eXo JCR in the standard node type nt:file (and nt:resource). So for this example I will simply list all the nt:file of a folder and aggregate the size of the file itself. It is important to understand how JCR is storing the binary content. The best way to understand it, is to view it. For that I am using the print information given by CRaSH, a shell for content repository developed by eXo team and lead by Julien Viet.

As you can see in this node the ‘binary’ is not visible, nothing bad here. As written in the specification in the section 6.7.22.6 nt:file, the binary content is an attribute of the child node jcr:content that is exposed below:

You can see now that the jcr:content contains some interesting attributes:

jcr:mimeType

jcr:data this is where the binary content, the PDF itself , is.
So using the JCR API you just need to get the content length using the following java code:

1

node.getProperty("jcr:content/jcr:data").getLength()

This returns the number of bits of the binary data.

So to calculate the size of a folder you just need to navigate in all the documents (nt:file or jcr:content) and cumulate the size of all the files. In this following code, I am calculating the size of the folder “/Documents” by navigating into all the files contains in this folder and subfolders. (I could have chose to query all the jcr:content type instead of nt:file)

123456789101112

Sessionsession=getSession();// use RepositoryService or context to get a sessionQueryManagermanager=session.getWorkspace().getQueryManager();StringqueryStatement="select * from nt:file where (jcr:path LIKE '/Documents/%')";Queryquery=manager.createQuery(queryStatement,Query.SQL);NodeIteratornodeIterator=query.execute().getNodes();Nodenode=null;longtotalSizeInMb=0;while(nodeIterator.hasNext()){node=nodeIterator.next();totalSizeInMb=totalSizeInMb+node.getProperty("jcr:content/jcr:data").getLength()/(1024*1024);}

As you can guess since we are navigating in the hierarchy you have to be very careful when using such query. This example is just a simple code sample to show you some of the cool features provided by the JCR API.