Bug 509819 - NPE in SearchIndex.removeDocument

Make sure the index writer is opened if we are going to delete invalid
entries.

Change-Id: I9736ed4b4f038867aab5dafabbbb51d3ad3b874c
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/search/IndexingOperation.java b/org.eclipse.help.base/src/org/eclipse/help/internal/search/IndexingOperation.java
index ec744ff..87bac9c 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/search/IndexingOperation.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/search/IndexingOperation.java
@@ -195,44 +195,45 @@
 	 */
 	private void removeNewDocuments(IProgressMonitor pm, Map<String, String[]> docsToDelete)
 			throws IndexingException {
+		if (docsToDelete.size() == 0) {
+			return;
+		}
 		pm = new LazyProgressMonitor(pm);
 		pm.beginTask("", docsToDelete.size()); //$NON-NLS-1$
 		checkCancelled(pm);
 		Set<String> keysToDelete = docsToDelete.keySet();
-		if (keysToDelete.size() > 0) {
-			if (!index.beginRemoveDuplicatesBatch()) {
-				throw new IndexingException();
+		if (!index.beginRemoveDuplicatesBatch()) {
+			throw new IndexingException();
+		}
+		MultiStatus multiStatus = null;
+		for (Iterator<String> it = keysToDelete.iterator(); it.hasNext();) {
+			String href = it.next();
+			String[] indexIds = docsToDelete.get(href);
+			if (indexIds == null) {
+				// delete all copies
+				index.removeDocument(href);
+				continue;
 			}
-			MultiStatus multiStatus = null;
-			for (Iterator<String> it = keysToDelete.iterator(); it.hasNext();) {
-				String href = it.next();
-				String[] indexIds = docsToDelete.get(href);
-				if (indexIds == null) {
-					// delete all copies
-					index.removeDocument(href);
-					continue;
+			IStatus status = index.removeDuplicates(href, indexIds);
+			if (status.getCode() != IStatus.OK) {
+				if (multiStatus == null) {
+					multiStatus = new MultiStatus(
+							HelpBasePlugin.PLUGIN_ID,
+							IStatus.WARNING,
+							"Some help documents could not removed from index.", //$NON-NLS-1$
+							null);
 				}
-				IStatus status = index.removeDuplicates(href, indexIds);
-				if (status.getCode() != IStatus.OK) {
-					if (multiStatus == null) {
-						multiStatus = new MultiStatus(
-								HelpBasePlugin.PLUGIN_ID,
-								IStatus.WARNING,
-								"Some help documents could not removed from index.", //$NON-NLS-1$
-								null);
-					}
-					multiStatus.add(status);
-				}
-				checkCancelled(pm);
-				pm.worked(1);
-				if (multiStatus != null) {
-					HelpBasePlugin.logStatus(multiStatus);
-				}
+				multiStatus.add(status);
 			}
-			if (!index.endRemoveDuplicatesBatch()) {
-				throw new IndexingException();
+			checkCancelled(pm);
+			pm.worked(1);
+			if (multiStatus != null) {
+				HelpBasePlugin.logStatus(multiStatus);
 			}
 		}
+		if (!index.endRemoveDuplicatesBatch()) {
+			throw new IndexingException();
+		}
 		pm.done();
 	}
 
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
index bd8de42..74d17d0 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
@@ -352,6 +352,9 @@
 				ir.close();
 			}
 			ir = DirectoryReader.open(luceneDirectory);
+			if (iw == null) {
+				return beginDeleteBatch();
+			}
 			return true;
 		} catch (IOException e) {
 			HelpBasePlugin.logError("Exception occurred in search indexing at beginDeleteBatch.", e); //$NON-NLS-1$
@@ -458,6 +461,8 @@
 				return false;
 			ir.close();
 			ir = null;
+			iw.close();
+			iw = null;
 			// save the update info:
 			// - all the docs
 			// - plugins (and their version) that were indexed