2019-11-09 15:33:07 +00:00
|
|
|
#include <Access/MultipleAccessStorage.h>
|
2021-03-11 20:41:10 +00:00
|
|
|
#include <Access/Credentials.h>
|
2019-11-09 15:33:07 +00:00
|
|
|
#include <Common/Exception.h>
|
2021-11-18 20:54:18 +00:00
|
|
|
#include <Common/quoteString.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2020-08-05 19:54:06 +00:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
2020-10-04 18:00:56 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2020-08-05 19:54:06 +00:00
|
|
|
#include <boost/range/algorithm/copy.hpp>
|
|
|
|
#include <boost/range/algorithm/find.hpp>
|
2019-11-09 15:33:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-26 10:50:13 +00:00
|
|
|
extern const int ACCESS_STORAGE_FOR_INSERTION_NOT_FOUND;
|
2020-08-14 21:30:47 +00:00
|
|
|
extern const int ACCESS_ENTITY_ALREADY_EXISTS;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
using Storage = IAccessStorage;
|
|
|
|
using StoragePtr = std::shared_ptr<Storage>;
|
|
|
|
using ConstStoragePtr = std::shared_ptr<const Storage>;
|
|
|
|
using Storages = std::vector<StoragePtr>;
|
2019-11-09 15:33:07 +00:00
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
|
|
|
|
MultipleAccessStorage::MultipleAccessStorage(const String & storage_name_)
|
2020-08-12 20:48:53 +00:00
|
|
|
: IAccessStorage(storage_name_)
|
2020-08-05 19:54:06 +00:00
|
|
|
, nested_storages(std::make_shared<Storages>())
|
2019-11-09 15:33:07 +00:00
|
|
|
, ids_cache(512 /* cache size */)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:00:56 +00:00
|
|
|
MultipleAccessStorage::~MultipleAccessStorage()
|
|
|
|
{
|
2020-10-06 15:37:35 +00:00
|
|
|
/// It's better to remove the storages in the reverse order because they could depend on each other somehow.
|
2020-10-04 19:56:25 +00:00
|
|
|
const auto storages = getStoragesPtr();
|
|
|
|
for (const auto & storage : *storages | boost::adaptors::reversed)
|
2020-10-04 18:00:56 +00:00
|
|
|
{
|
|
|
|
removeStorage(storage);
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
void MultipleAccessStorage::setStorages(const std::vector<StoragePtr> & storages)
|
|
|
|
{
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
nested_storages = std::make_shared<const Storages>(storages);
|
|
|
|
ids_cache.reset();
|
|
|
|
updateSubscriptionsToNestedStorages(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultipleAccessStorage::addStorage(const StoragePtr & new_storage)
|
|
|
|
{
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
if (boost::range::find(*nested_storages, new_storage) != nested_storages->end())
|
|
|
|
return;
|
|
|
|
auto new_storages = std::make_shared<Storages>(*nested_storages);
|
|
|
|
new_storages->push_back(new_storage);
|
|
|
|
nested_storages = new_storages;
|
|
|
|
updateSubscriptionsToNestedStorages(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultipleAccessStorage::removeStorage(const StoragePtr & storage_to_remove)
|
|
|
|
{
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
auto it = boost::range::find(*nested_storages, storage_to_remove);
|
|
|
|
if (it == nested_storages->end())
|
|
|
|
return;
|
|
|
|
size_t index = it - nested_storages->begin();
|
|
|
|
auto new_storages = std::make_shared<Storages>(*nested_storages);
|
|
|
|
new_storages->erase(new_storages->begin() + index);
|
|
|
|
nested_storages = new_storages;
|
|
|
|
ids_cache.reset();
|
|
|
|
updateSubscriptionsToNestedStorages(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StoragePtr> MultipleAccessStorage::getStorages()
|
|
|
|
{
|
|
|
|
return *getStoragesPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ConstStoragePtr> MultipleAccessStorage::getStorages() const
|
|
|
|
{
|
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
std::vector<ConstStoragePtr> res;
|
|
|
|
res.reserve(storages->size());
|
|
|
|
boost::range::copy(*storages, std::back_inserter(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const Storages> MultipleAccessStorage::getStoragesPtr()
|
|
|
|
{
|
|
|
|
return getStoragesInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const Storages> MultipleAccessStorage::getStoragesInternal() const
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
return nested_storages;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::optional<UUID> MultipleAccessStorage::findImpl(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto id = storage->find(type, name);
|
2019-11-09 15:33:07 +00:00
|
|
|
if (id)
|
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ids_cache.set(*id, storage);
|
|
|
|
return id;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 18:32:12 +00:00
|
|
|
return {};
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<UUID> MultipleAccessStorage::findAllImpl(AccessEntityType type) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
std::vector<UUID> all_ids;
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto ids = storage->findAll(type);
|
2019-11-09 15:33:07 +00:00
|
|
|
all_ids.insert(all_ids.end(), std::make_move_iterator(ids.begin()), std::make_move_iterator(ids.end()));
|
|
|
|
}
|
|
|
|
return all_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool MultipleAccessStorage::exists(const UUID & id) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return findStorage(id) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
StoragePtr MultipleAccessStorage::findStorage(const UUID & id)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
StoragePtr from_cache;
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
from_cache = ids_cache.get(id);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
2020-08-05 19:54:06 +00:00
|
|
|
if (from_cache && from_cache->exists(id))
|
|
|
|
return from_cache;
|
2019-11-09 15:33:07 +00:00
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
if (storage->exists(id))
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ids_cache.set(id, storage);
|
|
|
|
return storage;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
ConstStoragePtr MultipleAccessStorage::findStorage(const UUID & id) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return const_cast<MultipleAccessStorage *>(this)->findStorage(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
StoragePtr MultipleAccessStorage::getStorage(const UUID & id)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storage = findStorage(id);
|
2019-11-09 15:33:07 +00:00
|
|
|
if (storage)
|
2020-08-05 19:54:06 +00:00
|
|
|
return storage;
|
2019-11-09 15:33:07 +00:00
|
|
|
throwNotFound(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
ConstStoragePtr MultipleAccessStorage::getStorage(const UUID & id) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return const_cast<MultipleAccessStorage *>(this)->getStorage(id);
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
AccessEntityPtr MultipleAccessStorage::readImpl(const UUID & id, bool throw_if_not_exists) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
if (auto storage = findStorage(id))
|
|
|
|
return storage->read(id, throw_if_not_exists);
|
|
|
|
|
|
|
|
if (throw_if_not_exists)
|
|
|
|
throwNotFound(id);
|
|
|
|
else
|
|
|
|
return nullptr;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
std::optional<String> MultipleAccessStorage::readNameImpl(const UUID & id, bool throw_if_not_exists) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
if (auto storage = findStorage(id))
|
|
|
|
return storage->readName(id, throw_if_not_exists);
|
|
|
|
|
|
|
|
if (throw_if_not_exists)
|
|
|
|
throwNotFound(id);
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 22:14:11 +00:00
|
|
|
bool MultipleAccessStorage::isReadOnly() const
|
2020-02-26 10:50:13 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
2020-02-26 10:50:13 +00:00
|
|
|
{
|
2021-11-22 22:14:11 +00:00
|
|
|
if (!storage->isReadOnly())
|
|
|
|
return false;
|
2020-02-26 10:50:13 +00:00
|
|
|
}
|
2021-11-22 22:14:11 +00:00
|
|
|
return true;
|
2020-02-26 10:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 22:18:10 +00:00
|
|
|
std::optional<UUID> MultipleAccessStorage::insertImpl(const AccessEntityPtr & entity, bool replace_if_exists, bool throw_if_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
|
|
|
|
std::shared_ptr<IAccessStorage> storage_for_insertion;
|
|
|
|
for (const auto & storage : *storages)
|
2020-02-26 10:50:13 +00:00
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
if (!storage->isReadOnly() || storage->find(entity->getType(), entity->getName()))
|
2020-02-26 10:50:13 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
storage_for_insertion = storage;
|
2020-02-26 10:50:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
|
2020-08-05 19:54:06 +00:00
|
|
|
if (!storage_for_insertion)
|
2021-11-18 20:54:18 +00:00
|
|
|
throw Exception("Not found a storage to insert " + entity->formatTypeWithName(), ErrorCodes::ACCESS_STORAGE_FOR_INSERTION_NOT_FOUND);
|
2020-02-26 10:50:13 +00:00
|
|
|
|
2021-12-11 22:18:10 +00:00
|
|
|
auto id = storage_for_insertion->insert(entity, replace_if_exists, throw_if_exists);
|
|
|
|
if (id)
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ids_cache.set(*id, storage_for_insertion);
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 13:19:07 +00:00
|
|
|
bool MultipleAccessStorage::removeImpl(const UUID & id, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
if (auto storage = findStorage(id))
|
|
|
|
return storage->remove(id, throw_if_not_exists);
|
|
|
|
|
|
|
|
if (throw_if_not_exists)
|
|
|
|
throwNotFound(id);
|
|
|
|
else
|
|
|
|
return false;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 09:42:15 +00:00
|
|
|
bool MultipleAccessStorage::updateImpl(const UUID & id, const UpdateFunc & update_func, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
auto storage_for_updating = findStorage(id);
|
|
|
|
if (!storage_for_updating)
|
|
|
|
{
|
|
|
|
if (throw_if_not_exists)
|
|
|
|
throwNotFound(id);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-14 21:30:47 +00:00
|
|
|
|
|
|
|
/// If the updating involves renaming check that the renamed entity will be accessible by name.
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
if ((storages->size() > 1) && (storages->front() != storage_for_updating))
|
2020-08-14 21:30:47 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
if (auto old_entity = storage_for_updating->tryRead(id))
|
2020-08-14 21:30:47 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
auto new_entity = update_func(old_entity);
|
|
|
|
if (new_entity->getName() != old_entity->getName())
|
2020-08-14 21:30:47 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
for (const auto & storage : *storages)
|
2020-08-14 21:30:47 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
if (storage == storage_for_updating)
|
|
|
|
break;
|
|
|
|
if (storage->find(new_entity->getType(), new_entity->getName()))
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
old_entity->formatTypeWithName() + ": cannot rename to " + backQuote(new_entity->getName()) + " because "
|
|
|
|
+ new_entity->formatTypeWithName() + " already exists in " + storage->getStorageName(),
|
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
2020-08-14 21:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 09:42:15 +00:00
|
|
|
return storage_for_updating->update(id, update_func, throw_if_not_exists);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard MultipleAccessStorage::subscribeForChangesImpl(const UUID & id, const OnChangedHandler & handler) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storage = findStorage(id);
|
2019-11-09 15:33:07 +00:00
|
|
|
if (!storage)
|
2020-01-29 15:51:12 +00:00
|
|
|
return {};
|
2019-11-09 15:33:07 +00:00
|
|
|
return storage->subscribeForChanges(id, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool MultipleAccessStorage::hasSubscription(const UUID & id) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
if (storage->hasSubscription(id))
|
2019-11-09 15:33:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
scope_guard MultipleAccessStorage::subscribeForChangesImpl(AccessEntityType type, const OnChangedHandler & handler) const
|
2020-08-05 19:54:06 +00:00
|
|
|
{
|
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
auto & handlers = handlers_by_type[static_cast<size_t>(type)];
|
|
|
|
handlers.push_back(handler);
|
|
|
|
auto handler_it = std::prev(handlers.end());
|
|
|
|
if (handlers.size() == 1)
|
|
|
|
updateSubscriptionsToNestedStorages(lock);
|
|
|
|
|
|
|
|
return [this, type, handler_it]
|
|
|
|
{
|
|
|
|
std::unique_lock lock2{mutex};
|
|
|
|
auto & handlers2 = handlers_by_type[static_cast<size_t>(type)];
|
|
|
|
handlers2.erase(handler_it);
|
|
|
|
if (handlers2.empty())
|
|
|
|
updateSubscriptionsToNestedStorages(lock2);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool MultipleAccessStorage::hasSubscription(AccessEntityType type) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
const auto & handlers = handlers_by_type[static_cast<size_t>(type)];
|
|
|
|
return !handlers.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Updates subscriptions to nested storages.
|
|
|
|
/// We need the subscriptions to the nested storages if someone has subscribed to us.
|
|
|
|
/// If any of the nested storages is changed we call our subscribers.
|
|
|
|
void MultipleAccessStorage::updateSubscriptionsToNestedStorages(std::unique_lock<std::mutex> & lock) const
|
|
|
|
{
|
|
|
|
/// lock is already locked.
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<std::pair<StoragePtr, scope_guard>> added_subscriptions[static_cast<size_t>(AccessEntityType::MAX)];
|
2021-06-15 19:55:21 +00:00
|
|
|
std::vector<scope_guard> removed_subscriptions;
|
2020-08-05 19:54:06 +00:00
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
for (auto type : collections::range(AccessEntityType::MAX))
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-08-05 19:54:06 +00:00
|
|
|
auto & handlers = handlers_by_type[static_cast<size_t>(type)];
|
|
|
|
auto & subscriptions = subscriptions_to_nested_storages[static_cast<size_t>(type)];
|
|
|
|
if (handlers.empty())
|
|
|
|
{
|
|
|
|
/// None has subscribed to us, we need no subscriptions to the nested storages.
|
|
|
|
for (auto & subscription : subscriptions | boost::adaptors::map_values)
|
|
|
|
removed_subscriptions.push_back(std::move(subscription));
|
|
|
|
subscriptions.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Someone has subscribed to us, now we need to have a subscription to each nested storage.
|
|
|
|
for (auto it = subscriptions.begin(); it != subscriptions.end();)
|
|
|
|
{
|
|
|
|
const auto & storage = it->first;
|
|
|
|
auto & subscription = it->second;
|
|
|
|
if (boost::range::find(*nested_storages, storage) == nested_storages->end())
|
|
|
|
{
|
|
|
|
removed_subscriptions.push_back(std::move(subscription));
|
|
|
|
it = subscriptions.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & storage : *nested_storages)
|
|
|
|
{
|
|
|
|
if (!subscriptions.count(storage))
|
|
|
|
added_subscriptions[static_cast<size_t>(type)].push_back({storage, nullptr});
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
2020-08-05 19:54:06 +00:00
|
|
|
|
|
|
|
/// Unlock the mutex temporarily because it's much better to subscribe to the nested storages
|
|
|
|
/// with the mutex unlocked.
|
|
|
|
lock.unlock();
|
|
|
|
removed_subscriptions.clear();
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
for (auto type : collections::range(AccessEntityType::MAX))
|
2020-08-05 19:54:06 +00:00
|
|
|
{
|
|
|
|
if (!added_subscriptions[static_cast<size_t>(type)].empty())
|
|
|
|
{
|
|
|
|
auto on_changed = [this, type](const UUID & id, const AccessEntityPtr & entity)
|
|
|
|
{
|
|
|
|
Notifications notifications;
|
|
|
|
SCOPE_EXIT({ notify(notifications); });
|
|
|
|
std::lock_guard lock2{mutex};
|
|
|
|
for (const auto & handler : handlers_by_type[static_cast<size_t>(type)])
|
|
|
|
notifications.push_back({handler, id, entity});
|
|
|
|
};
|
|
|
|
for (auto & [storage, subscription] : added_subscriptions[static_cast<size_t>(type)])
|
|
|
|
subscription = storage->subscribeForChanges(type, on_changed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lock the mutex again to store added subscriptions to the nested storages.
|
|
|
|
lock.lock();
|
2021-03-11 20:41:10 +00:00
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
for (auto type : collections::range(AccessEntityType::MAX))
|
2020-08-05 19:54:06 +00:00
|
|
|
{
|
|
|
|
if (!added_subscriptions[static_cast<size_t>(type)].empty())
|
|
|
|
{
|
|
|
|
auto & subscriptions = subscriptions_to_nested_storages[static_cast<size_t>(type)];
|
|
|
|
for (auto & [storage, subscription] : added_subscriptions[static_cast<size_t>(type)])
|
|
|
|
{
|
|
|
|
if (!subscriptions.count(storage) && (boost::range::find(*nested_storages, storage) != nested_storages->end())
|
|
|
|
&& !handlers_by_type[static_cast<size_t>(type)].empty())
|
|
|
|
{
|
|
|
|
subscriptions.emplace(std::move(storage), std::move(subscription));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.unlock();
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
2020-08-05 19:54:06 +00:00
|
|
|
|
2020-09-17 09:58:34 +00:00
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
UUID MultipleAccessStorage::authenticateImpl(const Credentials & credentials, const Poco::Net::IPAddress & address, const ExternalAuthenticators & external_authenticators) const
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
|
|
|
auto storages = getStoragesInternal();
|
|
|
|
for (const auto & storage : *storages)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-22 21:50:15 +00:00
|
|
|
auto id = storage->authenticate(credentials, address, external_authenticators, /* replace_exception_with_cannot_authenticate = */ false);
|
2020-09-17 09:58:34 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
ids_cache.set(id, storage);
|
|
|
|
return id;
|
|
|
|
}
|
2020-10-06 16:32:06 +00:00
|
|
|
catch (...)
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
2021-11-18 20:54:18 +00:00
|
|
|
if (!storage->find(AccessEntityType::USER, credentials.getUserName()))
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
|
|
|
/// The authentication failed because there no users with such name in the `storage`
|
|
|
|
/// thus we can try to search in other nested storages.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 20:54:18 +00:00
|
|
|
throwNotFound(AccessEntityType::USER, credentials.getUserName());
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|