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 <mutex>
24#include <unordered_map>
25#include "meta.h"
26#include "meta_impl.h"
27
28namespace proxima {
29namespace be {
30namespace meta {
31
32//! Predefined class
33class MetaCache;
34//! Alias for MetaCache
35using MetaCachePtr = std::shared_ptr<MetaCache>;
36
37
38//! Alias for CollectionMetaMap
39using CollectionMetaMap =
40 std::unordered_map<std::string, CollectionImplPtrList>;
41using CollectionImplMap = std::unordered_map<std::string, CollectionImplPtr>;
42
43
44//! Alias CollectionFilter
45using CollectionFilter = std::function<bool(const CollectionImplPtr &)>;
46
47
48/*!
49 * MetaCache implementation
50 */
51class MetaCache {
52 public:
53 //! Default Filters
54 static CollectionFilter PassAllFilter;
55 static CollectionFilter IsCurrentFilter;
56
57 public:
58 //! Constructor
59 MetaCache() = default;
60
61 //! Destructor
62 ~MetaCache() = default;
63
64 public:
65 //! Clear cache
66 void clear();
67
68 //! Delete collections
69 void delete_collection(const std::string &name);
70
71 //! Has collection
72 bool exist_collection(const std::string &name) const;
73
74 //! Retrieve collection by name
75 const CollectionImplPtr &get_collection(const std::string &name) const;
76
77 //! Retrieve collection by name, with max revision id
78 const CollectionImplPtr &get_latest_collection(const std::string &name) const;
79
80 //! Retrieve collection
81 const CollectionImplPtr &get_collection(const std::string &name,
82 const CollectionFilter &filter) const;
83
84 //! Retrieve all the latest collections
85 void get_collections(CollectionMetaPtrList *collections) const;
86
87 //! Retrieve collections with specific collection name
88 void get_collections(const std::string &collection,
89 CollectionMetaPtrList *collections) const;
90
91 //! Retrieve all the collections
92 void get_collections(const CollectionFilter &filter,
93 CollectionMetaPtrList *collections) const;
94
95 //! Retrieve collections with specific repository
96 void get_collections_by_repo(const std::string &repo,
97 CollectionMetaPtrList *collections) const;
98
99
100 //! Append collection to the cache
101 int append_collection(const CollectionImplPtr &collection);
102
103 //! Append column to the cache
104 int append_column(ColumnImplPtr column);
105
106 //! Append repository to collection
107 int append_repository(DatabaseRepositoryImplPtr repository);
108
109 private:
110 //! Collection Cache
111 CollectionMetaMap cache_{};
112
113 //! Collection mapping, which help to allocate column with collection
114 CollectionImplMap mapping_{};
115};
116
117
118} // namespace meta
119} // namespace be
120} // namespace proxima
121