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
db_log.h \
db_messages.h \
server.h \
+ server_collection.h \
server_selector.h
///
/// 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.
///
--- /dev/null
+// 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 <database/server_collection.h>
+
+using namespace isc::data;
+
+namespace isc {
+namespace db {
+
+ServerPtr
+ServerFetcher::get(const ServerCollection& collection,
+ const ServerTag& server_tag) {
+ auto& index = collection.get<ServerTagIndexTag>();
+ auto s = index.find(server_tag.get());
+ if (s != index.end()) {
+ return (*s);
+ }
+ return (ServerPtr());
+}
+
+} // end of namespace isc::db
+} // end of namespace isc
--- /dev/null
+// 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 <database/server.h>
+#include <boost/multi_index/mem_fun.hpp>
+#include <boost/multi_index/indexed_by.hpp>
+#include <boost/multi_index/ordered_index.hpp>
+#include <boost/multi_index_container.hpp>
+
+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<ServerTagIndexTag>,
+ boost::multi_index::const_mem_fun<data::StampedElement, std::string,
+ &Server::getServerTag>
+ >
+ >
+> 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
-#include <database/server.h>
+#include <database/server_collection.h>
#include <exceptions/exceptions.h>
#include <gtest/gtest.h>
#include <string>
namespace {
+// Tests the constructor of the Server.
TEST(ServerTest, constructor) {
ServerPtr server;
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")));
+}
+
}