40# ifndef TPL_FILE_B_MAP_H
41# define TPL_FILE_B_MAP_H
51# include <type_traits>
89 template <
typename Key,
103 std::array<
unsigned char,
104 key_codec::encoded_size + value_codec::encoded_size>
bytes = {};
108 return out <<
"<record>";
113 return key_codec::decode(
bytes.data());
118 return value_codec::decode(
bytes.data() + key_codec::encoded_size);
126 key_codec::storage_id),
127 value_codec::storage_id);
129 key_codec::encoded_size + value_codec::encoded_size;
133 std::memcpy(
out, record.bytes.data(), record.bytes.size());
139 std::memcpy(record.
bytes.data(),
in, record.
bytes.size());
144 const std::uint32_t
crc,
145 const Record & record)
noexcept
148 record.bytes.size());
158 return cmp(
lhs.get_key(),
rhs.get_key());
166 "File_B_Map requires a fixed-size key codec");
168 "File_B_Map requires a fixed-size mapped-value codec");
169 static_assert(std::is_trivially_copyable_v<Record>,
170 "File_B_Map requires trivially copyable records");
177 key_codec::encode(key, record.
bytes.data());
185 key_codec::encode(key, record.
bytes.data());
186 value_codec::encode(value, record.
bytes.data() + key_codec::encoded_size);
190 [[
nodiscard]]
static std::pair<Key, Value>
248 const Compare &
cmp = Compare())
263 const Compare &
cmp = Compare())
278 const Compare &
cmp = Compare())
298 const Compare &
cmp = Compare())
496 if (
not record.has_value())
498 return record->get_value();
510 const auto value =
find(key);
512 <<
"File_B_Map::at(): key not found";
564 <<
"File_B_Map::insert_or_assign(): existing record not found";
566 <<
"File_B_Map::insert_or_assign(): internal replace failed";
570 <<
"File_B_Map::insert_or_assign(): internal reinsert failed";
574 auto ep = std::current_exception();
576 std::rethrow_exception(
ep);
610 if (
not record.has_value())
623 if (
not record.has_value())
637 if (
not record.has_value())
651 if (
not record.has_value())
667 out.append(
rec.get_key());
682 out.append(
rec.get_value());
709 template <
typename Key,
C++20 concepts for constraining comparison functors.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
Persistent ordered map backed by a paged File_B_Tree.
std::pair< Key, Value > value_type
static const char * validated_path(const char *p)
static constexpr auto Read_Only
const Compare & key_comp() const noexcept
Return the key comparator.
static std::pair< Key, Value > to_pair(const Record &record)
static constexpr auto Read_Write
std::uint64_t checkpoint_sequence() const noexcept
Return the latest durable checkpoint sequence.
std::optional< value_type > max_item() const
Return the largest stored key/value pair.
size_t size() const noexcept
Return the number of stored key/value pairs.
bool equals_key(const Key &lhs, const Key &rhs) const
Value at(const Key &key) const
Return the mapped value associated with a key.
bool verify() const
Verify the structural invariants of the backing tree.
size_t height() const noexcept
Return the current tree height.
Gen_File_B_Map(Gen_File_B_Map &&)=delete
Moving is disabled.
std::optional< value_type > lower_bound(const Key &key) const
Return the first key/value pair whose key is not less than key.
bool is_read_only() const noexcept
Return whether the map handle is read-only.
bool search(const Key &key) const
Synonym for contains().
size_t page_count() const noexcept
Return the number of allocated pages.
bool empty() const noexcept
Return whether the map is empty.
void checkpoint() const
Synonym for sync().
std::optional< value_type > upper_bound(const Key &key) const
Return the first key/value pair whose key is greater than key.
std::optional< Record > find_record(const Key &key) const
void clear()
Remove every key/value pair from the map.
Gen_File_B_Tree< Record, Record_Compare, MinDegree, Record_Codec > tree_impl_type
Gen_File_B_Map(const char *file_path, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a persistent B-Tree map for read-write access.
Array< Value > values() const
Materialize all mapped values in key order.
bool remove(const Key &key)
Remove a key and its mapped value, if present.
std::optional< Value > find(const Key &key) const
Return the mapped value associated with a key, if any.
Gen_File_B_Map(const Gen_File_B_Map &)=delete
Copying is disabled.
static Array< std::pair< Key, Value > > to_pairs(const Array< Record > &records)
void sync() const
Flush pending changes to the backing file.
bool contains(const Key &key) const
Return whether a key exists.
size_t page_size_bytes() const noexcept
Return the serialized page size of the backing tree.
open_mode_type open_mode() const noexcept
Return the open mode.
bool insert(const Key &key, const Value &value)
Insert a new key/value pair if the key is not present.
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic synchronization.
static Record make_probe(const Key &key)
Gen_File_B_Map(std::string file_path, const open_mode_type open_mode, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a persistent B-Tree map with an explicit mode.
typename tree_type::open_mode_type open_mode_type
Array< value_type > items() const
Materialize all key/value pairs in sorted order.
Gen_File_B_Map(const char *file_path, const open_mode_type open_mode, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a persistent B-Tree map with an explicit mode.
static Record make_record(const Key &key, const Value &value)
Gen_File_B_Map(std::string file_path, const bool auto_sync=true, const Compare &cmp=Compare())
Open or create a persistent B-Tree map for read-write access.
const std::string & file_path() const noexcept
Return the file path.
void reload()
Discard unsynchronized changes and reload the file.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
std::optional< value_type > min_item() const
Return the smallest stored key/value pair.
bool insert_or_assign(const Key &key, const Value &value)
Insert or overwrite a key/value pair.
Gen_File_B_Map & operator=(const Gen_File_B_Map &)=delete
Assignment is disabled.
Array< Key > keys() const
Materialize all keys in sorted order.
Persistent page-managed B-Tree stored in a single binary file.
size_t page_count() const noexcept
Return the number of allocated pages currently tracked.
bool verify() const
Verify structural B-Tree invariants across cached pages.
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic flushing of dirty pages.
std::optional< Key > max_key() const
Return the maximum key, if any.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
void clear()
Remove every key from the tree.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
size_t height() const noexcept
Return the current height of the paged tree.
Array< Key > keys() const
Materialize the tree contents in sorted order.
static constexpr auto Read_Only
std::optional< Key > min_key() const
Return the minimum key, if any.
size_t page_size_bytes() const noexcept
Return the fixed serialized page size.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
void checkpoint() const
Synonym for sync().
size_t size() const noexcept
Return the number of stored keys.
bool empty() const noexcept
Return whether the tree has no keys.
const Compare & key_comp() const noexcept
Return the comparison object.
Paged_File_Open_Mode open_mode() const noexcept
Return the access mode used to open the file.
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
bool remove(const Key &key)
Remove a key if present.
Paged_File_Open_Mode open_mode_type
File opening mode type.
void reload()
Discard unsynchronized changes and reload pages from disk.
const std::string & file_path() const noexcept
Return the backing file path.
std::uint64_t checkpoint_sequence() const noexcept
Return the durable checkpoint sequence stored in the backing file.
static constexpr auto Read_Write
bool is_read_only() const noexcept
Return whether this handle is read-only.
bool insert(const Key &key)
Insert a key if it is not already present.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
std::uint32_t crc32_add_bytes(std::uint32_t crc, const void *data, const size_t size) noexcept
constexpr std::uint32_t mix_codec_id(const std::uint32_t seed, const std::uint32_t value) noexcept
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
static Record decode(const unsigned char *in) noexcept
static constexpr std::uint32_t storage_id
static void encode(const Record &record, unsigned char *out) noexcept
static std::uint32_t add_to_crc(const std::uint32_t crc, const Record &record) noexcept
static constexpr size_t encoded_size
bool operator()(const Record &lhs, const Record &rhs) const
std::array< unsigned char, key_codec::encoded_size+value_codec::encoded_size > bytes
friend std::ostream & operator<<(std::ostream &out, const Record &)
Dynamic array container with automatic resizing.
Page-managed persistent B-Tree with file-backed storage.