ValueColumnMetadata.java 3.5 KB
Newer Older
彭勇升 pengys 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.skywalking.oap.server.core.storage.annotation;

21 22
import java.util.HashMap;
import java.util.Map;
wu-sheng's avatar
wu-sheng 已提交
23 24
import java.util.Optional;
import lombok.Getter;
wu-sheng's avatar
wu-sheng 已提交
25
import lombok.RequiredArgsConstructor;
彭勇升 pengys 已提交
26 27
import org.apache.skywalking.oap.server.core.query.sql.Function;

28 29 30 31 32
/**
 * ValueColumnMetadata holds the metadata for column values of metrics. The metadata of ValueColumn is declared through
 * {@link Column} annotation.
 */
public enum ValueColumnMetadata {
彭勇升 pengys 已提交
33 34
    INSTANCE;

35 36
    private final Map<String, ValueColumn> mapping = new HashMap<>();
    private final HashMap<String, String> columnNameOverrideRule = new HashMap<>();
彭勇升 pengys 已提交
37

38 39 40
    /**
     * Register the new metadata for the given model name.
     */
wu-sheng's avatar
wu-sheng 已提交
41 42 43 44
    public void putIfAbsent(String modelName,
                            String valueCName,
                            Column.ValueDataType dataType,
                            Function function,
45 46 47
                            int defaultValue,
                            int scopeId) {
        mapping.putIfAbsent(modelName, new ValueColumn(valueCName, dataType, function, defaultValue, scopeId));
彭勇升 pengys 已提交
48 49
    }

50 51 52 53
    public void overrideColumnName(String oldName, String newName) {
        columnNameOverrideRule.put(oldName, newName);
    }

54 55 56
    /**
     * Fetch the value column name of the given metrics name.
     */
57
    public String getValueCName(String metricsName) {
58 59
        final String valueCName = findColumn(metricsName).valueCName;
        return columnNameOverrideRule.getOrDefault(valueCName, valueCName);
彭勇升 pengys 已提交
60 61
    }

62 63 64
    /**
     * Fetch the function for the value column of the given metrics name.
     */
65 66 67 68
    public Function getValueFunction(String metricsName) {
        return findColumn(metricsName).function;
    }

wu-sheng's avatar
wu-sheng 已提交
69 70 71 72
    public int getDefaultValue(String metricsName) {
        return findColumn(metricsName).defaultValue;
    }

73 74 75
    /**
     * @return metric metadata if found
     */
wu-sheng's avatar
wu-sheng 已提交
76 77 78 79
    public Optional<ValueColumn> readValueColumnDefinition(String metricsName) {
        return Optional.ofNullable(mapping.get(metricsName));
    }

80 81 82 83 84 85 86
    /**
     * @return all metrics metadata.
     */
    public Map<String, ValueColumn> getAllMetadata() {
        return mapping;
    }

87 88 89 90 91 92
    private ValueColumn findColumn(String metricsName) {
        ValueColumn column = mapping.get(metricsName);
        if (column == null) {
            throw new RuntimeException("Metrics:" + metricsName + " doesn't have value column definition");
        }
        return column;
彭勇升 pengys 已提交
93 94
    }

wu-sheng's avatar
wu-sheng 已提交
95
    @Getter
wu-sheng's avatar
wu-sheng 已提交
96
    @RequiredArgsConstructor
97
    public static class ValueColumn {
彭勇升 pengys 已提交
98
        private final String valueCName;
wu-sheng's avatar
wu-sheng 已提交
99
        private final Column.ValueDataType dataType;
彭勇升 pengys 已提交
100
        private final Function function;
wu-sheng's avatar
wu-sheng 已提交
101
        private final int defaultValue;
102
        private final int scopeId;
彭勇升 pengys 已提交
103 104
    }
}