1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author guonix |
17 | * \date Nov 2020 |
18 | * \brief |
19 | */ |
20 | |
21 | #include "meta_impl.h" |
22 | #include <ailego/utility/string_helper.h> |
23 | #include "common/error_code.h" |
24 | #include "common/uuid_helper.h" |
25 | |
26 | namespace proxima { |
27 | namespace be { |
28 | namespace meta { |
29 | |
30 | ColumnImpl::ColumnImpl() { |
31 | meta_ = std::make_shared<ColumnMeta>(); |
32 | } |
33 | |
34 | ColumnImpl::ColumnImpl(const ColumnMeta &column_meta) { |
35 | meta_ = std::make_shared<ColumnMeta>(column_meta); |
36 | |
37 | // Serialize index params to json string |
38 | IndexParams::SerializeToBuffer(column_meta.parameters(), ¶meters_); |
39 | } |
40 | |
41 | ColumnImpl::ColumnImpl(std::string new_uid, std::string new_uuid, |
42 | const ColumnMeta &column_meta) |
43 | : collection_uid_(std::move(new_uid)), |
44 | collection_uuid_(std::move(new_uuid)) { |
45 | meta_ = std::make_shared<ColumnMeta>(column_meta); |
46 | |
47 | // Serialize index params to json string |
48 | IndexParams::SerializeToBuffer(column_meta.parameters(), ¶meters_); |
49 | } |
50 | |
51 | void ColumnImpl::transform() { |
52 | IndexParams::ParseFromBuffer(parameters_, meta_->mutable_parameters()); |
53 | } |
54 | |
55 | CollectionImpl::CollectionImpl() { |
56 | // Allocate new memory for meta |
57 | meta_ = std::make_shared<CollectionMeta>(); |
58 | meta_->set_revision(0); |
59 | meta_->set_current(true); |
60 | uuid_ = gen_uuid(); |
61 | } |
62 | |
63 | CollectionImpl::CollectionImpl(const char *collection_name) { |
64 | // Allocate new memory for meta |
65 | meta_ = std::make_shared<CollectionMeta>(); |
66 | meta_->set_name(collection_name); |
67 | uuid_ = gen_uuid(); |
68 | } |
69 | |
70 | CollectionImpl::CollectionImpl(const CollectionMeta &collection) { |
71 | meta_ = std::make_shared<CollectionMeta>(collection); |
72 | uuid_ = gen_uuid(); |
73 | init_from_meta(); |
74 | } |
75 | |
76 | CollectionImpl::CollectionImpl(CollectionMetaPtr collection) |
77 | : meta_(std::move(collection)) { |
78 | uuid_ = gen_uuid(); |
79 | init_from_meta(); |
80 | } |
81 | |
82 | int CollectionImpl::append(ColumnImplPtr &column_ptr, bool force) { |
83 | if (!force && (column_ptr->collection_uuid() != uuid() || |
84 | column_ptr->collection_uid() != uid())) { |
85 | return PROXIMA_BE_ERROR_CODE(InvalidArgument); |
86 | } |
87 | |
88 | // Reset uuid and uid |
89 | column_ptr->set_collection_uuid(uuid()); |
90 | column_ptr->set_collection_uid(uid()); |
91 | |
92 | // Link to columns |
93 | columns_.push_back(column_ptr); |
94 | // Collect meta |
95 | meta_->append(column_ptr->meta()); |
96 | return 0; |
97 | } |
98 | |
99 | int CollectionImpl::set_repository(DatabaseRepositoryImplPtr &repo) { |
100 | if (repo) { |
101 | repository_ = repo; |
102 | meta_->set_repository(repository_->meta()); |
103 | } |
104 | return 0; |
105 | } |
106 | |
107 | void CollectionImpl::transform() { |
108 | if (!forward_columns_.empty()) { |
109 | ailego::StringHelper::Split(forward_columns_, "," , |
110 | meta_->mutable_forward_columns()); |
111 | } |
112 | for (auto &column : columns()) { |
113 | column->set_collection_uid(uid()); |
114 | column->set_collection_uuid(uuid()); |
115 | } |
116 | } |
117 | |
118 | void CollectionImpl::init_from_meta() { |
119 | // merge forward columns |
120 | for (auto &c : meta_->forward_columns()) { |
121 | forward_columns_.append(c); |
122 | forward_columns_.append("," ); |
123 | } |
124 | if (!forward_columns_.empty()) { |
125 | forward_columns_.pop_back(); |
126 | } |
127 | |
128 | // parse columns |
129 | for (auto &c : meta_->index_columns()) { |
130 | columns_.emplace_back( |
131 | std::make_shared<ColumnImpl>(meta_->uid(), uuid_, *c)); |
132 | } |
133 | |
134 | if (meta_->repository()) { |
135 | auto repo = |
136 | RepositoryHelper::Child<DatabaseRepositoryMeta>(meta_->repository()); |
137 | repository_ = |
138 | std::make_shared<DatabaseRepositoryImpl>(meta_->uid(), uuid_, *repo); |
139 | } |
140 | } |
141 | |
142 | DatabaseRepositoryImpl::DatabaseRepositoryImpl() { |
143 | repository_ = std::make_shared<DatabaseRepositoryMeta>(); |
144 | } |
145 | |
146 | DatabaseRepositoryImpl::DatabaseRepositoryImpl( |
147 | const DatabaseRepositoryMeta &repo) |
148 | : collection_uid_(gen_uuid()), collection_uuid_(gen_uuid()) { |
149 | repository_ = std::make_shared<DatabaseRepositoryMeta>(repo); |
150 | } |
151 | |
152 | DatabaseRepositoryImpl::DatabaseRepositoryImpl( |
153 | const std::string &uid, const std::string &uuid, |
154 | const DatabaseRepositoryMeta &repo) |
155 | : collection_uid_(uid), collection_uuid_(uuid) { |
156 | repository_ = std::make_shared<DatabaseRepositoryMeta>(repo); |
157 | } |
158 | |
159 | } // namespace meta |
160 | } // namespace be |
161 | } // namespace proxima |
162 | |