Skip to content

Commit d58a361

Browse files
committed
[feature] Add missing implementations of Node#getLastChild() to persistent an in-memory DOM documents
1 parent 304856f commit d58a361

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,17 @@ public Node getFirstChild() {
660660

661661
@Override
662662
public Node getLastChild() {
663-
return getFirstChild();
663+
if (size > 1) {
664+
int nodeNum = 1;
665+
for (; nodeNum < size; nodeNum++) {
666+
if (treeLevel[nodeNum] > 1) {
667+
nodeNum--;
668+
break;
669+
}
670+
}
671+
return getNode(nodeNum);
672+
}
673+
return null;
664674
}
665675

666676
public int getAttributesCountFor(final int nodeNumber) {

exist-core/src/main/java/org/exist/dom/persistent/DocumentImpl.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,9 +1046,9 @@ protected NodeProxy getFirstChildProxy() {
10461046
}
10471047

10481048
/**
1049-
* The method <code>getFirstChildAddress</code>
1049+
* Get the address of the first child.
10501050
*
1051-
* @return a <code>long</code> value
1051+
* @return the address of the first child.
10521052
*/
10531053
@EnsureContainerLocked(mode=READ_LOCK)
10541054
public long getFirstChildAddress() {
@@ -1058,6 +1058,37 @@ public long getFirstChildAddress() {
10581058
return childAddress[0];
10591059
}
10601060

1061+
@Override
1062+
@EnsureContainerLocked(mode=READ_LOCK)
1063+
public Node getLastChild() {
1064+
if (children == 0) {
1065+
return null;
1066+
}
1067+
try (final DBBroker broker = pool.getBroker()) {
1068+
return broker.objectWith(getLastChildProxy());
1069+
} catch (final EXistException e) {
1070+
LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
1071+
}
1072+
return null;
1073+
}
1074+
1075+
@EnsureContainerLocked(mode=READ_LOCK)
1076+
protected NodeProxy getLastChildProxy() {
1077+
return new NodeProxy(getExpression(), this, NodeId.ROOT_NODE, Node.ELEMENT_NODE, childAddress[children - 1]);
1078+
}
1079+
1080+
/**
1081+
* Get the address of the last child.
1082+
*
1083+
* @return the address of the last child.
1084+
*/
1085+
@EnsureContainerLocked(mode=READ_LOCK)
1086+
public long getLastChildAddress() {
1087+
if (children == 0) {
1088+
return StoredNode.UNKNOWN_NODE_IMPL_ADDRESS;
1089+
}
1090+
return childAddress[children - 1];
1091+
}
10611092

10621093
@Override
10631094
public boolean hasChildNodes() {

0 commit comments

Comments
 (0)