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#pragma once
22
23#include "meta.h"
24#include "meta_types.h"
25
26namespace proxima {
27namespace be {
28namespace meta {
29
30//! Predefine class
31class ColumnImpl;
32class CollectionImpl;
33class DatabaseRepositoryImpl;
34
35//! Alias Pointers
36using ColumnImplPtr = std::shared_ptr<ColumnImpl>;
37using ColumnImplPtrList = std::list<ColumnImplPtr>;
38using CollectionImplPtr = std::shared_ptr<CollectionImpl>;
39using CollectionImplPtrList = std::list<CollectionImplPtr>;
40using DatabaseRepositoryImplPtr = std::shared_ptr<DatabaseRepositoryImpl>;
41
42
43/*!
44 * Column Implementation
45 */
46class ColumnImpl : public ColumnObject {
47 public:
48 //! Constructors
49 ColumnImpl();
50 explicit ColumnImpl(const ColumnMeta &column_meta);
51 ColumnImpl(std::string uid, std::string uuid, const ColumnMeta &meta);
52
53 //! Destructor
54 ~ColumnImpl() override = default;
55
56 public:
57 //! Retrieve id
58 uint64_t id() const override {
59 return id_;
60 }
61
62 //! Set id
63 void set_id(uint64_t new_id) override {
64 id_ = new_id;
65 }
66
67 //! Retrieve collection uid
68 const std::string &collection_uid() const override {
69 return collection_uid_;
70 }
71
72 //! Retrieve mutable collection uid
73 std::string *mutable_collection_uid() override {
74 return &collection_uid_;
75 }
76
77 //! Set collection uid
78 void set_collection_uid(const std::string &new_collection_uid) override {
79 collection_uid_ = new_collection_uid;
80 }
81
82 //! Retrieve collection uuid
83 const std::string &collection_uuid() const override {
84 return collection_uuid_;
85 }
86
87 //! Retrieve mutable collection uuid
88 std::string *mutable_collection_uuid() override {
89 return &collection_uuid_;
90 }
91
92 //! Set collection uuid
93 void set_collection_uuid(const std::string &new_collection_uuid) override {
94 collection_uuid_ = new_collection_uuid;
95 }
96
97 //! Retrieve column name
98 const std::string &name() const override {
99 return meta_->name();
100 }
101
102 //! Retrieve mutable column name
103 std::string *mutable_name() override {
104 return meta_->mutable_name();
105 }
106
107 //! Set column name
108 void set_name(const std::string &new_name) override {
109 meta_->set_name(new_name);
110 }
111
112 //! Retrieve column uid
113 const std::string &uid() const override {
114 return meta_->uid();
115 }
116
117 //! Retrieve mutable column uid
118 std::string *mutable_uid() override {
119 return meta_->mutable_uid();
120 }
121
122 //! Set column uid
123 void set_uid(const std::string &new_uid) override {
124 meta_->set_uid(new_uid);
125 }
126
127 //! Retrieve dimension of column
128 uint32_t dimension() const override {
129 return meta_->dimension();
130 }
131
132 //! Set dimension of column
133 void set_dimension(uint32_t new_dimension) override {
134 return meta_->set_dimension(new_dimension);
135 }
136
137 //! Retrieve column index type
138 uint32_t index_type() const override {
139 return static_cast<uint32_t>(meta_->index_type());
140 }
141
142 //! Set column index type
143 void set_index_type(uint32_t type) override {
144 meta_->set_index_type(static_cast<IndexTypes>(type));
145 }
146
147 //! Retrieve column data type
148 uint32_t data_type() const override {
149 return static_cast<uint32_t>(meta_->data_type());
150 }
151
152 //! Set column index type
153 void set_data_type(uint32_t type) override {
154 meta_->set_data_type(static_cast<DataTypes>(type));
155 }
156
157 //! Retrieve column parameters
158 const std::string &parameters() const override {
159 return parameters_;
160 }
161
162 //! Retrieve mutable column parameters
163 std::string *mutable_parameters() override {
164 return &parameters_;
165 }
166
167 //! Set column parameters
168 void set_parameters(const std::string &params) override {
169 parameters_ = params;
170 }
171
172 //! Retrieve mutable meta
173 const ColumnMetaPtr &meta() const {
174 return meta_;
175 }
176
177 ////! Retrieve mutable meta
178 // ColumnMetaPtr *mutable_meta() {
179 // return &meta_;
180 //}
181
182 //! Transform
183 void transform();
184
185 private:
186 //! Meta
187 ColumnMetaPtr meta_{nullptr};
188
189 //! Primary key in database
190 uint64_t id_{0};
191
192 //! Uid of column
193 std::string uid_{};
194
195 //! Collection uid, indicate which collection group belong to
196 std::string collection_uid_{};
197
198 //! Collection uuid, indicate which specific collection belong to
199 std::string collection_uuid_{};
200
201 //! Index params, Json string, which parsed to meta_.parameters field
202 std::string parameters_{};
203};
204
205
206/*!
207 * Collection implementation
208 */
209class CollectionImpl : public CollectionObject {
210 public:
211 //! Constructors
212 CollectionImpl();
213 CollectionImpl(const char *coll_name);
214 CollectionImpl(const CollectionMeta &coll_meta);
215 CollectionImpl(CollectionMetaPtr meta_ptr);
216
217 //! Destructor
218 ~CollectionImpl() override = default;
219
220 public:
221 //! Retrieve id
222 uint64_t id() const override {
223 return id_;
224 }
225
226 //! Set collection id
227 void set_id(uint64_t new_id) override {
228 id_ = new_id;
229 }
230
231 //! Retrieve collection name
232 const std::string &name() const override {
233 return meta_->name();
234 }
235
236 //! Retrieve mutable collection name
237 std::string *mutable_name() override {
238 return meta_->mutable_name();
239 }
240
241 //! Set collection name
242 void set_name(const std::string &new_name) override {
243 meta_->set_name(new_name);
244 }
245
246 //! Retrieve collection uid, indicate a group of collections
247 const std::string &uid() const override {
248 return meta_->uid();
249 }
250
251 //! Retrieve mutable collection uid, indicate a group of collections
252 std::string *mutable_uid() override {
253 return meta_->mutable_uid();
254 }
255
256 //! Set collection uid
257 void set_uid(const std::string &new_uid) override {
258 meta_->set_uid(new_uid);
259 }
260
261 //! Retrieve collection uuid, unique field
262 const std::string &uuid() const override {
263 return uuid_;
264 }
265
266 //! Retrieve mutable collection uuid, unique field
267 std::string *mutable_uuid() override {
268 return &uuid_;
269 }
270
271 //! Set collection uuid
272 void set_uuid(const std::string &new_uuid) override {
273 uuid_ = new_uuid;
274 }
275
276 //! Retrieve forward columns, separated by character ','
277 const std::string &forward_columns() const override {
278 return forward_columns_;
279 }
280
281 //! Retrieve mutable forward columns, separated by character ','
282 std::string *mutable_forward_columns() override {
283 return &forward_columns_;
284 }
285
286 //! Set forward columns
287 void set_forward_columns(const std::string &new_forward_columns) override {
288 forward_columns_ = new_forward_columns;
289 }
290
291 //! Retrieve split index size
292 uint64_t max_docs_per_segment() const override {
293 return meta_->max_docs_per_segment();
294 }
295
296 //! Set split index size
297 void set_max_docs_per_segment(uint64_t count) override {
298 meta_->set_max_docs_per_segment(count);
299 }
300
301 //! Retrieve collection revision
302 uint32_t revision() const override {
303 return meta_->revision();
304 }
305
306 //! Set collection revision
307 void set_revision(uint32_t next_revision) override {
308 meta_->set_revision(next_revision);
309 }
310
311 //! Retrieve collection status
312 uint32_t status() const override {
313 return static_cast<uint32_t>(meta_->status());
314 }
315
316 //! Check is serving
317 bool serving() const {
318 return meta_->serving();
319 }
320
321 //! Set collection status
322 void set_status(uint32_t new_status) override {
323 meta_->set_status(static_cast<CollectionStatus>(new_status));
324 }
325
326 //! Retrieve collection current flag
327 uint32_t current() const override {
328 return meta_->is_current() ? 1 : 0;
329 }
330
331 //! Set collection current flag
332 void set_current(uint32_t flag) override {
333 meta_->set_current(flag != 0);
334 }
335
336 //! Retrieve io_mode of collection
337 uint32_t io_mode() const override {
338 uint32_t mode = 0;
339 if (meta_->readable()) {
340 mode |= 0x1;
341 }
342 if (meta_->writable()) {
343 mode |= 0x2;
344 }
345 return mode;
346 }
347
348 //! Set io_mode of collection
349 void set_io_mode(uint32_t mode) override {
350 meta_->set_readable(mode & 0x1);
351 meta_->set_writable(mode & 0x2);
352 }
353
354 //! Retrieve meta
355 CollectionMetaPtr meta() const {
356 return meta_;
357 }
358
359 //! Retrieve columns
360 const ColumnImplPtrList &columns() const {
361 return columns_;
362 }
363
364 //! Retrieve column with name
365 const ColumnImplPtr &get_column(const std::string &column_name) const {
366 return get_column([&column_name](const ColumnImplPtr &c) -> bool {
367 return column_name == c->name();
368 });
369 }
370
371 ////! Check valid column
372 // bool has_column(const std::string &column_name) const {
373 // return get_column(column_name).operator bool();
374 //}
375
376 //! Append column, 0 for success, otherwise failed
377 //! @param do not check uuid and uid if force equals true,
378 int append(ColumnImplPtr &, bool force = true);
379
380 //! Retrieve repository
381 DatabaseRepositoryImplPtr repository() const {
382 return repository_;
383 }
384
385 //! Attach to repository
386 int set_repository(DatabaseRepositoryImplPtr &);
387
388 //! Transform
389 void transform();
390
391 private:
392 //! Init member
393 void init_from_meta();
394
395 //! Retrieve column with filter
396 const ColumnImplPtr &get_column(
397 std::function<bool(const ColumnImplPtr &)> filter) const {
398 static ColumnImplPtr DefaultColumn;
399 auto iter = std::find_if_not(
400 columns_.begin(), columns_.end(),
401 [&filter](const ColumnImplPtr &c) -> bool { return !filter(c); });
402 return iter != columns_.end() ? *iter : DefaultColumn;
403 }
404
405 private:
406 //! Meta
407 CollectionMetaPtr meta_{nullptr};
408
409 //! ID
410 uint64_t id_{0};
411
412 //! Uniq id
413 std::string uuid_{};
414
415 //! Forward columns, separated by character ','
416 std::string forward_columns_{};
417
418 //! Columns
419 ColumnImplPtrList columns_{};
420
421 DatabaseRepositoryImplPtr repository_{};
422};
423
424
425/*! Repository implementation
426 */
427class DatabaseRepositoryImpl : public DatabaseRepositoryObject {
428 public:
429 //! Constructor
430 DatabaseRepositoryImpl();
431 explicit DatabaseRepositoryImpl(const DatabaseRepositoryMeta &);
432 DatabaseRepositoryImpl(const std::string &, const std::string &,
433 const DatabaseRepositoryMeta &);
434
435 //! Destructor
436 ~DatabaseRepositoryImpl() override = default;
437
438 public:
439 //! Retrieve id
440 uint64_t id() const override {
441 return id_;
442 }
443
444 //! Set repository id
445 void set_id(uint64_t repo_id) override {
446 id_ = repo_id;
447 }
448
449 //! Retrieve repository name
450 const std::string &name() const override {
451 return repository_->name();
452 }
453
454 //! Retrieve mutable repository name
455 std::string *mutable_name() override {
456 return repository_->mutable_name();
457 }
458
459 //! Set repository name
460 void set_name(const std::string &repo_name) override {
461 repository_->set_name(repo_name);
462 }
463
464 //! Retrieve collection uid
465 const std::string &collection_uid() const override {
466 return collection_uid_;
467 }
468
469 //! Retrieve mutable collection uid
470 std::string *mutable_collection_uid() override {
471 return &collection_uid_;
472 }
473
474 //! Set collection uid
475 void set_collection_uid(const std::string &new_connection_uid) override {
476 collection_uid_ = new_connection_uid;
477 }
478
479 //! Retrieve collection uuid
480 const std::string &collection_uuid() const override {
481 return collection_uuid_;
482 }
483
484 //! Retrieve mutable collection uuid
485 std::string *mutable_collection_uuid() override {
486 return &collection_uuid_;
487 }
488
489 //! Set collection uuid
490 void set_collection_uuid(const std::string &new_collection_uuid) override {
491 collection_uuid_ = new_collection_uuid;
492 }
493
494 //! Retrieve connection uri
495 const std::string &connection() const override {
496 return repository_->connection();
497 }
498
499 //! Retrieve mutable connection uri
500 std::string *mutable_connection() override {
501 return repository_->mutable_connection();
502 }
503
504 //! Set connection uri
505 void set_connection(const std::string &uri) override {
506 repository_->set_connection(uri);
507 }
508
509 //! Retrieve user name of connection
510 const std::string &user() const override {
511 return repository_->user();
512 }
513
514 //! Retrieve mutable user name of connection
515 std::string *mutable_user() override {
516 return repository_->mutable_user();
517 }
518
519 //! Set user name of connection
520 void set_user(const std::string &user_name) override {
521 repository_->set_user(user_name);
522 }
523
524 //! Retrieve password of user
525 const std::string &password() const override {
526 return repository_->password();
527 }
528
529 //! Retrieve mutable password of user
530 std::string *mutable_password() override {
531 return repository_->mutable_password();
532 }
533
534 //! Set password of user
535 void set_password(const std::string &new_password) override {
536 repository_->set_password(new_password);
537 }
538
539 //! Retrieve repository table name
540 const std::string &table() const override {
541 return repository_->table_name();
542 }
543
544 //! Retrieve mutable repository table name
545 std::string *mutable_table() override {
546 return repository_->mutable_table_name();
547 }
548
549 //! Set repository table name
550 void set_table(const std::string &table_name) override {
551 repository_->set_table_name(table_name);
552 }
553
554 const DatabaseRepositoryMetaPtr meta() const {
555 return repository_;
556 }
557
558 private:
559 //! ID
560 uint64_t id_{0u};
561
562 //! Collection uid
563 std::string collection_uid_{};
564
565 //! Collection uuid
566 std::string collection_uuid_{};
567
568 //! Repository handler
569 DatabaseRepositoryMetaPtr repository_{nullptr};
570};
571
572
573} // namespace meta
574} // namespace be
575} // namespace proxima
576