zone_table.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2012 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 <datasrc/memory/zone_table.h>
  15. #include <datasrc/memory/zone_data.h>
  16. #include <datasrc/memory/domaintree.h>
  17. #include <datasrc/memory/segment_object_holder.h>
  18. #include <datasrc/memory/logger.h>
  19. #include <exceptions/exceptions.h>
  20. #include <util/memory_segment.h>
  21. #include <dns/name.h>
  22. #include <boost/function.hpp>
  23. #include <boost/bind.hpp>
  24. #include <cassert>
  25. using namespace std;
  26. using namespace isc::dns;
  27. namespace isc {
  28. namespace datasrc {
  29. namespace memory {
  30. using detail::SegmentObjectHolder;
  31. namespace {
  32. void
  33. deleteZoneData(util::MemorySegment* mem_sgmt, ZoneData* zone_data,
  34. RRClass rrclass)
  35. {
  36. // We shouldn't delete empty zone data here; the only empty zone
  37. // that can be passed here is the placeholder for broken zones maintained
  38. // in the zone table. It will stay there until the table is destroyed.
  39. if (zone_data && !zone_data->isEmpty()) {
  40. ZoneData::destroy(*mem_sgmt, zone_data, rrclass);
  41. }
  42. }
  43. typedef boost::function<void(ZoneData*)> ZoneDataDeleterType;
  44. }
  45. ZoneTable*
  46. ZoneTable::create(util::MemorySegment& mem_sgmt, const RRClass& zone_class) {
  47. // Create a placeholder "null" zone data
  48. SegmentObjectHolder<ZoneData, RRClass> zdholder(mem_sgmt, zone_class);
  49. zdholder.set(ZoneData::create(mem_sgmt));
  50. // create and setup the tree for the table.
  51. SegmentObjectHolder<ZoneTableTree, ZoneDataDeleterType> tree_holder(
  52. mem_sgmt, boost::bind(deleteZoneData, &mem_sgmt, _1, zone_class));
  53. tree_holder.set(ZoneTableTree::create(mem_sgmt));
  54. void* p = mem_sgmt.allocate(sizeof(ZoneTable));
  55. // Build zone table with the created objects. Its constructor doesn't
  56. // throw, so we can release them from the holder at this point.
  57. ZoneTable* zone_table = new(p) ZoneTable(zone_class, tree_holder.release(),
  58. zdholder.release());
  59. return (zone_table);
  60. }
  61. void
  62. ZoneTable::destroy(util::MemorySegment& mem_sgmt, ZoneTable* ztable, int)
  63. {
  64. ZoneTableTree::destroy(mem_sgmt, ztable->zones_.get(),
  65. boost::bind(deleteZoneData, &mem_sgmt, _1,
  66. ztable->rrclass_));
  67. ZoneData::destroy(mem_sgmt, ztable->null_zone_data_.get(),
  68. ztable->rrclass_);
  69. mem_sgmt.deallocate(ztable, sizeof(ZoneTable));
  70. }
  71. ZoneTable::AddResult
  72. ZoneTable::addZone(util::MemorySegment& mem_sgmt, RRClass zone_class,
  73. const Name& zone_name, ZoneData* content)
  74. {
  75. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_ADD_ZONE).
  76. arg(zone_name).arg(rrclass_);
  77. if (!content || content->isEmpty()) {
  78. isc_throw(InvalidParameter,
  79. (content ? "empty data" : "NULL") <<
  80. " is passed to Zone::addZone");
  81. }
  82. SegmentObjectHolder<ZoneData, RRClass> holder(mem_sgmt, zone_class);
  83. holder.set(content);
  84. const AddResult result =
  85. addZoneInternal(mem_sgmt, zone_name, holder.get());
  86. holder.release();
  87. return (result);
  88. }
  89. ZoneTable::AddResult
  90. ZoneTable::addEmptyZone(util::MemorySegment& mem_sgmt, const Name& zone_name) {
  91. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_ADD_EMPTY_ZONE).
  92. arg(zone_name).arg(rrclass_);
  93. return (addZoneInternal(mem_sgmt, zone_name, null_zone_data_.get()));
  94. }
  95. ZoneTable::AddResult
  96. ZoneTable::addZoneInternal(util::MemorySegment& mem_sgmt,
  97. const dns::Name& zone_name,
  98. ZoneData* content)
  99. {
  100. // Get the node where we put the zone
  101. ZoneTableNode* node(NULL);
  102. switch (zones_->insert(mem_sgmt, zone_name, &node)) {
  103. case ZoneTableTree::SUCCESS:
  104. case ZoneTableTree::ALREADYEXISTS:
  105. // These are OK
  106. break;
  107. default:
  108. // Can Not Happen
  109. assert(false);
  110. }
  111. // Can Not Happen
  112. assert(node != NULL);
  113. ZoneData* old = node->setData(content);
  114. if (old != NULL) {
  115. return (AddResult(result::EXIST, old->isEmpty() ? NULL : old));
  116. } else {
  117. ++zone_count_;
  118. return (AddResult(result::SUCCESS, NULL));
  119. }
  120. }
  121. ZoneTable::FindResult
  122. ZoneTable::findZone(const Name& name) const {
  123. const ZoneTableNode* node(NULL);
  124. result::Result my_result;
  125. // Translate the return codes
  126. switch (zones_->find(name, &node)) {
  127. case ZoneTableTree::EXACTMATCH:
  128. my_result = result::SUCCESS;
  129. break;
  130. case ZoneTableTree::PARTIALMATCH:
  131. my_result = result::PARTIALMATCH;
  132. break;
  133. case ZoneTableTree::NOTFOUND:
  134. // We have no data there, so translate the pointer to NULL as well
  135. return (FindResult(result::NOTFOUND, NULL));
  136. default:
  137. // Can Not Happen
  138. assert(0);
  139. // Because of warning
  140. return (FindResult(result::NOTFOUND, NULL));
  141. }
  142. // Can Not Happen (remember, NOTFOUND is handled). node should also have
  143. // data because the tree is constructed in the way empty nodes would
  144. // be "invisible" for find().
  145. assert(node != NULL);
  146. const ZoneData* zone_data = node->getData();
  147. assert(zone_data);
  148. const result::ResultFlags flags =
  149. zone_data->isEmpty() ? result::ZONE_EMPTY : result::FLAGS_DEFAULT;
  150. return (FindResult(my_result, zone_data->isEmpty() ? NULL : zone_data,
  151. flags));
  152. }
  153. } // end of namespace memory
  154. } // end of namespace datasrc
  155. } // end of namespace isc