Skip to content

Commit

Permalink
refactor: address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Bagritsevich <[email protected]>
  • Loading branch information
BagritsevichStepan committed Jun 19, 2024
1 parent e6ec9a2 commit 50a6d2f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/core/string_or_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@

#include <string>
#include <string_view>
#include <utility>
#include <variant>

namespace dfly {

class StringOrView {
public:
static StringOrView FromString(std::string s) {
StringOrView sov;
sov.val_ = std::move(s);
return sov;
return StringOrView{std::move(s)};
}

static StringOrView FromView(std::string_view sv) {
StringOrView sov;
sov.val_ = sv;
return sov;
return StringOrView{sv};
}

StringOrView() = default;
Expand All @@ -30,6 +27,11 @@ class StringOrView {
StringOrView& operator=(const StringOrView& o) = default;
StringOrView& operator=(StringOrView&& o) = default;

explicit StringOrView(std::string&& s) : val_(std::forward<std::string>(s)) {
}
explicit StringOrView(std::string_view sv) : val_(sv) {
}

bool operator==(const StringOrView& o) const {
return *this == o.view();
}
Expand Down
10 changes: 6 additions & 4 deletions src/facade/facade_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <variant>

#include "base/iterator.h"
#include "core/string_or_view.h"
#include "facade/op_status.h"

namespace facade {
Expand Down Expand Up @@ -155,26 +157,26 @@ struct FacadeStats {

struct ErrorReply {
explicit ErrorReply(std::string&& msg, std::string_view kind = {})
: message{std::move(msg)}, kind{kind} {
: message{std::forward<std::string>(msg)}, kind{kind} {
}
explicit ErrorReply(std::string_view msg, std::string_view kind = {}) : message{msg}, kind{kind} {
}
explicit ErrorReply(const char* msg,
std::string_view kind = {}) // to resolve ambiguity of constructors above
: message{std::string_view{msg}}, kind{kind} {
}
explicit ErrorReply(OpStatus status) : message{}, kind{}, status{status} {
explicit ErrorReply(OpStatus status) : status{status} {
}

bool HasStatus() const {
return status.has_value();
}

std::string_view ToSv() const {
return std::visit(kToSV, message);
return message.view();
}

std::variant<std::string, std::string_view> message;
dfly::StringOrView message;
std::string_view kind;
std::optional<OpStatus> status{std::nullopt};
};
Expand Down
4 changes: 0 additions & 4 deletions src/facade/reply_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ void SinkReplyBuilder::SendError(ErrorReply error) {
if (error.HasStatus())
return SendError(*error.status);

SendErrorReplyMessage(error);
}

void SinkReplyBuilder::SendErrorReplyMessage(const ErrorReply& error) {
SendError(error.ToSv(), error.kind);
}

Expand Down
4 changes: 1 addition & 3 deletions src/facade/reply_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class SinkReplyBuilder {
}

virtual void SendError(std::string_view str, std::string_view type = {}) = 0; // MC and Redis
void SendError(ErrorReply error);
virtual void SendError(OpStatus status);
void SendError(ErrorReply error);

virtual void SendStored() = 0; // Reply for set commands.
virtual void SendSetSkipped() = 0;
Expand Down Expand Up @@ -148,8 +148,6 @@ class SinkReplyBuilder {
static void ResetThreadLocalStats();

protected:
virtual void SendErrorReplyMessage(const ErrorReply& error);

void SendRaw(std::string_view str); // Sends raw without any formatting.
void SendRawVec(absl::Span<const std::string_view> msg_vec);

Expand Down
5 changes: 0 additions & 5 deletions src/facade/reply_capture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ void CapturingReplyBuilder::SendError(std::string_view str, std::string_view typ
Capture(Error{str, type});
}

void CapturingReplyBuilder::SendErrorReplyMessage(const ErrorReply& error) {
SKIP_LESS(ReplyMode::ONLY_ERR);
Capture(Error{error.ToSv(), error.kind});
}

void CapturingReplyBuilder::SendMGetResponse(MGetResponse resp) {
SKIP_LESS(ReplyMode::FULL);
Capture(std::move(resp));
Expand Down
3 changes: 0 additions & 3 deletions src/facade/reply_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ class CapturingReplyBuilder : public RedisReplyBuilder {
std::vector<Payload> arr;
};

protected:
void SendErrorReplyMessage(const ErrorReply& error) override;

private:
// Send payload directly, bypassing external interface. For efficient passing between two
// captures.
Expand Down

0 comments on commit 50a6d2f

Please sign in to comment.