forked from antgroup/vsag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathivf_parameter.cpp
More file actions
101 lines (82 loc) · 3.71 KB
/
ivf_parameter.cpp
File metadata and controls
101 lines (82 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2024-present the vsag project
//
// 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 "ivf_parameter.h"
#include <fmt/format.h>
#include "inner_string_params.h"
#include "utils/param_compat_macros.h"
#include "vsag/constants.h"
namespace vsag {
void
IVFParameter::FromJson(const JsonType& json) {
InnerIndexParameter::FromJson(json);
if (json.Contains(BUCKET_PER_DATA_KEY)) {
this->buckets_per_data = static_cast<BucketIdType>(json[BUCKET_PER_DATA_KEY].GetInt());
}
this->bucket_param = std::make_shared<BucketDataCellParameter>();
CHECK_ARGUMENT(json.Contains(BUCKET_PARAMS_KEY),
fmt::format("ivf parameters must contains {}", BUCKET_PARAMS_KEY));
this->bucket_param->FromJson(json[BUCKET_PARAMS_KEY]);
this->ivf_partition_strategy_parameter = std::make_shared<IVFPartitionStrategyParameters>();
if (json.Contains(IVF_PARTITION_STRATEGY_PARAMS_KEY)) {
this->ivf_partition_strategy_parameter->FromJson(json[IVF_PARTITION_STRATEGY_PARAMS_KEY]);
}
if (this->ivf_partition_strategy_parameter->partition_strategy_type ==
IVFPartitionStrategyType::GNO_IMI) {
this->bucket_param->buckets_count = static_cast<BucketIdType>(
this->ivf_partition_strategy_parameter->gnoimi_param->first_order_buckets_count *
this->ivf_partition_strategy_parameter->gnoimi_param->second_order_buckets_count);
}
}
JsonType
IVFParameter::ToJson() const {
JsonType json = InnerIndexParameter::ToJson();
json[TYPE_KEY].SetString(INDEX_IVF);
json[BUCKET_PARAMS_KEY].SetJson(this->bucket_param->ToJson());
json[IVF_PARTITION_STRATEGY_PARAMS_KEY].SetJson(
this->ivf_partition_strategy_parameter->ToJson());
json[BUCKET_PER_DATA_KEY].SetInt(this->buckets_per_data);
return json;
}
bool
IVFParameter::CheckCompatibility(const ParamPtr& other) const {
if (not InnerIndexParameter::CheckCompatibility(other)) {
return false;
}
PARAM_CAST_OR_RETURN(IVFParameter, p, other);
CHECK_FIELD_EQ(*this, *p, buckets_per_data);
CHECK_SUB_PARAM(*this, *p, bucket_param);
CHECK_SUB_PARAM(*this, *p, ivf_partition_strategy_parameter);
return true;
}
IVFSearchParameters
IVFSearchParameters::FromJson(const std::string& json_string) {
JsonType params = JsonType::Parse(json_string);
IVFSearchParameters obj;
CHECK_ARGUMENT(params.Contains(INDEX_TYPE_IVF),
fmt::format("parameters must contains {}", INDEX_TYPE_IVF));
obj.IndexSearchParameter::FromJson(params[INDEX_TYPE_IVF]);
// set obj.scan_buckets_count
CHECK_ARGUMENT(params[INDEX_TYPE_IVF].Contains(IVF_SEARCH_PARAM_SCAN_BUCKETS_COUNT),
fmt::format("parameters[{}] must contains {}",
INDEX_TYPE_IVF,
IVF_SEARCH_PARAM_SCAN_BUCKETS_COUNT));
obj.scan_buckets_count = params[INDEX_TYPE_IVF][IVF_SEARCH_PARAM_SCAN_BUCKETS_COUNT].GetInt();
// set obj.first_order_scan_ratio
if (params[INDEX_TYPE_IVF].Contains(GNO_IMI_SEARCH_PARAM_FIRST_ORDER_SCAN_RATIO)) {
obj.first_order_scan_ratio =
params[INDEX_TYPE_IVF][GNO_IMI_SEARCH_PARAM_FIRST_ORDER_SCAN_RATIO].GetFloat();
}
return obj;
}
} // namespace vsag