Browse Source

[2108] Save file name during load() and implement getFileName()

getFileName() is used by the client list when reloading a zone.
We may also want to add a reload() method in the future.
Mukund Sivaraman 12 years ago
parent
commit
bd22127826
2 changed files with 64 additions and 1 deletions
  1. 50 1
      src/lib/datasrc/memory/memory_client.cc
  2. 14 0
      src/lib/datasrc/memory/memory_client.h

+ 50 - 1
src/lib/datasrc/memory/memory_client.cc

@@ -18,6 +18,7 @@
 #include <datasrc/memory/zone_data.h>
 #include <datasrc/memory/rdata_serialization.h>
 #include <datasrc/memory/rdataset.h>
+#include <datasrc/memory/domaintree.h>
 
 #include <util/memory_segment_local.h>
 
@@ -60,6 +61,8 @@ namespace memory {
 
 namespace {
 // Some type aliases
+typedef DomainTree<std::string> FileNameTree;
+typedef DomainTreeNode<std::string> FileNameNode;
 
 // A functor type used for loading.
 typedef boost::function<void(ConstRRsetPtr)> LoadCallback;
@@ -73,13 +76,26 @@ typedef boost::function<void(ConstRRsetPtr)> LoadCallback;
 /// consists of (pointers to) \c InMemoryZoneFinder objects, we may add more
 /// member variables later for new features.
 class InMemoryClient::InMemoryClientImpl {
+private:
+    // The deleter for the filenames stored in the tree.
+    struct FileNameDeleter {
+        FileNameDeleter() {}
+        void operator()(std::string* filename) const {
+            delete filename;
+        }
+    };
+
 public:
     InMemoryClientImpl(RRClass rrclass) :
         rrclass_(rrclass),
         zone_count(0),
-        zone_table_(ZoneTable::create(local_mem_sgmt, rrclass))
+        zone_table_(ZoneTable::create(local_mem_sgmt, rrclass)),
+        file_name_tree_(FileNameTree::create(local_mem_sgmt, false))
     {}
     ~InMemoryClientImpl() {
+        FileNameDeleter deleter;
+        FileNameTree::destroy(local_mem_sgmt, file_name_tree_, deleter);
+
         ZoneTable::destroy(local_mem_sgmt, zone_table_, rrclass_);
 
         // see above for the assert().
@@ -93,6 +109,7 @@ public:
     RRClass rrclass_;
     unsigned int zone_count;
     ZoneTable* zone_table_;
+    FileNameTree* file_name_tree_;
 
     // Common process for zone load.
     // rrset_installer is a functor that takes another functor as an argument,
@@ -590,6 +607,26 @@ InMemoryClient::InMemoryClientImpl::load(
         arg(zone_name).arg(getClass().toText());
 
     ++impl_->zone_count;
+
+    // Set the filename in file_name_tree_ now, so that getFileName()
+    // can use it (during zone reloading).
+    FileNameNode* node(NULL);
+    switch (impl_->file_name_tree_->insert(impl_->local_mem_sgmt,
+                                           zone_name, &node)) {
+    case FileNameTree::SUCCESS:
+    case FileNameTree::ALREADYEXISTS:
+        // These are OK
+        break;
+    default:
+        // Can Not Happen
+        assert(false);
+    }
+    // node must point to a valid node now
+    assert(node != NULL);
+
+    std::string* tstr = node->setData(new std::string(filename));
+    delete tstr;
+
     return (result.code);
 }
 
@@ -682,6 +719,18 @@ InMemoryClient::load(const isc::dns::Name& zone_name,
                                     &iterator, _1)));
 }
 
+const std::string
+InMemoryClient::getFileName(const isc::dns::Name& zone_name) const {
+    FileNameNode* node(NULL);
+    FileNameTree::Result result = impl_->file_name_tree_->find(zone_name,
+                                                               &node);
+    if (result == FileNameTree::EXACTMATCH) {
+        return (*node->getData());
+    } else {
+        return (std::string());
+    }
+}
+
 result::Result
 InMemoryClient::add(const isc::dns::Name& zone_name,
                     const ConstRRsetPtr& rrset) {

+ 14 - 0
src/lib/datasrc/memory/memory_client.h

@@ -118,6 +118,20 @@ public:
     result::Result load(const isc::dns::Name& zone_name,
 			ZoneIterator& iterator);
 
+    /// Return the master file name of the zone
+    ///
+    /// This method returns the name of the zone's master file to be loaded.
+    /// The returned string will be an empty unless the data source client has
+    /// successfully loaded the zone before.
+    ///
+    /// This method should normally not throw an exception.  But the creation
+    /// of the return string may involve a resource allocation, and if it
+    /// fails, the corresponding standard exception will be thrown.
+    ///
+    /// \return The name of the zone file loaded in the client, or an empty
+    /// string if the client hasn't loaded any file.
+    const std::string getFileName(const isc::dns::Name& zone_name) const;
+
     /// \brief Inserts an rrset into the zone.
     ///
     /// It puts another RRset into the zone.