Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement value_schema_manager::get_value_schema #739

Merged
merged 7 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 works well in future version if it
acelyc111 marked this conversation as resolved.
Show resolved Hide resolved
// 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