use of java.util.SortedSet in project hbase by apache.
the class ReplicationSourceManager method recordLog.
/**
* Check and enqueue the given log to the correct source. If there's still no source for the
* group to which the given log belongs, create one
* @param logPath the log path to check and enqueue
* @throws IOException
*/
private void recordLog(Path logPath) throws IOException {
String logName = logPath.getName();
String logPrefix = AbstractFSWALProvider.getWALPrefixFromWALName(logName);
// synchronize on replicationPeers to avoid adding source for the to-be-removed peer
synchronized (replicationPeers) {
for (String id : replicationPeers.getConnectedPeerIds()) {
try {
this.replicationQueues.addLog(id, logName);
} catch (ReplicationException e) {
throw new IOException("Cannot add log to replication queue" + " when creating a new source, queueId=" + id + ", filename=" + logName, e);
}
}
}
// update walsById map
synchronized (walsById) {
for (Map.Entry<String, Map<String, SortedSet<String>>> entry : this.walsById.entrySet()) {
String peerId = entry.getKey();
Map<String, SortedSet<String>> walsByPrefix = entry.getValue();
boolean existingPrefix = false;
for (Map.Entry<String, SortedSet<String>> walsEntry : walsByPrefix.entrySet()) {
SortedSet<String> wals = walsEntry.getValue();
if (this.sources.isEmpty()) {
// If there's no slaves, don't need to keep the old wals since
// we only consider the last one when a new slave comes in
wals.clear();
}
if (logPrefix.equals(walsEntry.getKey())) {
wals.add(logName);
existingPrefix = true;
}
}
if (!existingPrefix) {
// The new log belongs to a new group, add it into this peer
LOG.debug("Start tracking logs for wal group " + logPrefix + " for peer " + peerId);
SortedSet<String> wals = new TreeSet<>();
wals.add(logName);
walsByPrefix.put(logPrefix, wals);
}
}
}
}
use of java.util.SortedSet in project buck by facebook.
the class FineGrainedJavaDependencySuggester method suggestRefactoring.
/**
* Suggests a refactoring by printing it to stdout (with warnings printed to stderr).
* @throws IllegalArgumentException
*/
void suggestRefactoring() {
final TargetNode<?, ?> suggestedNode = graph.get(suggestedTarget);
if (!(suggestedNode.getConstructorArg() instanceof JavaLibraryDescription.Arg)) {
console.printErrorText(String.format("'%s' does not correspond to a Java rule", suggestedTarget));
throw new IllegalArgumentException();
}
JavaLibraryDescription.Arg arg = (JavaLibraryDescription.Arg) suggestedNode.getConstructorArg();
JavaFileParser javaFileParser = javaDepsFinder.getJavaFileParser();
Multimap<String, String> providedSymbolToRequiredSymbols = HashMultimap.create();
Map<String, PathSourcePath> providedSymbolToSrc = new HashMap<>();
for (SourcePath src : arg.srcs) {
extractProvidedSymbolInfoFromSourceFile(src, javaFileParser, providedSymbolToRequiredSymbols, providedSymbolToSrc);
}
// Create a MutableDirectedGraph from the providedSymbolToRequiredSymbols.
MutableDirectedGraph<String> symbolsDependencies = new MutableDirectedGraph<>();
// dependencies.
for (String providedSymbol : providedSymbolToSrc.keySet()) {
// Add a node for the providedSymbol in case it has no edges.
symbolsDependencies.addNode(providedSymbol);
for (String requiredSymbol : providedSymbolToRequiredSymbols.get(providedSymbol)) {
if (providedSymbolToRequiredSymbols.containsKey(requiredSymbol) && !providedSymbol.equals(requiredSymbol)) {
symbolsDependencies.addEdge(providedSymbol, requiredSymbol);
}
}
}
// Determine the strongly connected components.
Set<Set<String>> stronglyConnectedComponents = symbolsDependencies.findStronglyConnectedComponents();
// Maps a providedSymbol to the component that contains it.
Map<String, NamedStronglyConnectedComponent> namedComponentsIndex = new TreeMap<>();
Set<NamedStronglyConnectedComponent> namedComponents = new TreeSet<>();
for (Set<String> stronglyConnectedComponent : stronglyConnectedComponents) {
// We just use the first provided symbol in the strongly connected component as the canonical
// name for the component. Maybe not the best name, but certainly not the worst.
String name = Iterables.getFirst(stronglyConnectedComponent, /* defaultValue */
null);
if (name == null) {
throw new IllegalStateException("A strongly connected component was created with zero nodes.");
}
NamedStronglyConnectedComponent namedComponent = new NamedStronglyConnectedComponent(name, stronglyConnectedComponent);
namedComponents.add(namedComponent);
for (String providedSymbol : stronglyConnectedComponent) {
namedComponentsIndex.put(providedSymbol, namedComponent);
}
}
// Visibility argument.
StringBuilder visibilityBuilder = new StringBuilder(" visibility = [\n");
SortedSet<String> visibilities = FluentIterable.from(suggestedNode.getVisibilityPatterns()).transform(VisibilityPattern::getRepresentation).toSortedSet(Ordering.natural());
for (String visibility : visibilities) {
visibilityBuilder.append(" '" + visibility + "',\n");
}
visibilityBuilder.append(" ],\n");
String visibilityArg = visibilityBuilder.toString();
// Print out the new version of the original rule.
console.getStdOut().printf("java_library(\n" + " name = '%s',\n" + " exported_deps = [\n", suggestedTarget.getShortName());
for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
console.getStdOut().printf(" ':%s',\n", namedComponent.name);
}
console.getStdOut().print(" ],\n" + visibilityArg + ")\n");
// Print out a rule for each of the strongly connected components.
JavaDepsFinder.DependencyInfo dependencyInfo = javaDepsFinder.findDependencyInfoForGraph(graph);
for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
String buildRuleDefinition = createBuildRuleDefinition(namedComponent, providedSymbolToSrc, providedSymbolToRequiredSymbols, namedComponentsIndex, dependencyInfo, symbolsDependencies, visibilityArg);
console.getStdOut().print(buildRuleDefinition);
}
}
use of java.util.SortedSet in project buck by facebook.
the class JavaDepsFinder method findDepsForBuildFiles.
private DepsForBuildFiles findDepsForBuildFiles(final TargetGraph graph, final DependencyInfo dependencyInfo, final Console console) {
// For the rules that expect to have their deps generated, look through all of their required
// symbols and try to find the build rule that provides each symbols. Store these build rules in
// the depsForBuildFiles data structure.
//
// Currently, we process each rule with autodeps=True on a single thread. See the class overview
// for DepsForBuildFiles about what it would take to do this work in a multi-threaded way.
DepsForBuildFiles depsForBuildFiles = new DepsForBuildFiles();
for (final TargetNode<?, ?> rule : dependencyInfo.rulesWithAutodeps) {
final Set<BuildTarget> providedDeps = dependencyInfo.rulesWithAutodepsToProvidedDeps.get(rule);
final Predicate<TargetNode<?, ?>> isVisibleDepNotAlreadyInProvidedDeps = provider -> provider.isVisibleTo(graph, rule) && !providedDeps.contains(provider.getBuildTarget());
final boolean isJavaTestRule = rule.getDescription() instanceof JavaTestDescription;
for (DependencyType type : DependencyType.values()) {
HashMultimap<TargetNode<?, ?>, String> ruleToSymbolsMap;
switch(type) {
case DEPS:
ruleToSymbolsMap = dependencyInfo.ruleToRequiredSymbols;
break;
case EXPORTED_DEPS:
ruleToSymbolsMap = dependencyInfo.ruleToExportedSymbols;
break;
default:
throw new IllegalStateException("Unrecognized type: " + type);
}
final DependencyType typeOfDepToAdd;
if (isJavaTestRule) {
// java_test rules do not honor exported_deps: add all dependencies to the ordinary deps.
typeOfDepToAdd = DependencyType.DEPS;
} else {
typeOfDepToAdd = type;
}
for (String requiredSymbol : ruleToSymbolsMap.get(rule)) {
BuildTarget provider = findProviderForSymbolFromBuckConfig(requiredSymbol);
if (provider != null) {
depsForBuildFiles.addDep(rule.getBuildTarget(), provider, typeOfDepToAdd);
continue;
}
Set<TargetNode<?, ?>> providers = dependencyInfo.symbolToProviders.get(requiredSymbol);
SortedSet<TargetNode<?, ?>> candidateProviders = providers.stream().filter(isVisibleDepNotAlreadyInProvidedDeps).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
int numCandidates = candidateProviders.size();
if (numCandidates == 1) {
depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(candidateProviders).getBuildTarget(), typeOfDepToAdd);
} else if (numCandidates > 1) {
// Warn the user that there is an ambiguity. This could be very common with macros that
// generate multiple versions of a java_library() with the same sources.
// If numProviders is 0, then hopefully the dep is provided by something the user
// hardcoded in the BUCK file.
console.printErrorText(String.format("WARNING: Multiple providers for %s: %s. " + "Consider adding entry to .buckconfig to eliminate ambiguity:\n" + "[autodeps]\n" + "java-package-mappings = %s => %s", requiredSymbol, Joiner.on(", ").join(candidateProviders), requiredSymbol, Iterables.getFirst(candidateProviders, null)));
} else {
// If there aren't any candidates, then see if there is a visible rule that can provide
// the symbol via its exported_deps. We make this a secondary check because we prefer to
// depend on the rule that defines the symbol directly rather than one of possibly many
// rules that provides it via its exported_deps.
ImmutableSortedSet<TargetNode<?, ?>> newCandidates = providers.stream().flatMap(candidate -> dependencyInfo.ruleToRulesThatExportIt.get(candidate).stream()).filter(ruleThatExportsCandidate -> ruleThatExportsCandidate.isVisibleTo(graph, rule)).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
int numNewCandidates = newCandidates.size();
if (numNewCandidates == 1) {
depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(newCandidates).getBuildTarget(), typeOfDepToAdd);
} else if (numNewCandidates > 1) {
console.printErrorText(String.format("WARNING: No providers found for '%s' for build rule %s, " + "but there are multiple rules that export a rule to provide %s: %s", requiredSymbol, rule.getBuildTarget(), requiredSymbol, Joiner.on(", ").join(newCandidates)));
}
// In the case that numNewCandidates is 0, we assume that the user is taking
// responsibility for declaring a provider for the symbol by hardcoding it in the deps.
}
}
}
}
return depsForBuildFiles;
}
use of java.util.SortedSet in project OpenGrok by OpenGrok.
the class JFlexXref method writeSymbolTable.
/**
* Write a JavaScript function that returns an array with the definitions to
* list in the navigation panel. Each element of the array is itself an
* array containing the name of the definition type, the CSS class name for
* the type, and an array of (symbol, line) pairs for the definitions of
* that type.
*/
private void writeSymbolTable() throws IOException {
if (defs == null) {
// No definitions, no symbol table to write
return;
}
// We want the symbol table to be sorted
Comparator<Tag> cmp = new Comparator<Tag>() {
@Override
public int compare(Tag tag1, Tag tag2) {
// Order by symbol name, and then by line number if multiple
// definitions use the same symbol name
int ret = tag1.symbol.compareTo(tag2.symbol);
if (ret == 0) {
ret = tag1.line - tag2.line;
}
return ret;
}
};
Map<String, SortedSet<Tag>> symbols = new HashMap<>();
for (Tag tag : defs.getTags()) {
Style style = getStyle(tag.type);
if (style != null && style.title != null) {
SortedSet<Tag> tags = symbols.get(style.name);
if (tags == null) {
tags = new TreeSet<>(cmp);
symbols.put(style.name, tags);
}
tags.add(tag);
}
}
//TODO try to get rid of included js scripts generated from here (all js should ideally be in util)
out.append("<script type=\"text/javascript\">/* <![CDATA[ */\n");
out.append("function get_sym_list(){return [");
boolean first = true;
for (Style style : DEFINITION_STYLES) {
SortedSet<Tag> tags = symbols.get(style.name);
if (tags != null) {
if (!first) {
out.append(',');
}
out.append("[\"");
out.append(style.title);
out.append("\",\"");
out.append(style.ssClass);
out.append("\",[");
boolean firstTag = true;
for (Tag tag : tags) {
if (!firstTag) {
out.append(',');
}
out.append('[');
out.append(Util.jsStringLiteral(tag.symbol));
out.append(',');
out.append(Integer.toString(tag.line));
out.append(']');
firstTag = false;
}
out.append("]]");
first = false;
}
}
/* no LF intentionally - xml is whitespace aware ... */
out.append("];} /* ]]> */</script>");
}
use of java.util.SortedSet in project pinot by linkedin.
the class ContributorViewHandler method getRowsSortedByTime.
private Map<String, SortedSet<Row>> getRowsSortedByTime(TimeOnTimeComparisonResponse response) {
Map<String, SortedSet<Row>> result = new HashMap<>();
int numRows = response.getNumRows();
for (int i = 0; i < numRows; i++) {
Row row = response.getRow(i);
String dimensionName = row.getDimensionName();
String dimensionValue = row.getDimensionValue();
String rowGroupKey = dimensionName + "." + dimensionValue;
if (result.containsKey(rowGroupKey)) {
result.get(rowGroupKey).add(row);
} else {
SortedSet<Row> rows = new TreeSet<>(rowComparator);
rows.add(row);
result.put(rowGroupKey, rows);
}
}
return result;
}
Aggregations