mirror of https://github.com/milvus-io/milvus.git
Add system property and optimize easy assert
Signed-off-by: FluorineDog <guilin.gou@zilliz.com>pull/4973/head^2
parent
48821690b3
commit
92261e38c5
|
@ -1,6 +1,7 @@
|
|||
set(COMMON_SRC
|
||||
Schema.cpp
|
||||
Types.cpp
|
||||
SystemProperty.cpp
|
||||
)
|
||||
|
||||
add_library(milvus_common
|
||||
|
|
|
@ -22,6 +22,8 @@ struct LoadIndexInfo {
|
|||
milvus::knowhere::VecIndexPtr index;
|
||||
};
|
||||
|
||||
// NOTE: field_id can be system field
|
||||
// NOTE: Refer to common/SystemProperty.cpp for details
|
||||
struct LoadFieldDataInfo {
|
||||
int64_t field_id;
|
||||
void* blob;
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "common/Schema.h"
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include "common/SystemProperty.h"
|
||||
|
||||
namespace milvus {
|
||||
|
||||
|
@ -32,15 +33,17 @@ Schema::ParseFrom(const milvus::proto::schema::CollectionSchema& schema_proto) {
|
|||
schema->set_auto_id(schema_proto.autoid());
|
||||
|
||||
// NOTE: only two system
|
||||
std::set<std::string> system_field_names = {"RowID", "Timestamp"};
|
||||
|
||||
for (const milvus::proto::schema::FieldSchema& child : schema_proto.fields()) {
|
||||
auto field_offset = FieldOffset(schema->size());
|
||||
auto field_id = child.fieldid();
|
||||
auto name = child.name();
|
||||
if (field_id < 100) {
|
||||
auto field_id = FieldId(child.fieldid());
|
||||
auto name = FieldName(child.name());
|
||||
|
||||
if (field_id.get() < 100) {
|
||||
// system field id
|
||||
AssertInfo(system_field_names.count(name), "id of non system field should be >= 100");
|
||||
auto is_system = SystemProperty::Instance().SystemFieldVerify(name, field_id);
|
||||
AssertInfo(is_system,
|
||||
"invalid system type: name(" + name.get() + "), id(" + std::to_string(field_id.get()) + ")");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -64,9 +67,9 @@ Schema::ParseFrom(const milvus::proto::schema::CollectionSchema& schema_proto) {
|
|||
auto dim = boost::lexical_cast<int64_t>(type_map.at("dim"));
|
||||
AssertInfo(index_map.count("metric_type"), "index not found");
|
||||
auto metric_type = GetMetricType(index_map.at("metric_type"));
|
||||
schema->AddField(FieldName(name), FieldId(field_id), data_type, dim, metric_type);
|
||||
schema->AddField(name, field_id, data_type, dim, metric_type);
|
||||
} else {
|
||||
schema->AddField(FieldName(name), FieldId(field_id), data_type);
|
||||
schema->AddField(name, field_id, data_type);
|
||||
}
|
||||
}
|
||||
return schema;
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright (C) 2019-2020 Zilliz. 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
|
||||
|
||||
#include "common/SystemProperty.h"
|
||||
#include "utils/EasyAssert.h"
|
||||
|
||||
namespace milvus {
|
||||
class SystemPropertyImpl : public SystemProperty {
|
||||
public:
|
||||
[[nodiscard]] bool
|
||||
SystemFieldVerify(const FieldName& field_name, FieldId field_id) const override {
|
||||
if (!name_to_types_.count(field_name)) {
|
||||
return false;
|
||||
}
|
||||
if (!id_to_types_.count(field_id)) {
|
||||
return false;
|
||||
}
|
||||
auto left_id = name_to_types_.at(field_name);
|
||||
auto right_id = id_to_types_.at(field_id);
|
||||
return left_id == right_id;
|
||||
}
|
||||
|
||||
SystemFieldType
|
||||
GetSystemFieldType(FieldName field_name) const override {
|
||||
Assert(name_to_types_.count(field_name));
|
||||
return name_to_types_.at(field_name);
|
||||
}
|
||||
|
||||
SystemFieldType
|
||||
GetSystemFieldType(FieldId field_id) const override {
|
||||
Assert(id_to_types_.count(field_id));
|
||||
return id_to_types_.at(field_id);
|
||||
}
|
||||
|
||||
friend const SystemProperty&
|
||||
SystemProperty::Instance();
|
||||
|
||||
private:
|
||||
std::map<FieldName, SystemFieldType> name_to_types_;
|
||||
std::map<FieldId, SystemFieldType> id_to_types_;
|
||||
};
|
||||
|
||||
const SystemProperty&
|
||||
SystemProperty::Instance() {
|
||||
static auto impl = [] {
|
||||
SystemPropertyImpl impl;
|
||||
using Type = SystemFieldType;
|
||||
impl.name_to_types_.emplace(FieldName("RowID"), Type::RowId);
|
||||
impl.id_to_types_.emplace(FieldId(0), Type::RowId);
|
||||
|
||||
impl.name_to_types_.emplace(FieldName("Timestamp"), Type::Timestamp);
|
||||
impl.id_to_types_.emplace(FieldId(1), Type::Timestamp);
|
||||
|
||||
return impl;
|
||||
}();
|
||||
return impl;
|
||||
}
|
||||
}; // namespace milvus
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright (C) 2019-2020 Zilliz. 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
|
||||
|
||||
#pragma once
|
||||
#include "common/Types.h"
|
||||
|
||||
namespace milvus {
|
||||
|
||||
enum class SystemFieldType {
|
||||
Invalid = 0,
|
||||
RowId = 1,
|
||||
Timestamp = 2,
|
||||
};
|
||||
|
||||
class SystemProperty {
|
||||
public:
|
||||
static const SystemProperty&
|
||||
Instance();
|
||||
|
||||
public:
|
||||
virtual bool
|
||||
SystemFieldVerify(const FieldName& field_name, FieldId field_id) const = 0;
|
||||
|
||||
virtual SystemFieldType
|
||||
GetSystemFieldType(FieldId field_id) const = 0;
|
||||
|
||||
virtual SystemFieldType
|
||||
GetSystemFieldType(FieldName field_name) const = 0;
|
||||
};
|
||||
|
||||
} // namespace milvus
|
|
@ -32,7 +32,15 @@ ThrowWithTrace(const std::exception& exception);
|
|||
|
||||
} // namespace milvus::impl
|
||||
|
||||
#define AssertInfo(expr, info) milvus::impl::EasyAssertInfo(bool(expr), #expr, __FILE__, __LINE__, (info))
|
||||
#define AssertInfo(expr, info) \
|
||||
do { \
|
||||
auto _expr_res = bool(expr); \
|
||||
/* call func only when needed */ \
|
||||
if (!_expr_res) { \
|
||||
milvus::impl::EasyAssertInfo(_expr_res, #expr, __FILE__, __LINE__, (info)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define Assert(expr) AssertInfo((expr), "")
|
||||
#define PanicInfo(info) \
|
||||
do { \
|
||||
|
|
Loading…
Reference in New Issue