Download wb.cpp
  1. /**
  2.  * WB-Tree benchmark, test and demonstration program
  3.  * @author: AbiusX (me@abiusx.com)
  4.  * @version 1.0
  5.  *
  6.  * The original WB-Tree was developed by a few of MIT fellows, its an official GNU project now available at :
  7.  * https://savannah.gnu.org/cvs/?group=wb
  8.  * The developer's presonal page is :
  9.  * http://people.csail.mit.edu/jaffer/WB
  10.  *
  11.  *
  12.  * Keep in mind that this sample is based on a very recent version only available through CVS at the time of this writing (8 jun 2011)
  13.  * and the wb-2b1.zip and wb.zip file available at the author's page are not correctly functional.
  14.  *
  15.  *
  16.  * This was intentionally done for the Shahid Beheshti University Data Storage And Retrieval Final Course Project, hope it helps.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <iostream>
  22. #include <fstream>
  23. #include <sstream>
  24. #include <string>
  25. extern "C"
  26. {
  27. #include <wb/wbsys.h>
  28. }
  29. using namespace std;
  30. typedef unsigned char uchar;
  31.  
  32. inline void uchar_out(uchar* str, int len, ostream *stream = 0)
  33. {
  34. if (stream == 0)
  35. stream = &cout;
  36. char *t = new char[len + 1];
  37. int i;
  38. for (i = 0; i < len; ++i)
  39. t[i] = str[i];
  40. t[i] = 0;
  41. (*stream) << t << endl;
  42. delete[] t;
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. const int benchmark = 1000000;
  47. diagout = stderr;
  48. init_wb(75, 150, 4096);
  49. HAND* handle; //B-Tree handle
  50.  
  51. //Verbose mode detection
  52. bool verbose = false;
  53. if (argc > 1)
  54. if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--verbose") == 0)
  55. {
  56. clog << "Verbose mode enabled.";
  57. verbose = true;
  58. }
  59.  
  60. if (!verbose)
  61. clog
  62. << "You could also enable the verbose (detailed) mode by providing the application with -v or --verbose as command line arguments."
  63. << endl;
  64.  
  65. ifstream f("btree.db");
  66. //check to see if file exists and is read/writable
  67. if (!f.good())
  68. {
  69. f.close();
  70. clog << "attempting to create B-Tree database for the first time..."
  71. << endl;
  72. SEGD*db = make_seg((uchar*) "btree.db", 2048);
  73. if (db == false)
  74. clog << "Can not create database" << endl;
  75. else
  76. clog << "btree.db B-Tree database successfully created." << endl;
  77.  
  78. handle = create_db(db, 'T', (unsigned char*) "mytable");
  79. clog << "creating root B-Tree in database result : " << endl;
  80. close_seg(db, 0);
  81. }
  82.  
  83. //open existing database
  84. SEGD *db = open_seg((uchar*) "btree.db", true);
  85. if (db == false)
  86. {
  87. clog << "unable to open database!" << endl;
  88. exit(1);
  89. }
  90.  
  91. //open first B-Tree in database
  92. handle = open_db(db, (unsigned char*) "mytable");
  93. clog << "open root B-Tree succeeded. benchmarking " << benchmark
  94. << " nodes ..." << endl;
  95.  
  96. //insert some key/value pairs in database if not exists
  97. clog << "trying to write " << benchmark << " nodes ..." << endl;
  98. int count = 0;
  99. for (int i = 0; i < benchmark; ++i)
  100. {
  101. stringstream key;
  102. key << "k" << i;
  103. stringstream value;
  104. value << "v" << i;
  105.  
  106. int r = bt_write(handle, (uchar*) key.str().c_str(), key.str().size(),
  107. (uchar*) value.str().c_str(), value.str().size());
  108. if (r < 0)
  109. {
  110. if (verbose)
  111. clog << "--error writing " << i << " : " << r << endl;
  112. }
  113. else
  114. count++;
  115. }
  116. clog << "done writing " << count << " items." << endl;
  117.  
  118. //reading inserted/existing key/value pairs
  119. clog << "attempting to read " << benchmark << " nodes ..." << endl;
  120. count = 0;
  121. for (int i = 0; i < benchmark; ++i)
  122. {
  123. uchar value[256];
  124. stringstream key;
  125. key << "k" << i;
  126.  
  127. int r = bt_get(handle, (uchar*) key.str().c_str(), key.str().size(),
  128. value);
  129. if (r < 0)
  130. {
  131. if (verbose)
  132. clog << "--get error : " << r << endl;
  133. }
  134. else
  135. {
  136. count++;
  137. if (verbose)
  138. uchar_out(value, r, &clog);
  139. }
  140. }
  141. clog << "done reading " << count << " items." << endl;
  142.  
  143. //changing values for those keys
  144. clog << "attempting to change " << benchmark << " nodes..." << endl;
  145. count = 0;
  146. for (int i = 0; i < benchmark; ++i)
  147. {
  148. stringstream key;
  149. key << "k" << i;
  150. stringstream value;
  151. value << "v" << i << "changed";
  152. int r = bt_put(handle, (uchar*) key.str().c_str(), key.str().size(),
  153. (uchar*) value.str().c_str(), value.str().size());
  154. if (r < 0)
  155. {
  156. if (verbose)
  157. clog << "--put error : " << r << endl;
  158. }
  159. else
  160. count++;
  161.  
  162. }
  163. clog << "done putting " << count << " items." << endl;
  164.  
  165. //reading values again to see changes
  166. clog << "attempting to read " << benchmark << " nodes ..." << endl;
  167. count = 0;
  168. for (int i = 0; i < benchmark; ++i)
  169. {
  170. uchar value[256];
  171. stringstream key;
  172. key << "k" << i;
  173.  
  174. int r = bt_get(handle, (uchar*) key.str().c_str(), key.str().size(),
  175. value);
  176. if (r < 0)
  177. {
  178. if (verbose)
  179. clog << "get error : " << r << endl;
  180. }
  181. else
  182. {
  183. if (verbose)
  184. uchar_out(value, r, &clog);
  185. count++;
  186. }
  187. }
  188. clog << "done reading " << count << " items." << endl;
  189.  
  190. //close B-Tree
  191. bt_close(handle);
  192.  
  193. //close database, mandatory
  194. if (close_seg(db, 1) < 0)
  195. clog << "error closing database" << endl;
  196.  
  197. final_wb();
  198. clog << "all done. :)" << endl;
  199. return 0;
  200. }
  201.