SharedStorage/main.cpp
2025-04-21 19:28:00 +00:00

112 lines
3.2 KiB
C++

#include <iostream>
#include "utils/toml/toml.hpp"
#include <iostream>
#include "utils/hash/xxhash/xxhash.h"
#include "utils/datastructures/LFSkipList.h"
#include "utils/io/SSTableIO.h"
using namespace usub::utils;
#if 0
#include "utils/io/VersionManager.h"
void test_skiplist_basic()
{
std::string db_name = "test_db";
VersionManager version_manager(db_name);
LFSkipList<Hash128, std::string> table(version_manager);
table.insert(compute_hash128("a", 1), "apple");
table.insert(compute_hash128("b", 1), "banana");
table.insert(compute_hash128("c", 1), "cherry");
auto a = table.find(compute_hash128("a", 1));
auto b = table.find(compute_hash128("b", 1));
auto c = table.find(compute_hash128("c", 1));
auto d = table.find(compute_hash128("d", 1));
assert(a.has_value() && a.value() == "apple");
assert(b.has_value() && b.value() == "banana");
assert(c.has_value() && c.value() == "cherry");
assert(!d.has_value());
std::cout << "SkipList basic test passed.\n";
}
void simulate_restart_and_check_version()
{
std::string db_name = "test_db";
{
VersionManager vm1(db_name);
uint64_t v1 = vm1.next_version();
uint64_t v2 = vm1.next_version();
uint64_t v3 = vm1.next_version();
(void)v1;
(void)v2;
(void)v3;
}
VersionManager vm2(db_name);
uint64_t v4 = vm2.next_version();
assert(v4 > 3 && "Version did not persist correctly");
std::cout << "Version persistence test passed.\n";
}
void test_sstable_write_read()
{
std::string db_name = "test_db";
VersionManager version_manager(db_name);
LFSkipList<Hash128, std::string> table(version_manager);
table.insert(compute_hash128("dog", 3), "bark");
table.insert(compute_hash128("cat", 3), "meow");
table.insert(compute_hash128("cow", 3), "moo");
write_sstable_with_index(table, "test_sstable.dat");
LFSkipList<Hash128, std::string> loaded_table(version_manager);
read_sstable_with_mmap(loaded_table, "test_sstable.dat");
assert(loaded_table.find(compute_hash128("dog", 3)).has_value());
assert(loaded_table.find(compute_hash128("cat", 3)).has_value());
assert(loaded_table.find(compute_hash128("cow", 3)).has_value());
std::cout << "SSTable write/read test passed.\n";
}
#endif
#include "core/DatabaseManager.h"
int main()
{
#if 0
test_skiplist_basic();
simulate_restart_and_check_version();
test_sstable_write_read();
LockFreeRingBuffer<int> q;
q.push(1);
std::cout << q.pop().value() << std::endl;
#endif
try
{
using namespace usub::core;
usub::core::DatabaseManager manager("../config.toml");
manager.run_all();
// std::cout << sizeof(usub::core::SharedCommandQueue) << ", " << SharedCommandQueue::calculate_shm_size(1024) << "\n";
// usub::core::SharedMemoryManager shm_manager{"rates_1", SharedCommandQueue::calculate_shm_size(1024)};
// new(shm_manager.base_ptr()) SharedCommandQueue(1024);
// auto* queue = static_cast<usub::core::SharedCommandQueue*>(shm_manager.base_ptr());
// std::cout << queue->head_.load() << '\n';
}
catch (const std::exception& ex)
{
std::cerr << "Fatal error: " << ex.what() << std::endl;
return 1;
}
return 0;
}