Skip to content

Commit 86615e7

Browse files
authored
Database interfaces and an implementation (#49)
* Implementation of Database Signed-off-by: jparisu <javierparis@eprosima.com> * Implement SimpleDatabase Signed-off-by: jparisu <javierparis@eprosima.com> * Add tests Signed-off-by: jparisu <javierparis@eprosima.com> * Final design and implementation of Database Signed-off-by: jparisu <javierparis@eprosima.com> * Add documentation to SafeDatabase Signed-off-by: jparisu <javierparis@eprosima.com> * change shared_mutex for timed_shared_mutex Signed-off-by: jparisu <javierparis@eprosima.com> * move impl iterator to ipp Signed-off-by: jparisu <javierparis@eprosima.com> * Add copy methods Signed-off-by: jparisu <javierparis@eprosima.com> * fix windows problem Signed-off-by: jparisu <javierparis@eprosima.com> * Fix error due to windows shared mutex design Signed-off-by: jparisu <javierparis@eprosima.com> * apply suggestions Signed-off-by: jparisu <javierparis@eprosima.com> * apply suggestion in test Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com> --------- Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent ec72ee5 commit 86615e7

16 files changed

Lines changed: 1677 additions & 6 deletions

File tree

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ jobs:
459459
# FLAKY TEST
460460
flaky-test:
461461
runs-on: ubuntu-20.04
462-
environment:
463-
name: codecov
464462

465463
steps:
466464
- name: Sync eProsima/dev-utils repository
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <utility>
18+
19+
namespace eprosima {
20+
namespace utils {
21+
22+
/**
23+
* Class that represents a generic database of values indexed by key.
24+
*
25+
* Database is a really extended interface. These are the uses for what this is made:
26+
* - thread safe in its access and iteration.
27+
* - easy accessors to internal values.
28+
* - methods to be notify with changes (specialized classes).
29+
*
30+
* @tparam Key type of the key that indexes the values.
31+
* @tparam Value type of the values stored inside.
32+
* @tparam Iterator type of the iterator to iterate over elements.
33+
*
34+
* @todo this is difficult to use because of Iterator type. This should change in the future.
35+
*/
36+
template <typename Key, typename Value, typename Iterator>
37+
class IDatabase
38+
{
39+
public:
40+
41+
/**
42+
* @brief Add a new element to the database by moving it.
43+
*
44+
* If the key already exists in the database, the element will not be added.
45+
*
46+
* @note this interface only uses movement because it is the only way to avoid unnecessary copies
47+
* and allow to pass every kind of type.
48+
* However It may make more difficult to use from a user.
49+
*
50+
* @param key index or key for the new element
51+
* @param value new value to store
52+
*
53+
* @return true if the element was correctly added.
54+
* @return false if key is repeated.
55+
*/
56+
virtual bool add(
57+
Key&& key,
58+
Value&& value) = 0;
59+
60+
/**
61+
* @brief Whether an element with this key exist in the database.
62+
*
63+
* @param key key to look for.
64+
* @return true if there is an element stored with such key.
65+
* @return false if the key does not exist.
66+
*/
67+
virtual bool is(
68+
const Key& key) const = 0;
69+
70+
/**
71+
* @brief Look for a value indexed with a key.
72+
*
73+
* @param key key to look for.
74+
* @return Iterator pointing to the element to find. It points to end() if the element is not found.
75+
*/
76+
virtual Iterator find(
77+
const Key& key) const = 0;
78+
79+
/**
80+
* @brief Points to the first element on the database.
81+
*
82+
* @note As Database must be thread safe, these iterators force the database to be kept in a coherent state.
83+
* This means that holding one of this iterators for too long may block other database functionality.
84+
*
85+
* @return Iterator point to the first element in the database.
86+
*/
87+
virtual Iterator begin() const = 0;
88+
89+
/**
90+
* @brief Points to the last + 1 element on the database.
91+
*
92+
* @note As Database must be thread safe, these iterators force the database to be kept in a coherent state.
93+
* This means that holding one of this iterators for too long may block other database functionality.
94+
*
95+
* @return Iterator point to the end of the database. No element is stored under this iterator.
96+
*/
97+
virtual Iterator end() const = 0;
98+
};
99+
100+
} /* namespace utils */
101+
} /* namespace eprosima */
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <cpp_utils/collection/database/IDatabase.hpp>
18+
19+
namespace eprosima {
20+
namespace utils {
21+
22+
/**
23+
* Database specialization that allow to register callbacks to get notified each time a new element is added.
24+
*
25+
* @todo it requires a way to unregister callbacks. This could be done by only accepting a callback (and then
26+
* that callback could be to multiple other ones) or by setting an index for each callback (maybe a ptr is enough).
27+
*/
28+
template <typename Key, typename Value, typename Iterator>
29+
class IDynamicDatabase : public IDatabase<Key, Value, Iterator>
30+
{
31+
public:
32+
33+
/**
34+
* @brief Registering a callback to receive a notification every time a data is added to the database.
35+
*
36+
* @param callback function to call when value added.
37+
*/
38+
virtual void register_callback_add(void(const Key&, const Value&) && callback) = 0;
39+
};
40+
41+
} /* namespace utils */
42+
} /* namespace eprosima */
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <cpp_utils/collection/database/IModificableDatabase.hpp>
18+
19+
namespace eprosima {
20+
namespace utils {
21+
22+
/**
23+
* Database specialization that allow to register callbacks to get notified each time a new element is added,
24+
* modified or removed.
25+
*/
26+
template <typename Key, typename Value, typename Iterator>
27+
class IDynamicModificableDatabase : public IModificableDatabase<Key, Value, Iterator>, public IDynamicDatabase<Key,
28+
Value, Iterator>
29+
{
30+
public:
31+
32+
/**
33+
* @brief Registering a callback to receive a notification every time a data is modified in the database.
34+
*
35+
* @param callback function to call when value modified.
36+
*/
37+
virtual void register_callback_modify(void(const Key&, const Value&) && callback) = 0;
38+
39+
/**
40+
* @brief Registering a callback to receive a notification every time a data is removed from the database.
41+
*
42+
* @param callback function to call when value removed.
43+
*/
44+
virtual void register_callback_remove(void(const Key&, const Value&) && callback) = 0;
45+
46+
};
47+
48+
} /* namespace utils */
49+
} /* namespace eprosima */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <cpp_utils/collection/database/IDatabase.hpp>
18+
19+
namespace eprosima {
20+
namespace utils {
21+
22+
/**
23+
* Inheritance from \c IDatabase that allows to modify or erase a value from a database.
24+
*/
25+
template <typename Key, typename Value, typename Iterator>
26+
class IModificableDatabase : public IDatabase<Key, Value, Iterator>
27+
{
28+
public:
29+
30+
/**
31+
* @brief Modify a value that already exist in the database.
32+
*
33+
* @param key key indexing the new or existent value.
34+
* @param value new value to set.
35+
*
36+
* @return true if value has been modified.
37+
* @return false if value did not exist in the database.
38+
*/
39+
virtual bool modify(
40+
const Key& key,
41+
Value&& value) = 0;
42+
43+
/**
44+
* @brief Add a new value or modify an existent one with the new value.
45+
*
46+
* @param key key indexing the new or existent value.
47+
* @param value new value to set.
48+
*
49+
* @return true if value has been added.
50+
* @return false if value already exists, in this case the value is modified.
51+
*/
52+
virtual bool add_or_modify(
53+
Key&& key,
54+
Value&& value) = 0;
55+
56+
/**
57+
* @brief Remove the value indexed by \c key that already exist in the database.
58+
*
59+
* @param key key indexing the value.
60+
*
61+
* @return true if value has been removed.
62+
* @return false if value did not exist in the database.
63+
*/
64+
virtual bool erase(
65+
const Key& key) = 0;
66+
};
67+
68+
} /* namespace utils */
69+
} /* namespace eprosima */

0 commit comments

Comments
 (0)