9#include <gtest/gtest.h>
26 namespace fs = std::filesystem;
28 fs::path make_temp_path()
30 static std::atomic<unsigned long long>
counter{0};
33 static std::mt19937_64
rng{[] {
34 std::random_device rd;
36 static_cast<unsigned long long>
37 (std::chrono::steady_clock::now().time_since_epoch().count());
38 return std::mt19937_64{now_seed ^
39 (
static_cast<unsigned long long>(rd()) << 1u)};
42 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
43 std::uniform_int_distribution<unsigned long long> dist;
44 const auto random_value = dist(
rng);
46 const auto id = std::to_string(now) +
"_" +
47 std::to_string(random_value) +
"_" +
50 const fs::path dir = fs::temp_directory_path() /
"aleph_file_bmap_tests";
51 fs::create_directories(dir);
52 return dir / (
id +
".idx");
59 explicit TempFile(fs::path p) : path(
std::move(p)) {}
65 fs::remove(path.string() +
".wal", ec);
66 fs::remove(path.string() +
".wal.tmp", ec);
67 fs::remove(path.string() +
".journal", ec);
68 fs::remove(path.string() +
".journal.tmp", ec);
69 fs::remove(path.string() +
".lock", ec);
78 for (
size_t i = 0; i < arr.
size(); ++i)
79 out.push_back(arr[i]);
84TEST(FileBMap, InsertFindAssignAndReopen)
86 TempFile tmp(make_temp_path());
90 EXPECT_TRUE(map.
insert(10, 100));
91 EXPECT_TRUE(map.
insert(20, 200));
92 EXPECT_FALSE(map.
insert(10, 999));
93 ASSERT_TRUE(map.
find(10).has_value());
94 EXPECT_EQ(*map.
find(10), 100);
96 EXPECT_EQ(map.
at(10), 150);
105 ASSERT_TRUE(reopened.
find(20).has_value());
106 EXPECT_EQ(*reopened.
find(20), 200);
108 (std::vector<std::pair<int, long>>{{10, 150}, {20, 200}}));
111TEST(FileBMap, BoundsItemsAndRemove)
113 TempFile tmp(make_temp_path());
116 EXPECT_TRUE(map.
insert(30, 300));
117 EXPECT_TRUE(map.
insert(10, 100));
118 EXPECT_TRUE(map.
insert(50, 500));
119 EXPECT_TRUE(map.
insert(40, 400));
121 EXPECT_EQ(map.
min_item(), (std::optional<std::pair<int, int>>{{10, 100}}));
122 EXPECT_EQ(map.max_item(), (std::optional<std::pair<int, int>>{{50, 500}}));
123 EXPECT_EQ(map.lower_bound(35), (std::optional<std::pair<int, int>>{{40, 400}}));
124 EXPECT_EQ(map.upper_bound(40), (std::optional<std::pair<int, int>>{{50, 500}}));
125 EXPECT_EQ(
to_vector(map.keys()), (std::vector<int>{10, 30, 40, 50}));
126 EXPECT_EQ(
to_vector(map.values()), (std::vector<int>{100, 300, 400, 500}));
128 EXPECT_TRUE(map.remove(30));
129 EXPECT_FALSE(map.contains(30));
131 (std::vector<std::pair<int, int>>{{10, 100}, {40, 400}, {50, 500}}));
132 EXPECT_TRUE(map.verify());
135TEST(FileBMap, SupportsBoundedStringCodecs)
137 TempFile tmp(make_temp_path());
143 key_codec, value_codec> map(tmp.path.string(),
false);
144 EXPECT_TRUE(map.
insert(
"apple",
"green"));
145 EXPECT_TRUE(map.
insert(
"banana",
"yellow"));
146 EXPECT_TRUE(map.
insert(
"kiwi",
"brown fuzz"));
148 EXPECT_EQ(map.
at(
"banana"),
"ripe yellow");
150 (std::optional<std::pair<std::string, std::string>>{
151 {
"banana",
"ripe yellow"}}));
152 EXPECT_TRUE(map.verify());
157 key_codec, value_codec> reopened(
161 EXPECT_TRUE(reopened.verify());
163 (std::vector<std::pair<std::string, std::string>>{
165 {
"banana",
"ripe yellow"},
166 {
"kiwi",
"brown fuzz"}}));
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Persistent ordered map backed by a paged File_B_Tree.
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.
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.
std::optional< Value > find(const Key &key) const
Return the mapped value associated with a key, if any.
void sync() const
Flush pending changes to the backing file.
bool insert(const Key &key, const Value &value)
Insert a new key/value pair if the key is not present.
Array< value_type > items() const
Materialize all key/value pairs in sorted order.
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.
Main namespace for Aleph-w library functions.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
Persistent key/value map built on top of Aleph::File_B_Tree.