#pragma once
#include <whiteout/common_types.h>
#include <whiteout/interfaces.h>
#include <array>
#include <memory>
#include <span>
#include <string>
#include <utility>
#include <vector>
#include "flat_hash_map.h"
namespace whiteout::storages::casc {
struct IndexEntry {
std::array<u8, 16> eKey{}; u32 archiveIndex = 0; u32 archiveOffset = 0; u32 encodedSize = 0; bool directBLTE = false; };
class IndexTable {
public:
IndexTable();
~IndexTable();
IndexTable(IndexTable&&) noexcept;
IndexTable& operator=(IndexTable&&) noexcept;
IndexTable(const IndexTable&) = delete;
IndexTable& operator=(const IndexTable&) = delete;
static IndexTable load(const std::string& dataDir, interfaces::WorkerPool* pool = nullptr);
static IndexTable loadLazyBuckets(const std::string& dataDir,
interfaces::WorkerPool* pool = nullptr);
void ensureAllBucketsLoaded() const;
bool isValid() const;
void loadArchiveIndices(const std::string& dataDir,
const std::vector<std::array<u8, 16>>& archiveEKeys,
interfaces::WorkerPool* pool = nullptr);
void loadArchiveIndicesLazy(const std::string& dataDir,
const std::vector<std::array<u8, 16>>& archiveEKeys,
interfaces::WorkerPool* pool = nullptr);
void ensureAllArchivesLoaded() const;
const IndexEntry* find(std::span<const u8> eKeyPrefix) const;
void insert(const IndexEntry& entry);
std::vector<std::pair<std::string, std::vector<u8>>> serialize() const;
size_t entryCount() const;
private:
static u64 eKeyHash(std::span<const u8> eKey);
void loadArchive(u32 archiveIdx) const;
void loadBucket(u8 bucket) const;
FlatHashMap<IndexEntry> m_entries;
struct LazyArchives;
mutable std::unique_ptr<LazyArchives> m_lazyArchives;
struct LazyBuckets;
mutable std::unique_ptr<LazyBuckets> m_lazyBuckets;
};
}