DataDefine.java 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19
package org.skywalking.apm.collector.storage.define;
P
pengys5 已提交
20

P
pengys5 已提交
21
import org.skywalking.apm.collector.core.stream.Data;
P
pengys5 已提交
22
import org.skywalking.apm.collector.remote.grpc.proto.RemoteData;
23 24 25 26 27 28 29 30

/**
 * @author pengys5
 */
public abstract class DataDefine {
    private Attribute[] attributes;
    private int stringCapacity;
    private int longCapacity;
31
    private int doubleCapacity;
P
pengys5 已提交
32
    private int integerCapacity;
P
pengys5 已提交
33
    private int booleanCapacity;
P
pengys5 已提交
34
    private int byteCapacity;
35 36

    public DataDefine() {
P
pengys5 已提交
37
        initial();
38 39
    }

P
pengys5 已提交
40
    private void initial() {
P
pengys5 已提交
41 42
        attributes = new Attribute[initialCapacity()];
        attributeDefine();
43 44 45 46 47
        for (Attribute attribute : attributes) {
            if (AttributeType.STRING.equals(attribute.getType())) {
                stringCapacity++;
            } else if (AttributeType.LONG.equals(attribute.getType())) {
                longCapacity++;
48 49
            } else if (AttributeType.DOUBLE.equals(attribute.getType())) {
                doubleCapacity++;
P
pengys5 已提交
50 51
            } else if (AttributeType.INTEGER.equals(attribute.getType())) {
                integerCapacity++;
P
pengys5 已提交
52 53
            } else if (AttributeType.BOOLEAN.equals(attribute.getType())) {
                booleanCapacity++;
P
pengys5 已提交
54 55
            } else if (AttributeType.BYTE.equals(attribute.getType())) {
                byteCapacity++;
56 57 58 59 60 61 62 63 64 65 66 67
            }
        }
    }

    public final void addAttribute(int position, Attribute attribute) {
        attributes[position] = attribute;
    }

    protected abstract int initialCapacity();

    protected abstract void attributeDefine();

P
pengys5 已提交
68
    public final Data build(String id) {
69
        return new Data(id, stringCapacity, longCapacity, doubleCapacity, integerCapacity, booleanCapacity, byteCapacity);
70 71 72 73 74
    }

    public void mergeData(Data newData, Data oldData) {
        int stringPosition = 0;
        int longPosition = 0;
75
        int doublePosition = 0;
P
pengys5 已提交
76
        int integerPosition = 0;
P
pengys5 已提交
77
        int booleanPosition = 0;
P
pengys5 已提交
78
        int bytePosition = 0;
79 80 81
        for (int i = 0; i < initialCapacity(); i++) {
            Attribute attribute = attributes[i];
            if (AttributeType.STRING.equals(attribute.getType())) {
82 83
                String stringData = attribute.getOperation().operate(newData.getDataString(stringPosition), oldData.getDataString(stringPosition));
                newData.setDataString(stringPosition, stringData);
84 85
                stringPosition++;
            } else if (AttributeType.LONG.equals(attribute.getType())) {
86 87
                Long longData = attribute.getOperation().operate(newData.getDataLong(longPosition), oldData.getDataLong(longPosition));
                newData.setDataLong(longPosition, longData);
88
                longPosition++;
89
            } else if (AttributeType.DOUBLE.equals(attribute.getType())) {
90 91
                Double doubleData = attribute.getOperation().operate(newData.getDataDouble(doublePosition), oldData.getDataDouble(doublePosition));
                newData.setDataDouble(doublePosition, doubleData);
92
                doublePosition++;
P
pengys5 已提交
93
            } else if (AttributeType.INTEGER.equals(attribute.getType())) {
94 95
                Integer integerData = attribute.getOperation().operate(newData.getDataInteger(integerPosition), oldData.getDataInteger(integerPosition));
                newData.setDataInteger(integerPosition, integerData);
P
pengys5 已提交
96
                integerPosition++;
P
pengys5 已提交
97
            } else if (AttributeType.BOOLEAN.equals(attribute.getType())) {
98 99 100
                Boolean booleanData = attribute.getOperation().operate(newData.getDataBoolean(booleanPosition), oldData.getDataBoolean(booleanPosition));
                newData.setDataBoolean(booleanPosition, booleanData);
                booleanPosition++;
P
pengys5 已提交
101
            } else if (AttributeType.BYTE.equals(attribute.getType())) {
102 103
                byte[] byteData = attribute.getOperation().operate(newData.getDataBytes(bytePosition), oldData.getDataBytes(integerPosition));
                newData.setDataBytes(bytePosition, byteData);
P
pengys5 已提交
104
                bytePosition++;
105 106 107
            }
        }
    }
P
pengys5 已提交
108

P
pengys5 已提交
109 110 111
    public abstract Object deserialize(RemoteData remoteData);

    public abstract RemoteData serialize(Object object);
112
}