/** * @param writer * @param task the task * @param taskData may be null for local tasks * @throws CorruptIndexException * @throws IOException */ private void add(IndexWriter writer, ITask task, TaskData taskData) throws CorruptIndexException, IOException { if (!taskIsIndexable(task, taskData)) { return; } Document document = new Document(); document.add( new Field( FIELD_IDENTIFIER.getIndexKey(), task.getHandleIdentifier(), Store.YES, org.apache.lucene.document.Field.Index.ANALYZED)); if (taskData == null) { if ("local".equals(((AbstractTask) task).getConnectorKind())) { // $NON-NLS-1$ addIndexedAttributes(document, task); } else { return; } } else { addIndexedAttributes(document, task, taskData.getRoot()); } writer.addDocument(document); }
/** * Indicates if the given task matches the given pattern string. Uses the backing index to detect * a match by looking for tasks that match the given pattern string. The results of the search are * cached such that future calls to this method using the same pattern string do not require use * of the backing index, making this method very efficient for multiple calls with the same * pattern string. Cached results for a given pattern string are discarded if this method is * called with a different pattern string. * * @param task the task to match * @param patternString the pattern used to detect a match */ public boolean matches(ITask task, String patternString) { if (patternString.equals(COMMAND_RESET_INDEX)) { reindex(); } Lock readLock = indexReaderLock.readLock(); readLock.lock(); try { IndexReader indexReader = getIndexReader(); if (indexReader != null) { Set<String> hits; final boolean needIndexHit; synchronized (this) { needIndexHit = lastResults == null || (lastPatternString == null || !lastPatternString.equals(patternString)); } if (needIndexHit) { this.lastPatternString = patternString; hits = new HashSet<String>(); IndexSearcher indexSearcher = new IndexSearcher(indexReader); try { Query query = computeQuery(patternString); TopDocs results = indexSearcher.search(query, maxMatchSearchHits); for (ScoreDoc scoreDoc : results.scoreDocs) { Document document = indexReader.document(scoreDoc.doc); hits.add(document.get(FIELD_IDENTIFIER.getIndexKey())); } } catch (IOException e) { StatusHandler.log( new Status( IStatus.ERROR, TasksIndexCore.ID_PLUGIN, "Unexpected failure within task list index", e)); //$NON-NLS-1$ } finally { try { indexSearcher.close(); } catch (IOException e) { // ignore } } } else { hits = lastResults; } synchronized (this) { if (this.indexReader == indexReader) { this.lastPatternString = patternString; this.lastResults = hits; } } String taskIdentifier = task.getHandleIdentifier(); return hits != null && hits.contains(taskIdentifier); } } finally { readLock.unlock(); } return false; }
private void indexQueuedTasks(SubMonitor monitor) throws CorruptIndexException, LockObtainFailedException, IOException, CoreException { synchronized (reindexQueue) { if (reindexQueue.isEmpty()) { return; } monitor.beginTask(Messages.TaskListIndex_task_rebuilding_index, reindexQueue.size()); } try { IndexWriter writer = null; try { Map<ITask, TaskData> workingQueue = new HashMap<ITask, TaskData>(); // reindex tasks that are in the reindexQueue, making multiple passes so that we catch // anything // added/changed while we were reindexing for (; ; ) { workingQueue.clear(); synchronized (reindexQueue) { if (reindexQueue.isEmpty()) { break; } // move items from the reindexQueue to the temporary working queue workingQueue.putAll(reindexQueue); reindexQueue.keySet().removeAll(workingQueue.keySet()); } if (writer == null) { try { writer = createIndexWriter(false); } catch (CorruptIndexException e) { rebuildIndex = true; synchronized (reindexQueue) { reindexQueue.clear(); } rebuildIndexCompletely(monitor); return; } } monitor.setWorkRemaining(workingQueue.size()); for (Entry<ITask, TaskData> entry : workingQueue.entrySet()) { ITask task = entry.getKey(); TaskData taskData = entry.getValue(); writer.deleteDocuments( new Term(FIELD_IDENTIFIER.getIndexKey(), task.getHandleIdentifier())); add(writer, task, taskData); monitor.worked(1); } } } finally { if (writer != null) { writer.close(); } } } finally { monitor.done(); } }