|
From: <caw...@us...> - 2007-04-13 19:36:10
|
Revision: 2313
http://svn.sourceforge.net/rubyeclipse/?rev=2313&view=rev
Author: cawilliams
Date: 2007-04-13 12:36:06 -0700 (Fri, 13 Apr 2007)
Log Message:
-----------
try to work towards the external loadpaths (like ruby std library) to get indexed properly.
Modified Paths:
--------------
branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/IndexManager.java
Added Paths:
-----------
branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFolderToIndex.java
Removed Paths:
-------------
branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFileToIndex.java
Deleted: branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFileToIndex.java
===================================================================
--- branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFileToIndex.java 2007-04-13 18:53:42 UTC (rev 2312)
+++ branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFileToIndex.java 2007-04-13 19:36:06 UTC (rev 2313)
@@ -1,17 +0,0 @@
-package org.rubypeople.rdt.internal.core.search.indexing;
-
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-
-public class AddExternalFileToIndex extends IndexRequest {
-
- public AddExternalFileToIndex(IPath containerPath, IndexManager manager) {
- super(containerPath, manager);
- }
-
- public boolean execute(IProgressMonitor progress) {
- // TODO Auto-generated method stub
- return false;
- }
-
-}
Added: branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFolderToIndex.java
===================================================================
--- branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFolderToIndex.java (rev 0)
+++ branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/AddExternalFolderToIndex.java 2007-04-13 19:36:06 UTC (rev 2313)
@@ -0,0 +1,192 @@
+package org.rubypeople.rdt.internal.core.search.indexing;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.rubypeople.rdt.core.search.SearchParticipant;
+import org.rubypeople.rdt.internal.compiler.util.SimpleLookupTable;
+import org.rubypeople.rdt.internal.core.RubyModelManager;
+import org.rubypeople.rdt.internal.core.index.Index;
+import org.rubypeople.rdt.internal.core.search.BasicSearchEngine;
+import org.rubypeople.rdt.internal.core.search.RubySearchDocument;
+import org.rubypeople.rdt.internal.core.search.processing.JobManager;
+import org.rubypeople.rdt.internal.core.util.Util;
+
+public class AddExternalFolderToIndex extends IndexRequest {
+
+ public AddExternalFolderToIndex(IPath containerPath, IndexManager manager) {
+ super(containerPath, manager);
+ }
+
+ public boolean execute(IProgressMonitor progressMonitor) {
+
+ if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
+ return true;
+
+ try {
+ // if index is already cached, then do not perform any check
+ // MUST reset the IndexManager if a jar file is changed
+ Index index = this.manager.getIndexForUpdate(this.containerPath, false, /* do not reuse index file */false /* do not create if none */);
+ if (index != null) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> no indexing required (index already exists) for " + this.containerPath); //$NON-NLS-1$
+ return true;
+ }
+
+ index = this.manager.getIndexForUpdate(this.containerPath, true, /* reuse index file */true /* create if none */);
+ if (index == null) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> index could not be created for " + this.containerPath); //$NON-NLS-1$
+ return true;
+ }
+ ReadWriteMonitor monitor = index.monitor;
+ if (monitor == null) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> index for " + this.containerPath + " just got deleted"); //$NON-NLS-1$//$NON-NLS-2$
+ return true; // index got deleted since acquired
+ }
+ File file = null;
+ try {
+ monitor.enterWrite(); // ask permission to write
+
+ if (RubyModelManager.ZIP_ACCESS_VERBOSE)
+ System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + this.containerPath); //$NON-NLS-1$ //$NON-NLS-2$
+ // external file -> it is ok to use toFile()
+ file = this.containerPath.toFile();
+ // path is already canonical since coming from a library
+ // classpath entry
+
+ if (this.isCancelled) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> indexing of " + file.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
+ return false;
+ }
+
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> indexing " + file.getName()); //$NON-NLS-1$
+ long initialTime = System.currentTimeMillis();
+
+ String[] paths = index.queryDocumentNames(""); // all file
+ // names
+ // //$NON-NLS-1$
+ if (paths != null) {
+ int max = paths.length;
+ /*
+ * check integrity of the existing index file if the length
+ * is equal to 0, we want to index the whole jar again If
+ * not, then we want to check that there is no missing
+ * entry, if one entry is missing then we recreate the index
+ */
+ String EXISTS = "OK"; //$NON-NLS-1$
+ String DELETED = "DELETED"; //$NON-NLS-1$
+ SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
+ for (int i = 0; i < max; i++)
+ indexedFileNames.put(paths[i], DELETED);
+
+ addDirectorysChildren(file, EXISTS, indexedFileNames);
+ boolean needToReindex = indexedFileNames.elementSize != max; // a
+ // new
+ // file
+ // was
+ // added
+ if (!needToReindex) {
+ Object[] valueTable = indexedFileNames.valueTable;
+ for (int i = 0, l = valueTable.length; i < l; i++) {
+ if (valueTable[i] == DELETED) {
+ needToReindex = true; // a file was deleted so
+ // re-index
+ break;
+ }
+ }
+ if (!needToReindex) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> no indexing required (index is consistent with library) for " //$NON-NLS-1$
+ + file.getName() + " (" //$NON-NLS-1$
+ + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
+ this.manager.saveIndex(index); // to ensure its
+ // placed into the
+ // saved state
+ return true;
+ }
+ }
+ }
+
+ // Index the jar for the first time or reindex the jar in case
+ // the previous index file has been corrupted
+ // index already existed: recreate it so that we forget about
+ // previous entries
+ SearchParticipant participant = BasicSearchEngine.getDefaultSearchParticipant();
+ index = manager.recreateIndex(this.containerPath);
+ if (index == null) {
+ // failed to recreate index, see 73330
+ manager.removeIndex(this.containerPath);
+ return false;
+ }
+
+ File[] children = file.listFiles();
+ for (int i = 0; i < children.length; i++) {
+ if (this.isCancelled) {
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> indexing of " + file.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
+ return false;
+ }
+ String name = children[i].getName();
+ if (Util.isRubyLikeFileName(name)) {
+ RubySearchDocument entryDocument = new RubySearchDocument(children[i].getAbsolutePath(), participant);
+ this.manager.indexDocument(entryDocument, participant, index, this.containerPath);
+ }
+ }
+ this.manager.saveIndex(index);
+ if (JobManager.VERBOSE)
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> done indexing of " //$NON-NLS-1$
+ + file.getName() + " (" //$NON-NLS-1$
+ + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
+ } finally {
+ monitor.exitWrite(); // free write lock
+ }
+ } catch (IOException e) {
+ if (JobManager.VERBOSE) {
+ org.rubypeople.rdt.internal.core.util.Util.verbose("-> failed to index " + this.containerPath + " because of the following exception:"); //$NON-NLS-1$ //$NON-NLS-2$
+ e.printStackTrace();
+ }
+ manager.removeIndex(this.containerPath);
+ return false;
+ }
+ return true;
+ }
+
+ private void addDirectorysChildren(File file, String EXISTS, SimpleLookupTable indexedFileNames) {
+ File[] children = file.listFiles();
+ for (int i = 0; i < children.length; i++) {
+ String name = children[i].getName();
+ if (Util.isRubyLikeFileName(name)) {
+ indexedFileNames.put(name, EXISTS);
+ }
+ addDirectorysChildren(children[i], EXISTS, indexedFileNames);
+ }
+ }
+
+ protected Integer updatedIndexState() {
+ return IndexManager.REBUILDING_STATE;
+ }
+ public String toString() {
+ return "indexing " + this.containerPath.toString(); //$NON-NLS-1$
+ }
+
+ public boolean equals(Object o) {
+ if (o instanceof AddExternalFolderToIndex) {
+ if (this.containerPath != null)
+ return this.containerPath.equals(((AddExternalFolderToIndex) o).containerPath);
+ }
+ return false;
+ }
+ public int hashCode() {
+ if (this.containerPath != null)
+ return this.containerPath.hashCode();
+ return -1;
+ }
+
+}
Modified: branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/IndexManager.java
===================================================================
--- branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/IndexManager.java 2007-04-13 18:53:42 UTC (rev 2312)
+++ branches/search_engine/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/search/indexing/IndexManager.java 2007-04-13 19:36:06 UTC (rev 2313)
@@ -254,12 +254,8 @@
IProject p = (IProject) target;
if (RubyProject.hasRubyNature(p))
request = new IndexAllProject(p, this);
-// } else if (target instanceof IFolder) {
-// request = new IndexBinaryFolder((IFolder) target, this);
-// } else if (target instanceof IFile) {
-// request = new AddJarFileToIndex((IFile) target, this);
} else if (target instanceof File) {
- request = new AddExternalFileToIndex(containerPath, this);
+ request = new AddExternalFolderToIndex(containerPath, this);
}
if (request != null)
request(request);
@@ -500,8 +496,25 @@
// TODO take into account the project?
return new SourceParser(requestor);
}
- public void indexLibrary(IPath projectPath, IProject project) {
- // XXX Actually implement this?!
+ public void indexLibrary(IPath path, IProject project) {
+// requestingProject is no longer used to cancel jobs but leave it here just in case
+ if (RubyCore.getPlugin() == null) return;
+
+ Object target = RubyModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
+ IndexRequest request = null;
+ if (target instanceof java.io.File) {
+ if (((java.io.File) target).isDirectory()) {
+ request = new AddExternalFolderToIndex(path, this);
+ } else {
+ return;
+ }
+ } else {
+ return;
+ }
+
+ // check if the same request is not already in the queue
+ if (!isJobWaiting(request))
+ this.request(request);
}
/**
* Trigger addition of the entire content of a project
@@ -553,4 +566,35 @@
for (int i = 0, length = toRemove.size(); i < length; i++)
this.removeIndex((IPath) toRemove.get(i));
}
+
+ /**
+ * Recreates the index for a given path, keeping the same read-write monitor.
+ * Returns the new empty index or null if it didn't exist before.
+ * Warning: Does not check whether index is consistent (not being used)
+ */
+ public synchronized Index recreateIndex(IPath containerPath) {
+ // only called to over write an existing cached index...
+ String containerPathString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
+ try {
+ // Path is already canonical
+ IPath indexLocation = computeIndexLocation(containerPath);
+
+ Index index = (Index) this.indexes.get(indexLocation);
+ ReadWriteMonitor monitor = index == null ? null : index.monitor;
+
+ if (VERBOSE)
+ Util.verbose("-> recreating index: "+indexLocation+" for path: "+containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
+ index = new Index(indexLocation.toString(), containerPathString, false /*reuse index file*/);
+ this.indexes.put(indexLocation, index);
+ index.monitor = monitor;
+ return index;
+ } catch (IOException e) {
+ // The file could not be created. Possible reason: the project has been deleted.
+ if (VERBOSE) {
+ Util.verbose("-> failed to recreate index for path: "+containerPathString); //$NON-NLS-1$
+ e.printStackTrace();
+ }
+ return null;
+ }
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|