Skip to content

Commit

Permalink
Implement Database
Browse files Browse the repository at this point in the history
Signed-off-by: jparisu <javierparis@eprosima.com>
  • Loading branch information
jparisu committed Sep 20, 2022
1 parent f790369 commit 2a4c1d4
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 0 deletions.
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 */
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 */
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>
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 */
1 change: 1 addition & 0 deletions ddsrouter_utils/test/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

# Add test subdirectories
add_subdirectory(collection)
add_subdirectory(event)
add_subdirectory(exception)
add_subdirectory(macros)
Expand Down
16 changes: 16 additions & 0 deletions ddsrouter_utils/test/unittest/collection/CMakeLists.txt
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)
Loading

0 comments on commit 2a4c1d4

Please sign in to comment.