rbt_datasrc.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <cstdio>
  15. #include <iostream>
  16. #include <cassert>
  17. #include "rbt_datasrc.h"
  18. using namespace isc::dns;
  19. namespace {
  20. /// helper function to remove the base domain from super domain
  21. /// the precondition of this function is thant super_name contains sub_name
  22. Name operator-(const Name& super_name, const Name& sub_name) {
  23. return (super_name.split(0, super_name.getLabelCount() - sub_name.getLabelCount()));
  24. }
  25. }
  26. namespace isc {
  27. namespace datasrc {
  28. RBNode::RBNode(const Name& name, RRsetListPtr rrsets, RBNode* nullnode)
  29. : parent_(nullnode),
  30. left_(nullnode),
  31. right_(nullnode),
  32. color_(RED),
  33. name_(name),
  34. rrsets_(rrsets),
  35. down_(NULL),
  36. is_delegate_(false),
  37. is_nonterminal_(false) {
  38. }
  39. RBNode::~RBNode() {
  40. if (down_)
  41. delete down_;
  42. }
  43. RBNode*
  44. RBNode::successor() {
  45. RBNode* current = this;
  46. if (right_ != right_->right_) {
  47. current = right_;
  48. while (current->left_ != current->left_->left_)
  49. current = current->left_;
  50. return (current);
  51. }
  52. RBNode* s = current->parent_;
  53. while (s != s->left_ && current == s->right_) {
  54. current = s;
  55. s = s->parent_;
  56. }
  57. return (s);
  58. }
  59. /// no duplication is checked now
  60. int
  61. RBNode::addRRset(RRsetPtr rrset) {
  62. if (rrset->getType() == RRType::NS())
  63. is_delegate_ = true;
  64. if (rrsets_.get() == NULL)
  65. rrsets_.reset(new RRsetList());
  66. rrsets_->addRRset(rrset);
  67. is_nonterminal_ = false;
  68. return (0);
  69. }
  70. void
  71. RBNode::cloneDNSData(RBNode& node) {
  72. node.name_ = name_;
  73. node.rrsets_ = rrsets_;
  74. node.is_delegate_ = is_delegate_;
  75. node.is_nonterminal_ = is_nonterminal_;
  76. }
  77. void
  78. RBNode::setDownTree(RBTree* down) {
  79. down_ = down;
  80. if (down)
  81. down->up_ = this;
  82. }
  83. RBTree::RBTree() {
  84. NULLNODE = new RBNode(Name(" "));
  85. NULLNODE->parent_ = NULLNODE->left_ = NULLNODE->right_ = NULLNODE;
  86. NULLNODE->color_ = BLACK;
  87. root_ = NULLNODE;
  88. node_count_ = 0;
  89. up_ = NULL;
  90. }
  91. RBTree::~RBTree() {
  92. assert(root_ != NULL);
  93. delete NULLNODE;
  94. if (root_ == NULLNODE)
  95. return;
  96. RBNode* node = root_;
  97. while (root_->left_ != NULLNODE || root_->right_ != NULLNODE) {
  98. while (node->left_ != NULLNODE || node->right_ != NULLNODE)
  99. node = (node->left_ != NULLNODE) ? node->left_ : node->right_;
  100. RBNode* parent = node->parent_;
  101. if (parent->left_ == node)
  102. parent->left_ = NULLNODE;
  103. else
  104. parent->right_ = NULLNODE;
  105. delete node;
  106. node = parent;
  107. }
  108. delete root_;
  109. root_ = NULL;
  110. }
  111. RBTree::FindResult
  112. RBTree::find(const Name& name, RBNode** node)const {
  113. RBTree* tree;
  114. return (findHelper(name, &tree, node));
  115. }
  116. RBTree::FindResult
  117. RBTree::findHelper(const Name& name, RBTree** tree, RBNode** ret)const {
  118. RBNode* node = root_;
  119. while (node != NULLNODE) {
  120. NameComparisonResult compare_result = name.compare(node->name_);
  121. NameComparisonResult::NameRelation relation = compare_result.getRelation();
  122. if (relation == NameComparisonResult::EQUAL) {
  123. *tree = (RBTree*)this;
  124. *ret = node;
  125. return (RBTree::EXACTMATCH);
  126. }
  127. else {
  128. int common_label_count = compare_result.getCommonLabels();
  129. ///common label count equal one means, there is no common between two names
  130. if (common_label_count == 1)
  131. node = (compare_result.getOrder() < 0) ? node->left_ : node->right_;
  132. else if (NameComparisonResult::SUBDOMAIN == relation) {
  133. if (node->isDelegate()) {
  134. *tree = (RBTree*)this;
  135. *ret = node;
  136. return (RBTree::FINDREFERRAL);
  137. }
  138. else if (node->down_)
  139. /// the node all save the relative name, so we need to remove the suffix
  140. return (node->down_->findHelper(name - node->name_, tree, ret));
  141. else
  142. return (RBTree::NOTFOUND);
  143. }
  144. else
  145. return (RBTree::NOTFOUND);
  146. }
  147. }
  148. return (RBTree::NOTFOUND);
  149. }
  150. int
  151. RBTree::getNodeCount() const {
  152. return (getNodeCountHelper(root_));
  153. }
  154. int
  155. RBTree::getNodeCountHelper(const RBNode *node) const {
  156. if (NULLNODE == node)
  157. return (0);
  158. int sub_tree_node_count = node->down_ ? node->down_->getNodeCount() : 0;
  159. return (1 + sub_tree_node_count + getNodeCountHelper(node->left_) + getNodeCountHelper(node->right_));
  160. }
  161. int
  162. RBTree::insert(const Name& name, RBNode** new_node) {
  163. RBNode* parent = NULLNODE;
  164. RBNode* current = root_;
  165. int order = -1;
  166. while (current != NULLNODE) {
  167. parent = current;
  168. NameComparisonResult compare_result = name.compare(current->name_);
  169. NameComparisonResult::NameRelation relation = compare_result.getRelation();
  170. if (relation == NameComparisonResult::EQUAL) {
  171. if (new_node)
  172. *new_node = current;
  173. /// if the node is non-ternimal, it doesn't exist, so we return 0
  174. return (current->rrsets_.get() ? 1 : 0);
  175. }
  176. else {
  177. int common_label_count = compare_result.getCommonLabels();
  178. if (common_label_count == 1) {
  179. order = compare_result.getOrder();
  180. current = order < 0 ? current->left_ : current->right_;
  181. } else {
  182. //insert sub domain to sub tree
  183. if (relation == NameComparisonResult::SUBDOMAIN) {
  184. if (NULL == current->down_)
  185. current->setDownTree(new RBTree());
  186. return (current->down_->insert(name - current->name_, new_node));
  187. } else {
  188. // for super domain or has common label domain, create common node first
  189. // then insert current name and new name into the sub tree
  190. Name common_ancestor = name.split(name.getLabelCount() - common_label_count, common_label_count);
  191. Name sub_name = current->name_ - common_ancestor;
  192. current->name_ = common_ancestor;
  193. RBTree* down_old = current->down_;
  194. current->setDownTree(new RBTree());
  195. RBNode* sub_root;
  196. current->down_->insert(sub_name, &sub_root);
  197. current->cloneDNSData(*sub_root);
  198. sub_root->setDownTree(down_old);
  199. sub_root->name_ = sub_name;
  200. current->rrsets_.reset();
  201. //if insert name is the super domain of current node, no need insert
  202. //otherwise insert it into the down tree
  203. if (name.getLabelCount() == common_label_count) {
  204. *new_node = current;
  205. return (0);
  206. } else {
  207. current->is_nonterminal_ = true;
  208. return (current->down_->insert(name - common_ancestor, new_node));
  209. }
  210. }
  211. }
  212. }
  213. }
  214. RBNode* node = new RBNode(name, RBNode::RRsetListPtr(), NULLNODE);
  215. node->parent_ = parent;
  216. if (parent == NULLNODE)
  217. root_ = node;
  218. else if (order < 0)
  219. parent->left_ = node;
  220. else
  221. parent->right_ = node;
  222. insertRebalance(node);
  223. if (new_node)
  224. *new_node = node;
  225. ++node_count_;
  226. return (0);
  227. }
  228. void
  229. RBTree::insertRebalance(RBNode* node) {
  230. RBNode* uncle;
  231. while (node->parent_->color_ == RED) {
  232. if (node->parent_ == node->parent_->parent_->left_) {
  233. uncle = node->parent_->parent_->right_;
  234. if (uncle->color_ == RED) {
  235. node->parent_->color_ = BLACK;
  236. uncle->color_ = BLACK;
  237. node->parent_->parent_->color_ = RED;
  238. node = node->parent_->parent_;
  239. } else {
  240. if (node == node->parent_->right_) {
  241. node = node->parent_;
  242. leftRotate(node);
  243. }
  244. node->parent_->color_ = BLACK;
  245. node->parent_->parent_->color_ = RED;
  246. rightRotate(node->parent_->parent_);
  247. }
  248. } else {
  249. uncle = node->parent_->parent_->left_;
  250. if (uncle->color_ == RED) {
  251. node->parent_->color_ = BLACK;
  252. uncle->color_ = BLACK;
  253. node->parent_->parent_->color_ = RED;
  254. node = node->parent_->parent_;
  255. } else {
  256. if (node == node->parent_->left_) {
  257. node = node->parent_;
  258. rightRotate(node);
  259. }
  260. node->parent_->color_ = BLACK;
  261. node->parent_->parent_->color_ = RED;
  262. leftRotate(node->parent_->parent_);
  263. }
  264. }
  265. }
  266. root_->color_ = BLACK;
  267. }
  268. RBNode*
  269. RBTree::leftRotate(RBNode* p) {
  270. RBNode* c = p->right_;
  271. p->right_ = c->left_;
  272. if (c->left_ != NULLNODE)
  273. c->left_->parent_ = p;
  274. c->parent_ = p->parent_;
  275. if (p->parent_ == NULLNODE)
  276. root_ = c;
  277. else if (p == p->parent_->left_)
  278. p->parent_->left_ = c;
  279. else
  280. p->parent_->right_ = c;
  281. c->left_ = p;
  282. p->parent_ = c;
  283. return (c);
  284. }
  285. RBNode*
  286. RBTree::rightRotate(RBNode* p) {
  287. RBNode* c = p->left_;
  288. p->left_ = c->right_;
  289. if (c->right_ != NULLNODE)
  290. c->right_->parent_ = p;
  291. c->parent_ = p->parent_;
  292. if (p->parent_ == NULLNODE)
  293. root_ = c;
  294. else if (p == p->parent_->left_)
  295. p->parent_->left_ = c;
  296. else
  297. p->parent_->right_ = c;
  298. c->right_ = p;
  299. p->parent_ = c;
  300. return (c);
  301. }
  302. int
  303. RBTree::erase(const Name& name) {
  304. RBNode* node;
  305. RBTree* tree;
  306. if (findHelper(name, &tree, &node) != RBTree::EXACTMATCH)
  307. return (1);
  308. /// cann't delete non terminal
  309. if (node->down_ != NULL)
  310. return (1);
  311. tree->eraseNode(node);
  312. ///merge down to up
  313. if (tree->node_count_ == 1 && tree->up_ != NULL && tree->up_->isNonterminal()) {
  314. RBNode* up = tree->up_;
  315. Name merged_name = tree->root_->name_.concatenate(up->name_);
  316. tree->root_->cloneDNSData(*up);
  317. up->setDownTree(tree->root_->down_);
  318. tree->root_->setDownTree(NULL);
  319. up->name_ = merged_name;
  320. up->is_nonterminal_ = false;
  321. delete tree;
  322. } else if (tree->node_count_ == 0 && tree->up_) { ///delete empty tree
  323. tree->up_->setDownTree(NULL);
  324. delete tree;
  325. }
  326. return (0);
  327. }
  328. void
  329. RBTree::eraseNode(RBNode *node) {
  330. RBNode* y = NULLNODE;
  331. RBNode* x = NULLNODE;
  332. if (node->left_ == NULLNODE || node->right_ == NULLNODE)
  333. y = node;
  334. else
  335. y = node->successor();
  336. if (y->left_ != NULLNODE)
  337. x = y->left_;
  338. else
  339. x = y->right_;
  340. x->parent_ = y->parent_;
  341. if (y->parent_ == NULLNODE)
  342. root_ = x;
  343. else if ( y == y->parent_->left_ )
  344. y->parent_->left_ = x;
  345. else
  346. y->parent_->right_ = x;
  347. if (y != node) {
  348. y->cloneDNSData(*node);
  349. node->setDownTree(y->down_);
  350. y->down_ = NULL;
  351. }
  352. if (y->color_ == BLACK)
  353. deleteRebalance(x);
  354. if ( y == root_)
  355. root_ = NULLNODE;
  356. y->left_ = NULL;
  357. y->right_ = NULL;
  358. delete y;
  359. --node_count_;
  360. }
  361. void
  362. RBTree::deleteRebalance(RBNode* node) {
  363. RBNode* w = NULLNODE;
  364. while (node != root_ && node->color_ == BLACK) {
  365. if (node == node->parent_->left_) {
  366. w = node->parent_->right_;
  367. if (w->color_ == RED) {
  368. w->color_ = BLACK;
  369. node->parent_->color_ = RED;
  370. leftRotate(node->parent_);
  371. w = node->parent_->right_;
  372. }
  373. if (w->left_->color_ == BLACK && w->right_->color_ == BLACK) {
  374. w->color_ = RED;
  375. node = node->parent_;
  376. } else {
  377. if (w->right_->color_ == BLACK) {
  378. w->left_->color_ = BLACK;
  379. w->color_ = RED;
  380. rightRotate(w);
  381. w = node->parent_->right_;
  382. }
  383. w->color_ = node->parent_->color_;
  384. node->parent_->color_ = BLACK;
  385. w->right_->color_ = BLACK;
  386. leftRotate(node->parent_);
  387. node = root_;
  388. }
  389. } else {
  390. w = node->parent_->left_;
  391. if (w->color_ == RED) {
  392. w->color_ = BLACK;
  393. node->parent_->color_ = RED;
  394. rightRotate(node->parent_);
  395. w = node->parent_->left_;
  396. } if (w->right_->color_ == BLACK && w->left_->color_ == BLACK) {
  397. w->color_ = RED;
  398. node = node->parent_;
  399. } else {
  400. if (w->left_->color_ == BLACK) {
  401. w->right_->color_ = BLACK;
  402. w->color_ = RED;
  403. leftRotate(w);
  404. w = node->parent_->left_;
  405. }
  406. w->color_ = node->parent_->color_;
  407. node->parent_->color_ = BLACK;
  408. w->left_->color_ = BLACK;
  409. rightRotate(node->parent_);
  410. node = root_;
  411. }
  412. }
  413. }
  414. node->color_ = BLACK;
  415. }
  416. #define INDNET(depth) do{\
  417. int i = 0;\
  418. for (; i < (depth) * 5; ++i)\
  419. std::cout << " ";\
  420. }while(0)
  421. void
  422. RBTree::printTree(int depth) const {
  423. INDNET(depth);
  424. std::cout << "tree has node " << node_count_ << "\n";
  425. printTreeHelper(root_, depth);
  426. }
  427. void
  428. RBTree::printTreeHelper(RBNode* node, int depth) const {
  429. INDNET(depth);
  430. std::cout << node->name_.toText() << " (" << ((node->color_ == BLACK) ? "black" : "red") << ")\n";
  431. std::cout << ((node->isNonterminal()) ? "[non-terminal] \n" : "\n");
  432. if (node->down_) {
  433. assert(node->down_->up_ == node);
  434. INDNET(depth + 1);
  435. std::cout << "begin down from "<< node->name_.toText() << "\n";
  436. node->down_->printTree(depth + 1);
  437. INDNET(depth + 1);
  438. std::cout << "end down from" << node->name_.toText() <<"\n";
  439. }
  440. if (node->left_ != NULLNODE)
  441. printTreeHelper(node->left_, depth + 1);
  442. else {
  443. INDNET(depth + 1);
  444. std::cout << "NULL\n";
  445. }
  446. if (node->right_ != NULLNODE)
  447. printTreeHelper(node->right_, depth + 1);
  448. else {
  449. INDNET(depth + 1);
  450. std::cout << "NULL\n";
  451. }
  452. }
  453. }
  454. }