jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bart van der Schans <b.vandersch...@onehippo.com>
Subject Re: hasProperty vs getProperty
Date Tue, 12 Jun 2012 14:40:43 GMT
Hi,

On Tue, Jun 12, 2012 at 3:01 PM, Julian Reschke <julian.reschke@gmx.de> wrote:
> Hi there,
>
> seen in SLING (JcrNodeResource.java):
>
> // check stuff for nt:resource (or similar) nodes
> if (node.hasProperty(JCR_MIMETYPE)) {
>    metadata.setContentType(node.getProperty(JCR_MIMETYPE).getString());
> }
>
> if (node.hasProperty(JCR_ENCODING)) {
>
> metadata.setCharacterEncoding(node.getProperty(JCR_ENCODING).getString());
> }
>
> if (node.hasProperty(JCR_LASTMODIFIED)) {
>    // We don't check node type, so JCR_LASTMODIFIED might not be a long
>    final Property prop = node.getProperty(JCR_LASTMODIFIED);
>    try {
>        metadata.setModificationTime(prop.getLong());
>    } catch(ValueFormatException vfe) {
>        LOGGER.debug("Property {} cannot be converted to a long, ignored
> ({})",
>                prop.getPath(), vfe);
>    }
> }
>
> if (node.hasProperty(JCR_DATA)) {
>    final Property prop = node.getProperty(JCR_DATA);
>    try {
>        metadata.setContentLength(prop.getLength());
>    } catch (ValueFormatException vfe) {
>        LOGGER.debug(
>            "Length of Property {} cannot be retrieved, ignored ({})",
>            prop.getPath(), vfe);
>    }
> }
>
> So instead of just fetching the property, it checks for the presence first.
> This seems like a waste of time to me.
>
> Is this just to avoid catching exceptions? (keep in mind that the exception
> might be thrown anyway...)
>
> If the concern is that fetching properties that "most of the time" do not
> exist would be slow due to exceptions being constructed, then it might be
> worthwhile using
>
>  Node.getProperties(namepattern)
>
> (which we then could optimized in jackrabbit-core).

It's a quite common pattern. Once the node is loaded in the
BundleCache (or an item/session cache) the hasProperty and getProperty
calls are not that expensive. A call to the persistence layer to fetch
the node data is much more expensive. If it is a performance hit in
your case, do you have some profiler logs or benchmarks?

Regards,
Bart

Mime
View raw message