You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2009/05/22 16:19:43 UTC

svn commit: r777541 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene: MultiIndex.java SearchIndex.java

Author: jukka
Date: Fri May 22 14:19:42 2009
New Revision: 777541

URL: http://svn.apache.org/viewvc?rev=777541&view=rev
Log:
JCR-2087: Upgrade to Java 5 as the base platform

Generify MultiIndex.update()

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndex.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndex.java?rev=777541&r1=777540&r2=777541&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndex.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndex.java Fri May 22 14:19:42 2009
@@ -400,7 +400,9 @@
      *               indicate that a node could not be indexed successfully.
      * @throws IOException if an error occurs while updating the index.
      */
-    synchronized void update(Collection remove, Collection add) throws IOException {
+    synchronized void update(
+            Collection<UUID> remove, Collection<Document> add)
+            throws IOException {
         // make sure a reader is available during long updates
         if (add.size() > handler.getBufferSize()) {
             getIndexReader().release();
@@ -414,13 +416,14 @@
             executeAndLog(new Start(transactionId));
 
             boolean flush = false;
-            for (Iterator it = remove.iterator(); it.hasNext(); ) {
-                executeAndLog(new DeleteNode(transactionId, (UUID) it.next()));
+
+            for (UUID uuid : remove) {
+                executeAndLog(new DeleteNode(transactionId, uuid));
             }
-            for (Iterator it = add.iterator(); it.hasNext(); ) {
-                Document doc = (Document) it.next();
-                if (doc != null) {
-                    executeAndLog(new AddNode(transactionId, doc));
+
+            for (Document document : add) {
+                if (document != null) {
+                    executeAndLog(new AddNode(transactionId, document));
                     // commit volatile index if needed
                     flush |= checkVolatileCommit();
                 }
@@ -448,7 +451,8 @@
      *                     index.
      */
     void addDocument(Document doc) throws IOException {
-        update(Collections.EMPTY_LIST, Arrays.asList(new Document[]{doc}));
+        Collection<UUID> empty = Collections.emptyList();
+        update(empty, Collections.singleton(doc));
     }
 
     /**
@@ -458,7 +462,8 @@
      * @throws IOException if an error occurs while deleting the document.
      */
     void removeDocument(UUID uuid) throws IOException {
-        update(Arrays.asList(new UUID[]{uuid}), Collections.EMPTY_LIST);
+        Collection<Document> empty = Collections.emptyList();
+        update(Collections.singleton(uuid), empty);
     }
 
     /**
@@ -1183,11 +1188,10 @@
      *                           the indexing queue to the index.
      */
     private void checkIndexingQueue(boolean transactionPresent) {
-        Document[] docs = indexingQueue.getFinishedDocuments();
-        Map finished = new HashMap();
-        for (int i = 0; i < docs.length; i++) {
-            String uuid = docs[i].get(FieldNames.UUID);
-            finished.put(UUID.fromString(uuid), docs[i]);
+        Map<UUID, Document> finished = new HashMap<UUID, Document>();
+        for (Document document : indexingQueue.getFinishedDocuments()) {
+            UUID uuid = UUID.fromString(document.get(FieldNames.UUID));
+            finished.put(uuid, document);
         }
 
         // now update index with the remaining ones if there are any
@@ -1196,18 +1200,17 @@
                     new Long(finished.size()));
 
             // remove documents from the queue
-            for (Iterator it = finished.keySet().iterator(); it.hasNext(); ) {
-                indexingQueue.removeDocument(it.next().toString());
+            for (UUID uuid : finished.keySet()) {
+                indexingQueue.removeDocument(uuid.toString());
             }
 
             try {
                 if (transactionPresent) {
-                    for (Iterator it = finished.keySet().iterator(); it.hasNext(); ) {
-                        executeAndLog(new DeleteNode(getTransactionId(), (UUID) it.next()));
+                    for (UUID uuid : finished.keySet()) {
+                        executeAndLog(new DeleteNode(getTransactionId(), uuid));
                     }
-                    for (Iterator it = finished.values().iterator(); it.hasNext(); ) {
-                        executeAndLog(new AddNode(
-                                getTransactionId(), (Document) it.next()));
+                    for (Document document : finished.values()) {
+                        executeAndLog(new AddNode(getTransactionId(), document));
                     }
                 } else {
                     update(finished.keySet(), finished.values());

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java?rev=777541&r1=777540&r2=777541&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java Fri May 22 14:19:42 2009
@@ -62,10 +62,6 @@
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.Fieldable;
-import org.apache.commons.collections.iterators.TransformIterator;
-import org.apache.commons.collections.collection.TransformedCollection;
-import org.apache.commons.collections.IteratorUtils;
-import org.apache.commons.collections.Transformer;
 import org.xml.sax.SAXException;
 import org.w3c.dom.Element;
 
@@ -587,39 +583,39 @@
     public void updateNodes(NodeIdIterator remove, NodeStateIterator add)
             throws RepositoryException, IOException {
         checkOpen();
-        final Map aggregateRoots = new HashMap();
-        final HashSet removedUUIDs = new HashSet();
-        final Set addedUUIDs = new HashSet();
-
-        index.update(IteratorUtils.toList(new TransformIterator(remove,
-                new Transformer() {
-                    public Object transform(Object input) {
-                        UUID uuid = ((NodeId) input).getUUID();
-                        removedUUIDs.add(uuid);
-                        return uuid;
-                    }
-                })), IteratorUtils.toList(new TransformIterator(add,
-                new Transformer() {
-                    public Object transform(Object input) {
-                        NodeState state = (NodeState) input;
-                        if (state == null) {
-                            return null;
-                        }
-                        UUID uuid = state.getNodeId().getUUID();
-                        addedUUIDs.add(uuid);
-                        removedUUIDs.remove(uuid);
-                        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;
-                    }
-                })));
+
+        Map<UUID, NodeState> aggregateRoots = new HashMap<UUID, NodeState>();
+        Set<UUID> removedUUIDs = new HashSet<UUID>();
+        Set<UUID> addedUUIDs = new HashSet<UUID>();
+
+        Collection<UUID> removeCollection = new ArrayList<UUID>();
+        while (remove.hasNext()) {
+            UUID uuid = remove.nextNodeId().getUUID();
+            removeCollection.add(uuid);
+            removedUUIDs.add(uuid);
+        }
+
+        Collection<Document> addCollection = new ArrayList<Document>();
+        while (add.hasNext()) {
+            NodeState state = add.nextNodeState();
+            if (state != null) {
+                UUID uuid = state.getNodeId().getUUID();
+                addedUUIDs.add(uuid);
+                removedUUIDs.remove(uuid);
+                retrieveAggregateRoot(state, aggregateRoots);
+
+                try {
+                    addCollection.add(createDocument(
+                            state, getNamespaceMappings(),
+                            index.getIndexFormatVersion()));
+                } catch (RepositoryException e) {
+                    log.warn("Exception while creating document for node: "
+                            + state.getNodeId() + ": " + e.toString());
+                }
+            }
+        }
+
+        index.update(removeCollection, addCollection);
 
         // remove any aggregateRoot nodes that are new
         // and therefore already up-to-date
@@ -629,24 +625,21 @@
         retrieveAggregateRoot(removedUUIDs, aggregateRoots);
 
         // update aggregates if there are any affected
-        if (aggregateRoots.size() > 0) {
-            Collection modified = TransformedCollection.decorate(
-                    new ArrayList(),
-                    new Transformer() {
-                        public Object transform(Object input) {
-                            NodeState state = (NodeState) input;
-                            try {
-                                return createDocument(state,
-                                        getNamespaceMappings(),
-                                        index.getIndexFormatVersion());
-                            } catch (RepositoryException e) {
-                                log.warn("Exception while creating document for node: "
-                                        + state.getNodeId() + ": " + e.toString());
-                            }
-                            return null;
-                        }
-                    });
-            modified.addAll(aggregateRoots.values());
+        if (!aggregateRoots.isEmpty()) {
+            Collection<Document> modified =
+                new ArrayList<Document>(aggregateRoots.size());
+
+            for (NodeState state : aggregateRoots.values()) {
+                try {
+                    modified.add(createDocument(
+                            state, getNamespaceMappings(),
+                            index.getIndexFormatVersion()));
+                } catch (RepositoryException e) {
+                    log.warn("Exception while creating document for node: "
+                            + state.getNodeId(), e);
+                }
+            }
+
             index.update(aggregateRoots.keySet(), modified);
         }
     }
@@ -1356,7 +1349,8 @@
      * @param map   aggregate roots are collected in this map. Key=UUID,
      *              value=NodeState.
      */
-    protected void retrieveAggregateRoot(NodeState state, Map map) {
+    protected void retrieveAggregateRoot(
+            NodeState state, Map<UUID, NodeState> map) {
         if (indexingConfig != null) {
             AggregateRule[] aggregateRules = indexingConfig.getAggregateRules();
             if (aggregateRules == null) {
@@ -1381,10 +1375,10 @@
      * and puts it into <code>map</code>.
      *
      * @param removedUUIDs   the UUIDs of removed nodes.
-     * @param map            aggregate roots are collected in this map.
-     *                       Key=UUID, value=NodeState.
+     * @param map            aggregate roots are collected in this map
      */
-    protected void retrieveAggregateRoot(Set removedUUIDs, Map map) {
+    protected void retrieveAggregateRoot(
+            Set<UUID> removedUUIDs, Map<UUID, NodeState> map) {
         if (indexingConfig != null) {
             AggregateRule[] aggregateRules = indexingConfig.getAggregateRules();
             if (aggregateRules == null) {
@@ -1395,22 +1389,21 @@
             try {
                 CachingMultiIndexReader reader = index.getIndexReader();
                 try {
-                    Term aggregateUUIDs = new Term(
-                            FieldNames.AGGREGATED_NODE_UUID, "");
+                    Term aggregateUUIDs =
+                        new Term(FieldNames.AGGREGATED_NODE_UUID, "");
                     TermDocs tDocs = reader.termDocs();
                     try {
                         ItemStateManager ism = getContext().getItemStateManager();
-                        Iterator it = removedUUIDs.iterator();
-                        while (it.hasNext()) {
-                            UUID uuid = (UUID) it.next();
-                            aggregateUUIDs = aggregateUUIDs.createTerm(
-                                    uuid.toString());
+                        for (UUID uuid : removedUUIDs) {
+                            aggregateUUIDs =
+                                aggregateUUIDs.createTerm(uuid.toString());
                             tDocs.seek(aggregateUUIDs);
                             while (tDocs.next()) {
-                                Document doc = reader.document(tDocs.doc(), FieldSelectors.UUID);
-                                NodeId nId = new NodeId(UUID.fromString(
-                                        doc.get(FieldNames.UUID)));
-                                map.put(nId.getUUID(), ism.getItemState(nId));
+                                Document doc = reader.document(
+                                        tDocs.doc(), FieldSelectors.UUID);
+                                NodeId nId = new NodeId(
+                                        UUID.fromString(doc.get(FieldNames.UUID)));
+                                map.put(nId.getUUID(), (NodeState) ism.getItemState(nId));
                                 found++;
                             }
                         }
@@ -1424,8 +1417,7 @@
                 log.warn("Exception while retrieving aggregate roots", e);
             }
             time = System.currentTimeMillis() - time;
-            log.debug("Retrieved {} aggregate roots in {} ms.",
-                    new Integer(found), new Long(time));
+            log.debug("Retrieved {} aggregate roots in {} ms.", found, time);
         }
     }