zonetable.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <cassert>
  15. #include <dns/name.h>
  16. #include <datasrc/zonetable.h>
  17. #include <datasrc/rbtree.h>
  18. using namespace std;
  19. using namespace isc::dns;
  20. namespace isc {
  21. namespace datasrc {
  22. /// \short Private data and implementation of ZoneTable
  23. struct ZoneTable::ZoneTableImpl {
  24. // Type aliases to make it shorter
  25. typedef RBTree<Zone> ZoneTree;
  26. typedef RBNode<Zone> ZoneNode;
  27. // The actual storage
  28. ZoneTree zones_;
  29. /*
  30. * The implementation methods are here and just wrap-called in the
  31. * ZoneTable. We have variables locally (without impl_->), have
  32. * type aliases, etc. And they will get inlined anyway.
  33. */
  34. // Implementation of ZoneTable::addZone
  35. result::Result addZone(ZonePtr zone) {
  36. // Sanity check
  37. if (!zone) {
  38. isc_throw(InvalidParameter,
  39. "Null pointer is passed to ZoneTable::addZone()");
  40. }
  41. // Get the node where we put the zone
  42. ZoneNode* node(NULL);
  43. switch (zones_.insert(zone->getOrigin(), &node)) {
  44. // This is OK
  45. case ZoneTree::SUCCESS:
  46. case ZoneTree::ALREADYEXISTS:
  47. break;
  48. // Can Not Happen
  49. default:
  50. assert(0);
  51. }
  52. // Can Not Happen
  53. assert(node);
  54. // Is it empty? We either just created it or it might be nonterminal
  55. if (node->isEmpty()) {
  56. node->setData(zone);
  57. return (result::SUCCESS);
  58. } else { // There's something there already
  59. return (result::EXIST);
  60. }
  61. }
  62. // Implementation of ZoneTable::findZone
  63. ZoneTable::FindResult findZone(const Name& name) const {
  64. ZoneNode *node(NULL);
  65. result::Result my_result;
  66. // Translate the return codes
  67. switch (zones_.find(name, &node)) {
  68. case ZoneTree::EXACTMATCH:
  69. my_result = result::SUCCESS;
  70. break;
  71. case ZoneTree::PARTIALMATCH:
  72. my_result = result::PARTIALMATCH;
  73. break;
  74. // We have no data there, so translate the pointer to NULL as well
  75. case ZoneTree::NOTFOUND:
  76. return (FindResult(result::NOTFOUND, ZonePtr()));
  77. // Can Not Happen
  78. default:
  79. assert(0);
  80. // Because of warning
  81. return (FindResult(result::NOTFOUND, ZonePtr()));
  82. }
  83. // Can Not Happen (remember, NOTFOUND is handled)
  84. assert(node);
  85. return (FindResult(my_result, node->getData()));
  86. }
  87. };
  88. ZoneTable::ZoneTable() : impl_(new ZoneTableImpl)
  89. {}
  90. ZoneTable::~ZoneTable() {
  91. delete impl_;
  92. }
  93. result::Result
  94. ZoneTable::addZone(ZonePtr zone) {
  95. return (impl_->addZone(zone));
  96. }
  97. result::Result
  98. ZoneTable::removeZone(const Name&) {
  99. // TODO Implement
  100. assert(0);
  101. // This should not ever be returned, the assert should kill us by now
  102. return (result::SUCCESS);
  103. }
  104. ZoneTable::FindResult
  105. ZoneTable::findZone(const Name& name) const {
  106. return (impl_->findZone(name));
  107. }
  108. } // end of namespace datasrc
  109. } // end of namespace isc