mmdb_btree.C 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: mmdb_btree.cc /main/3 1996/06/11 17:14:32 cde-hal $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "btree/mmdb_btree.h"
  50. btree::btree(const char* store_name)
  51. {
  52. // let the package figure out all these parameters
  53. btree_info.flags = 0;
  54. btree_info.cachesize = 0;
  55. btree_info.maxkeypage = 0;
  56. btree_info.minkeypage = 0;
  57. btree_info.psize = 0;
  58. btree_info.compare = NULL;
  59. btree_info.prefix = NULL;
  60. btree_info.lorder = 0;
  61. key_DBT.data = 0;
  62. key_DBT.size = 0;
  63. int mode = O_CREAT|O_RDWR;
  64. //btree_DB = dbopen(store_name, mode, 0640, DB_BTREE, &btree_info);
  65. btree_DB = dbopen(store_name, mode, 0640, DB_BTREE, NULL);
  66. if ( btree_DB == 0 )
  67. throw(stringException("btree dbopen failed"));
  68. }
  69. btree::~btree()
  70. {
  71. if ( btree_DB->sync(btree_DB, 0) == RET_ERROR ) {
  72. cerr << "btree sync failed";
  73. std::exit(1);
  74. }
  75. if ( btree_DB->close(btree_DB) == RET_ERROR ) {
  76. cerr << "btree close failed";
  77. std::exit(1);
  78. }
  79. }
  80. void btree::clean()
  81. {
  82. throw(stringException("void btree::clean(): not implemented yet"));
  83. }
  84. void btree::data_t_2_DBT(data_t& w)
  85. {
  86. switch (w.flag) {
  87. case data_t::INT:
  88. key_DBT.data = &w.key.int_key;
  89. key_DBT.size = sizeof(w.key.int_key);
  90. break;
  91. case data_t::STRING:
  92. key_DBT.data = w.key.str_key;
  93. key_DBT.size = strlen(w.key.str_key);
  94. break;
  95. case data_t::VOID:
  96. throw(stringException("btree data_t_2_DBT: unknown key type"));
  97. }
  98. }
  99. Boolean btree::insert(data_t& w)
  100. {
  101. data_t_2_DBT(w);
  102. DBT data_DBT;
  103. data_DBT.data = &w.dt;
  104. data_DBT.size = sizeof(w.dt);
  105. //int status = btree_DB->put(btree_DB, &key_DBT, &data_DBT, R_NOOVERWRITE);
  106. int status = btree_DB->put(btree_DB, &key_DBT, &data_DBT, 0);
  107. switch (status) {
  108. case RET_ERROR:
  109. throw(stringException("btree put failed"));
  110. break;
  111. case RET_SPECIAL:
  112. throw(stringException("btree put: dup key"));
  113. break;
  114. case RET_SUCCESS:
  115. return true;
  116. }
  117. return false;
  118. }
  119. Boolean btree::remove(data_t& w)
  120. {
  121. data_t_2_DBT(w);
  122. int status = btree_DB->del(btree_DB, &key_DBT, 0);
  123. switch (status) {
  124. case RET_ERROR:
  125. throw(stringException("btree delete failed"));
  126. break;
  127. case RET_SPECIAL:
  128. case RET_SUCCESS:
  129. return true;
  130. }
  131. return false;
  132. }
  133. Boolean btree::member(data_t& w)
  134. {
  135. data_t_2_DBT(w);
  136. DBT data_DBT;
  137. int status = btree_DB->get(btree_DB, &key_DBT, &data_DBT, 0);
  138. switch (status) {
  139. case RET_ERROR:
  140. throw(stringException("btree get failed"));
  141. break;
  142. case RET_SPECIAL:
  143. return false;
  144. case RET_SUCCESS:
  145. if ( data_DBT.size != sizeof(w.dt) )
  146. throw(stringException("btree get: tree corrupted"));
  147. memcpy((char*)&w.dt, data_DBT.data, data_DBT.size);
  148. return true;
  149. }
  150. return false;
  151. }
  152. ostream& btree::asciiOut(ostream& out)
  153. {
  154. return out;
  155. }
  156. istream& btree::asciiIn(istream& in)
  157. {
  158. return in;
  159. }