]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#641,!352] Implemented ServerCollection.
authorMarcin Siodelski <marcin@isc.org>
Mon, 3 Jun 2019 13:46:31 +0000 (15:46 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 10 Jun 2019 12:39:17 +0000 (08:39 -0400)
src/lib/database/Makefile.am
src/lib/database/server.h
src/lib/database/server_collection.cc [new file with mode: 0644]
src/lib/database/server_collection.h [new file with mode: 0644]
src/lib/database/tests/server_unittest.cc

index 519c4e6482e9386dad74c07e5b416aa6c0685ada..b5c2b595bb5ca250e494dca5a39f355e68f057d2 100644 (file)
@@ -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
index 4162d1b49972d02197c50e570c13968aa24bc2b2..aa4e5b27c31c2319fc56c3fa5f0ba25b94c68488 100644 (file)
@@ -28,16 +28,14 @@ typedef boost::shared_ptr<Server> 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 (file)
index 0000000..da892c7
--- /dev/null
@@ -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 <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
diff --git a/src/lib/database/server_collection.h b/src/lib/database/server_collection.h
new file mode 100644 (file)
index 0000000..3834243
--- /dev/null
@@ -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 <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
index ac1e512579d929701bde9a36425ac29b15c44688..75e96b20b6644b97815f1b86cfd63d2e85c240f0 100644 (file)
@@ -5,7 +5,7 @@
 // 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>
@@ -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")));
+}
+
 }