Java Code Examples for java.util.TreeMap#computeIfAbsent()

The following examples show how to use java.util.TreeMap#computeIfAbsent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sailfish-core   File: StatisticsUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static List<KnownBugCategoryRow> groupKnownBugsByCategory(List<KnownBugRow> knownBugRows) {
    TreeMap<String[], KnownBugCategoryRow> bugs = new TreeMap<>(CATEGORIES_COMPARATOR);
    for (KnownBugRow knownBugRow : knownBugRows) {
        String[] categories = knownBugRow.getCategories();
        KnownBugCategoryRow bugCategoryRow = bugs.computeIfAbsent(categories, KnownBugCategoryRow::new);

        if (BooleanUtils.isTrue(knownBugRow.getReproduced())) {
            bugCategoryRow.getReproducedBugs().add(knownBugRow.getSubject());
        } else {
            bugCategoryRow.getNonReproducedBugs().add(knownBugRow.getSubject());
        }
    }
    return new ArrayList<>(bugs.values());
}
 
Example 2
/**
 * Precondition:
 * 1. Index are rotated with name pattern ".opendistro-anomaly-results-history-{now/d}-1" and now is using UTC.
 * 2. Latest entry with error is recorded within enabled and disabled time.  Note disabled time can be null.
 *
 * Error is populated if error of the latest anomaly result is not empty.
 *
 * Two optimization to avoid scanning all anomaly result indices to get a detector's most recent error
 *
 * First, when a detector is running, we only need to scan the current index, not all of the rolled over ones
 *  since we are interested in the latest error.
 * Second, when a detector is disabled, we only need to scan the latest anomaly result indices created before the
 *  detector's enable time.
 *
 * @param detectorId detector id
 * @param enabledTimeMillis the time when AD job is enabled in milliseconds
 * @param listener listener to process the returned error or exception
 */
private void profileError(
    String detectorId,
    long enabledTimeMillis,
    Instant disabledTime,
    MultiResponsesDelegateActionListener<DetectorProfile> listener
) {
    String[] latestIndex = null;

    long disabledTimeMillis = 0;
    if (disabledTime != null) {
        disabledTimeMillis = disabledTime.toEpochMilli();
    }
    if (enabledTimeMillis > disabledTimeMillis) {
        // detector is still running
        latestIndex = new String[1];
        latestIndex[0] = AnomalyResult.ANOMALY_RESULT_INDEX;
    } else {
        String[] concreteIndices = indexNameExpressionResolver
            .concreteIndexNames(
                clusterService.state(),
                IndicesOptions.lenientExpandOpen(),
                AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN
            );

        // find the latest from result indices such as .opendistro-anomaly-results-history-2020.04.06-1 and
        // /.opendistro-anomaly-results-history-2020.04.07-000002
        long maxTimestamp = -1;
        TreeMap<Long, List<String>> candidateIndices = new TreeMap<>();
        for (String indexName : concreteIndices) {
            Matcher m = Pattern.compile("\\.opendistro-anomaly-results-history-(\\d{4})\\.(\\d{2})\\.(\\d{2})-\\d+").matcher(indexName);
            if (m.matches()) {
                int year = Integer.parseInt(m.group(1));
                int month = Integer.parseInt(m.group(2));
                int date = Integer.parseInt(m.group(3));
                // month starts with 0
                calendar.clear();
                calendar.set(year, month - 1, date);
                // 2020.05.08 is translated to 1588896000000
                long timestamp = calendar.getTimeInMillis();

                // a candidate index can be created before or after enabled time, but the index is definitely created before disabled
                // time
                if (timestamp <= disabledTimeMillis && maxTimestamp <= timestamp) {
                    maxTimestamp = timestamp;
                    // we can have two rotations on the same day and we don't know which one has our data, so we keep all
                    List<String> indexList = candidateIndices.computeIfAbsent(timestamp, k -> new ArrayList<String>());
                    indexList.add(indexName);
                }
            }
        }
        List<String> candidates = new ArrayList<String>();
        List<String> latestCandidate = candidateIndices.get(maxTimestamp);

        if (latestCandidate != null) {
            candidates.addAll(latestCandidate);
        }

        // look back one more index for an edge case:
        // Suppose detector interval is 1 minute. Detector last run is at 2020-05-07, 11:59:50 PM,
        // then AD result indices rolled over as .opendistro-anomaly-results-history-2020.05.07-001
        // Detector next run will be 2020-05-08, 00:00:50 AM. If a user stop the detector at
        // 2020-05-08 00:00:10 AM, detector will not have AD result on 2020-05-08.
        // We check AD result indices one day earlier to make sure we can always get AD result.
        Map.Entry<Long, List<String>> earlierCandidate = candidateIndices.lowerEntry(maxTimestamp);
        if (earlierCandidate != null) {
            candidates.addAll(earlierCandidate.getValue());
        }
        latestIndex = candidates.toArray(new String[0]);
    }

    if (latestIndex == null || latestIndex.length == 0) {
        // no result index found: can be due to anomaly result is not created yet or result indices for the detector have been deleted.
        listener.onResponse(new DetectorProfile());
        return;
    }
    SearchRequest searchLatestResult = createLatestAnomalyResultRequest(detectorId, enabledTimeMillis, disabledTimeMillis, latestIndex);
    client.search(searchLatestResult, onGetLatestAnomalyResult(listener, detectorId));
}
 
Example 3
public Schema(Config config) throws Exception {

        this.config = config;

        String informationSchemaSql = (String) config.config.get("information_schema.query");
        if (informationSchemaSql.isEmpty()){
            throw new IllegalArgumentException("information_schema.query is missing");
        }

        Connection conSrc = config.connect(config.source);
        Statement statement = conSrc.createStatement();
        ResultSet resultSet = statement.executeQuery(informationSchemaSql);

        if (!resultSet.isBeforeFirst()){
            System.out.println("information_schema.query returned no results: \n" + informationSchemaSql);
            throw new IllegalArgumentException("information_schema.query returned no results");
        }

        ResultSetMetaData metaData = resultSet.getMetaData();

        int columnCount = metaData.getColumnCount();
        int[] columnTypes = new int[columnCount];
        for (int i=1; i <= columnCount; i++){
            columnTypes[i - 1] = metaData.getColumnType(i);
        }

        schema = new TreeMap(String.CASE_INSENSITIVE_ORDER);

        while (resultSet.next()){

            String schemaName = resultSet.getString(TABLE_SCHEMA);
            String tableName  = resultSet.getString(TABLE_NAME);
            String fullTableName = schemaName + "." + tableName;

            Table table = schema.computeIfAbsent(fullTableName, Table::new);

            String columnName = resultSet.getString(COLUMN_NAME);
            boolean isIdentity = Util.isSqlTrue(resultSet.getString(IS_IDENTITY));
            boolean isComputed = Util.isSqlTrue(resultSet.getString(IS_COMPUTED));
            String definition = isComputed ? resultSet.getString(COLUMN_DEFINITION) : resultSet.getString(COLUMN_DEFAULT);

            Column col = new Column(
                 columnName
                ,resultSet.getString(DATA_TYPE)
                ,resultSet.getInt(ORDINAL_POSITION)
                ,Util.isSqlTrue(resultSet.getObject(IS_NULLABLE))
                ,resultSet.getInt(CHARACTER_MAXIMUM_LENGTH)
                ,resultSet.getInt(NUMERIC_PRECISION)
                ,isIdentity
                ,isComputed
                ,definition    // in MSSQL user must have GRANT VIEW DEFINITION ON database to see default values
            );

            table.columns.add(col);

            if (isIdentity)
                table.setIdentity(col);
        }
    }
 
Example 4
public SortedMap<Long, List<Long>> getMatrixRunAndTestCaseRunIDs(AggregateReportParameters params) {
    boolean tagsFilter = CollectionUtils.isNotEmpty(params.getTags());
    boolean sfInstancesFilter = CollectionUtils.isNotEmpty(params.getSfInstances());
    boolean fromFilter = params.getFrom() != null;
    boolean toFilter = params.getTo() != null;

    String queryString = "select MR.id, TCR.id "
            + "from TestCaseRun as TCR "
            + "right join TCR.matrixRun as MR "
            + "join MR.sfInstance as SF "
            + "where ";

    ICondition whereClause = null;
    if (fromFilter) {
        whereClause = create("MR.startTime >= :from");
    }
    if (toFilter) {
        whereClause = and(whereClause, create("MR.finishTime <= :to"));
    }
    if (tagsFilter) {
        ICondition tagCondition = create("(exists (select MR2.id from "
                        + "MatrixRun as MR2 "
                        + "join MR2.tags as T "
                        + "where MR2.id = MR.id "
                        + "and T.id in (:mrTags) "
                        + "group by MR2.id ) "
                        + "or "
                        + "exists (select TCR2.id from "
                        + "TestCaseRun as TCR2 "
                        + "join TCR2.tags as TCRT "
                        + "where TCR2.id = TCR.id "
                        + "and TCRT.tag.id in (:tcrTags) "
                        + "group by TCR2.id ))");
        whereClause = params.getLogicalOperator().create(whereClause, tagCondition);
    }
    if ((fromFilter || toFilter) && tagsFilter) {
        whereClause = wrap(whereClause);
    }
    if (sfInstancesFilter) {
        whereClause = and(whereClause, create("SF.id in (:ids)"));
    }
    queryString += whereClause == null ? "" : whereClause.getCondition();
    queryString += " order by MR.startTime, MR.id";

    Session session = sessionFactory.openSession();
    try (AutoCloseable ignore = session::close) {
        Query query = session.createQuery(queryString);
        if (fromFilter) {
            query.setParameter("from", params.getFrom());
        }
        if (toFilter) {
            query.setParameter("to", params.getTo());
        }
        if (sfInstancesFilter) {
            query.setParameterList("ids", toIds(params.getSfInstances()));
        }
        if (tagsFilter) {
            Object[] tagsIds = tagsToIds(params.getTags());
            query.setParameterList("mrTags", tagsIds);
            query.setParameterList("tcrTags", tagsIds);
        }
        @SuppressWarnings("unchecked")
        List<Object[]>  matrixAndTestCaseIDs = query.list();
        TreeMap<Long, List<Long>> result = new TreeMap<>();
        for (Object[] ids : matrixAndTestCaseIDs) {
            Long matrixId = (Long) ids[0];
            Long testCaseId = (Long) ids[1];
            List<Long> testCaseIds = result.computeIfAbsent(matrixId, l -> new ArrayList<>());
            if (testCaseId != null) {
                testCaseIds.add(testCaseId);
            }
        }
        return result;
    } catch (Exception e) {
        throw new EPSCommonException(e);
    }
}
 
Example 5
Source Project: revapi   File: JavaApiAnalyzer.java    License: Apache License 2.0 4 votes vote down vote up
private static void add(MethodElement method, TreeMap<String, List<MethodElement>> methods) {
    String name = method.getDeclaringElement().getSimpleName().toString();
    List<MethodElement> overloads = methods.computeIfAbsent(name, __ -> new ArrayList<>());

    overloads.add(method);
}