-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jparisu <javierparis@eprosima.com>
- Loading branch information
jparisu
committed
Sep 20, 2022
1 parent
f790369
commit 2a4c1d4
Showing
8 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
ddsrouter_utils/include/ddsrouter_utils/collection/database/IDataBase.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file DataBase.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
|
||
template <typename Key, typename Value> | ||
class IDataBase | ||
{ | ||
public: | ||
|
||
virtual void add(const Key& key, const Value& value) = 0; | ||
|
||
virtual void modify(const Key& key, const Value& value) = 0; | ||
|
||
virtual void remove(const Key& key) = 0; | ||
|
||
virtual bool exist(const Key& key) = 0; | ||
|
||
virtual Value get(const Key& key) = 0; | ||
|
||
}; | ||
|
||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ |
47 changes: 47 additions & 0 deletions
47
ddsrouter_utils/include/ddsrouter_utils/collection/database/IWatchDataBase.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file IWatchDataBase.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <ddsrouter_utils/collection/database/IDataBase.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
|
||
template <typename Key, typename Value> | ||
class IWatchDataBase : public IDataBase<Key, Value> | ||
{ | ||
public: | ||
|
||
virtual void register_addition_callback( | ||
const std::function<void(Key, Value)>& callback) = 0; | ||
|
||
virtual void register_modification_callback( | ||
const std::function<void(Key, Value)>& callback) = 0; | ||
|
||
virtual void register_deletion_callback( | ||
const std::function<void(Key, Value)>& callback) = 0; | ||
|
||
}; | ||
|
||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ |
94 changes: 94 additions & 0 deletions
94
ddsrouter_utils/include/ddsrouter_utils/collection/database/StdWatchDataBase.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file DataBase.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
|
||
#include <ddsrouter_utils/collection/database/IWatchDataBase.hpp> | ||
#include <ddsrouter_utils/thread/manager/IManager.hpp> | ||
#include <ddsrouter_utils/types/Atomicable.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
|
||
enum DataBaseActionKind | ||
{ | ||
add = 0, | ||
modify = 1, | ||
remove = 2 | ||
}; | ||
|
||
template <typename Key, typename Value> | ||
class StdWatchDataBase : public IWatchDataBase<Key, Value> | ||
{ | ||
|
||
using CallbackType = std::function<void(Key, Value)>; | ||
|
||
public: | ||
|
||
StdWatchDataBase( | ||
std::shared_ptr<thread::IManager> thread_manager); | ||
|
||
virtual ~StdWatchDataBase() = default; | ||
|
||
virtual void add(const Key& key, const Value& value) override; | ||
|
||
virtual void modify(const Key& key, const Value& value) override; | ||
|
||
virtual void remove(const Key& key) override; | ||
|
||
virtual bool exist(const Key& key) override; | ||
|
||
virtual Value get(const Key& key) override; | ||
|
||
virtual void register_addition_callback( | ||
const CallbackType& callback) override; | ||
|
||
virtual void register_modification_callback( | ||
const CallbackType& callback) override; | ||
|
||
virtual void register_deletion_callback( | ||
const CallbackType& callback) override; | ||
|
||
protected: | ||
|
||
void call_callback_common_( | ||
const Key& key, | ||
const Value& value, | ||
DataBaseActionKind action_kind); | ||
|
||
void register_callback_common_( | ||
const CallbackType& callback, | ||
DataBaseActionKind action_kind); | ||
|
||
std::shared_ptr<thread::IManager> thread_manager_; | ||
|
||
SharedAtomicable<std::map<Key, Value>> map_database_; | ||
|
||
std::array<SharedAtomicable<std::vector<CallbackType>>, 3> callbacks_; | ||
|
||
}; | ||
|
||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ | ||
|
||
// Include implementation template file | ||
#include <ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp> |
153 changes: 153 additions & 0 deletions
153
ddsrouter_utils/include/ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file StdWatchDataBase.ipp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ddsrouter_utils/exception/InconsistencyException.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
|
||
template <typename Key, typename Value> | ||
StdWatchDataBase<Key, Value>::StdWatchDataBase( | ||
std::shared_ptr<thread::IManager> thread_manager) | ||
: thread_manager_(thread_manager) | ||
{ | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::add(const Key& key, const Value& value) | ||
{ | ||
{ | ||
std::unique_lock<std::shared_timed_mutex> lock_db(map_database_); | ||
auto it = map_database_.find(key); | ||
if (it != map_database_.end()) | ||
{ | ||
throw InconsistencyException( | ||
STR_ENTRY << "Value to add " << key << " already in Database."); | ||
} | ||
map_database_[key] = value; | ||
} | ||
|
||
call_callback_common_(key, value, DataBaseActionKind::add); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::modify(const Key& key, const Value& value) | ||
{ | ||
{ | ||
std::unique_lock<std::shared_timed_mutex> lock_db(map_database_); | ||
auto it = map_database_.find(key); | ||
if (it == map_database_.end()) | ||
{ | ||
throw InconsistencyException( | ||
STR_ENTRY << "Value to modify " << key << " not in Database."); | ||
} | ||
map_database_[key] = value; | ||
} | ||
|
||
call_callback_common_(key, value, DataBaseActionKind::modify); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::remove(const Key& key) | ||
{ | ||
Value value; | ||
|
||
{ | ||
std::unique_lock<std::shared_timed_mutex> lock_db(map_database_); | ||
auto it = map_database_.find(key); | ||
if (it == map_database_.end()) | ||
{ | ||
throw InconsistencyException( | ||
STR_ENTRY << "Value to remove " << key << " not in Database."); | ||
} | ||
value = it->second; | ||
map_database_.erase(it); | ||
} | ||
|
||
call_callback_common_(key, value, DataBaseActionKind::remove); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
bool StdWatchDataBase<Key, Value>::exist(const Key& key) | ||
{ | ||
std::shared_lock<std::shared_timed_mutex> lock_db(map_database_); | ||
return map_database_.find(key) == map_database_.end(); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
Value StdWatchDataBase<Key, Value>::get(const Key& key) | ||
{ | ||
std::shared_lock<std::shared_timed_mutex> lock_db(map_database_); | ||
auto it = map_database_.find(key); | ||
if (it == map_database_.end()) | ||
{ | ||
throw InconsistencyException( | ||
STR_ENTRY << "Value to get " << key << " not in Database."); | ||
} | ||
return it->second; | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::register_addition_callback( | ||
const CallbackType& callback) | ||
{ | ||
register_callback_common_(callback, DataBaseActionKind::add); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::register_modification_callback( | ||
const CallbackType& callback) | ||
{ | ||
register_callback_common_(callback, DataBaseActionKind::modify); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::register_deletion_callback( | ||
const CallbackType& callback) | ||
{ | ||
register_callback_common_(callback, DataBaseActionKind::remove); | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::call_callback_common_( | ||
const Key& key, | ||
const Value& value, | ||
DataBaseActionKind action_kind) | ||
{ | ||
std::shared_lock<std::shared_timed_mutex> lock_db(callbacks_[action_kind]); | ||
for (auto& callback : callbacks_[action_kind]) | ||
{ | ||
callback(key, value); | ||
} | ||
} | ||
|
||
template <typename Key, typename Value> | ||
void StdWatchDataBase<Key, Value>::register_callback_common_( | ||
const CallbackType& callback, | ||
DataBaseActionKind action_kind) | ||
{ | ||
std::unique_lock<std::shared_timed_mutex> lock_db(callbacks_[action_kind]); | ||
callbacks_[action_kind].push_back(callback); | ||
} | ||
|
||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Add test subdirectories | ||
add_subdirectory(database) |
Oops, something went wrong.