1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-07-30 07:23:07 +03:00

codership/wsrep-lib#35 Rewrote member_index() to use iterators

Use iterators for scanning members vector in order to avoid
issues with integer signedness and range checks. The vector is
usually rather small and not in hot codepath, so performance
is here not an issue.

Added unit test for member_index() method.
This commit is contained in:
Teemu Ollakka
2018-12-15 13:46:42 +00:00
parent 80dcf4b6d2
commit 36dbb37645
4 changed files with 50 additions and 12 deletions

View File

@ -116,7 +116,10 @@ namespace wsrep
} }
/** /**
* Return member index in the view * Return member index in the view.
*
* @return Member index if found, -1 if member is not present
* in the view.
*/ */
int member_index(const wsrep::id& member_id) const; int member_index(const wsrep::id& member_id) const;

View File

@ -22,21 +22,14 @@
int wsrep::view::member_index(const wsrep::id& member_id) const int wsrep::view::member_index(const wsrep::id& member_id) const
{ {
// first, quick guess for (std::vector<member>::const_iterator i(members_.begin());
if (own_index_ >= 0 && members_[own_index_].id() == member_id) i != members_.end(); ++i)
{ {
return own_index_; if (i->id() == member_id)
}
// guesing didn't work, scan the list
for (size_t i(0); i < members_.size(); ++i)
{
if (static_cast<int>(i) != own_index_ && members_[i].id() == member_id)
{ {
return i; return (i - members_.begin());
} }
} }
return -1; return -1;
} }

View File

@ -11,6 +11,7 @@ add_executable(wsrep-lib_test
server_context_test.cpp server_context_test.cpp
transaction_test.cpp transaction_test.cpp
transaction_test_2pc.cpp transaction_test_2pc.cpp
view_test.cpp
wsrep-lib_test.cpp wsrep-lib_test.cpp
) )

41
test/view_test.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2018 Codership Oy <info@codership.com>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#include "wsrep/view.hpp"
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(view_test_member_index)
{
std::vector<wsrep::view::member> members;
members.push_back(wsrep::view::member(wsrep::id("1"), "", ""));
members.push_back(wsrep::view::member(wsrep::id("2"), "", ""));
members.push_back(wsrep::view::member(wsrep::id("3"), "", ""));
wsrep::view view(wsrep::gtid(wsrep::id("cluster"), wsrep::seqno(1)),
wsrep::seqno(1),
wsrep::view::primary,
0,
1,
0,
members);
BOOST_REQUIRE(view.member_index(wsrep::id("1")) == 0);
BOOST_REQUIRE(view.member_index(wsrep::id("2")) == 1);
BOOST_REQUIRE(view.member_index(wsrep::id("3")) == 2);
BOOST_REQUIRE(view.member_index(wsrep::id("4")) == -1);
}