krysalis-update/src/java/org/krysalis/depot/update/query ArtifactQueryHelper.java,NONE,1.1 ArtifactResult.java,NONE,1.1 ArtifactQuery.java,NONE,1.1 QueryEngine.java,NONE,1.1 ArtifactResultHelper.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:42 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/query
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/query
Added Files:
ArtifactQueryHelper.java ArtifactResult.java
ArtifactQuery.java QueryEngine.java ArtifactResultHelper.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: QueryEngine.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.query;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.UpdateException;
import org.krysalis.depot.update.artifact.ArtifactGroup;
import org.krysalis.depot.update.artifact.compare.StandardArtifactComparisons;
import org.krysalis.depot.update.impl.RepositorySetWrapper;
import org.krysalis.depot.update.impl.RepositoryWrapper;
import org.krysalis.depot.update.monitor.ArtifactEvent;
import org.krysalis.depot.update.monitor.Monitor;
import org.krysalis.depot.update.util.select.AllSelector;
import org.krysalis.depot.update.util.select.ISelector;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;
/**
* @author arb_jack
*/
public class QueryEngine {
/**
* Run a query over a set of repositories.
*
* @param wrappers
* @param query
* @return @throws
* UpdateException
*/
public ArtifactResult queryRepositories(RepositorySetWrapper wrappers,
ArtifactQuery query) throws UpdateException {
// Extract query components
List resourceGroups = query.getGroups();
// Trawl the repositories to gather resources
return queryArtifacts(gatherArtifacts(wrappers, resourceGroups, query),
query);
}
/**
* Run a query over a set of resources:
*
* @param resources
* @param query
* @return
*/
public ArtifactResult queryArtifacts(List resources, ArtifactQuery query) {
if (Logger.getLogger().isDebug()) {
DebugUtils.printSeparator();
DebugUtils.dump(query);
}
ArtifactResult result;
// Package the results..
if (resources.isEmpty()) {
Logger.getLogger().warn(
Messages.getString(MessageConstants.NO_RESOURCES_FOUND));
result = new ArtifactResult(Collections.EMPTY_LIST);
} else {
Logger.getLogger().info(
Messages.getString(MessageConstants.RESOURCES_FOUND));
Comparator groupBy = query.getGroupBy();
Comparator orderBy = query.getOrderBy();
if (Logger.getLogger().isDebug()) {
DebugUtils.printSeparator();
DebugUtils.dump("resources ", resources);
}
// Perform the group by...
result = groupBy(resources, groupBy);
if (Logger.getLogger().isDebug()) {
DebugUtils.printSeparator();
DebugUtils.dump("Grouped Results", result);
}
// Order the various groups
if (null == orderBy)
orderBy = StandardArtifactComparisons
.getNameTypeVersionComparatorSequence();
orderBy(result, orderBy);
if (Logger.getLogger().isDebug()) {
DebugUtils.printSeparator();
DebugUtils.dump("Final (Grouped/Ordered) Results", result);
for (Iterator i = ArtifactResultHelper.getArtifactList(result)
.iterator(); i.hasNext();) {
Monitor.getMonitor().notify(
new ArtifactEvent(ArtifactEvent.RESULT,
(ArtifactInstance) i.next()));
}
}
}
return result;
}
/**
* Gather all the artifacts for these groups from these repositories, and
* return (for filtering/grouping/ordering).
*
* @param wrappers
* @param resourceGroups
* @param query
* @return
*/
private List gatherArtifacts(RepositorySetWrapper wrappers,
List resourceGroups, ArtifactQuery query) {
List results = new ArrayList();
ISelector whereClause = query.getWhere();
if (null == whereClause)
whereClause = AllSelector.getInstance();
//:TODO: Total Hack, just to allow something...
// Rework w/ correct configurable components
int searched = 0;
for (Iterator i = wrappers.getRepositoryWrappers().iterator(); i
.hasNext();) {
RepositoryWrapper repo = (RepositoryWrapper) i.next();
try {
Logger.getLogger().debug(
Messages.getString(MessageConstants.SEARCH_REPOSITORY,
repo));
searched += 1;
// Look for all groups in lists
int gsearched = 0;
for (Iterator ii = resourceGroups.iterator(); ii.hasNext();) {
ArtifactGroup resourceGroup = (ArtifactGroup) ii.next();
Logger.getLogger().debug(
Messages.getString(MessageConstants.RESOURCE_GROUP,
resourceGroup));
gsearched += 1;
// Look...
List instances = repo.listInstances(resourceGroup,
whereClause);
if (instances.isEmpty()) {
Logger.getLogger().info(
Messages.getString(
MessageConstants.NO_RESOURCES_FOUND_IN,
query, repo, resourceGroup));
} else {
Logger.getLogger().info(
Messages.getString(
MessageConstants.RESOURCES_FOUND_IN,
query, repo, resourceGroup));
// Do the query
results.addAll(instances);
}
}
if (0 == gsearched)
Logger.getLogger().warn("No groups searched");
} catch (Exception e) {
String message = Messages
.getString(MessageConstants.LIST_FAILED);
Logger.getLogger().warn(message, e);
// :TODO: results.getNotes().warning(message, e);
}
}
if (0 == searched)
Logger.getLogger().warn("No repositories searched");
return results;
}
//
//
// Group into lists by comparator equality adn order the same,
// each group lists be same comparator.
//
//
private ArtifactResult groupBy(List allResults, Comparator groupBy) {
ArtifactResult result = new ArtifactResult();
if (null != groupBy) {
// Sort by groupBy, to split into groups
//:TODO: If we sorted by a CominbationComparator
// of groupBy followed by orderBy we'd save a sort,
// no?
Collections.sort(allResults, groupBy);
ArtifactInstance currentResource = null;
List currentList = null;
for (Iterator i = allResults.iterator(); i.hasNext();) {
ArtifactInstance resource = (ArtifactInstance) i.next();
// Do we have a new group?
if ((null == currentList) || (null == currentResource)
|| (groupBy.compare(resource, currentResource) != 0)) {
currentList = new ArrayList();
// Add group to results
result.add(currentList);
}
// Stash this resource
currentList.add(resource);
// For comparing group changes..
currentResource = resource;
}
} else {
// A single list (one group)...
result.add(allResults);
}
return result;
}
//
// Sort each sub-list by the order comparator
//
private void orderBy(ArtifactResult result, Comparator orderBy) {
if (null != orderBy)
for (Iterator i = result.iterator(); i.hasNext();) {
List currentList = (List) i.next();
Collections.sort(currentList, orderBy);
}
}
}
--- NEW FILE: ArtifactResultHelper.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.query;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.krysalis.depot.update.ArtifactInstance;
/**
* @author arb_jack
*
* A ArtifactResultHelper.
*
*/
public class ArtifactResultHelper {
public static boolean noResult(ArtifactResult result) {
return (
(null == result)
|| result.isEmpty()
|| (null == getArtifactList(result))
|| getArtifactList(result).isEmpty());
}
public static int groupCount(ArtifactResult result) {
return (null == result) ? 0 : result.size();
}
public static int artifactCount(ArtifactResult result) {
int artifacts = 0;
for (Iterator i = result.iterator(); i.hasNext();) {
artifacts += ((List) i.next()).size();
}
return artifacts;
}
public static List getArtifactList(ArtifactResult result) {
List list = null;
int size = result.size();
if (0 != size) {
// Return the 'last' list, since the normal
// order sort will put 'best' last.
list = (List) result.get(size - 1);
}
return list;
}
/**
* Get contents of all lists
*
* @param result
* @return
*/
public static List getAllArtifacts(ArtifactResult result) {
List list = new ArrayList();
for (Iterator j = result.iterator(); j.hasNext();) {
List artifacts = (List) j.next();
for (Iterator i = artifacts.iterator(); i.hasNext();) {
list.add(i.next());
}
}
return list;
}
/**
* Get contents of all lists but last one (the 'best')
*
* @param result
* @return
*/
public static List getRemainderArtifacts(ArtifactResult result) {
List list = new ArrayList();
// Return contents of all, but last..
List lists = getRemainderArtifactLists(result);
for (Iterator j = lists.iterator(); j.hasNext();) {
List artifacts = (List) j.next();
for (Iterator i = artifacts.iterator(); i.hasNext();) {
list.add(i.next());
}
}
return list;
}
/**
* Get all lists but last one (the 'best')
*
* @param result
* @return
*/
public static List getRemainderArtifactLists(ArtifactResult result) {
List list = new ArrayList();
int size = result.size();
if (0 != size) {
// Return contents of all, but last..
for (int j = 0; j < (size - 1); ++j) {
list.add(result.get(j));
}
}
return list;
}
// Two results sets compare based off the comparison of their last
// entry (ascending sort)(of their first list)
public static int compareTo(
ArtifactResult result1,
ArtifactResult result2,
Comparator comp) {
int comparison = 0;
List list1 = ArtifactResultHelper.getArtifactList(result1);
List list2 = ArtifactResultHelper.getArtifactList(result2);
if (null != list1) {
if (null != list2) {
if (!list1.isEmpty()) {
if (!list2.isEmpty()) {
//:TODO: Hmm ... sort w/ comp?
ArtifactInstance artifact1 =
(ArtifactInstance) list1.get(list1.size() - 1);
ArtifactInstance artifact2 =
(ArtifactInstance) list2.get(list2.size() - 1);
comparison = comp.compare(artifact1, artifact2);
}
else
comparison = 1;
}
else if (!list2.isEmpty())
comparison = -1;
}
else
comparison = 1;
}
else if (null != list2)
comparison = -1;
return comparison;
}
}
--- NEW FILE: ArtifactQuery.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.query;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.update.artifact.ArtifactGroup;
import org.krysalis.depot.update.repository.RepositorySet;
import org.krysalis.depot.update.util.select.ISelector;
/**
* @author arb_jack
* @author anou_mana
*
* A ResourceQuery is (primarily) an internal class derived from simpler user
* ResourceRequest objects.
*
* A ResourceQuery is effectively an SQL statement like this:
*
* <pre>
*
*
* SELECT * FROM {REPOSITORIES:group}
* WHERE ((name='junit' AND version>=1.0))
* GROUP BY name, version
* ORDER BY name, version, type
*
*
* </pre>
*
* The REPOSITORIES are a list of repositories, imaging them as tables. The
* "group" is which group on those repositories (if it exists). The * means
* resources, imagine resources as rows in the tables The GROUP BY is a bit of a
* stretch (it groups results this way) The ORDER BY is self explanatory.
*
*/
public class ArtifactQuery implements Dumpable {
private List m_groups = null;
// Repository
// :TODO: Hmm, ought repositories be on query at all?
private RepositorySet m_repositorySet = null;
private ISelector m_where = null;
private Comparator m_orderBy = null;
private Comparator m_groupBy = null;
//
// :TODO: Nice if we could make queries referenceable
// to allow re-use (especially for the more generic)
//
public ArtifactQuery(ArtifactGroup group, RepositorySet repositories,
ISelector where, Comparator groupBy, Comparator orderBy) {
m_groups = new ArrayList();
m_groups.add(group);
m_repositorySet = repositories;
m_where = where;
m_groupBy = groupBy;
m_orderBy = orderBy;
}
public ArtifactQuery(ArtifactGroup group, ISelector where,
Comparator groupBy, Comparator orderBy) {
m_groups = new ArrayList();
m_groups.add(group);
m_where = where;
m_groupBy = groupBy;
m_orderBy = orderBy;
}
public ArtifactQuery(ArtifactGroup group) {
m_groups = new ArrayList();
m_groups.add(group);
}
//
// :TODO: Nice if we could make queries referenceable
// to allow re-use (especially for the more generic)
//
public ArtifactQuery(ArtifactQuery other) {
m_groups = other.m_groups;
m_repositorySet = other.m_repositorySet;
m_where = other.m_where;
m_groupBy = other.m_groupBy;
m_orderBy = other.m_orderBy;
}
//
// Same query, different repositories
//
public ArtifactQuery(ArtifactQuery other, RepositorySet repositories) {
this(other);
m_repositorySet = repositories;
}
/**
* @return
*/
public Comparator getGroupBy() {
return m_groupBy;
}
/**
* @return
*/
public Comparator getOrderBy() {
return m_orderBy;
}
/**
* @return
*/
public RepositorySet getRepositorySet() {
return m_repositorySet;
}
/**
* @return
*/
public ISelector getWhere() {
return m_where;
}
/**
* @param comparator
*/
public void setGroupBy(Comparator comparator) {
m_groupBy = comparator;
}
/**
* @param comparator
*/
public void setOrderBy(Comparator comparator) {
m_orderBy = comparator;
}
/**
* @param set
*/
public void setRepositorySet(RepositorySet set) {
m_repositorySet = set;
}
/**
* @param selector
*/
public void setWhere(ISelector selector) {
m_where = selector;
}
/**
* @return
*/
public List getGroups() {
return m_groups;
}
/**
* @param list
*/
public void setGroups(List list) {
m_groups = list;
}
public void dump(PrintWriter out, int depth, boolean verbose) {
//String indent = DebugUtils.getIndent(depth);
DebugUtils.dump(out, depth + 1, "Resource Groups: ", verbose, m_groups);
DebugUtils.printSeparator(out, depth + 1);
DebugUtils.dump(out, depth + 1, "Repositories: ", verbose,
m_repositorySet);
if (null != m_where) {
DebugUtils.printSeparator(out, depth + 1);
DebugUtils.dump(out, depth + 1, "Where: ", verbose, m_where);
}
if (null != m_groupBy) {
DebugUtils.printSeparator(out, depth + 1);
DebugUtils.dump(out, depth + 1, "Group By: ", verbose, m_groupBy);
}
if (null != m_orderBy) {
DebugUtils.printSeparator(out, depth + 1);
DebugUtils.dump(out, depth + 1, "Order By: ", verbose, m_orderBy);
}
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT * FROM ");
if (null != m_repositorySet) {
buffer.append(m_repositorySet.getIdentifier());
} else {
buffer.append("Default Repositories");
}
if (null != m_groups) {
buffer.append(":");
buffer.append(m_groups);
}
buffer.append(" WHERE ");
buffer.append(m_where);
if (null != m_groupBy) {
buffer.append(" GROUP_BY ");
buffer.append(m_groupBy);
}
if (null != m_orderBy) {
buffer.append(" ORDER_BY ");
buffer.append(m_orderBy);
}
return buffer.toString();
}
}
--- NEW FILE: ArtifactQueryHelper.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.query;
import java.util.Comparator;
import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.update.Artifact;
import org.krysalis.depot.update.ArtifactInstance;
import org.krysalis.depot.update.UpdateException;
import org.krysalis.depot.update.artifact.ArtifactGroup;
import org.krysalis.depot.update.artifact.compare.StandardArtifactComparisons;
import org.krysalis.depot.update.artifact.select.StandardSelections;
import org.krysalis.depot.update.repository.RepositorySet;
import org.krysalis.depot.update.util.UpdateConstants;
import org.krysalis.depot.update.util.select.AllSelector;
import org.krysalis.depot.update.util.select.ISelector;
import org.krysalis.depot.update.util.text.MessageConstants;
import org.krysalis.depot.update.util.text.Messages;
/**
* @author arb_jack
*/
public class ArtifactQueryHelper {
public static ArtifactQuery getArtifactQuery(ArtifactInstance instance)
throws UpdateException {
return getArtifactQuery(instance.getArtifact());
}
public static ArtifactQuery getArtifactQuery(
ArtifactInstance instance,
RepositorySet repos)
throws UpdateException {
return getArtifactQuery(instance.getArtifact(), repos);
}
public static ArtifactQuery getArtifactQuery(Artifact artifact)
throws UpdateException {
return getArtifactQuery(
artifact,
RepositorySet.getRepositorySet(UpdateConstants.DEFAULT, true));
}
public static ArtifactQuery getArtifactQuery(
Artifact artifact,
RepositorySet repos)
throws UpdateException {
ArtifactQuery artifactQuery = null;
if (artifact != null) {
ISelector where = StandardSelections.getNamedTypedVersion(artifact);
Comparator orderBy =
StandardArtifactComparisons
.getNameTypeVersionComparatorSequence();
//Comparator groupBy = StandardResourceComparisons.getNameVersion();
Comparator groupBy =
StandardArtifactComparisons.getNameVersionComparatorSequence();
artifactQuery =
new ArtifactQuery(
artifact.getGroup(),
repos,
where,
groupBy,
orderBy);
}
else {
String message = Messages.getString(MessageConstants.NULL_RESOURCE);
Logger.getLogger().error(message);
throw new UpdateException(message);
}
return artifactQuery;
}
public static ArtifactQuery getArtifactGroupQuery(ArtifactGroup group)
throws UpdateException {
return getArtifactGroupQuery(
group,
RepositorySet.getRepositorySet(UpdateConstants.DEFAULT, true));
}
public static ArtifactQuery getArtifactGroupQuery(
ArtifactGroup group,
RepositorySet repos)
throws UpdateException {
ArtifactQuery artifactQuery = null;
if (group != null) {
//
// Extract the query
//
ISelector where = AllSelector.getInstance();
Comparator orderBy =
StandardArtifactComparisons
.getNameTypeVersionComparatorSequence();
Comparator groupBy =
StandardArtifactComparisons.getNameVersionComparatorSequence();
artifactQuery =
new ArtifactQuery(group, repos, where, groupBy, orderBy);
}
else {
String message = Messages.getString(MessageConstants.NULL_GROUP);
Logger.getLogger().error(message);
throw new UpdateException(message);
}
return artifactQuery;
}
}
--- NEW FILE: ArtifactResult.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.query;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.common.util.note.AnnotationScratchpad;
import org.krysalis.depot.update.ArtifactInstance;
/**
* A ArtifactResult is a list of lists of resources.
*
* @author arb_jack
*
*
*
*/
public class ArtifactResult extends ArrayList implements Dumpable {
public boolean m_successful = true;
public AnnotationScratchpad m_notes = null;
/**
*
*/
public ArtifactResult() {
super();
classInit();
}
/**
* @param arg0
*/
public ArtifactResult(int arg0) {
super(arg0);
classInit();
}
/**
* @param arg0
*/
public ArtifactResult(Collection arg0) {
super(arg0);
classInit();
}
private void classInit() {
m_notes = new AnnotationScratchpad();
}
/**
* @return
*/
public AnnotationScratchpad getNotes() {
return m_notes;
}
/**
* @return
*/
public boolean isSuccessful() {
return m_successful;
}
/**
* @param scratchpad
*/
public void setNotes(AnnotationScratchpad scratchpad) {
m_notes = scratchpad;
}
/**
* @param b
*/
public void setSuccessful(boolean b) {
m_successful = b;
}
public void dump(PrintWriter out, int depth, boolean verbose) {
//String indent = DebugUtils.getIndent(depth);
out.println("Successful:" + m_successful);
DebugUtils.printSeparator(out, depth + 1);
DebugUtils.dumpList(out, depth + 1, "Resources: ", verbose, this);
if ((null != m_notes) && m_notes.hasAnnotations()) {
DebugUtils.printSeparator(out, depth + 1);
m_notes.dump(out, depth + 1, verbose);
}
}
/**
* @param this
* @return
*/
public static ArtifactInstance getFirstArtifact(ArtifactResult res) {
List l = (List) res.get(0);
ArtifactInstance resource = (ArtifactInstance) l.get(0);
return resource;
}
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/