From: Marcin Siodelski Date: Mon, 3 Jun 2019 13:46:31 +0000 (+0200) Subject: [#641,!352] Implemented ServerCollection. X-Git-Tag: Kea-1.6.0-beta2~318 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4256234c1705dc34aa069d307300156cfe0ab80f;p=thirdparty%2Fkea.git [#641,!352] Implemented ServerCollection. --- diff --git a/src/lib/database/Makefile.am b/src/lib/database/Makefile.am index 519c4e6482..b5c2b595bb 100644 --- a/src/lib/database/Makefile.am +++ b/src/lib/database/Makefile.am @@ -19,6 +19,7 @@ libkea_database_la_SOURCES += db_exceptions.h libkea_database_la_SOURCES += db_log.cc db_log.h libkea_database_la_SOURCES += db_messages.cc db_messages.h libkea_database_la_SOURCES += server.cc server.h +libkea_database_la_SOURCES += server_collection.cc server_collection.h libkea_database_la_SOURCES += server_selector.h libkea_database_la_LIBADD = $(top_builddir)/src/lib/cc/libkea-cc.la @@ -75,4 +76,5 @@ libkea_database_include_HEADERS = \ db_log.h \ db_messages.h \ server.h \ + server_collection.h \ server_selector.h diff --git a/src/lib/database/server.h b/src/lib/database/server.h index 4162d1b499..aa4e5b27c3 100644 --- a/src/lib/database/server.h +++ b/src/lib/database/server.h @@ -28,16 +28,14 @@ typedef boost::shared_ptr ServerPtr; /// /// This class extends the base class with the server description field. class Server : public data::StampedElement { -private: +public: - /// @brief Private constructor. + /// @brief Constructor. /// /// @param tag server tag. /// @param server description. Server(const data::ServerTag& tag, const std::string& description); -public: - /// @brief Factory function to be used to create an instance of the /// @c Server object. /// diff --git a/src/lib/database/server_collection.cc b/src/lib/database/server_collection.cc new file mode 100644 index 0000000000..da892c7d95 --- /dev/null +++ b/src/lib/database/server_collection.cc @@ -0,0 +1,26 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +using namespace isc::data; + +namespace isc { +namespace db { + +ServerPtr +ServerFetcher::get(const ServerCollection& collection, + const ServerTag& server_tag) { + auto& index = collection.get(); + auto s = index.find(server_tag.get()); + if (s != index.end()) { + return (*s); + } + return (ServerPtr()); +} + +} // end of namespace isc::db +} // end of namespace isc diff --git a/src/lib/database/server_collection.h b/src/lib/database/server_collection.h new file mode 100644 index 0000000000..383424306b --- /dev/null +++ b/src/lib/database/server_collection.h @@ -0,0 +1,53 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef DB_SERVER_COLLECTION_H +#define DB_SERVER_COLLECTION_H + +#include +#include +#include +#include +#include + +namespace isc { +namespace db { + +/// @brief Tag identifying an index by server tag. +struct ServerTagIndexTag { }; + +/// @brief Multi index container for @c Server. +/// +/// It merely contains one index at the moment, but the number of +/// indexes is likely to grow. +typedef boost::multi_index_container< + ServerPtr, + boost::multi_index::indexed_by< + boost::multi_index::ordered_unique< + boost::multi_index::tag, + boost::multi_index::const_mem_fun + > + > +> ServerCollection; + +/// @brief Utility class used to fetch @c Server objects from the +/// @c ServerCollection. +class ServerFetcher { +public: + + /// @brief Fetches server from the collection by tag. + /// + /// @return Pointer to the @c Server object or null if no such object + /// was found. + static ServerPtr get(const ServerCollection& collection, + const data::ServerTag& server_tag); +}; + +} // end of namespace isc::db +} // end of namespace isc + +#endif diff --git a/src/lib/database/tests/server_unittest.cc b/src/lib/database/tests/server_unittest.cc index ac1e512579..75e96b20b6 100644 --- a/src/lib/database/tests/server_unittest.cc +++ b/src/lib/database/tests/server_unittest.cc @@ -5,7 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include +#include #include #include #include @@ -16,6 +16,7 @@ using namespace isc::db; namespace { +// Tests the constructor of the Server. TEST(ServerTest, constructor) { ServerPtr server; @@ -27,9 +28,36 @@ TEST(ServerTest, constructor) { EXPECT_EQ("my first server", server->getDescription()); } +// Tests that too long description is rejected. TEST(ServerTest, tooLongDescription) { EXPECT_THROW(Server::create(ServerTag("xyz"), std::string(65537, 'c')), BadValue); } +// Tests that it is possible to fetch server by tag fromn the collection. +TEST(ServerFetcherTest, getByTag) { + ServerCollection servers; + servers.insert(Server::create(ServerTag("alpha"), "alpha description")); + servers.insert(Server::create(ServerTag("beta"), "beta description")); + servers.insert(Server::create(ServerTag("gamma"), "gamma description")); + + auto alpha = ServerFetcher::get(servers, ServerTag("alpha")); + ASSERT_TRUE(alpha); + EXPECT_EQ("alpha", alpha->getServerTag()); + EXPECT_EQ("alpha description", alpha->getDescription()); + + auto beta = ServerFetcher::get(servers, ServerTag("beta")); + ASSERT_TRUE(beta); + EXPECT_EQ("beta", beta->getServerTag()); + EXPECT_EQ("beta description", beta->getDescription()); + + auto gamma = ServerFetcher::get(servers, ServerTag("gamma")); + ASSERT_TRUE(gamma); + EXPECT_EQ("gamma", gamma->getServerTag()); + EXPECT_EQ("gamma description", gamma->getDescription()); + + // Null pointer should be returned when a given server does not exist. + EXPECT_FALSE(ServerFetcher::get(servers, ServerTag("non-existent"))); +} + }