throws RepositoryException, IOException {
checkOpen();
final Map aggregateRoots = new HashMap();
final Set removedNodeIds = new HashSet();
final Set addedNodeIds = new HashSet();
index.update(new AbstractIteratorDecorator(remove) {
public Object next() {
NodeId nodeId = (NodeId) super.next();
removedNodeIds.add(nodeId);
return nodeId.getUUID();
}
}, new AbstractIteratorDecorator(add) {
public Object next() {
NodeState state = (NodeState) super.next();
if (state == null) {
return null;
}
addedNodeIds.add(state.getNodeId());
removedNodeIds.remove(state.getNodeId());
Document doc = null;
try {
doc = createDocument(state, getNamespaceMappings(),
index.getIndexFormatVersion());
retrieveAggregateRoot(state, aggregateRoots);
} catch (RepositoryException e) {
log.warn("Exception while creating document for node: "
+ state.getNodeId() + ": " + e.toString());
}
return doc;
}
});
// remove any aggregateRoot nodes that are new
// and therefore already up-to-date
aggregateRoots.keySet().removeAll(addedNodeIds);
// based on removed NodeIds get affected aggregate root nodes
retrieveAggregateRoot(removedNodeIds, aggregateRoots);
// update aggregates if there are any affected
if (aggregateRoots.size() > 0) {
index.update(new AbstractIteratorDecorator(
aggregateRoots.keySet().iterator()) {
public Object next() {
return ((NodeId) super.next()).getUUID();
}
}, new AbstractIteratorDecorator(aggregateRoots.values().iterator()) {
public Object next() {
NodeState state = (NodeState) super.next();
try {
return createDocument(state, getNamespaceMappings(),
index.getIndexFormatVersion());