Skip to content

Commit

Permalink
Updates group serial/deserial to use sizeof only on types (#3399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-dark committed Aug 2, 2022
1 parent a577f1e commit eac140e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tiledb/sm/group/group.cc
Expand Up @@ -660,7 +660,7 @@ Status Group::apply_and_serialize(Buffer* buff) {
std::tuple<Status, std::optional<tdb_shared_ptr<Group>>> Group::deserialize(
ConstBuffer* buff, const URI& group_uri, StorageManager* storage_manager) {
uint32_t version = 0;
RETURN_NOT_OK_TUPLE(buff->read(&version, sizeof(version)), std::nullopt);
RETURN_NOT_OK_TUPLE(buff->read(&version, sizeof(uint32_t)), std::nullopt);
if (version == 1) {
return GroupV1::deserialize(buff, group_uri, storage_manager);
}
Expand Down
2 changes: 1 addition & 1 deletion tiledb/sm/group/group_member.cc
Expand Up @@ -73,7 +73,7 @@ Status GroupMember::serialize(Buffer*) {
std::tuple<Status, std::optional<tdb_shared_ptr<GroupMember>>>
GroupMember::deserialize(ConstBuffer* buff) {
uint32_t version = 0;
RETURN_NOT_OK_TUPLE(buff->read(&version, sizeof(version)), std::nullopt);
RETURN_NOT_OK_TUPLE(buff->read(&version, sizeof(uint32_t)), std::nullopt);
if (version == 1) {
return GroupMemberV1::deserialize(buff);
}
Expand Down
36 changes: 18 additions & 18 deletions tiledb/sm/group/group_member_v1.cc
Expand Up @@ -48,30 +48,29 @@ GroupMemberV1::GroupMemberV1(
// format_version (uint32_t)
// type (uint8_t)
// relative (uint8_t)
// uri_size (uint32_t)
// uri_size (uint64_t)
// uri (string)
Status GroupMemberV1::serialize(Buffer* buff) {
RETURN_NOT_OK(buff->write(
&GroupMemberV1::format_version_, sizeof(GroupMemberV1::format_version_)));
RETURN_NOT_OK(buff->write(&GroupMemberV1::format_version_, sizeof(uint32_t)));

// Write type
uint8_t type = static_cast<uint8_t>(type_);
RETURN_NOT_OK(buff->write(&type, sizeof(type)));
RETURN_NOT_OK(buff->write(&type, sizeof(uint8_t)));

// Write relative
RETURN_NOT_OK(buff->write(&relative_, sizeof(relative_)));
RETURN_NOT_OK(buff->write(&relative_, sizeof(uint8_t)));

// Write uri
uint64_t uri_size = uri_.to_string().size();
RETURN_NOT_OK(buff->write(&uri_size, sizeof(uri_size)));
RETURN_NOT_OK(buff->write(uri_.c_str(), uri_size));

// Write name
bool name_set = name_.has_value();
RETURN_NOT_OK(buff->write(&name_set, sizeof(bool)));
if (name_set) {
auto name_set = static_cast<uint8_t>(name_.has_value());
RETURN_NOT_OK(buff->write(&name_set, sizeof(uint8_t)));
if (name_.has_value()) {
uint64_t name_size = name_->size();
RETURN_NOT_OK(buff->write(&name_size, sizeof(name_size)));
RETURN_NOT_OK(buff->write(&name_size, sizeof(uint64_t)));
RETURN_NOT_OK(buff->write(name_->data(), name_size));
}

Expand All @@ -82,26 +81,27 @@ std::tuple<Status, std::optional<tdb_shared_ptr<GroupMember>>>
GroupMemberV1::deserialize(ConstBuffer* buff) {
uint8_t type_placeholder;
RETURN_NOT_OK_TUPLE(
buff->read(&type_placeholder, sizeof(type_placeholder)), std::nullopt);
buff->read(&type_placeholder, sizeof(uint8_t)), std::nullopt);
ObjectType type = static_cast<ObjectType>(type_placeholder);

bool relative;
RETURN_NOT_OK_TUPLE(buff->read(&relative, sizeof(relative)), std::nullopt);
uint8_t relative_int;
RETURN_NOT_OK_TUPLE(buff->read(&relative_int, sizeof(uint8_t)), std::nullopt);
auto relative = static_cast<bool>(relative_int);

uint64_t uri_size = 0;
RETURN_NOT_OK_TUPLE(buff->read(&uri_size, sizeof(uri_size)), std::nullopt);
RETURN_NOT_OK_TUPLE(buff->read(&uri_size, sizeof(uint64_t)), std::nullopt);

std::string uri_string;
uri_string.resize(uri_size);
RETURN_NOT_OK_TUPLE(buff->read(&uri_string[0], uri_size), std::nullopt);

bool name_set;
uint8_t name_set_int;
std::optional<std::string> name;
RETURN_NOT_OK_TUPLE(buff->read(&name_set, sizeof(name_set)), std::nullopt);
RETURN_NOT_OK_TUPLE(buff->read(&name_set_int, sizeof(uint8_t)), std::nullopt);
auto name_set = static_cast<bool>(name_set_int);
if (name_set) {
uint64_t name_size = 0;
RETURN_NOT_OK_TUPLE(
buff->read(&name_size, sizeof(name_size)), std::nullopt);
RETURN_NOT_OK_TUPLE(buff->read(&name_size, sizeof(uint64_t)), std::nullopt);

std::string name_string;
name_string.resize(name_size);
Expand All @@ -115,4 +115,4 @@ GroupMemberV1::deserialize(ConstBuffer* buff) {
}

} // namespace sm
} // namespace tiledb
} // namespace tiledb
9 changes: 4 additions & 5 deletions tiledb/sm/group/group_v1.cc
Expand Up @@ -48,10 +48,9 @@ GroupV1::GroupV1(const URI& group_uri, StorageManager* storage_manager)
// group_member #2
// ...
Status GroupV1::serialize(Buffer* buff) {
RETURN_NOT_OK(
buff->write(&GroupV1::format_version_, sizeof(GroupV1::format_version_)));
RETURN_NOT_OK(buff->write(&GroupV1::format_version_, sizeof(uint32_t)));
uint64_t group_member_num = members_.size();
RETURN_NOT_OK(buff->write(&group_member_num, sizeof(group_member_num)));
RETURN_NOT_OK(buff->write(&group_member_num, sizeof(uint64_t)));
for (auto& it : members_) {
RETURN_NOT_OK(it.second->serialize(buff));
}
Expand All @@ -65,7 +64,7 @@ std::tuple<Status, std::optional<tdb_shared_ptr<Group>>> GroupV1::deserialize(

uint64_t member_count = 0;
RETURN_NOT_OK_TUPLE(
buff->read(&member_count, sizeof(member_count)), std::nullopt);
buff->read(&member_count, sizeof(uint64_t)), std::nullopt);

for (uint64_t i = 0; i < member_count; i++) {
auto&& [st, member] = GroupMember::deserialize(buff);
Expand All @@ -76,4 +75,4 @@ std::tuple<Status, std::optional<tdb_shared_ptr<Group>>> GroupV1::deserialize(
return {Status::Ok(), group};
}
} // namespace sm
} // namespace tiledb
} // namespace tiledb

0 comments on commit eac140e

Please sign in to comment.