00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "stx-cbtreedb.h"
00024
00025 #include <iostream>
00026 #include <fstream>
00027 #include <sstream>
00028 #include <assert.h>
00029 #include <math.h>
00030
00031 int main()
00032 {
00033
00034 typedef stx::CBTreeDB< uint32_t, std::less<uint32_t> > cbtreedb;
00035
00036 const unsigned int items = 64;
00037
00038 #if 1
00039
00040 {
00041
00042 cbtreedb::Writer writer;
00043
00044
00045 for(unsigned int i = 0; i < items; ++i)
00046 {
00047 std::ostringstream oss;
00048 oss << "value " << i;
00049
00050 writer.Add(i*i, oss.str());
00051 }
00052
00053
00054 std::ofstream testdb("example1.db");
00055 writer.Write(testdb);
00056 }
00057 #else
00058
00059 {
00060
00061 cbtreedb::WriterSequential writer;
00062
00063
00064 for(unsigned int i = 0; i < items; ++i)
00065 {
00066 writer.Add(i*i, strlen("value ") + (i > 0 ? (log10(i) + 1) : 1));
00067 }
00068
00069
00070 std::ofstream testdb("example1.db");
00071 writer.WriteHeader(testdb);
00072
00073
00074 for(unsigned int i = 0; i < items; ++i)
00075 {
00076 std::ostringstream oss;
00077 oss << "value " << i;
00078
00079 writer.WriteValue(i*i, oss.str());
00080 }
00081
00082
00083 writer.WriteFinalize();
00084 }
00085 #endif
00086
00087
00088 {
00089 cbtreedb::Reader reader;
00090
00091
00092 cbtreedb::PageCache cache(128);
00093 reader.SetPageCache(&cache);
00094
00095
00096 std::ifstream testdb("example1.db");
00097 std::string errorstring;
00098 if (!reader.Open(testdb, &errorstring))
00099 {
00100 std::cout << "Error loading database: " << errorstring << std::endl;
00101 return -1;
00102 }
00103
00104
00105 assert( reader.Verify() );
00106
00107
00108 std::cout << "Full listing:" << std::endl;
00109 for(unsigned int i = 0; i < reader.Size(); ++i)
00110 {
00111 uint32_t key;
00112 std::string value;
00113
00114 reader.GetIndex(i, key, value);
00115
00116 std::cout << "key " << key << " -> " << value << ", ";
00117 }
00118 std::cout << std::endl;
00119
00120
00121 std::cout << "sqrt(2500) -> " << reader[2500] << std::endl;
00122 std::cout << "sqrt(2501) -> " << reader[2501] << std::endl;
00123 std::cout << "sqrt(2601) -> " << reader[2601] << std::endl;
00124
00125
00126 std::cout << "isSquare(2704) -> " << reader.Exists(2704) << std::endl;
00127 std::cout << "isSquare(2705) -> " << reader.Exists(2705) << std::endl;
00128
00129
00130 std::string out;
00131
00132 if (reader.Lookup(2808, out))
00133 std::cout << "Lookup 2808 -> " << out << " (was found.)" << std::endl;
00134 else
00135 std::cout << "Lookup 2808 has failed." << std::endl;
00136
00137 if (reader.Lookup(2809, out))
00138 std::cout << "Lookup 2809 -> " << out << " (was found.)" << std::endl;
00139 else
00140 std::cout << "Lookup 2809 has failed." << std::endl;
00141 }
00142
00143 return 0;
00144 }