41# ifndef TPL_FILE_BPLUS_MAP_H
42# define TPL_FILE_BPLUS_MAP_H
51# include <type_traits>
94 template <
typename Key,
108 std::array<
unsigned char,
109 key_codec::encoded_size + value_codec::encoded_size>
bytes = {};
113 return out <<
"<record>";
118 return key_codec::decode(
bytes.data());
123 return value_codec::decode(
bytes.data() + key_codec::encoded_size);
131 key_codec::storage_id),
132 value_codec::storage_id);
134 key_codec::encoded_size + value_codec::encoded_size;
138 std::memcpy(
out, record.bytes.data(), record.bytes.size());
144 std::memcpy(record.
bytes.data(),
in, record.
bytes.size());
149 const std::uint32_t
crc,
150 const Record & record)
noexcept
153 record.bytes.size());
163 return cmp(
lhs.get_key(),
rhs.get_key());
171 "File_BPlus_Map requires a fixed-size key codec");
173 "File_BPlus_Map requires a fixed-size mapped-value codec");
174 static_assert(std::is_trivially_copyable_v<Record>,
175 "File_BPlus_Map requires trivially copyable records");
182 key_codec::encode(key, record.
bytes.data());
190 key_codec::encode(key, record.
bytes.data());
191 value_codec::encode(value, record.
bytes.data() + key_codec::encoded_size);
195 [[
nodiscard]]
static std::pair<Key, Value>
206 for (
size_t i = 0; i <
records.size(); ++i)
321 const Compare &
cmp = Compare())
335 const Compare &
cmp = Compare())
347 const Compare &
cmp = Compare())
361 const Compare &
cmp = Compare())
535 if (
not record.has_value())
537 return record->get_value();
548 const auto value =
find(key);
550 <<
"File_BPlus_Map::at(): key not found";
592 <<
"File_BPlus_Map::insert_or_assign(): internal replace failed";
594 <<
"File_BPlus_Map::insert_or_assign(): internal reinsert failed";
615 if (
not record.has_value())
627 if (
not record.has_value())
640 if (
not record.has_value())
653 if (
not record.has_value())
679 for (
size_t i = 0; i <
records.size(); ++i)
693 for (
size_t i = 0; i <
records.size(); ++i)
738 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.
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
Lazy iterator over ordered key/value pairs.
value_type get_curr() const
Return the current key/value pair.
void next()
Synonym for next_ne().
Value get_value() const
Return the current mapped value.
bool has_curr() const noexcept
Return whether the iterator still points to an item.
Iterator() noexcept=default
Construct an exhausted iterator.
void next_ne()
Advance to the next item.
Key get_key() const
Return the current key.
Persistent ordered map backed by a paged File_BPlus_Tree.
static Record make_record(const Key &key, const Value &value)
size_t page_size_bytes() const noexcept
Return the serialized page size of the backing tree.
Gen_File_BPlus_Tree< Record, Record_Compare, MinDegree, Record_Codec > tree_impl_type
Array< Value > values() const
Materialize all mapped values in key order.
Gen_File_BPlus_Map(Gen_File_BPlus_Map &&)=delete
Moving is disabled.
Iterator get_range_it(const Key &first, const Key &last) const
Return a lazy iterator over an inclusive key range.
static constexpr auto Read_Only
const Compare & key_comp() const noexcept
Return the key comparator.
bool remove(const Key &key)
Remove a key and its mapped value, if present.
std::optional< Record > find_record(const Key &key) const
Gen_File_BPlus_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.
std::optional< value_type > lower_bound(const Key &key) const
Return the first key/value pair whose key is not less than key.
std::optional< value_type > min_item() const
Return the smallest stored key/value pair.
Array< value_type > range(const Key &first, const Key &last) const
Collect all key/value pairs in the inclusive key range.
size_t size() const noexcept
Return the number of stored key/value pairs.
open_mode_type open_mode() const noexcept
Return the open mode.
void reload()
Discard unsynchronized changes and reload the file.
Array< Key > keys() const
Materialize all keys in sorted order.
Iterator get_it() const noexcept
Return a lazy iterator over all items in key order.
size_t page_count() const noexcept
Return the number of allocated pages.
Gen_File_BPlus_Map & operator=(const Gen_File_BPlus_Map &)=delete
Assignment is disabled.
std::optional< Value > find(const Key &key) const
Return the mapped value associated with a key, if any.
Gen_File_BPlus_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.
void sync() const
Flush pending changes to the backing file.
Value at(const Key &key) const
Return the mapped value associated with a key.
bool search(const Key &key) const
Synonym for contains().
bool contains(const Key &key) const
Return whether a key exists.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
void clear()
Remove every key/value pair from the map.
bool empty() const noexcept
Return whether the map is empty.
size_t height() const noexcept
Return the current tree height.
bool is_read_only() const noexcept
Return whether the map handle is read-only.
typename tree_type::open_mode_type open_mode_type
static Array< std::pair< Key, Value > > to_pairs(const Array< Record > &records)
std::optional< value_type > max_item() const
Return the largest stored key/value pair.
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic synchronization.
std::uint64_t checkpoint_sequence() const noexcept
Return the latest durable checkpoint sequence.
bool verify() const
Verify the structural invariants of the backing tree.
Gen_File_BPlus_Map(const Gen_File_BPlus_Map &)=delete
Copying is disabled.
bool equals_key(const Key &lhs, const Key &rhs) const
bool insert_or_assign(const Key &key, const Value &value)
Insert or overwrite a key/value pair.
Gen_File_BPlus_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.
const std::string & file_path() const noexcept
Return the file path.
bool insert(const Key &key, const Value &value)
Insert a new key/value pair if the key is not present.
Gen_File_BPlus_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.
void checkpoint() const
Synonym for sync().
Array< value_type > items() const
Materialize all key/value pairs in sorted order.
static Record make_probe(const Key &key)
static std::pair< Key, Value > to_pair(const Record &record)
static constexpr auto Read_Write
std::pair< Key, Value > value_type
std::optional< value_type > upper_bound(const Key &key) const
Return the first key/value pair whose key is greater than key.
Lazy iterator over ordered keys stored in leaf pages.
void next_ne()
Advance to the next key.
bool has_curr() const noexcept
Return whether the iterator still points to a key.
const Key & get_curr() const
Return the current key.
Persistent page-managed B+ Tree stored in a single binary file.
const Compare & key_comp() const noexcept
Return the comparison object.
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.
bool empty() const noexcept
Return whether the tree has no keys.
const std::string & file_path() const noexcept
Return the backing file path.
Iterator get_it() const noexcept
Return a lazy iterator over the full key order.
Paged_File_Open_Mode open_mode_type
File opening mode type.
Paged_File_Open_Mode open_mode() const noexcept
Return the access mode used to open the file.
std::optional< Key > max_key() const
Return the maximum key, if any.
std::uint64_t checkpoint_sequence() const noexcept
Return the durable checkpoint sequence stored in the backing file.
void sync() const
Flush the current tree image using redo WAL or full-image fallback.
bool auto_sync_enabled() const noexcept
Return whether automatic synchronization is enabled.
bool verify() const
Verify structural B+ Tree invariants across cached pages.
bool remove(const Key &key)
Remove a key if present.
static constexpr auto Read_Only
void reload()
Discard unsynchronized changes and reload pages from disk.
void clear()
Remove every key from the tree.
std::optional< Key > min_key() const
Return the minimum key, if any.
size_t size() const noexcept
Return the number of stored keys.
Array< Key > range(const Key &first, const Key &last) const
Collect all keys in the inclusive range [first, last].
void set_auto_sync(const bool enabled) noexcept
Enable or disable automatic flushing of dirty pages.
static constexpr auto Read_Write
size_t page_count() const noexcept
Return the number of allocated pages currently tracked.
void checkpoint() const
Synonym for sync().
Array< Key > keys() const
Materialize the tree contents in sorted order.
Iterator get_range_it(const Key &first, const Key &last) const
Return a lazy iterator over an inclusive key range.
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
size_t page_size_bytes() const noexcept
Return the fixed serialized page size.
size_t height() const noexcept
Return the current height of the paged tree.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
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 std::uint32_t add_to_crc(const std::uint32_t crc, const Record &record) noexcept
static constexpr size_t encoded_size
static void encode(const Record &record, unsigned char *out) noexcept
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.