Skip to content

Commit fd11286

Browse files
author
Daniele Sciascia
committed
Add method transaction::has_key()
Add a method to check if transaction has appended a given key in its sr_key_set.
1 parent 1c61b80 commit fd11286

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

include/wsrep/sr_key_set.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace wsrep
3636
: root_()
3737
{ }
3838
void insert(const wsrep::key& key);
39+
bool contains(const wsrep::key& key) const;
3940
const branch_type& root() const { return root_; }
4041
void clear();
4142
bool empty() const { return root_.empty(); }

include/wsrep/transaction.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ namespace wsrep
171171

172172
int append_key(const wsrep::key&);
173173

174+
bool has_key(const wsrep::key&) const;
175+
174176
int append_data(const wsrep::const_buffer&);
175177

176178
int after_row();

src/sr_key_set.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ void wsrep::sr_key_set::insert(const wsrep::key& key)
3737
key.key_parts()[1].size()));
3838
}
3939

40+
bool wsrep::sr_key_set::contains(const wsrep::key& key) const
41+
{
42+
assert(key.size() >= 2);
43+
if (key.size() < 2)
44+
{
45+
throw wsrep::runtime_error("Invalid key size");
46+
}
47+
48+
std::string key_part_1(static_cast<const char*>(key.key_parts()[0].data()),
49+
key.key_parts()[0].size());
50+
std::string key_part_2(static_cast<const char*>(key.key_parts()[1].data()),
51+
key.key_parts()[1].size());
52+
53+
auto it(root_.find(key_part_1));
54+
if (it == root_.end())
55+
return false;
56+
leaf_type leafs(it->second);
57+
return (leafs.find(key_part_2) != leafs.end());
58+
}
59+
4060
void wsrep::sr_key_set::clear()
4161
{
4262
root_.clear();

src/transaction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ int wsrep::transaction::append_key(const wsrep::key& key)
253253
}
254254
}
255255

256+
bool wsrep::transaction::has_key(const wsrep::key& key) const
257+
{
258+
assert(active());
259+
return sr_keys_.contains(key);
260+
}
261+
256262
int wsrep::transaction::append_data(const wsrep::const_buffer& data)
257263
{
258264
assert(active());

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(TEST_SOURCES
1414
nbo_test.cpp
1515
rsu_test.cpp
1616
server_context_test.cpp
17+
sr_key_set_test.cpp
1718
toi_test.cpp
1819
transaction_test.cpp
1920
transaction_test_2pc.cpp

test/sr_key_set_test.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2024 Codership Oy <[email protected]>
3+
*
4+
* This file is part of wsrep-lib.
5+
*
6+
* Wsrep-lib is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Wsrep-lib is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "wsrep/key.hpp"
21+
#include "wsrep/sr_key_set.hpp"
22+
#include <boost/test/unit_test.hpp>
23+
24+
static void append_key_parts(wsrep::key& key,
25+
const std::vector<std::string>& parts)
26+
{
27+
for (const std::string& part : parts)
28+
{
29+
key.append_key_part(part.c_str(), part.length());
30+
}
31+
}
32+
33+
BOOST_AUTO_TEST_CASE(sr_key_set_test_contains)
34+
{
35+
wsrep::sr_key_set key_set;
36+
37+
{ // contains same key
38+
wsrep::key key(wsrep::key::exclusive);
39+
std::vector<std::string> parts = { "1", "2" };
40+
append_key_parts(key, parts);
41+
BOOST_REQUIRE(!key_set.contains(key));
42+
key_set.insert(key);
43+
BOOST_REQUIRE(key_set.contains(key));
44+
}
45+
46+
{ // contains same key with different type
47+
wsrep::key key(wsrep::key::shared);
48+
std::vector<std::string> parts = { "1", "2" };
49+
append_key_parts(key, parts);
50+
BOOST_REQUIRE(key_set.contains(key));
51+
}
52+
53+
{ // contains same key with one more level
54+
wsrep::key key(wsrep::key::shared);
55+
std::vector<std::string> parts = { "1", "2", "3" };
56+
append_key_parts(key, parts);
57+
BOOST_REQUIRE(key_set.contains(key));
58+
}
59+
60+
{ // does not contain different key first at first level
61+
wsrep::key key(wsrep::key::shared);
62+
std::vector<std::string> parts = { "different", "2" };
63+
append_key_parts(key, parts);
64+
BOOST_REQUIRE(!key_set.contains(key));
65+
}
66+
67+
{ // does not contain different key part at second level
68+
wsrep::key key(wsrep::key::shared);
69+
std::vector<std::string> parts = { "1", "different" };
70+
append_key_parts(key, parts);
71+
BOOST_REQUIRE(!key_set.contains(key));
72+
}
73+
}

0 commit comments

Comments
 (0)