diff options
author | jmisinco | 2011-10-28 18:54:37 +0000 |
---|---|---|
committer | Roberto E. Escobar | 2011-10-28 18:54:37 +0000 |
commit | e9845ef58e1c1ae955c7767abab6a4278dd6962e (patch) | |
tree | 67cfe3990fccb958f07a1d771bf9b042aea2f623 /plugins/org.eclipse.osee.display.presenter | |
parent | 6b72acacfff7209893da66cd9592fb51d9fcc191 (diff) | |
download | org.eclipse.osee-e9845ef58e1c1ae955c7767abab6a4278dd6962e.tar.gz org.eclipse.osee-e9845ef58e1c1ae955c7767abab6a4278dd6962e.tar.xz org.eclipse.osee-e9845ef58e1c1ae955c7767abab6a4278dd6962e.zip |
feature[ats_18K4T]: Move cached search results to provider
Diffstat (limited to 'plugins/org.eclipse.osee.display.presenter')
5 files changed, 238 insertions, 90 deletions
diff --git a/plugins/org.eclipse.osee.display.presenter/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.display.presenter/META-INF/MANIFEST.MF index b764166dc29..2d7e33c977e 100644 --- a/plugins/org.eclipse.osee.display.presenter/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.display.presenter/META-INF/MANIFEST.MF @@ -20,6 +20,7 @@ Import-Package: com.google.common.collect, org.eclipse.osee.logger, org.eclipse.osee.orcs, org.eclipse.osee.orcs.data, - org.eclipse.osee.orcs.search + org.eclipse.osee.orcs.search, + org.eclipse.osee.orcs.utility Export-Package: org.eclipse.osee.display.presenter Require-Bundle: org.eclipse.core.runtime diff --git a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/ArtifactProviderImpl.java b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/ArtifactProviderImpl.java index c932fa2f9e4..5bef80b1fd5 100644 --- a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/ArtifactProviderImpl.java +++ b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/ArtifactProviderImpl.java @@ -15,6 +15,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -28,9 +29,11 @@ import org.eclipse.osee.framework.core.data.IOseeBranch; import org.eclipse.osee.framework.core.data.IRelationTypeSide; import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; import org.eclipse.osee.framework.core.enums.CoreRelationTypes; +import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist; +import org.eclipse.osee.framework.core.exception.MultipleArtifactsExist; import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.exception.OseeExceptions; import org.eclipse.osee.framework.core.model.type.RelationType; -import org.eclipse.osee.framework.jdk.core.util.Lib; import org.eclipse.osee.logger.Log; import org.eclipse.osee.orcs.ApplicationContext; import org.eclipse.osee.orcs.Graph; @@ -56,7 +59,10 @@ public class ArtifactProviderImpl implements ArtifactProvider { private final Graph graph; private final ConcurrentMap<ReadableArtifact, ReadableArtifact> parentCache; private final ArtifactSanitizer sanitizer; - Future<ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>>> searchFuture; + private Future<ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>>> searchFuture; + private ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> cachedResults; + private SearchParameters lastSearchParameters; + private final ExecutorAdmin executor; private final Log logger; @@ -66,6 +72,7 @@ public class ArtifactProviderImpl implements ArtifactProvider { this.context = context; this.graph = oseeApi.getGraph(context); sanitizer = new ArtifactSanitizer(executorAdmin, this); + executor = executorAdmin; this.parentCache = new MapMaker()// .initialCapacity(500)// @@ -89,12 +96,26 @@ public class ArtifactProviderImpl implements ArtifactProvider { @Override public void getSearchResults(IOseeBranch branch, boolean nameOnly, String searchPhrase, AsyncSearchListener callback) throws OseeCoreException { - IAttributeType type = nameOnly ? CoreAttributeTypes.Name : QueryBuilder.ANY_ATTRIBUTE_TYPE; - QueryBuilder builder = getFactory().fromBranch(branch); - builder.and(type, StringOperator.TOKENIZED_ANY_ORDER, CaseType.IGNORE_CASE, searchPhrase); + SearchParameters params = new SearchParameters(branch, nameOnly, searchPhrase); + ProviderExecutionCallback providerCallback = new ProviderExecutionCallback(callback); + if (lastSearchParameters != null && lastSearchParameters.equals(params)) { + CachedResultsCallable callable = new CachedResultsCallable(cachedResults); + try { + searchFuture = executor.schedule(callable, providerCallback); + } catch (Exception ex) { + OseeExceptions.wrapAndThrow(ex); + } + } else { + IAttributeType type = nameOnly ? CoreAttributeTypes.Name : QueryBuilder.ANY_ATTRIBUTE_TYPE; + QueryBuilder builder = getFactory().fromBranch(branch); + builder.and(type, StringOperator.TOKENIZED_ANY_ORDER, CaseType.IGNORE_CASE, searchPhrase); + + lastSearchParameters = params; + cachedResults = null; - searchFuture = builder.searchWithMatches(providerCallback); + searchFuture = builder.searchWithMatches(providerCallback); + } } @Override @@ -145,7 +166,6 @@ public class ArtifactProviderImpl implements ArtifactProvider { private class ProviderExecutionCallback implements ExecutionCallback<ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>>> { private final AsyncSearchListener callback; - private final long startTime = 0; public ProviderExecutionCallback(AsyncSearchListener callback) { this.callback = callback; @@ -153,39 +173,132 @@ public class ArtifactProviderImpl implements ArtifactProvider { @Override public void onSuccess(ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> result) { - long delta = 0; - if (logger.isTraceEnabled()) { - logger.trace("End Query: [%s]", Lib.getElapseString(startTime)); - delta = System.currentTimeMillis(); - } + FilteredResultSet results = new FilteredResultSet(result); + cachedResults = results; try { - List<Match<ReadableArtifact, ReadableAttribute<?>>> filtered = null; - if (result.getList() != null) { - filtered = sanitizer.filter(result.getList()); - Utility.sortResults(filtered); - } else { - filtered = Collections.emptyList(); - } - callback.onSearchComplete(filtered); + callback.onSearchComplete(results.getList()); } catch (Exception ex) { logger.error(ex, "Error in async search"); - } finally { - if (logger.isTraceEnabled()) { - logger.trace("Sanitized in: [%s]", Lib.getElapseString(delta)); - logger.trace("Total Time: [%s]", Lib.getElapseString(startTime)); - } } } @Override public void onFailure(Throwable throwable) { + lastSearchParameters = null; callback.onSearchFailed(throwable); } @Override public void onCancelled() { + lastSearchParameters = null; callback.onSearchCancelled(); } } + private class FilteredResultSet implements ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> { + + private final ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> decorated; + + private List<Match<ReadableArtifact, ReadableAttribute<?>>> filtered; + + public FilteredResultSet(ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> decorated) { + this.decorated = decorated; + this.filtered = null; + } + + @Override + public Match<ReadableArtifact, ReadableAttribute<?>> getOneOrNull() throws OseeCoreException { + List<Match<ReadableArtifact, ReadableAttribute<?>>> result = getList(); + return result.isEmpty() ? null : result.iterator().next(); + } + + @Override + public Match<ReadableArtifact, ReadableAttribute<?>> getExactlyOne() throws OseeCoreException { + List<Match<ReadableArtifact, ReadableAttribute<?>>> result = getList(); + if (result.isEmpty()) { + throw new ArtifactDoesNotExist("No artifacts found"); + } else if (result.size() > 1) { + throw new MultipleArtifactsExist("Multiple artifact found - total [%s]", result.size()); + } + return result.iterator().next(); + } + + @Override + public List<Match<ReadableArtifact, ReadableAttribute<?>>> getList() throws OseeCoreException { + if (filtered == null) { + filter(); + } + return filtered; + } + + @Override + public Iterable<Match<ReadableArtifact, ReadableAttribute<?>>> getIterable(int fetchSize) throws OseeCoreException { + return getList(); + } + + private synchronized void filter() throws OseeCoreException { + try { + filtered = sanitizer.filter(decorated.getList()); + Utility.sortResults(filtered); + } catch (Exception ex) { + OseeExceptions.wrapAndThrow(ex); + } + } + + } + + private class CachedResultsCallable implements Callable<ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>>> { + + private final ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> lastResults; + + public CachedResultsCallable(ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> lastResults) { + this.lastResults = lastResults; + } + + @Override + public ResultSet<Match<ReadableArtifact, ReadableAttribute<?>>> call() throws Exception { + return lastResults; + } + } + + private class SearchParameters { + private final IOseeBranch branch; + private final boolean nameOnly; + private final String searchPhrase; + + public SearchParameters(IOseeBranch branch, boolean nameOnly, String searchPhrase) { + this.branch = branch; + this.nameOnly = nameOnly; + this.searchPhrase = searchPhrase; + } + + public IOseeBranch getBranch() { + return branch; + } + + public boolean isNameOnly() { + return nameOnly; + } + + public String getSearchPhrase() { + return searchPhrase; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof SearchParameters) { + SearchParameters sObj = (SearchParameters) obj; + return sObj.getBranch().getGuid().equals(branch.getGuid()) && sObj.isNameOnly() == nameOnly && sObj.getSearchPhrase().equals( + searchPhrase); + } + return false; + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + } + } diff --git a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/AttributeTypeUtil.java b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/AttributeTypeUtil.java new file mode 100644 index 00000000000..b27e0ff0b57 --- /dev/null +++ b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/AttributeTypeUtil.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.display.presenter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.eclipse.osee.framework.core.data.IAttributeType; +import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.orcs.data.ReadableArtifact; +import org.eclipse.osee.orcs.data.ReadableAttribute; + +/** + * @author Roberto E. Escobar + */ +public class AttributeTypeUtil { + + public static List<IAttributeType> getEmptyTypes(ReadableArtifact artifact) throws OseeCoreException { + List<IAttributeType> items = new ArrayList<IAttributeType>(); + for (IAttributeType type : artifact.getAttributeTypes()) { + if (!CoreAttributeTypes.Name.equals(type) && artifact.getAttributes(type).isEmpty()) { + items.add(type); + } + } + Collections.sort(items); + return items; + } + + public static List<IAttributeType> getTypesWithData(ReadableArtifact artifact) throws OseeCoreException { + List<IAttributeType> items = new ArrayList<IAttributeType>(); + + Set<IAttributeType> typesInExistence = new HashSet<IAttributeType>(); + for (ReadableAttribute<?> attribute : artifact.getAttributes()) { + typesInExistence.add(attribute.getAttributeType()); + } + + IAttributeType nameType = null; + IAttributeType annotations = null; + IAttributeType relationOrder = null; + + for (IAttributeType type : typesInExistence) { + if (CoreAttributeTypes.Name.equals(type)) { + nameType = type; + } else if (CoreAttributeTypes.Annotation.equals(type)) { + annotations = type; + } else if (CoreAttributeTypes.RelationOrder.equals(type)) { + relationOrder = type; + } else { + items.add(type); + } + } + Collections.sort(items); + if (nameType != null) { + items.add(0, nameType); + } + if (annotations != null) { + items.add(annotations); + } + if (relationOrder != null) { + // Skip relation order items + // items.add(relationOrder); + } + return items; + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/SearchPresenterImpl.java b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/SearchPresenterImpl.java index 9cef1d0c6c8..7c19b367258 100644 --- a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/SearchPresenterImpl.java +++ b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/SearchPresenterImpl.java @@ -67,8 +67,6 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View protected final Log logger; private final AsyncSearchHandler searchHandler = new AsyncSearchHandler(); protected final Set<SearchProgressListener> searchListeners = new HashSet<SearchProgressListener>(); - private SearchParameters lastSearchParameters; - private List<Match<ReadableArtifact, ReadableAttribute<?>>> lastSearchResults; public SearchPresenterImpl(ArtifactProvider artifactProvider, Log logger) { this.artifactProvider = artifactProvider; @@ -77,48 +75,27 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View @Override public void initSearchResults(String url, T searchHeaderComp, SearchResultsListComponent searchResultsComp, DisplayOptionsComponent options) { - searchResultsComp.clearAll(); sendSearchCompleted(); SearchParameters params = decodeSearchUrl(url); - boolean skipSearch = false; - - if (!Strings.isValid(url)) { - return; - } - if (!params.isValid()) { + if (!Strings.isValid(url) || !params.isValid()) { return; } - if (params.onlyVerboseIsDifferent(lastSearchParameters)) { - skipSearch = true; - } - - for (SearchProgressListener listener : searchListeners) { - listener.searchInProgress(); - } - options.setDisplayOptions(new DisplayOptions(params.isVerbose())); - if (skipSearch) { - try { - processSearchResults(lastSearchResults, searchResultsComp, params.isVerbose()); - } catch (OseeCoreException ex) { - setErrorMessage(searchResultsComp, "Error loading search results", ex); - } - } else { - try { - searchHandler.setSearchValues(searchResultsComp, params.isVerbose()); - artifactProvider.getSearchResults(TokenFactory.createBranch(params.getBranchId(), ""), params.isNameOnly(), - params.getSearchPhrase(), searchHandler); - } catch (Exception ex) { - setErrorMessage(searchResultsComp, "Error loading search results", ex); - } + try { + searchHandler.setSearchValues(searchResultsComp, params.isVerbose()); + artifactProvider.getSearchResults(TokenFactory.createBranch(params.getBranchId(), ""), params.isNameOnly(), + params.getSearchPhrase(), searchHandler); + } catch (Exception ex) { + setErrorMessage(searchResultsComp, "Error loading search results", ex); } - lastSearchParameters = params; + sendSearchInProgress(); } private void processSearchResults(List<Match<ReadableArtifact, ReadableAttribute<?>>> searchResults, SearchResultsListComponent searchResultsComp, boolean isVerbose) throws OseeCoreException { + searchResultsComp.clearAll(); if (searchResults != null && searchResults.isEmpty()) { searchResultsComp.noSearchResultsFound(); } else { @@ -141,7 +118,6 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View } } sendSearchCompleted(); - lastSearchResults = searchResults; } @Override @@ -214,7 +190,7 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View attrComp.clearAll(); Collection<IAttributeType> attributeTypes = null; try { - attributeTypes = displayArt.getAttributeTypes(); + attributeTypes = AttributeTypeUtil.getTypesWithData(displayArt); } catch (Exception ex) { setErrorMessage(attrComp, "Error in initArtifactPage:\n Cannot load attribute types", ex); return; @@ -366,8 +342,6 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View @Override public void selectCancel() { - lastSearchParameters = null; - lastSearchResults = null; artifactProvider.cancelSearch(); } @@ -377,6 +351,12 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View } } + protected void sendSearchInProgress() { + for (SearchProgressListener listener : searchListeners) { + listener.searchInProgress(); + } + } + protected void sendSearchCompleted() { for (SearchProgressListener listener : searchListeners) { listener.searchCompleted(); @@ -484,15 +464,6 @@ public class SearchPresenterImpl<T extends SearchHeaderComponent, K extends View public boolean isValid() { return Strings.isValid(branchId); } - - public boolean onlyVerboseIsDifferent(SearchParameters params) { - if (params == null) { - return false; - } else { - return params.getBranchId().equals(branchId) && params.isNameOnly() == nameOnly && params.getSearchPhrase().equals( - searchPhrase); - } - } } } diff --git a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/Utility.java b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/Utility.java index d81f2c6876d..454d35e1fc6 100644 --- a/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/Utility.java +++ b/plugins/org.eclipse.osee.display.presenter/src/org/eclipse/osee/display/presenter/Utility.java @@ -15,18 +15,19 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.eclipse.osee.display.api.data.StyledText; -import org.eclipse.osee.framework.core.data.Named; import org.eclipse.osee.framework.jdk.core.type.MatchLocation; import org.eclipse.osee.framework.jdk.core.util.Strings; import org.eclipse.osee.orcs.data.ReadableArtifact; import org.eclipse.osee.orcs.data.ReadableAttribute; import org.eclipse.osee.orcs.search.Match; +import org.eclipse.osee.orcs.utility.ArtifactMatchComparator; +import org.eclipse.osee.orcs.utility.ArtifactNameComparator; +import org.eclipse.osee.orcs.utility.SortOrder; /** * @author Roberto E. Escobar @@ -37,27 +38,13 @@ public final class Utility { // } - public static List<? extends Named> sort(List<? extends Named> toSort) { - Collections.sort(toSort, new Comparator<Named>() { - - @Override - public int compare(Named o1, Named o2) { - return o1.getName().compareTo(o2.getName()); - } - - }); + public static List<ReadableArtifact> sort(List<ReadableArtifact> toSort) { + Collections.sort(toSort, new ArtifactNameComparator(SortOrder.ASCENDING)); return toSort; } public static List<Match<ReadableArtifact, ReadableAttribute<?>>> sortResults(List<Match<ReadableArtifact, ReadableAttribute<?>>> toSort) { - Collections.sort(toSort, new Comparator<Match<ReadableArtifact, ReadableAttribute<?>>>() { - - @Override - public int compare(Match<ReadableArtifact, ReadableAttribute<?>> o1, Match<ReadableArtifact, ReadableAttribute<?>> o2) { - return o1.getItem().getName().compareTo(o2.getItem().getName()); - } - - }); + Collections.sort(toSort, new ArtifactMatchComparator(SortOrder.ASCENDING)); return toSort; } |