Skip to content

Commit

Permalink
Save B and BL in vector pools
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Jun 27, 2024
1 parent 1ddeaec commit bce6f57
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
31 changes: 31 additions & 0 deletions immer/extra/persist/rbts/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ class same_depth_children_exception : public pool_exception
}
};

class incompatible_bits_parameters : public pool_exception
{
public:
incompatible_bits_parameters(immer::detail::rbts::bits_t loader_bits,
immer::detail::rbts::bits_t loader_bits_leaf,
immer::detail::rbts::bits_t pool_bits,
immer::detail::rbts::bits_t pool_bits_leaf)
: pool_exception{
fmt::format("B and BL parameters must be the same. Loader "
"expects {} and {} but the pool has {} and {}",
loader_bits,
loader_bits_leaf,
pool_bits,
pool_bits_leaf)}
{
}
};

template <class T,
typename MemoryPolicy,
immer::detail::rbts::bits_t B,
Expand Down Expand Up @@ -110,6 +128,8 @@ class loader
throw invalid_container_id{id};
}

validate_bits_params();

const auto& info = pool_.vectors[id.value];

const auto relaxed_allowed = false;
Expand All @@ -136,6 +156,8 @@ class loader
throw invalid_container_id{id};
}

validate_bits_params();

const auto& info = pool_.vectors[id.value];

const auto relaxed_allowed = true;
Expand Down Expand Up @@ -486,6 +508,15 @@ class loader
}));
}

void validate_bits_params() const
{
auto good = pool_.bits == B && pool_.bits_leaf == BL;
if (!good) {
throw incompatible_bits_parameters{
B, BL, pool_.bits, pool_.bits_leaf};
}
}

private:
const Pool pool_;
const TransformF transform_;
Expand Down
24 changes: 19 additions & 5 deletions immer/extra/persist/rbts/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ struct output_pool
template <class Archive>
void save(Archive& ar) const
{
ar(CEREAL_NVP(leaves), CEREAL_NVP(inners), CEREAL_NVP(vectors));
ar(CEREAL_NVP(B),
CEREAL_NVP(BL),
CEREAL_NVP(leaves),
CEREAL_NVP(inners),
CEREAL_NVP(vectors));
}
};

Expand All @@ -98,6 +102,8 @@ make_output_pool_for(const immer::flex_vector<T, MemoryPolicy, B, BL>&)
template <typename T>
struct input_pool
{
immer::detail::rbts::bits_t bits{};
immer::detail::rbts::bits_t bits_leaf{};
immer::map<node_id, values_load<T>> leaves;
immer::map<node_id, inner_node> inners;
immer::vector<rbts_info> vectors;
Expand All @@ -108,7 +114,13 @@ struct input_pool
template <class Archive>
void load(Archive& ar)
{
ar(CEREAL_NVP(leaves), CEREAL_NVP(inners), CEREAL_NVP(vectors));
auto& B = bits;
auto& BL = bits_leaf;
ar(CEREAL_NVP(B),
CEREAL_NVP(BL),
CEREAL_NVP(leaves),
CEREAL_NVP(inners),
CEREAL_NVP(vectors));
}

void merge_previous(const input_pool& other) {}
Expand All @@ -128,9 +140,11 @@ input_pool<T> to_input_pool(output_pool<T, MemoryPolicy, B, BL> ar)
}

return {
.leaves = std::move(leaves),
.inners = std::move(ar.inners),
.vectors = std::move(ar.vectors),
.bits = B,
.bits_leaf = BL,
.leaves = std::move(leaves),
.inners = std::move(ar.inners),
.vectors = std::move(ar.vectors),
};
}

Expand Down
20 changes: 20 additions & 0 deletions test/extra/persist/test_special_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,22 @@ TEST_CASE("Special pool loads empty test_data")
},
"pools": {
"ints": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"strings": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"flex_ints": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
Expand All @@ -466,11 +472,15 @@ TEST_CASE("Special pool loads empty test_data")
{"values": [], "children": [], "nodemap": 0, "datamap": 0, "collisions": false}
],
"metas": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"meta_metas": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
Expand Down Expand Up @@ -514,16 +524,22 @@ TEST_CASE("Special pool throws cereal::Exception")
},
"pools": {
"ints": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"strings": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"flex_ints": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
Expand All @@ -532,11 +548,15 @@ TEST_CASE("Special pool throws cereal::Exception")
{"values": [], "children": [], "nodemap": 0, "datamap": 0, "collisions": false}
],
"metas": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
},
"meta_metas": {
"B": 5,
"BL": 1,
"leaves": [{"key": 1, "value": []}],
"inners": [{"key": 0, "value": {"children": [], "relaxed": false}}],
"vectors": [{"root": 0, "tail": 1}]
Expand Down
4 changes: 4 additions & 0 deletions test/extra/persist/test_special_pool_auto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ TEST_CASE("Test loading broken table")
{"ones": 1, "key": {"str": "123"}}
],
"ones": {
"B": 5,
"BL": 1,
"leaves": [
{"key": 1, "value": []},
{"key": 2, "value": [{"twos": 1, "twos_table": 1}]}
Expand All @@ -503,6 +505,8 @@ TEST_CASE("Test loading broken table")
"vectors": [{"root": 0, "tail": 1}, {"root": 0, "tail": 2}]
},
"twos": {
"B": 5,
"BL": 1,
"leaves": [
{"key": 1, "value": [{"two": 0}, {"two": 1}]},
{"key": 2, "value": []}
Expand Down
30 changes: 30 additions & 0 deletions test/extra/persist/test_vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ TEST_CASE("A loop with 2 nodes")
{
const auto json = std::string{R"({
"value0": {
"B": 5,
"BL": 1,
"leaves": [
{
"key": 32,
Expand Down Expand Up @@ -858,6 +860,8 @@ TEST_CASE("Test vector with very big objects")
TEST_CASE("Test modifying vector nodes")
{
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{
{"key", 1},
Expand Down Expand Up @@ -908,6 +912,22 @@ TEST_CASE("Test modifying vector nodes")
immer::persist::pool_exception);
}

SECTION("Load different B")
{
auto b = GENERATE(1, 3, 6, 7);
data["value0"]["B"] = b;
REQUIRE_THROWS_AS(load_vec(data.dump(), 0),
immer::persist::rbts::incompatible_bits_parameters);
}

SECTION("Load different BL")
{
auto bl = GENERATE(2, 3, 5, 6);
data["value0"]["BL"] = bl;
REQUIRE_THROWS_AS(load_vec(data.dump(), 0),
immer::persist::rbts::incompatible_bits_parameters);
}

SECTION("Invalid root id")
{
data["value0"]["vectors"][0]["root"] = 1;
Expand Down Expand Up @@ -998,6 +1018,8 @@ TEST_CASE("Test modifying vector nodes")
TEST_CASE("Test modifying flex vector nodes")
{
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{
{"key", 1},
Expand Down Expand Up @@ -1205,6 +1227,8 @@ TEST_CASE("Print shift calculation", "[.print_shift]")
TEST_CASE("Test more inner nodes")
{
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{{"key", 32}, {"value", {58, 59}}}, {{"key", 34}, {"value", {62, 63}}},
{{"key", 3}, {"value", {0, 1}}}, {{"key", 5}, {"value", {4, 5}}},
Expand Down Expand Up @@ -1350,6 +1374,8 @@ TEST_CASE("Exception while loading children")
{
// spdlog::set_level(spdlog::level::trace);
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{
{"key", 1},
Expand Down Expand Up @@ -1411,6 +1437,8 @@ TEST_CASE("Exception while loading children")
TEST_CASE("Test flex vector with a weird shape relaxed")
{
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{{"key", 1}, {"value", {66}}},
{{"key", 36}, {"value", {64, 65}}},
Expand Down Expand Up @@ -1449,6 +1477,8 @@ TEST_CASE("Test flex vector with a weird shape relaxed")
TEST_CASE("Test flex vector with a weird shape strict", "[.broken]")
{
json_t data;
data["value0"]["B"] = 5;
data["value0"]["BL"] = 1;
data["value0"]["leaves"] = {
{{"key", 1}, {"value", {66}}},
{{"key", 36}, {"value", {64, 65}}},
Expand Down

0 comments on commit bce6f57

Please sign in to comment.