List of usage examples for org.apache.lucene.util CollectionUtil timSort
public static <T> void timSort(List<T> list, Comparator<? super T> comp)
From source file:org.codelibs.bench.core.action.CompetitionSummary.java
License:Apache License
/** * Calculates statistical measures from raw measurements. Should be called prior to accessing * individual measurements./* w w w .j a va 2 s . c o m*/ */ public void computeSummaryStatistics() { if (computed) { return; } long totalWarmupTime = 0; final SinglePassStatistics single = new SinglePassStatistics(); for (CompetitionNodeResult nodeResult : nodeResults) { totalWarmupTime += nodeResult.warmUpTime(); totalIterations += nodeResult.totalIterations(); completedIterations += nodeResult.completedIterations(); // only calculate statistics for iterations completed thus far for (int i = 0; i < nodeResult.completedIterations(); i++) { CompetitionIteration competitionIteration = nodeResult.iterations().get(i); CompetitionIterationData iterationData = competitionIteration.competitionIterationData(); long[] data = iterationData.data(); for (long datum : data) { if (datum > -1) { // ignore unset values in the underlying array single.push(datum); } } totalQueries += competitionIteration.numQueries(); totalTime += competitionIteration.totalTime(); sumTotalHits += competitionIteration.sumTotalHits(); // keep track of slowest requests if (competitionIteration.slowRequests() != null) { for (CompetitionIteration.SlowRequest slow : competitionIteration.slowRequests()) { slowest.add(new Tuple<>(nodeResult.nodeName(), slow)); } } } } min = single.min(); max = single.max(); mean = single.mean(); stdDeviation = single.stddev(); avgWarmupTime = (nodeResults.size() > 0) ? totalWarmupTime / nodeResults.size() : 0.0; queriesPerSecond = (single.sum() > 0) ? (totalQueries * (1000.0 / (double) single.sum())) : 0.0; millisPerHit = (sumTotalHits > 0) ? (totalTime / (double) sumTotalHits) : 0.0; for (double percentile : percentiles) { percentileValues.put(percentile, single.percentile(percentile / 100.0d)); } CollectionUtil.timSort(slowest, new Comparator<Tuple<String, CompetitionIteration.SlowRequest>>() { @Override public int compare(Tuple<String, CompetitionIteration.SlowRequest> o1, Tuple<String, CompetitionIteration.SlowRequest> o2) { return Long.compare(o2.v2().maxTimeTaken(), o1.v2().maxTimeTaken()); } }); computed = true; }
From source file:org.codelibs.elasticsearch.common.settings.AbstractScopedSettings.java
License:Apache License
/** * Validates that the setting is valid/*from w w w . ja va 2s . com*/ */ public final void validate(String key, Settings settings) { Setting setting = get(key); if (setting == null) { LevensteinDistance ld = new LevensteinDistance(); List<Tuple<Float, String>> scoredKeys = new ArrayList<>(); for (String k : this.keySettings.keySet()) { float distance = ld.getDistance(key, k); if (distance > 0.7f) { scoredKeys.add(new Tuple<>(distance, k)); } } CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1())); String msg = "unknown setting [" + key + "]"; List<String> keys = scoredKeys.stream().map((a) -> a.v2()).collect(Collectors.toList()); if (keys.isEmpty() == false) { msg += " did you mean " + (keys.size() == 1 ? "[" + keys.get(0) + "]" : "any of " + keys.toString()) + "?"; } else { msg += " please check that any required plugins are installed, or check the breaking changes documentation for removed " + "settings"; } throw new IllegalArgumentException(msg); } setting.get(settings); }
From source file:org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction.java
License:Apache License
private List<IndexTemplateMetaData> findTemplates(BulkCreateIndicesRequest request, ClusterState state, IndexTemplateFilter indexTemplateFilter) { List<IndexTemplateMetaData> templates = new ArrayList<>(); CreateIndexClusterStateUpdateRequest dummyRequest = new CreateIndexClusterStateUpdateRequest(request, "bulk-create", request.indices().iterator().next()); // note: only use the first index name to see if template matches. // this means for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) { IndexTemplateMetaData template = cursor.value; if (indexTemplateFilter.apply(dummyRequest, template)) { templates.add(template);/*from ww w .j ava 2s .co m*/ } } CollectionUtil.timSort(templates, new Comparator<IndexTemplateMetaData>() { @Override public int compare(IndexTemplateMetaData o1, IndexTemplateMetaData o2) { return o2.order() - o1.order(); } }); return templates; }
From source file:org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction.java
License:Apache License
private List<IndexTemplateMetaData> getAllTemplatesByOrder(ClusterState state) { List<IndexTemplateMetaData> templateMetadata = new ArrayList<>(); for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) { IndexTemplateMetaData metadata = cursor.value; templateMetadata.add(metadata);//ww w . j a v a 2 s . c o m } CollectionUtil.timSort(templateMetadata, Comparator.comparingInt(IndexTemplateMetaData::order).reversed()); return templateMetadata; }
From source file:org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction.java
License:Apache License
private List<IndexTemplateMetaData> findTemplates(CreatePartitionsRequest request, ClusterState state) { List<IndexTemplateMetaData> templates = new ArrayList<>(); String firstIndex = request.indices().iterator().next(); // note: only use the first index name to see if template matches. // this means for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) { IndexTemplateMetaData template = cursor.value; for (String pattern : template.getPatterns()) { if (Regex.simpleMatch(pattern, firstIndex)) { templates.add(template); break; }/* w w w . j a v a 2 s . c o m*/ } } CollectionUtil.timSort(templates, (o1, o2) -> o2.order() - o1.order()); return templates; }
From source file:org.elasticsearch.action.admin.indices.get.GetIndexResponse.java
License:Apache License
public static GetIndexResponse fromXContent(XContentParser parser) throws IOException { ImmutableOpenMap.Builder<String, List<AliasMetaData>> aliases = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> mappings = ImmutableOpenMap .builder();/*from www.ja v a 2 s.co m*/ ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder(); List<String> indices = new ArrayList<>(); if (parser.currentToken() == null) { parser.nextToken(); } ensureExpectedToken(Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation); parser.nextToken(); while (!parser.isClosed()) { if (parser.currentToken() == Token.START_OBJECT) { // we assume this is an index entry String indexName = parser.currentName(); indices.add(indexName); IndexEntry indexEntry = parseIndexEntry(parser); // make the order deterministic CollectionUtil.timSort(indexEntry.indexAliases, Comparator.comparing(AliasMetaData::alias)); aliases.put(indexName, Collections.unmodifiableList(indexEntry.indexAliases)); mappings.put(indexName, indexEntry.indexMappings); settings.put(indexName, indexEntry.indexSettings); if (indexEntry.indexDefaultSettings.isEmpty() == false) { defaultSettings.put(indexName, indexEntry.indexDefaultSettings); } } else if (parser.currentToken() == Token.START_ARRAY) { parser.skipChildren(); } else { parser.nextToken(); } } return new GetIndexResponse(indices.toArray(new String[0]), mappings.build(), aliases.build(), settings.build(), defaultSettings.build()); }
From source file:org.elasticsearch.action.admin.indices.get.GetIndexResponseTests.java
License:Apache License
@Override protected GetIndexResponse createTestInstance() { String[] indices = generateRandomStringArray(5, 5, false, false); ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> mappings = ImmutableOpenMap .builder();/*ww w . j a va 2s .com*/ ImmutableOpenMap.Builder<String, List<AliasMetaData>> aliases = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder(); IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; boolean includeDefaults = randomBoolean(); for (String index : indices) { mappings.put(index, GetMappingsResponseTests.createMappingsForIndex()); List<AliasMetaData> aliasMetaDataList = new ArrayList<>(); int aliasesNum = randomIntBetween(0, 3); for (int i = 0; i < aliasesNum; i++) { aliasMetaDataList.add(GetAliasesResponseTests.createAliasMetaData()); } CollectionUtil.timSort(aliasMetaDataList, Comparator.comparing(AliasMetaData::alias)); aliases.put(index, Collections.unmodifiableList(aliasMetaDataList)); Settings.Builder builder = Settings.builder(); builder.put(RandomCreateIndexGenerator.randomIndexSettings()); settings.put(index, builder.build()); if (includeDefaults) { defaultSettings.put(index, indexScopedSettings.diff(settings.get(index), Settings.EMPTY)); } } return new GetIndexResponse(indices, mappings.build(), aliases.build(), settings.build(), defaultSettings.build()); }
From source file:org.elasticsearch.cluster.metadata.MetaData.java
License:Apache License
/** * Finds the specific index aliases that match with the specified aliases directly or partially via wildcards and * that point to the specified concrete indices or match partially with the indices via wildcards. * * @param aliases The names of the index aliases to find * @param concreteIndices The concrete indexes the index aliases must point to order to be returned. * @return the found index aliases grouped by index *///from w w w. j av a2 s.co m public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[] aliases, String[] concreteIndices) { assert aliases != null; assert concreteIndices != null; if (concreteIndices.length == 0) { return ImmutableOpenMap.of(); } boolean matchAllAliases = matchAllAliases(aliases); ImmutableOpenMap.Builder<String, List<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder(); Iterable<String> intersection = HppcMaps.intersection(ObjectHashSet.from(concreteIndices), indices.keys()); for (String index : intersection) { IndexMetaData indexMetaData = indices.get(index); List<AliasMetaData> filteredValues = new ArrayList<>(); for (ObjectCursor<AliasMetaData> cursor : indexMetaData.getAliases().values()) { AliasMetaData value = cursor.value; if (matchAllAliases || Regex.simpleMatch(aliases, value.alias())) { filteredValues.add(value); } } if (!filteredValues.isEmpty()) { // Make the list order deterministic CollectionUtil.timSort(filteredValues, new Comparator<AliasMetaData>() { @Override public int compare(AliasMetaData o1, AliasMetaData o2) { return o1.alias().compareTo(o2.alias()); } }); mapBuilder.put(index, Collections.unmodifiableList(filteredValues)); } } return mapBuilder.build(); }
From source file:org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.java
License:Apache License
private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateRequest request, ClusterState state) {//from w w w. java 2s . co m List<IndexTemplateMetaData> templates = Lists.newArrayList(); for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) { IndexTemplateMetaData template = cursor.value; if (Regex.simpleMatch(template.template(), request.index())) { templates.add(template); } } // see if we have templates defined under config File templatesDir = new File(environment.configFile(), "templates"); if (templatesDir.exists() && templatesDir.isDirectory()) { File[] templatesFiles = templatesDir.listFiles(); if (templatesFiles != null) { for (File templatesFile : templatesFiles) { XContentParser parser = null; try { byte[] templatesData = Streams.copyToByteArray(templatesFile); parser = XContentHelper.createParser(templatesData, 0, templatesData.length); IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser); if (Regex.simpleMatch(template.template(), request.index())) { templates.add(template); } } catch (Exception e) { logger.warn("[{}] failed to read template [{}] from config", e, request.index(), templatesFile.getAbsolutePath()); } finally { IOUtils.closeWhileHandlingException(parser); } } } } CollectionUtil.timSort(templates, new Comparator<IndexTemplateMetaData>() { @Override public int compare(IndexTemplateMetaData o1, IndexTemplateMetaData o2) { return o2.order() - o1.order(); } }); return templates; }
From source file:org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService.java
License:Apache License
/** * Finds index templates whose index pattern matched with the given index name. * The result is sorted by {@link IndexTemplateMetaData#order} descending. *//* w w w.jav a 2 s . co m*/ public static List<IndexTemplateMetaData> findTemplates(MetaData metaData, String indexName) { final List<IndexTemplateMetaData> matchedTemplates = new ArrayList<>(); for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) { final IndexTemplateMetaData template = cursor.value; final boolean matched = template.patterns().stream() .anyMatch(pattern -> Regex.simpleMatch(pattern, indexName)); if (matched) { matchedTemplates.add(template); } } CollectionUtil.timSort(matchedTemplates, Comparator.comparingInt(IndexTemplateMetaData::order).reversed()); return matchedTemplates; }