zone_table.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. result::Result
  60. ZoneTable::addZone(util::MemorySegment& mem_sgmt, RRClass zone_class,
  61. const Name& zone_name,
  62. SegmentObjectHolder<ZoneData, RRClass>& content)
  63. {
  64. // Get the node where we put the zone
  65. ZoneTableNode* node(NULL);
  66. switch (zones_->insert(mem_sgmt, zone_name, &node)) {
  67. case ZoneTableTree::SUCCESS:
  68. case ZoneTableTree::ALREADYEXISTS:
  69. // These are OK
  70. break;
  71. default:
  72. // Can Not Happen
  73. assert(false);
  74. }
  75. // Can Not Happen
  76. assert(node != NULL);
  77. // We can release now, setData never throws
  78. ZoneData* old = node->setData(content.release());
  79. if (old != NULL) {
  80. ZoneData::destroy(mem_sgmt, old, zone_class);
  81. return (result::EXIST);
  82. } else {
  83. return (result::SUCCESS);
  84. }
  85. }
  86. ZoneTable::FindResult
  87. ZoneTable::findZone(const Name& name) const {
  88. const ZoneTableNode* node(NULL);
  89. result::Result my_result;
  90. // Translate the return codes
  91. switch (zones_->find(name, &node)) {
  92. case ZoneTableTree::EXACTMATCH:
  93. my_result = result::SUCCESS;
  94. break;
  95. case ZoneTableTree::PARTIALMATCH:
  96. my_result = result::PARTIALMATCH;
  97. break;
  98. case ZoneTableTree::NOTFOUND:
  99. // We have no data there, so translate the pointer to NULL as well
  100. return (FindResult(result::NOTFOUND, NULL));
  101. default:
  102. // Can Not Happen
  103. assert(0);
  104. // Because of warning
  105. return (FindResult(result::NOTFOUND, NULL));
  106. }
  107. // Can Not Happen (remember, NOTFOUND is handled)
  108. assert(node != NULL);
  109. return (FindResult(my_result, node->getData()));
  110. }
  111. } // end of namespace memory
  112. } // end of namespace datasrc
  113. } // end of namespace isc