Search in sources :

Example 1 with EventImpl

use of org.apache.jackrabbit.core.observation.EventImpl in project jackrabbit by apache.

the class SearchManager method onEvent.

//---------------< EventListener interface >--------------------------------
public void onEvent(EventIterator events) {
    log.debug("onEvent: indexing started");
    long time = System.currentTimeMillis();
    // nodes that need to be removed from the index.
    final Set<NodeId> removedNodes = new HashSet<NodeId>();
    // nodes that need to be added to the index.
    final Map<NodeId, EventImpl> addedNodes = new HashMap<NodeId, EventImpl>();
    // property events
    List<EventImpl> propEvents = new ArrayList<EventImpl>();
    while (events.hasNext()) {
        EventImpl e = (EventImpl) events.nextEvent();
        if (!isExcluded(e)) {
            long type = e.getType();
            if (type == Event.NODE_ADDED) {
                addedNodes.put(e.getChildId(), e);
                if (e.isShareableChildNode()) {
                    // simply re-index shareable nodes
                    removedNodes.add(e.getChildId());
                }
            } else if (type == Event.NODE_REMOVED) {
                removedNodes.add(e.getChildId());
                if (e.isShareableChildNode()) {
                    // check if there is a node remaining in the shared set
                    if (itemMgr.hasItemState(e.getChildId())) {
                        addedNodes.put(e.getChildId(), e);
                    }
                }
            } else {
                propEvents.add(e);
            }
        }
    }
    // sort out property events
    for (EventImpl e : propEvents) {
        NodeId nodeId = e.getParentId();
        if (e.getType() == Event.PROPERTY_ADDED) {
            if (addedNodes.put(nodeId, e) == null) {
                // only property added
                // need to re-index
                removedNodes.add(nodeId);
            } else {
            // the node where this prop belongs to is also new
            }
        } else if (e.getType() == Event.PROPERTY_CHANGED) {
            // need to re-index
            addedNodes.put(nodeId, e);
            removedNodes.add(nodeId);
        } else {
            // property removed event is only generated when node still exists
            addedNodes.put(nodeId, e);
            removedNodes.add(nodeId);
        }
    }
    Iterator<NodeState> addedStates = new Iterator<NodeState>() {

        private final Iterator<NodeId> iter = addedNodes.keySet().iterator();

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public boolean hasNext() {
            return iter.hasNext();
        }

        public NodeState next() {
            NodeState item = null;
            NodeId id = (NodeId) iter.next();
            try {
                item = (NodeState) itemMgr.getItemState(id);
            } catch (ItemStateException ise) {
                // check whether this item state change originated from
                // an external event
                EventImpl e = addedNodes.get(id);
                if (e == null || !e.isExternal()) {
                    log.error("Unable to index node " + id + ": does not exist");
                } else {
                    log.info("Node no longer available " + id + ", skipped.");
                }
            }
            return item;
        }
    };
    Iterator<NodeId> removedIds = removedNodes.iterator();
    if (removedNodes.size() > 0 || addedNodes.size() > 0) {
        try {
            handler.updateNodes(removedIds, addedStates);
        } catch (RepositoryException e) {
            log.error("Error indexing node.", e);
        } catch (IOException e) {
            log.error("Error indexing node.", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("onEvent: indexing finished in " + String.valueOf(System.currentTimeMillis() - time) + " ms.");
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EventImpl(org.apache.jackrabbit.core.observation.EventImpl) NodeId(org.apache.jackrabbit.core.id.NodeId) EventIterator(javax.jcr.observation.EventIterator) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 2 with EventImpl

use of org.apache.jackrabbit.core.observation.EventImpl in project jackrabbit by apache.

the class LockManagerImpl method consolidateEvents.

/**
     * Consolidate an event iterator obtained from observation, merging
     * add and remove operations on nodes with the same UUID into a move
     * operation.
     */
@SuppressWarnings("unchecked")
private Iterator<HierarchyEvent> consolidateEvents(EventIterator events) {
    LinkedMap eventMap = new LinkedMap();
    while (events.hasNext()) {
        EventImpl event = (EventImpl) events.nextEvent();
        HierarchyEvent he;
        try {
            he = new HierarchyEvent(event.getChildId(), sysSession.getQPath(event.getPath()).getNormalizedPath(), event.getType());
        } catch (MalformedPathException e) {
            log.info("Unable to get event's path: " + e.getMessage());
            continue;
        } catch (RepositoryException e) {
            log.info("Unable to get event's path: " + e.getMessage());
            continue;
        }
        HierarchyEvent heExisting = (HierarchyEvent) eventMap.get(he.id);
        if (heExisting != null) {
            heExisting.merge(he);
        } else {
            eventMap.put(he.id, he);
        }
    }
    return eventMap.values().iterator();
}
Also used : EventImpl(org.apache.jackrabbit.core.observation.EventImpl) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) RepositoryException(javax.jcr.RepositoryException) LinkedMap(org.apache.commons.collections.map.LinkedMap)

Aggregations

RepositoryException (javax.jcr.RepositoryException)2 EventImpl (org.apache.jackrabbit.core.observation.EventImpl)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 EventIterator (javax.jcr.observation.EventIterator)1 LinkedMap (org.apache.commons.collections.map.LinkedMap)1 NodeId (org.apache.jackrabbit.core.id.NodeId)1 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)1 NodeState (org.apache.jackrabbit.core.state.NodeState)1 MalformedPathException (org.apache.jackrabbit.spi.commons.conversion.MalformedPathException)1