2025-04-20 18:24:28 +03:00

44 lines
1.2 KiB
C++

//
// Created by Kirill Zhukov on 20.04.2025.
//
#include "Wal.h"
namespace usub::utils
{
WAL::WAL(const std::string& filename)
{
this->out.open(filename, std::ios::binary | std::ios::app);
if (!this->out.is_open()) throw std::runtime_error("Failed to open WAL");
}
void WAL::write_put(const std::string& key, const std::string& value)
{
uint8_t op = 0;
uint32_t key_len = key.size();
uint32_t value_len = value.size();
this->out.write(reinterpret_cast<char*>(&op), sizeof(op));
this->out.write(reinterpret_cast<char*>(&key_len), sizeof(key_len));
this->out.write(key.data(), key_len);
this->out.write(reinterpret_cast<char*>(&value_len), sizeof(value_len));
this->out.write(value.data(), value_len);
this->out.flush();
}
void WAL::write_delete(const std::string& key)
{
uint8_t op = 1;
uint32_t key_len = key.size();
this->out.write(reinterpret_cast<char*>(&op), sizeof(op));
this->out.write(reinterpret_cast<char*>(&key_len), sizeof(key_len));
this->out.write(key.data(), key_len);
this->out.flush();
}
void WAL::close()
{
this->out.close();
}
} // utils
// usub