FieldMeta.h 5.0 KB
Newer Older
Z
zhenshan.cao 已提交
1 2 3 4 5 6
// Licensed to the LF AI & Data foundation 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
F
FluorineDog 已提交
7 8
// with the License. You may obtain a copy of the License at
//
Z
zhenshan.cao 已提交
9
//     http://www.apache.org/licenses/LICENSE-2.0
F
FluorineDog 已提交
10
//
Z
zhenshan.cao 已提交
11 12 13 14 15
// 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.
F
FluorineDog 已提交
16

17
#pragma once
18 19 20 21 22

#include <optional>
#include <stdexcept>
#include <string>

X
XuanYang-cn 已提交
23
#include "common/Types.h"
F
FluorineDog 已提交
24
#include "exceptions/EasyAssert.h"
25
#include "utils/Status.h"
N
neza2017 已提交
26

X
XuanYang-cn 已提交
27
namespace milvus {
28

29
inline int
N
neza2017 已提交
30
datatype_sizeof(DataType data_type, int dim = 1) {
31 32 33 34
    switch (data_type) {
        case DataType::BOOL:
            return sizeof(bool);
        case DataType::INT8:
35
            return sizeof(int8_t);
36
        case DataType::INT16:
37
            return sizeof(int16_t);
38
        case DataType::INT32:
39
            return sizeof(int32_t);
40
        case DataType::INT64:
41
            return sizeof(int64_t);
42 43 44 45
        case DataType::FLOAT:
            return sizeof(float);
        case DataType::DOUBLE:
            return sizeof(double);
46 47 48
        case DataType::VECTOR_FLOAT:
            return sizeof(float) * dim;
        case DataType::VECTOR_BINARY: {
B
bigsheeper 已提交
49
            Assert(dim % 8 == 0);
50 51 52
            return dim / 8;
        }
        default: {
53
            throw std::invalid_argument("unsupported data type");
54
        }
X
xige-16 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }
}

// TODO: use magic_enum when available
inline std::string
datatype_name(DataType data_type) {
    switch (data_type) {
        case DataType::BOOL:
            return "bool";
        case DataType::INT8:
            return "int8_t";
        case DataType::INT16:
            return "int16_t";
        case DataType::INT32:
            return "int32_t";
        case DataType::INT64:
            return "int64_t";
72 73 74 75
        case DataType::FLOAT:
            return "float";
        case DataType::DOUBLE:
            return "double";
X
xige-16 已提交
76 77 78 79 80 81 82 83 84
        case DataType::VECTOR_FLOAT:
            return "vector_float";
        case DataType::VECTOR_BINARY: {
            return "vector_binary";
        }
        default: {
            auto err_msg = "Unsupported DataType(" + std::to_string((int)data_type) + ")";
            PanicInfo(err_msg);
        }
85 86 87
    }
}

88
inline bool
N
neza2017 已提交
89
datatype_is_vector(DataType datatype) {
90 91 92
    return datatype == DataType::VECTOR_BINARY || datatype == DataType::VECTOR_FLOAT;
}

93
inline bool
94
datatype_is_integer(DataType datatype) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    switch (datatype) {
        case DataType::INT8:
        case DataType::INT16:
        case DataType::INT32:
        case DataType::INT64:
            return true;
        default:
            return false;
    }
}

inline bool
datatype_is_floating(DataType datatype) {
    switch (datatype) {
        case DataType::FLOAT:
110
        case DataType::DOUBLE:
111 112 113 114 115 116
            return true;
        default:
            return false;
    }
}

F
FluorineDog 已提交
117
class FieldMeta {
118
 public:
119
    static const FieldMeta RowIdMeta;
C
cai.zhang 已提交
120 121 122 123 124 125 126
    FieldMeta(const FieldMeta&) = delete;
    FieldMeta(FieldMeta&&) = default;
    FieldMeta&
    operator=(const FieldMeta&) = delete;
    FieldMeta&
    operator=(FieldMeta&&) = default;

G
GuoRentong 已提交
127
    FieldMeta(const FieldName& name, FieldId id, DataType type) : name_(name), id_(id), type_(type) {
X
XuanYang-cn 已提交
128 129 130
        Assert(!is_vector());
    }

F
FluorineDog 已提交
131
    FieldMeta(const FieldName& name, FieldId id, DataType type, int64_t dim, std::optional<MetricType> metric_type)
G
GuoRentong 已提交
132
        : name_(name), id_(id), type_(type), vector_info_(VectorInfo{dim, metric_type}) {
X
XuanYang-cn 已提交
133
        Assert(is_vector());
134 135 136 137
    }

    bool
    is_vector() const {
B
bigsheeper 已提交
138
        Assert(type_ != DataType::NONE);
139 140 141
        return type_ == DataType::VECTOR_BINARY || type_ == DataType::VECTOR_FLOAT;
    }

X
XuanYang-cn 已提交
142
    int64_t
143
    get_dim() const {
X
XuanYang-cn 已提交
144 145 146
        Assert(is_vector());
        Assert(vector_info_.has_value());
        return vector_info_->dim_;
147 148
    }

F
FluorineDog 已提交
149
    std::optional<MetricType>
F
FluorineDog 已提交
150 151 152 153 154 155
    get_metric_type() const {
        Assert(is_vector());
        Assert(vector_info_.has_value());
        return vector_info_->metric_type_;
    }

G
GuoRentong 已提交
156
    const FieldName&
157 158 159 160
    get_name() const {
        return name_;
    }

G
GuoRentong 已提交
161 162 163 164 165
    const FieldId&
    get_id() const {
        return id_;
    }

166 167 168 169 170 171 172
    DataType
    get_data_type() const {
        return type_;
    }

    int
    get_sizeof() const {
X
XuanYang-cn 已提交
173
        if (is_vector()) {
N
neza2017 已提交
174
            return datatype_sizeof(type_, get_dim());
X
XuanYang-cn 已提交
175
        } else {
176
            return datatype_sizeof(type_);
X
XuanYang-cn 已提交
177
        }
178 179 180
    }

 private:
X
XuanYang-cn 已提交
181 182
    struct VectorInfo {
        int64_t dim_;
F
FluorineDog 已提交
183
        std::optional<MetricType> metric_type_;
X
XuanYang-cn 已提交
184
    };
G
GuoRentong 已提交
185 186
    FieldName name_;
    FieldId id_;
187
    DataType type_ = DataType::NONE;
X
XuanYang-cn 已提交
188
    std::optional<VectorInfo> vector_info_;
189
};
190

F
FluorineDog 已提交
191
}  // namespace milvus