1#pragma once
2
3#include <Access/IAccessStorage.h>
4#include <Common/LRUCache.h>
5#include <mutex>
6
7
8namespace DB
9{
10/// Implementation of IAccessStorage which contains multiple nested storages.
11class MultipleAccessStorage : public IAccessStorage
12{
13public:
14 using Storage = IAccessStorage;
15
16 MultipleAccessStorage(std::vector<std::unique_ptr<Storage>> nested_storages_, size_t index_of_nested_storage_for_insertion_ = 0);
17 ~MultipleAccessStorage() override;
18
19 std::vector<UUID> findMultiple(std::type_index type, const String & name) const;
20
21 template <typename EntityType>
22 std::vector<UUID> findMultiple(const String & name) const { return findMultiple(EntityType::TYPE, name); }
23
24 const Storage * findStorage(const UUID & id) const;
25 Storage * findStorage(const UUID & id);
26 const Storage & getStorage(const UUID & id) const;
27 Storage & getStorage(const UUID & id);
28
29 Storage & getStorageByIndex(size_t i) { return *(nested_storages[i]); }
30 const Storage & getStorageByIndex(size_t i) const { return *(nested_storages[i]); }
31
32protected:
33 std::optional<UUID> findImpl(std::type_index type, const String & name) const override;
34 std::vector<UUID> findAllImpl(std::type_index type) const override;
35 bool existsImpl(const UUID & id) const override;
36 AccessEntityPtr readImpl(const UUID & id) const override;
37 String readNameImpl(const UUID &id) const override;
38 UUID insertImpl(const AccessEntityPtr & entity, bool replace_if_exists) override;
39 void removeImpl(const UUID & id) override;
40 void updateImpl(const UUID & id, const UpdateFunc & update_func) override;
41 SubscriptionPtr subscribeForChangesImpl(const UUID & id, const OnChangedHandler & handler) const override;
42 SubscriptionPtr subscribeForChangesImpl(std::type_index type, const OnChangedHandler & handler) const override;
43 bool hasSubscriptionImpl(const UUID & id) const override;
44 bool hasSubscriptionImpl(std::type_index type) const override;
45
46private:
47 std::vector<std::unique_ptr<Storage>> nested_storages;
48 IAccessStorage * nested_storage_for_insertion;
49 mutable LRUCache<UUID, Storage *> ids_cache;
50 mutable std::mutex ids_cache_mutex;
51};
52
53}
54