zone_table.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <util/memory_segment.h>
  15. #include <dns/name.h>
  16. #include <datasrc/memory/zone_data.h>
  17. #include <datasrc/memory/zone_table.h>
  18. #include <datasrc/memory/domaintree.h>
  19. #include <cassert>
  20. using namespace std;
  21. using namespace isc::dns;
  22. namespace isc {
  23. namespace datasrc {
  24. namespace memory {
  25. namespace {
  26. struct ZoneDataDeleter {
  27. ZoneDataDeleter() {}
  28. void operator()(util::MemorySegment&, ZoneData*) const {}
  29. };
  30. }
  31. /// \short Private data and implementation of ZoneTable
  32. struct ZoneTable::ZoneTableImpl {
  33. // Type aliases to make it shorter
  34. typedef DomainTree<ZoneData, ZoneDataDeleter> ZoneTableTree;
  35. typedef DomainTreeNode<ZoneData, ZoneDataDeleter> ZoneTableNode;
  36. // The actual storage
  37. ZoneTableTree* zones_;
  38. // Constructor
  39. ZoneTableImpl(util::MemorySegment& mem_sgmt) :
  40. zones_(ZoneTableTree::create(mem_sgmt))
  41. {}
  42. /*
  43. * The implementation methods are here and just wrap-called in the
  44. * ZoneTable. We have variables locally (without impl_->), have
  45. * type aliases, etc. And they will get inlined anyway.
  46. */
  47. // Implementation of ZoneTable::addZone
  48. result::Result addZone(util::MemorySegment& mem_sgmt,
  49. const Name& zone_name, ZoneData* zone_data)
  50. {
  51. // Sanity check
  52. if (zone_data == NULL) {
  53. isc_throw(InvalidParameter,
  54. "Null pointer is passed to ZoneTable::addZone()");
  55. }
  56. // Get the node where we put the zone
  57. ZoneTableNode* node(NULL);
  58. switch (zones_->insert(mem_sgmt, zone_name, &node)) {
  59. // This is OK
  60. case ZoneTableTree::SUCCESS:
  61. case ZoneTableTree::ALREADYEXISTS:
  62. break;
  63. // Can Not Happen
  64. default:
  65. assert(0);
  66. }
  67. // Can Not Happen
  68. assert(node != NULL);
  69. // Is it empty? We either just created it or it might be nonterminal
  70. if (node->isEmpty()) {
  71. node->setData(mem_sgmt, zone_data);
  72. return (result::SUCCESS);
  73. } else { // There's something there already
  74. return (result::EXIST);
  75. }
  76. }
  77. // Implementation of ZoneTable::findZone
  78. ZoneTable::FindResult findZone(const Name& name) const {
  79. ZoneTableNode* node(NULL);
  80. result::Result my_result;
  81. // Translate the return codes
  82. switch (zones_->find(name, &node)) {
  83. case ZoneTableTree::EXACTMATCH:
  84. my_result = result::SUCCESS;
  85. break;
  86. case ZoneTableTree::PARTIALMATCH:
  87. my_result = result::PARTIALMATCH;
  88. break;
  89. // We have no data there, so translate the pointer to NULL as well
  90. case ZoneTableTree::NOTFOUND:
  91. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  92. // Can Not Happen
  93. default:
  94. assert(0);
  95. // Because of warning
  96. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  97. }
  98. // Can Not Happen (remember, NOTFOUND is handled)
  99. assert(node != NULL);
  100. // Temporarily return an easy fake value
  101. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  102. //return (FindResult(my_result, node->getData()));
  103. }
  104. };
  105. ZoneTable::ZoneTable(util::MemorySegment& mem_sgmt) :
  106. impl_(new ZoneTableImpl(mem_sgmt))
  107. {}
  108. ZoneTable::~ZoneTable() {
  109. delete impl_;
  110. }
  111. ZoneTable*
  112. ZoneTable::create(util::MemorySegment& mem_sgmt) {
  113. // The ZoneTable constructor can throw, so we need to prevent memory leak.
  114. // This is ugly, but for now this seems to be the only place we need
  115. // this, and since we'll substantially revise this code soon, so we don't
  116. // work around it by this hack at the moment.
  117. void* p = mem_sgmt.allocate(sizeof(ZoneTable));
  118. try {
  119. return (new(p) ZoneTable(mem_sgmt));
  120. } catch (...) {
  121. mem_sgmt.deallocate(p, sizeof(ZoneTable));
  122. throw;
  123. }
  124. }
  125. void
  126. ZoneTable::destroy(util::MemorySegment& mem_sgmt, ZoneTable* ztable) {
  127. ZoneTableImpl::ZoneTableTree::destroy(mem_sgmt, ztable->impl_->zones_);
  128. ztable->~ZoneTable();
  129. mem_sgmt.deallocate(ztable, sizeof(ZoneTable));
  130. }
  131. result::Result
  132. ZoneTable::addZone(util::MemorySegment& mem_sgmt, const Name& zone_name,
  133. ZoneData* zone_data)
  134. {
  135. return (impl_->addZone(mem_sgmt, zone_name, zone_data));
  136. }
  137. result::Result
  138. ZoneTable::removeZone(const Name&) {
  139. // TODO Implement
  140. assert(0);
  141. // This should not ever be returned, the assert should kill us by now
  142. return (result::SUCCESS);
  143. }
  144. ZoneTable::FindResult
  145. ZoneTable::findZone(const Name& name) const {
  146. return (impl_->findZone(name));
  147. }
  148. } // end of namespace memory
  149. } // end of namespace datasrc
  150. } // end of namespace isc