Parcourir la source

[2850] Move reset() method to the base ZoneTableSegment class

Also update ZoneTableSegmentMapped testcases to test the created segment
as a base ZoneTableSegment.
Mukund Sivaraman il y a 12 ans
Parent
commit
1815740f41

+ 45 - 0
src/lib/datasrc/memory/zone_table_segment.h

@@ -140,6 +140,51 @@ public:
     ///
     /// \param segment The segment to destroy.
     static void destroy(ZoneTableSegment* segment);
+
+    /// \brief The mode using which to open a ZoneTableSegment.
+    ///
+    /// - CREATE: If the backing memory store doesn't exist, create
+    ///           it. If it exists, overwrite it with a newly created
+    ///           memory store. In both cases, open the newly created
+    ///           memory store in read+write mode.
+    ///
+    /// - READ_WRITE: If the backing memory store doesn't exist, create
+    ///               it. If it exists, use the existing memory store
+    ///               as-is. In both cases, open the memory store in
+    ///               read+write mode.
+    ///
+    /// - READ_ONLY: If the backing memory store doesn't exist, throw an
+    ///              exception. If it exists, open the existing memory
+    ///              store in read-only mode.
+    enum MemorySegmentOpenMode {
+        CREATE,
+        READ_WRITE,
+        READ_ONLY
+    };
+
+    /// \brief Unload the current memory store (if loaded) and load the
+    /// specified one.
+    ///
+    /// See the \c MemorySegmentOpenMode documentation above for the
+    /// various modes in which a ZoneTableSegment can be created.
+    ///
+    /// \c params should contain an implementation-defined
+    /// configuration. See the specific \c ZoneTableSegment
+    /// implementation class for details of what to pass in this
+    /// argument.
+    ///
+    /// \throws isc::InvalidParameter if the configuration in \c params
+    /// has incorrect syntax.
+    /// \throws isc::Unexpected for a variety of cases where an
+    /// unexpected condition occurs. These should not occur normally in
+    /// correctly written code.
+    ///
+    /// \param mode The open mode (see the MemorySegmentOpenMode
+    /// documentation).
+    /// \param params An element containing implementation-specific
+    /// config (see the description).
+    virtual void reset(MemorySegmentOpenMode mode,
+                       isc::data::ConstElementPtr params) = 0;
 };
 
 } // namespace memory

+ 7 - 0
src/lib/datasrc/memory/zone_table_segment_local.cc

@@ -37,6 +37,13 @@ ZoneTableSegmentLocal::~ZoneTableSegmentLocal() {
     assert(mem_sgmt_.allMemoryDeallocated());
 }
 
+void
+ZoneTableSegmentLocal::reset(MemorySegmentOpenMode,
+                             isc::data::ConstElementPtr)
+{
+    // This method doesn't do anything in this implementation.
+}
+
 // After more methods' definitions are added here, it would be a good
 // idea to move getHeader() and getMemorySegment() definitions to the
 // header file.

+ 6 - 0
src/lib/datasrc/memory/zone_table_segment_local.h

@@ -61,6 +61,12 @@ public:
         return (true);
     }
 
+    /// \brief This method currently doesn't do anything.
+    ///
+    /// \c mode and \c params args are currently ignored.
+    virtual void reset(MemorySegmentOpenMode mode,
+                       isc::data::ConstElementPtr params);
+
 private:
     isc::util::MemorySegmentLocal mem_sgmt_;
     ZoneTableHeader header_;

+ 5 - 25
src/lib/datasrc/memory/zone_table_segment_mapped.h

@@ -71,32 +71,12 @@ public:
     /// writable.
     virtual bool isWritable() const;
 
-    /// \brief The mode using which to open a ZoneTableSegment around a
-    /// mapped file.
-    ///
-    /// - CREATE: If the mapped file doesn't exist, create it. If it
-    ///           exists, overwrite it with a newly created mapped
-    ///           file. In both cases, open the newly created mapped
-    ///           file in read+write mode.
-    ///
-    /// - READ_WRITE: If the mapped file doesn't exist, create it. If it
-    ///               exists, use the existing mapped file. In both
-    ///               cases, open the mapped file in read+write mode.
-    ///
-    /// - READ_ONLY: If the mapped file doesn't exist, throw an
-    ///              exception. If it exists, open the existing mapped
-    ///              file in read-only mode.
-    enum MemorySegmentOpenMode {
-        CREATE,
-        READ_WRITE,
-        READ_ONLY
-    };
-
     /// \brief Unmap the current file (if mapped) and map the specified
     /// one.
     ///
-    /// See the \c MemorySegmentOpenMode documentation above for the
-    /// various modes in which a ZoneTableSegment can be created.
+    /// See the \c MemorySegmentOpenMode documentation (in
+    /// \c ZoneTableSegment class) for the various modes in which a
+    /// \c ZoneTableSegmentMapped can be created.
     ///
     /// \c params should be a map containing a "mapped-file" key that
     /// points to a string value containing the filename of a mapped
@@ -110,8 +90,8 @@ public:
     /// unexpected condition occurs. These should not occur normally in
     /// correctly written code.
     ///
-    /// \param mode The open mode (see the MemorySegmentOpenMode
-    /// documentation).
+    /// \param mode The open mode (see the \c MemorySegmentOpenMode
+    /// documentation in \c ZoneTableSegment class).
     /// \param params An element containing config for the mapped file
     /// (see the description).
     virtual void reset(MemorySegmentOpenMode mode,

+ 17 - 14
src/lib/datasrc/tests/memory/zone_table_segment_mapped_unittest.cc

@@ -33,13 +33,16 @@ const std::string mapped_file = TEST_DATA_BUILDDIR "/test.mapped";
 class ZoneTableSegmentMappedTest : public ::testing::Test {
 protected:
     ZoneTableSegmentMappedTest() :
-        ztable_segment_(dynamic_cast<ZoneTableSegmentMapped*>(
-            ZoneTableSegment::create(RRClass::IN(), "mapped"))),
+        ztable_segment_(
+            ZoneTableSegment::create(RRClass::IN(), "mapped")),
         config_params_(
             Element::fromJSON("{\"mapped-file\": \"" + mapped_file + "\"}"))
     {
-        // Verify that a ZoneTableSegmentMapped is created.
         EXPECT_NE(static_cast<void*>(NULL), ztable_segment_);
+        // Verify that a ZoneTableSegmentMapped is created.
+        ZoneTableSegmentMapped* mapped_segment =
+            dynamic_cast<ZoneTableSegmentMapped*>(ztable_segment_);
+        EXPECT_NE(static_cast<void*>(NULL), mapped_segment);
     }
 
     ~ZoneTableSegmentMappedTest() {
@@ -51,7 +54,7 @@ protected:
         ztable_segment_ = NULL;
     }
 
-    ZoneTableSegmentMapped* ztable_segment_;
+    ZoneTableSegment* ztable_segment_;
     const ConstElementPtr config_params_;
 };
 
@@ -80,25 +83,25 @@ loadAction(MemorySegment&) {
 TEST_F(ZoneTableSegmentMappedTest, resetBadConfig) {
     // Not a map
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+        ztable_segment_->reset(ZoneTableSegment::CREATE,
                                Element::fromJSON("42"));
     }, isc::InvalidParameter);
 
     // Empty map
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+        ztable_segment_->reset(ZoneTableSegment::CREATE,
                                Element::fromJSON("{}"));
     }, isc::InvalidParameter);
 
     // No "mapped-file" key
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+        ztable_segment_->reset(ZoneTableSegment::CREATE,
                                Element::fromJSON("{\"foo\": \"bar\"}"));
     }, isc::InvalidParameter);
 
     // Value of "mapped-file" key is not a string
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+        ztable_segment_->reset(ZoneTableSegment::CREATE,
                                Element::fromJSON("{\"mapped-file\": 42}"));
     }, isc::InvalidParameter);
 
@@ -115,7 +118,7 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
     // By default, the mapped file doesn't exist, so we cannot open it
     // in READ_ONLY mode (which does not create the file).
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::READ_ONLY,
+        ztable_segment_->reset(ZoneTableSegment::READ_ONLY,
                                config_params_);
     }, MemorySegmentOpenError);
 
@@ -129,7 +132,7 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
 
     // READ_WRITE mode must create the mapped file if it doesn't exist
     // (and must not result in an exception).
-    ztable_segment_->reset(ZoneTableSegmentMapped::READ_WRITE,
+    ztable_segment_->reset(ZoneTableSegment::READ_WRITE,
                            config_params_);
     // This must not throw now.
     EXPECT_TRUE(ztable_segment_->isWritable());
@@ -140,21 +143,21 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
 
     // Let's try to re-open the mapped file in READ_ONLY mode. It should
     // not fail now.
-    ztable_segment_->reset(ZoneTableSegmentMapped::READ_ONLY,
+    ztable_segment_->reset(ZoneTableSegment::READ_ONLY,
                            config_params_);
     EXPECT_FALSE(ztable_segment_->isWritable());
 
     // Re-creating the mapped file should erase old data and should not
     // trigger any exceptions inside reset() due to old data (such as
     // named addresses).
-    ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+    ztable_segment_->reset(ZoneTableSegment::CREATE,
                            config_params_);
     EXPECT_TRUE(ztable_segment_->isWritable());
 
     // When we reset() and it fails, then the segment should be
     // unusable.
     EXPECT_THROW({
-        ztable_segment_->reset(ZoneTableSegmentMapped::CREATE,
+        ztable_segment_->reset(ZoneTableSegment::CREATE,
                                Element::fromJSON("{}"));
     }, isc::InvalidParameter);
     // The following should throw now.
@@ -166,7 +169,7 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
 
     // READ_WRITE with an existing map file ought to work too. This
     // would use existing named addresses.
-    ztable_segment_->reset(ZoneTableSegmentMapped::READ_WRITE,
+    ztable_segment_->reset(ZoneTableSegment::READ_WRITE,
                            config_params_);
     EXPECT_TRUE(ztable_segment_->isWritable());
 }

+ 4 - 0
src/lib/datasrc/tests/memory/zone_table_segment_test.h

@@ -41,6 +41,10 @@ public:
         ZoneTable::destroy(mem_sgmt_, header_.getTable());
     }
 
+    virtual void reset(MemorySegmentOpenMode, isc::data::ConstElementPtr) {
+        // This method doesn't do anything.
+    }
+
     virtual ZoneTableHeader& getHeader() {
         return (header_);
     }

+ 7 - 0
src/lib/datasrc/tests/memory/zone_table_segment_unittest.cc

@@ -51,6 +51,13 @@ TEST_F(ZoneTableSegmentTest, create) {
                  UnknownSegmentType);
 }
 
+TEST_F(ZoneTableSegmentTest, reset) {
+    // reset() currently doesn't do anything in a local segment. But
+    // test the API.
+    ztable_segment_->reset(ZoneTableSegment::CREATE,
+                           Element::fromJSON("{}"));
+}
+
 // Helper function to check const and non-const methods.
 template <typename TS, typename TH, typename TT>
 void