Navigation Menu

Skip to content

Commit

Permalink
More separation of NexusIndexerManager from rest of it (not yet done)…
Browse files Browse the repository at this point in the history
…, a cleanup and concentrating on focused component for search, and removing all the inter-references to Manager. Use IIndex for searches, and that's all iface you need.
  • Loading branch information
cstamas authored and Pascal Rapicault committed Dec 3, 2010
1 parent e258a07 commit 61abf54
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 162 deletions.
57 changes: 50 additions & 7 deletions org.eclipse.m2e.core/src/org/eclipse/m2e/core/index/IIndex.java
Expand Up @@ -13,11 +13,13 @@

import java.io.File;
import java.util.Collection;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.m2e.core.embedder.ArtifactKey;


/**
* @author igor
*/
Expand All @@ -41,9 +43,24 @@ public interface IIndex {
* like SEARCH_ARTIFACT but will only return artifacts with packaging == pom
*/
public static final String SEARCH_PARENTS = "parents"; //$NON-NLS-1$

// search classifiers

// public enum SearchClassifiers {
// JARS,
//
// JAVADOCS,
//
// SOURCES,
//
// TESTS
// }
//
// public Set<SearchClassifiers> ALL_CLASSIFIERS = new HashSet<IIndex.SearchClassifiers>(Arrays.asList(SearchClassifiers
// .values()));

//

public static final int SEARCH_JARS = 1 << 0;

public static final int SEARCH_JAVADOCS = 1 << 1;
Expand All @@ -55,11 +72,11 @@ public interface IIndex {
public static final int SEARCH_ALL = 15;

// availability flags

public static final int PRESENT = 1;

public static final int NOT_PRESENT = 0;

public static final int NOT_AVAILABLE = 2;

// index queries
Expand All @@ -68,7 +85,33 @@ public interface IIndex {

public IndexedArtifactFile identify(File file) throws CoreException;

public Collection<IndexedArtifact> find(String groupId, String artifactId, String version,
String packaging) throws CoreException;

/**
* Performs a search for artifacts with given parameters.
*
* @param groupId
* @param artifactId
* @param version
* @param packaging
* @return
* @throws CoreException
*/
public Collection<IndexedArtifact> find(String groupId, String artifactId, String version, String packaging)
throws CoreException;

/**
* Convenience method to search in all indexes enabled for repositories defined in settings.xml. This method always
* performs "scored" search.
*/
public Map<String, IndexedArtifact> search(String term, String searchType) throws CoreException;

/**
* Convenience method to search in all indexes enabled for repositories defined in settings.xml. This method always
* performs "scored" search.
*
* @param term - search term
* @param searchType - query type. Should be one of the SEARCH_* values.
* @param classifier - the type of classifiers to search for, SEARCH_ALL, SEARCH_JAVADOCS, SEARCH_SOURCES,
* SEARCH_TESTS
*/
public Map<String, IndexedArtifact> search(String term, String searchType, int classifier) throws CoreException;
}
Expand Up @@ -11,70 +11,43 @@

package org.eclipse.m2e.core.index;

import java.io.File;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

import org.apache.lucene.search.Query;

import org.apache.maven.index.Field;
import org.apache.maven.index.SearchType;


public interface IndexManager {

public static final int MIN_CLASS_QUERY_LENGTH = 6;

// well-known indexes

public static final String LOCAL_INDEX = "local"; //$NON-NLS-1$
String LOCAL_INDEX = "local"; //$NON-NLS-1$

public static final String WORKSPACE_INDEX = "workspace"; //$NON-NLS-1$
String WORKSPACE_INDEX = "workspace"; //$NON-NLS-1$

public abstract IMutableIndex getWorkspaceIndex();
//

public abstract IMutableIndex getLocalIndex();
IMutableIndex getWorkspaceIndex();

IMutableIndex getLocalIndex();

/**
* For Maven projects, returns index of all repositories configured for the project. Index includes repositories
* defined in the project pom.xml, inherited from parent projects and defined in enabled profiles in settings.xml. If
* project is null or is not a maven project, returns index that includes repositories defined in profiles enabled by
* default in settings.xml.
*/
public abstract IIndex getIndex(IProject project) throws CoreException;

/**
* Convenience method to search in all indexes enabled for repositories defined in settings.xml
*/
public abstract Map<String, IndexedArtifact> search(String term, String searchType) throws CoreException;
IIndex getIndex(IProject project) throws CoreException;

/**
* Convenience method to search in all indexes enabled for repositories defined in settings.xml
* Returns index aggregating all indexes enabled for repositories defined in settings.xml
*
* @param term - search term
* @param searchType - query type. Should be one of the SEARCH_* values.
* @param classifier - the type of classifiers to search for, SEARCH_ALL, SEARCH_JAVADOCS, SEARCH_SOURCES,
* SEARCH_TESTS
* @return
* @throws CoreException
*/
public abstract Map<String, IndexedArtifact> search(String term, String type, int classifier) throws CoreException;
IIndex getAllIndexes() throws CoreException;

/**
* Convenience method to search in all indexes enabled for repositories defined in settings.xml
*/
public abstract IndexedArtifactFile identify(File file) throws CoreException;
//

/**
* Method to construct Lucene Queries without need to actually know the structure and details (field names, analyze
* details, etc) of the underlying index. Also, using this methods makes you "future proof". Naturally, at caller
* level you can still combine these queries using BooleanQuery to suit your needs.
*
* @param field
* @param query
* @param type
* @return
*/
public abstract Query constructQuery(Field field, String query, SearchType type);
void removeIndexListener(IndexListener listener);

void addIndexListener(IndexListener listener);
}
Expand Up @@ -13,8 +13,10 @@

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -69,11 +71,33 @@ public Collection<IndexedArtifact> find(String groupId, String artifactId, Strin
Set<IndexedArtifact> result = new LinkedHashSet<IndexedArtifact>();
for(IIndex index : indexes) {
Collection<IndexedArtifact> findResults = index.find(groupId, artifactId, version, packaging);
if(findResults != null){
if(findResults != null) {
result.addAll(findResults);
}
}
return result;
}

public Map<String, IndexedArtifact> search(String term, String searchType) throws CoreException {
Map<String, IndexedArtifact> result = new HashMap<String, IndexedArtifact>();
for(IIndex index : indexes) {
Map<String, IndexedArtifact> iresult = index.search(term, searchType);
if(iresult != null) {
result.putAll(iresult);
}
}
return result;
}

public Map<String, IndexedArtifact> search(String term, String searchType, int classifier) throws CoreException {
Map<String, IndexedArtifact> result = new HashMap<String, IndexedArtifact>();
for(IIndex index : indexes) {
Map<String, IndexedArtifact> iresult = index.search(term, searchType, classifier);
if(iresult != null) {
result.putAll(iresult);
}
}
return result;
}

}
Expand Up @@ -13,6 +13,7 @@

import java.io.File;
import java.util.Collection;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -124,8 +125,8 @@ public void scheduleIndexUpdate(boolean force) {
indexManager.scheduleIndexUpdate(repository, force);
}

public IndexedArtifactGroup[] getRootGroups() throws CoreException {
return indexManager.getRootGroups(repository);
public IndexedArtifactGroup[] getRootIndexedArtifactGroups() throws CoreException {
return indexManager.getRootIndexedArtifactGroups(repository);
}

public boolean isUpdating() {
Expand All @@ -143,4 +144,12 @@ public boolean isEnabled() {
public void setIndexDetails(String details) throws CoreException {
indexManager.setIndexDetails(repository, details, null/*async*/);
}

public Map<String, IndexedArtifact> search(String term, String searchType) throws CoreException {
return indexManager.search(getRepository(), term, searchType);
}

public Map<String, IndexedArtifact> search(String term, String searchType, int classifier) throws CoreException {
return indexManager.search(getRepository(), term, searchType, classifier);
}
}
Expand Up @@ -108,6 +108,8 @@
*/
public class NexusIndexManager implements IndexManager, IMavenProjectChangedListener, IRepositoryIndexer {

public static final int MIN_CLASS_QUERY_LENGTH = 6;

/**
* Lazy instantiated nexus indexer instance.
*/
Expand Down Expand Up @@ -274,6 +276,16 @@ protected IndexedArtifactFile identify(IRepository repository, File file) throws
}
}

/**
* Method to construct Lucene Queries without need to actually know the structure and details (field names, analyze
* details, etc) of the underlying index. Also, using this methods makes you "future proof". Naturally, at caller
* level you can still combine these queries using BooleanQuery to suit your needs.
*
* @param field
* @param query
* @param type
* @return
*/
public Query constructQuery(Field field, String query, SearchType type) {
return getIndexer().constructQuery(field, query, type);
}
Expand Down Expand Up @@ -626,7 +638,23 @@ public void run(IProgressMonitor monitor) throws CoreException {
}
}

protected IndexedArtifactGroup[] getRootGroups(IRepository repository) throws CoreException {
protected Set<String> getRootGroups(IRepository repository) throws CoreException {
synchronized(getIndexLock(repository)) {
IndexingContext context = getIndexingContext(repository);
if(context != null) {
try {
Set<String> rootGroups = context.getRootGroups();
return rootGroups;
} catch(IOException ex) {
throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, //
NLS.bind(Messages.NexusIndexManager_error_root_grp, repository.toString()), ex));
}
}
return Collections.emptySet();
}
}

protected IndexedArtifactGroup[] getRootIndexedArtifactGroups(IRepository repository) throws CoreException {
synchronized(getIndexLock(repository)) {
IndexingContext context = getIndexingContext(repository);
if(context != null) {
Expand Down Expand Up @@ -762,6 +790,25 @@ public IIndex getIndex(IProject project) {
return new CompositeIndex(indexes);
}

public IIndex getAllIndexes() {
ArrayList<IIndex> indexes = new ArrayList<IIndex>();
indexes.add(getWorkspaceIndex());
indexes.add(getLocalIndex());

for(IMavenProjectFacade facade : projectManager.getProjects()) {
LinkedHashSet<ArtifactRepositoryRef> repositories = new LinkedHashSet<ArtifactRepositoryRef>();
repositories.addAll(facade.getArtifactRepositoryRefs());
repositories.addAll(facade.getPluginArtifactRepositoryRefs());

for(ArtifactRepositoryRef repositoryRef : repositories) {
IRepository repository = repositoryRegistry.getRepository(repositoryRef);
indexes.add(getIndex(repository));
}
}

return new CompositeIndex(indexes);
}

public NexusIndex getIndex(IRepository repository) {
String details = getIndexDetails(repository);
return new NexusIndex(this, repository, details);
Expand Down Expand Up @@ -807,7 +854,8 @@ public void repositoryAdded(IRepository repository, IProgressMonitor monitor) th
setIndexDetails(repository, null, details, null/*async*/);
}

protected String getIndexDetails(IRepository repository) {
/** For tests only */
public String getIndexDetails(IRepository repository) {
String details = indexDetails.getProperty(repository.getUid());

if(details == null) {
Expand All @@ -829,7 +877,7 @@ protected String getIndexDetails(IRepository repository) {
* Updates index synchronously if monitor!=null. Schedules index update otherwise. ... and yes, I know this ain't
* kosher. Public for unit tests only!
*/
protected void setIndexDetails(IRepository repository, String details, IProgressMonitor monitor) throws CoreException {
public void setIndexDetails(IRepository repository, String details, IProgressMonitor monitor) throws CoreException {
setIndexDetails(repository, details, details, monitor);
}

Expand Down
Expand Up @@ -545,7 +545,11 @@ protected IStatus run(IProgressMonitor monitor) {

try {
setResults(IStatus.OK, Messages.AddDependencyDialog_searching, Collections.<String, IndexedArtifact> emptyMap());
Map<String, IndexedArtifact> results = indexManager.search(query, IIndex.SEARCH_ARTIFACT, IIndex.SEARCH_ALL);
// TODO: before it was searching all indexes, but it should current project? (cstamas)
// If not, the change getIndex(project) to getAllIndexes() and done
// TODO: cstamas identified this as "user input", true?
Map<String, IndexedArtifact> results = indexManager.getIndex(project).search(query, IIndex.SEARCH_ARTIFACT,
IIndex.SEARCH_ALL);
setResults(IStatus.OK, NLS.bind(Messages.AddDependencyDialog_searchDone, results.size()), results);
} catch(BooleanQuery.TooManyClauses exception) {
setResults(IStatus.ERROR, Messages.AddDependencyDialog_tooManyResults,
Expand Down

0 comments on commit 61abf54

Please sign in to comment.