#include "external_hashtable.h"
#include <iostream>
#include <filesystem>
ExternalHashTable::ExternalHashTable(const std::string& base_filename, size_t table_size)
: table_size(table_size) {
index_filename = base_filename + ".idx";
data_filename = base_filename + ".dat";
if (!open_files()) {
throw std::runtime_error("Failed to open hash table files");
}
// Initialize index file if it's new
if (!std::filesystem::exists(index_filename) ||
std::filesystem::file_size(index_filename) == 0) {
if (!initialize_index_file()) {
throw std::runtime_error("Failed to initialize index file");
}
}
}
ExternalHashTable::~ExternalHashTable() {
close_files();
}
bool ExternalHashTable::open_files() {
index_file.open(index_filename, std::ios::in | std::ios::out | std::ios::binary);
if (!index_file.is_open()) {
// Create the file if it doesn't exist
index_file.open(index_filename, std::ios::out | std::ios::binary);
if (!index_file.is_open()) return false;
index_file.close();
index_file.open(index_filename, std::ios::in | std::ios::out | std::ios::binary);
if (!index_file.is_open()) return false;
}
data_file.open(data_filename, std::ios::in | std::ios::out | std::ios::binary);
if (!data_file.is_open()) {
// Create the file if it doesn't exist
data_file.open(data_filename, std::ios::out | std::ios::binary);
if (!data_file.is_open()) return false;
data_file.close();
data_file.open(data_filename, std::ios::in | std::ios::out | std::ios::binary);
if (!data_file.is_open()) return false;
}
return true;
}
void ExternalHashTable::close_files() {
if (index_file.is_open()) {
index_file.close();
}
if (data_file.is_open()) {
data_file.close();
}
}
bool ExternalHashTable::initialize_index_file() {
IndexEntry empty_entry;
index_file.seekp(0);
for (size_t i = 0; i < table_size; ++i) {
index_file.write(reinterpret_cast<const char*>(&empty_entry), sizeof(IndexEntry));
if (index_file.fail()) return false;
}
index_file.flush();
return true;
}
size_t ExternalHashTable::hash(const std::string& key) const {
// Simple hash function (djb2)
size_t hash_value = 5381;
for (char c : key) {
hash_value = ((hash_value << 5) + hash_value) + static_cast<unsigned char>(c);
}
return hash_value % table_size;
}
int64_t ExternalHashTable::write_record(const std::string& key, const std::string& value, int64_t next_offset) {
data_file.seekp(0, std::ios::end);
int64_t record_offset = data_file.tellp();
DataRecord record;
record.next_record_offset = next_offset;
record.key_length = static_cast<uint32_t>(key.length());
record.value_length = static_cast<uint32_t>(value.length());
// Write record header
data_file.write(reinterpret_cast<const char*>(&record), sizeof(DataRecord));
if (data_file.fail()) return EMPTY_OFFSET;
// Write key
data_file.write(key.c_str(), key.length());
if (data_file.fail()) return EMPTY_OFFSET;
// Write value
data_file.write(value.c_str(), value.length());
if (data_file.fail()) return EMPTY_OFFSET;
data_file.flush();
return record_offset;
}
bool ExternalHashTable::read_record(int64_t offset, std::string& key, std::string& value, int64_t& next_offset) const {
if (offset == EMPTY_OFFSET) return false;
data_file.seekg(offset);
DataRecord record;
data_file.read(reinterpret_cast<char*>(&record), sizeof(DataRecord));
if (data_file.fail()) return false;
next_offset = record.next_record_offset;
// Read key
key.resize(record.key_length);
data_file.read(&key[0], record.key_length);
if (data_file.fail()) return false;
// Read value
value.resize(record.value_length);
data_file.read(&value[0], record.value_length);
if (data_file.fail()) return false;
return true;
}
bool ExternalHashTable::update_index_entry(size_t hash_index, int64_t record_offset) {
index_file.seekp(hash_index * sizeof(IndexEntry));
IndexEntry entry;
entry.first_record_offset = record_offset;
index_file.write(reinterpret_cast<const char*>(&entry), sizeof(IndexEntry));
if (index_file.fail()) return false;
index_file.flush();
return true;
}
ExternalHashTable::IndexEntry ExternalHashTable::read_index_entry(size_t hash_index) const {
IndexEntry entry;
index_file.seekg(hash_index * sizeof(IndexEntry));
index_file.read(reinterpret_cast<char*>(&entry), sizeof(IndexEntry));
return entry;
}
bool ExternalHashTable::put(const std::string& key, const std::string& value) {
size_t hash_index = hash(key);
IndexEntry index_entry = read_index_entry(hash_index);
// Check if key already exists and update if found
int64_t current_offset = index_entry.first_record_offset;
int64_t prev_offset = EMPTY_OFFSET;
while (current_offset != EMPTY_OFFSET) {
std::string existing_key, existing_value;
int64_t next_offset;
if (!read_record(current_offset, existing_key, existing_value, next_offset)) {
return false;
}
if (existing_key == key) {
// Key exists, we need to replace the record
// For simplicity, we'll add a new record and mark the old one as deleted
// In a production system, you'd want more sophisticated space management
int64_t new_record_offset = write_record(key, value, next_offset);
if (new_record_offset == EMPTY_OFFSET) return false;
if (prev_offset == EMPTY_OFFSET) {
// Update index entry
return update_index_entry(hash_index, new_record_offset);
} else {
// Update previous record's next pointer
// This is complex and requires rewriting the previous record
// For simplicity, we'll just add to the chain
break;
}
}
prev_offset = current_offset;
current_offset = next_offset;
}
// Key doesn't exist, add new record
int64_t new_record_offset = write_record(key, value, index_entry.first_record_offset);
if (new_record_offset == EMPTY_OFFSET) return false;
return update_index_entry(hash_index, new_record_offset);
}
bool ExternalHashTable::get(const std::string& key, std::string& value) const {
size_t hash_index = hash(key);
IndexEntry index_entry = read_index_entry(hash_index);
int64_t current_offset = index_entry.first_record_offset;
while (current_offset != EMPTY_OFFSET) {
std::string stored_key, stored_value;
int64_t next_offset;
if (!read_record(current_offset, stored_key, stored_value, next_offset)) {
return false;
}
if (stored_key == key) {
value = stored_value;
return true;
}
current_offset = next_offset;
}
return false; // Key not found
}
bool ExternalHashTable::contains(const std::string& key) const {
std::string dummy;
return get(key, dummy);
}
bool ExternalHashTable::remove(const std::string& key) {
// Implementation of remove is complex as it requires updating chains
// For a complete implementation, you'd need to handle:
// 1. Find the record to delete
// 2. Update the previous record's next pointer (or index entry if it's the first)
// 3. Mark space as available for reuse
// Simplified version - mark as deleted by updating with empty value
// In production, you'd want proper space management
return put(key, ""); // Mark as deleted with empty value
}
void ExternalHashTable::clear() {
close_files();
// Remove files
std::filesystem::remove(index_filename);
std::filesystem::remove(data_filename);
// Recreate files
if (!open_files()) {
throw std::runtime_error("Failed to recreate hash table files");
}
if (!initialize_index_file()) {
throw std::runtime_error("Failed to initialize cleared index file");
}
}