Browse Source

[2833] make ZoneTableSegment::create generic so it checks segment type.

the actually behavior is still same: it only supports the "local" segment.
JINMEI Tatuya 12 years ago
parent
commit
d47e9e7db1

+ 12 - 6
src/lib/datasrc/memory/zone_table_segment.cc

@@ -16,6 +16,8 @@
 #include <datasrc/memory/zone_table_segment_local.h>
 #include <datasrc/zone_table_config.h>
 
+#include <string>
+
 using namespace isc::dns;
 
 namespace isc {
@@ -24,14 +26,18 @@ namespace memory {
 
 ZoneTableSegment*
 ZoneTableSegment::create(const RRClass& rrclass,
-                         const internal::ZoneTableConfig&)
+                         const internal::ZoneTableConfig& config)
 {
-    /// FIXME: For now, we always return ZoneTableSegmentLocal. This
-    /// should be updated eventually to parse the passed Element
-    /// argument and construct a corresponding ZoneTableSegment
-    /// implementation.
+    const std::string& sgmt_type = config.getSegmentType();
 
-    return (new ZoneTableSegmentLocal(rrclass));
+    // This will be a few sequences of if-else and hardcoded.  Not really
+    // sophisticated, but we don't expect to have too many types at the moment.
+    // Until that it becomes a real issue we won't be too smart.
+    if (sgmt_type == "local") {
+        return (new ZoneTableSegmentLocal(rrclass));
+    }
+    isc_throw(UnknownSegmentType, "Zone table segment type not supported: "
+              << sgmt_type);
 }
 
 void

+ 14 - 2
src/lib/datasrc/memory/zone_table_segment.h

@@ -15,6 +15,8 @@
 #ifndef ZONE_TABLE_SEGMENT_H
 #define ZONE_TABLE_SEGMENT_H
 
+#include <exceptions/exceptions.h>
+
 #include <dns/rrclass.h>
 
 #include <datasrc/memory/zone_table.h>
@@ -40,6 +42,15 @@ class ZoneTableConfig;
 namespace memory {
 class ZoneWriter;
 
+/// \brief Exception thrown when unknown or unsupported type of zone table
+/// segment is specified.
+class UnknownSegmentType : public Exception {
+public:
+    UnknownSegmentType(const char* file, size_t line, const char* what) :
+        Exception(file, line, what)
+    {}
+};
+
 /// \brief Memory-management independent entry point that contains a
 /// pointer to a zone table in memory.
 ///
@@ -102,9 +113,10 @@ public:
     /// dynamically-allocated object. The caller is responsible for
     /// destroying it with \c ZoneTableSegment::destroy().
     ///
-    /// FIXME: For now, we always return ZoneTableSegmentLocal
-    /// regardless of the passed \c config.
+    /// \throw UnknownSegmentType The memory segment type specified in
+    /// \c config is not known or not supported in this implementation.
     ///
+    /// \param rrclass The RR class of the zones to be maintained in the table.
     /// \param config The configuration based on which a derived object
     ///               is returned.
     /// \return Returns a ZoneTableSegment object

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

@@ -49,6 +49,13 @@ protected:
 TEST_F(ZoneTableSegmentTest, create) {
     // By default, a local zone table segment is created.
     EXPECT_NE(static_cast<void*>(NULL), ztable_segment_);
+
+    // Unknown types of segment are rejected.
+    const isc::datasrc::internal::ZoneTableConfig bad_ztconf(
+        "MasterFiles", 0, *Element::fromJSON("{\"cache-type\": \"unknown\","
+                                             " \"params\": {}}"));
+    EXPECT_THROW(ZoneTableSegment::create(RRClass::IN(), bad_ztconf),
+                 UnknownSegmentType);
 }
 
 // Helper function to check const and non-const methods.