Skip to content

Commit

Permalink
feat: implement value_schema_manager::get_value_schema (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored Jun 1, 2021
1 parent 376dfc8 commit b5e73d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/base/test/value_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,30 @@ TEST(value_schema_manager, get_value_schema)
}
}
}

TEST(pegasus_value_manager, get_value_schema)
{
struct test_case
{
uint32_t meta_store_data_version;
uint32_t value_schema_version;
data_version expect_version;
} tests[] = {
{0, 0, pegasus::data_version::VERSION_0},
{1, 0, pegasus::data_version::VERSION_1},
{0, 1, pegasus::data_version::VERSION_0},
{1, 1, pegasus::data_version::VERSION_1},
{0, 2, pegasus::data_version::VERSION_2},
{1, 2, pegasus::data_version::VERSION_2},
};

for (const auto &t : tests) {
auto generate_schema =
value_schema_manager::instance().get_value_schema(t.value_schema_version);
std::string raw_value = generate_value(generate_schema, 0, 0, "");

auto schema =
value_schema_manager::instance().get_value_schema(t.meta_store_data_version, raw_value);
ASSERT_EQ(t.expect_version, schema->version());
}
}
21 changes: 19 additions & 2 deletions src/base/value_schema_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,25 @@ void value_schema_manager::register_schema(std::unique_ptr<value_schema> schema)
value_schema *value_schema_manager::get_value_schema(uint32_t meta_cf_data_version,
dsn::string_view value) const
{
/// TBD(zlw)
return nullptr;
dsn::data_input input(value);
uint8_t first_byte = input.read_u8();
// first bit = 1 means the data version is >= VERSION_2
if (first_byte & 0x80) {
// In order to keep backward compatibility, we should return latest version if the data
// version in value is not found. In other words, it will work well in future version if it
// is compatible with latest version in current
auto schema = get_value_schema(first_byte & 0x7F);
if (nullptr == schema) {
return get_latest_value_schema();
}
return schema;
} else {
auto schema = get_value_schema(meta_cf_data_version);
if (nullptr == schema) {
dassert_f(false, "data version({}) in meta cf is not supported", meta_cf_data_version);
}
return schema;
}
}

value_schema *value_schema_manager::get_value_schema(uint32_t version) const
Expand Down
3 changes: 2 additions & 1 deletion src/base/value_schema_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ std::unique_ptr<value_field> value_schema_v2::extract_field(dsn::string_view val
dsn::blob value_schema_v2::extract_user_data(std::string &&value)
{
auto ret = dsn::blob::create_from_bytes(std::move(value));
return ret.range(sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t));
static const auto USER_DATA_OFFSET = sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t);
return ret.range(USER_DATA_OFFSET);
}

void value_schema_v2::update_field(std::string &value, std::unique_ptr<value_field> field)
Expand Down

0 comments on commit b5e73d4

Please sign in to comment.