Skip to content

Commit ec16cce

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

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
@@ -972,9 +972,9 @@ protected NodeProxy getFirstChildProxy() {
972972
}
973973

974974
/**
975-
* The method <code>getFirstChildAddress</code>
975+
* Get the address of the first child.
976976
*
977-
* @return a <code>long</code> value
977+
* @return the address of the first child.
978978
*/
979979
@EnsureContainerLocked(mode=READ_LOCK)
980980
public long getFirstChildAddress() {
@@ -984,6 +984,37 @@ public long getFirstChildAddress() {
984984
return childAddress[0];
985985
}
986986

987+
@Override
988+
@EnsureContainerLocked(mode=READ_LOCK)
989+
public Node getLastChild() {
990+
if (children == 0) {
991+
return null;
992+
}
993+
try (final DBBroker broker = pool.getBroker()) {
994+
return broker.objectWith(getLastChildProxy());
995+
} catch (final EXistException e) {
996+
LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
997+
}
998+
return null;
999+
}
1000+
1001+
@EnsureContainerLocked(mode=READ_LOCK)
1002+
protected NodeProxy getLastChildProxy() {
1003+
return new NodeProxy(getExpression(), this, NodeId.ROOT_NODE, Node.ELEMENT_NODE, childAddress[children - 1]);
1004+
}
1005+
1006+
/**
1007+
* Get the address of the last child.
1008+
*
1009+
* @return the address of the last child.
1010+
*/
1011+
@EnsureContainerLocked(mode=READ_LOCK)
1012+
public long getLastChildAddress() {
1013+
if (children == 0) {
1014+
return StoredNode.UNKNOWN_NODE_IMPL_ADDRESS;
1015+
}
1016+
return childAddress[children - 1];
1017+
}
9871018

9881019
@Override
9891020
public boolean hasChildNodes() {

0 commit comments

Comments
 (0)