#include "stx-cbtreedb.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <assert.h>
#include <math.h>
int main()
{
typedef stx::CBTreeDB< uint32_t, std::less<uint32_t> > cbtreedb;
const unsigned int items = 64;
#if 1
{
cbtreedb::Writer writer;
for(unsigned int i = 0; i < items; ++i)
{
std::ostringstream oss;
oss << "value " << i;
writer.Add(i*i, oss.str());
}
std::ofstream testdb("example1.db");
writer.Write(testdb);
}
#else
{
cbtreedb::WriterSequential writer;
for(unsigned int i = 0; i < items; ++i)
{
writer.Add(i*i, strlen("value ") + (i > 0 ? (log10(i) + 1) : 1));
}
std::ofstream testdb("example1.db");
writer.WriteHeader(testdb);
for(unsigned int i = 0; i < items; ++i)
{
std::ostringstream oss;
oss << "value " << i;
writer.WriteValue(i*i, oss.str());
}
writer.WriteFinalize();
}
#endif
{
cbtreedb::Reader reader;
cbtreedb::PageCache cache(128);
reader.SetPageCache(&cache);
std::ifstream testdb("example1.db");
std::string errorstring;
if (!reader.Open(testdb, &errorstring))
{
std::cout << "Error loading database: " << errorstring << std::endl;
return -1;
}
assert( reader.Verify() );
std::cout << "Full listing:" << std::endl;
for(unsigned int i = 0; i < reader.Size(); ++i)
{
uint32_t key;
std::string value;
reader.GetIndex(i, key, value);
std::cout << "key " << key << " -> " << value << ", ";
}
std::cout << std::endl;
std::cout << "sqrt(2500) -> " << reader[2500] << std::endl;
std::cout << "sqrt(2501) -> " << reader[2501] << std::endl;
std::cout << "sqrt(2601) -> " << reader[2601] << std::endl;
std::cout << "isSquare(2704) -> " << reader.Exists(2704) << std::endl;
std::cout << "isSquare(2705) -> " << reader.Exists(2705) << std::endl;
std::string out;
if (reader.Lookup(2808, out))
std::cout << "Lookup 2808 -> " << out << " (was found.)" << std::endl;
else
std::cout << "Lookup 2808 has failed." << std::endl;
if (reader.Lookup(2809, out))
std::cout << "Lookup 2809 -> " << out << " (was found.)" << std::endl;
else
std::cout << "Lookup 2809 has failed." << std::endl;
}
return 0;
}