#ifndef OPTION_SPACE_CONTAINER_H
#define OPTION_SPACE_CONTAINER_H
+#include <exceptions/exceptions.h>
#include <list>
#include <string>
option_space_map_.clear();
}
+ /// @brief Remove all options or option definitions with a given
+ /// database identifier.
+ ///
+ /// Note that there are cases when there will be multiple options
+ /// or option definitions having the same id (typically id of 0).
+ /// When configuration backend is in use it sets the unique ids
+ /// from the database. In cases when the configuration backend is
+ /// not used, the ids default to 0.
+
+ /// @param id Identifier of the items to be deleted.
+ ///
+ /// @return Number of deleted options or option definitions.
+ uint64_t deleteItems(const uint64_t id) {
+ uint64_t num_deleted = 0;
+ for (auto space : option_space_map_) {
+ auto container = space.second;
+ auto& index = container->template get<OptionIdIndexTag>();
+ num_deleted += index.erase(id);
+ }
+
+ return (num_deleted);
+ }
+
/// @brief Check if two containers are equal.
///
/// This method checks if option space container contains exactly
OptionDefinitionPtr get(const std::string& option_space,
const std::string& option_name) const;
+ /// @brief Deletes all option definitions having a given database id.
+ ///
+ /// Note that there are cases when there will be multiple option
+ /// definitions having the same id (typically id of 0). When
+ /// configuration backend is in use it sets the unique ids from the
+ /// database. In cases when the configuration backend is not used,
+ /// the ids default to 0.
+ ///
+ /// @param id Identifier of the option definitions to be deleted.
+ ///
+ /// @return Number of deleted option definitions.
+ uint64_t del(const uint64_t id);
+
/// @brief Returns reference to container holding option definitions.
const OptionDefSpaceContainer& getContainer() const {
return (option_definitions_);
}
// This test verifies that multiple option definitions can be added
-// under different option spaces.
-TEST(CfgOptionDefTest, getAll) {
+// under different option spaces and then removed.
+TEST(CfgOptionDefTest, getAllThenDelete) {
CfgOptionDef cfg;
// Create a set of option definitions with codes between 100 and 109.
option_name << "option-" << code;
OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
"uint16"));
+ def->setId(123);
+
// Add option definition to "isc" option space.
// Option codes are not duplicated so expect no error
// when adding them.
option_name << "option-" << code;
OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code,
"uint16"));
+
+ def->setId(234);
+
ASSERT_NO_THROW(cfg.add(def, "abcde"));
}
OptionDefContainerPtr option_defs3 = cfg.getAll("non-existing");
ASSERT_TRUE(option_defs3);
EXPECT_TRUE(option_defs3->empty());
+
+ // Check that we can delete option definitions by id.
+ uint64_t num_deleted = 0;
+ ASSERT_NO_THROW(num_deleted = cfg.del(123));
+ EXPECT_EQ(10, num_deleted);
+
+ option_defs1 = cfg.getAll("isc");
+ ASSERT_TRUE(option_defs1);
+ ASSERT_EQ(0, option_defs1->size());
+
+ option_defs2 = cfg.getAll("abcde");
+ ASSERT_TRUE(option_defs2);
+ ASSERT_EQ(10, option_defs2->size());
+
+ // Second attempt to delete the same option definitions should
+ // result in 0 deletions.
+ ASSERT_NO_THROW(num_deleted = cfg.del(123));
+ EXPECT_EQ(0, num_deleted);
+
+ // Delete all other option definitions.
+ ASSERT_NO_THROW(num_deleted = cfg.del(234));
+ EXPECT_EQ(10, num_deleted);
+
+ option_defs2 = cfg.getAll("abcde");
+ ASSERT_TRUE(option_defs2);
+ ASSERT_EQ(0, option_defs2->size());
}
// This test verifies that single option definition is correctly