-
Notifications
You must be signed in to change notification settings - Fork 434
Add C++ builder pattern #1018
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
base: main
Are you sure you want to change the base?
Add C++ builder pattern #1018
Changes from all commits
47add82
9c644f0
58650f8
4fbe66a
2fb2b7e
a9aa54c
58766bc
8010e07
a6a7dc3
ff674c5
9ebae76
1daeb48
1780a31
67d1268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include <chrono> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <string_view> | ||
| #include <optional> | ||
| #include <ranges> | ||
| #include <thread> | ||
|
|
@@ -406,6 +407,80 @@ std::optional<std::shared_ptr<Client>> Client::Create( | |
| return client; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithLocalHostname( | ||
| std::string local_hostname) { | ||
| local_hostname_ = std::move(local_hostname); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| return *this; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithMetadataConnectionString( | ||
| std::string metadata_connstring) { | ||
| metadata_connstring_ = std::move(metadata_connstring); | ||
| return *this; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithProtocol(std::string protocol) { | ||
| protocol_ = std::move(protocol); | ||
| return *this; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithProtocolArgs( | ||
| std::string protocol_args) { | ||
| // Can add some other engine arguments | ||
| protocol_args_ = std::move(protocol_args); | ||
| return *this; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithMasterEndpoint( | ||
| std::string master_server_entry) { | ||
| master_server_entry_ = std::move(master_server_entry); | ||
| return *this; | ||
| } | ||
|
|
||
| MooncakeStoreBuilder& MooncakeStoreBuilder::WithExistingTransferEngine( | ||
| std::shared_ptr<TransferEngine> transfer_engine) { | ||
| transfer_engine_ = std::move(transfer_engine); | ||
| return *this; | ||
| } | ||
|
|
||
| tl::expected<std::shared_ptr<Client>, std::string> MooncakeStoreBuilder::Build() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning string for errors is not a good practice. String is not structured and hard to be programmatically processed. You can see the bad result in the unit tests that you must search the returned string to check whether it is expected. I suggest to return an ErrorCode for failure. For example, |
||
| const { | ||
| std::vector<std::string_view> missing; | ||
| if (!local_hostname_) { | ||
| missing.emplace_back("local_hostname"); | ||
| } | ||
| if (!metadata_connstring_) { | ||
| missing.emplace_back("metadata_connstring"); | ||
| } | ||
|
|
||
| if (!missing.empty()) { | ||
| std::string joined; | ||
| joined.reserve(missing.size() * 16); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| for (size_t i = 0; i < missing.size(); ++i) { | ||
| if (i != 0) { | ||
| joined.append(", "); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| joined.append(missing[0]); | ||
| for (size_t i = 1; i < missing.size(); ++i) { | ||
| joined.append(", "); | ||
| joined.append(missing[i]); | ||
| } | ||
| } | ||
| auto error_msg = | ||
| "MooncakeStoreBuilder missing required fields: " + joined; | ||
| LOG(ERROR) << error_msg; | ||
| return tl::make_unexpected(std::move(error_msg)); | ||
| } | ||
| auto client = | ||
| Client::Create(*local_hostname_, *metadata_connstring_, protocol_, | ||
| protocol_args_, master_server_entry_, transfer_engine_); | ||
| if (!client) { | ||
| std::string error_msg = "Client creation failed"; | ||
| return tl::make_unexpected(std::move(error_msg)); | ||
| } | ||
| return *client; | ||
| } | ||
|
|
||
| tl::expected<void, ErrorCode> Client::Get(const std::string& object_key, | ||
| std::vector<Slice>& slices) { | ||
| auto query_result = Query(object_key); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
ClientBuildershould be a better name in the context, as we are infact building aClient, not aMooncakeStore.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about MooncakeStoreClientBuilder? Don't be afraid of a long name.