From 75b2dea0674f047505b7e19f8f2a143611bc93a0 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 25 Feb 2004 13:11:13 +0000 Subject: [PATCH] Implement isParentOf() method that is similar to contains() but returns boolean true instead of the index position of the child node. The advantage is that we don't need to load the subnode index for relational nodes. We're using isParentOf() in getElementName() and getParent(). --- src/helma/objectmodel/db/Node.java | 46 +++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/helma/objectmodel/db/Node.java b/src/helma/objectmodel/db/Node.java index 530a107b..2bdcfce8 100644 --- a/src/helma/objectmodel/db/Node.java +++ b/src/helma/objectmodel/db/Node.java @@ -438,13 +438,13 @@ public final class Node implements INode, Serializable { if ((propvalue != null) && (propvalue.length() > 0)) { setName(propvalue); anonymous = false; - } else if (!anonymous && (p.contains(this) > -1)) { + } else if (!anonymous && p.isParentOf(this)) { anonymous = true; } } else { anonymous = true; } - } else if (!anonymous && (p.contains(this) > -1)) { + } else if (!anonymous && p.isParentOf(this)) { anonymous = true; } } catch (Exception ignore) { @@ -716,7 +716,7 @@ public final class Node implements INode, Serializable { if ((parentInfo != null) && (state != TRANSIENT)) { for (int i = 0; i < parentInfo.length; i++) { ParentInfo pinfo = parentInfo[i]; - INode pn = null; + Node pn = null; // see if there is an explicit relation defined for this parent info // we only try to fetch a node if an explicit relation is specified for the prop name @@ -724,7 +724,7 @@ public final class Node implements INode, Serializable { if ((rel != null) && (rel.reftype == Relation.REFERENCE || rel.reftype == Relation.COMPLEX_REFERENCE)) { - pn = getNode(pinfo.propname); + pn = (Node) getNode(pinfo.propname); } // the parent of this node is the app's root node... @@ -736,7 +736,7 @@ public final class Node implements INode, Serializable { if (pn != null) { // see if dbmapping specifies anonymity for this node if (pinfo.virtualname != null) { - pn = pn.getNode(pinfo.virtualname); + pn = (Node) pn.getNode(pinfo.virtualname); if (pn == null) System.err.println("Error: Can't retrieve parent "+ "node "+pinfo+" for "+this); @@ -748,11 +748,11 @@ public final class Node implements INode, Serializable { if ((dbm != null) && (dbm.getSubnodeGroupby() != null)) { // check for groupby rel = dbmap.columnNameToRelation(dbm.getSubnodeGroupby()); - pn = pn.getSubnode(getString(rel.propName)); + pn = (Node) pn.getSubnode(getString(rel.propName)); } - if (pn != null && pn.contains(this) >= 0) { - setParent((Node) pn); + if (pn != null && pn.isParentOf(this)) { + setParent(pn); lastParentSet = System.currentTimeMillis(); return pn; @@ -1316,11 +1316,12 @@ public final class Node implements INode, Serializable { } /** + * Check if the given node is contained in this node's child list. + * If it is contained return its index in the list, otherwise return -1. * + * @param n a node * - * @param n ... - * - * @return ... + * @return the node's index position in the child list, or -1 */ public int contains(INode n) { if (n == null) { @@ -1344,6 +1345,29 @@ public final class Node implements INode, Serializable { return subnodes.indexOf(node.getHandle()); } + /** + * Check if the given node is contained in this node's child list. This + * is similar to contains(INode) but does not load the + * child index for relational nodes. + * + * @param n a node + * @return true if the given node is contained in this node's child list + */ + public boolean isParentOf(Node n) { + if (dbmap != null) { + Relation subrel = dbmap.getSubnodeRelation(); + // if we're dealing with relational child nodes use + // Relation.checkConstraints to avoid loading the child index + if (subrel != null && + subrel.otherType != null && + subrel.otherType.isRelational()) { + return subrel.checkConstraints(this, n); + } + } + // just fall back to contains() for non-relational nodes + return contains(n) > -1; + } + /** * Count the subnodes of this node. If they're stored in a relational data source, we * may actually load their IDs in order to do this.