zone_table.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <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 <datasrc/memory/segment_object_holder.h>
  20. #include <boost/function.hpp>
  21. #include <boost/bind.hpp>
  22. #include <cassert>
  23. using namespace std;
  24. using namespace isc::dns;
  25. namespace isc {
  26. namespace datasrc {
  27. namespace memory {
  28. using detail::SegmentObjectHolder;
  29. namespace {
  30. void
  31. deleteZoneData(util::MemorySegment* mem_sgmt, ZoneData* zone_data,
  32. RRClass rrclass)
  33. {
  34. if (zone_data != NULL) {
  35. ZoneData::destroy(*mem_sgmt, zone_data, rrclass);
  36. }
  37. }
  38. typedef boost::function<void(ZoneData*)> ZoneDataDeleterType;
  39. }
  40. ZoneTable*
  41. ZoneTable::create(util::MemorySegment& mem_sgmt, RRClass zone_class) {
  42. SegmentObjectHolder<ZoneTableTree, ZoneDataDeleterType> holder(
  43. mem_sgmt, ZoneTableTree::create(mem_sgmt),
  44. boost::bind(deleteZoneData, &mem_sgmt, _1, zone_class));
  45. void* p = mem_sgmt.allocate(sizeof(ZoneTable));
  46. ZoneTable* zone_table = new(p) ZoneTable(holder.get());
  47. holder.release();
  48. return (zone_table);
  49. }
  50. void
  51. ZoneTable::destroy(util::MemorySegment& mem_sgmt, ZoneTable* ztable,
  52. RRClass zone_class)
  53. {
  54. ZoneTableTree::destroy(mem_sgmt, ztable->zones_.get(),
  55. boost::bind(deleteZoneData, &mem_sgmt, _1,
  56. zone_class));
  57. mem_sgmt.deallocate(ztable, sizeof(ZoneTable));
  58. }
  59. ZoneTable::AddResult
  60. ZoneTable::addZone(util::MemorySegment& mem_sgmt, RRClass zone_class,
  61. const Name& zone_name, ZoneData* content)
  62. {
  63. if (content == NULL) {
  64. isc_throw(isc::BadValue, "Zone content must not be NULL");
  65. }
  66. SegmentObjectHolder<ZoneData, RRClass> holder(mem_sgmt, content,
  67. zone_class);
  68. // Get the node where we put the zone
  69. ZoneTableNode* node(NULL);
  70. switch (zones_->insert(mem_sgmt, zone_name, &node)) {
  71. case ZoneTableTree::SUCCESS:
  72. case ZoneTableTree::ALREADYEXISTS:
  73. // These are OK
  74. break;
  75. default:
  76. // Can Not Happen
  77. assert(false);
  78. }
  79. // Can Not Happen
  80. assert(node != NULL);
  81. // We can release now, setData never throws
  82. ZoneData* old = node->setData(holder.release());
  83. if (old != NULL) {
  84. return (AddResult(result::EXIST, old));
  85. } else {
  86. return (AddResult(result::SUCCESS, NULL));
  87. }
  88. }
  89. ZoneTable::FindResult
  90. ZoneTable::findZone(const Name& name) const {
  91. const ZoneTableNode* node(NULL);
  92. result::Result my_result;
  93. // Translate the return codes
  94. switch (zones_->find(name, &node)) {
  95. case ZoneTableTree::EXACTMATCH:
  96. my_result = result::SUCCESS;
  97. break;
  98. case ZoneTableTree::PARTIALMATCH:
  99. my_result = result::PARTIALMATCH;
  100. break;
  101. case ZoneTableTree::NOTFOUND:
  102. // We have no data there, so translate the pointer to NULL as well
  103. return (FindResult(result::NOTFOUND, NULL));
  104. default:
  105. // Can Not Happen
  106. assert(0);
  107. // Because of warning
  108. return (FindResult(result::NOTFOUND, NULL));
  109. }
  110. // Can Not Happen (remember, NOTFOUND is handled)
  111. assert(node != NULL);
  112. return (FindResult(my_result, node->getData()));
  113. }
  114. } // end of namespace memory
  115. } // end of namespace datasrc
  116. } // end of namespace isc