Skip to content

Commit

Permalink
chore(facade): RedisReplyBuilder2 (extensions)
Browse files Browse the repository at this point in the history
  • Loading branch information
dranikpg committed Aug 8, 2024
1 parent 5258bba commit 1c19d68
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/facade/reply_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,53 @@ void RedisReplyBuilder::SendStringArrInternal(
Send(vec.data(), vec_indx + 1);
}

void RedisReplyBuilder2::SendSimpleStrArr(const facade::ArgRange& strs) {
ReplyScope scope(this);
StartArray(strs.Size());
for (std::string_view str : strs)
SendSimpleString(str);
}

void RedisReplyBuilder2::SendBulkStrArr(const facade::ArgRange& strs, CollectionType ct) {
ReplyScope scope(this);
StartCollection(ct == CollectionType::MAP ? strs.Size() / 2 : strs.Size(), ct);
for (std::string_view str : strs)
SendBulkString(str);
}

void RedisReplyBuilder2::SendScoredArray(absl::Span<const std::pair<std::string, double>> arr,
bool with_scores) {
ReplyScope scope(this);
StartArray((with_scores && !IsResp3()) ? arr.size() * 2 : arr.size());
for (const auto& [str, score] : arr) {
if (with_scores && IsResp3())
StartArray(2);
SendBulkString(str);
if (with_scores)
SendDouble(score);
}
}

void RedisReplyBuilder2::SendStored() {
SendSimpleString("OK");
}

void RedisReplyBuilder2::SendSetSkipped() {
SendSimpleString("SKIPPED");
}

void RedisReplyBuilder2::StartArray(unsigned len) {
StartCollection(len, CollectionType::ARRAY);
}

void RedisReplyBuilder2::SendEmptyArray() {
StartArray(0);
}

void RedisReplyBuilder2::SendVerbatimString(std::string_view str, VerbatimFormat format) {
SendBulkString(str);
}

void ReqSerializer::SendCommand(std::string_view str) {
VLOG(2) << "SendCommand: " << str;

Expand Down
20 changes: 20 additions & 0 deletions src/facade/reply_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,26 @@ class RedisReplyBuilder : public SinkReplyBuilder {
bool is_resp3_ = false;
};

// Non essential redis reply builder functions implemented on top of the base resp protocol
class RedisReplyBuilder2 : public RedisReplyBuilder2Base {
public:
RedisReplyBuilder(io::Sink* sink) : RedisReplyBuilder2Base(sink) {
}

void SendSimpleStrArr(const facade::ArgRange& strs);
void SendBulkStrArr(const facade::ArgRange& strs, CollectionType ct = ARRAY);
void SendScoredArray(absl::Span<const std::pair<std::string, double>> arr, bool with_scores);

void SendStored() final;
void SendSetSkipped() final;

void StartArray(unsigned len);
void SendEmptyArray();

void SendVerbatimString(std::string_view str, VerbatimFormat format = TXT);
static std::string SerializeCommmand(std::string_view cmd);
};

class ReqSerializer {
public:
explicit ReqSerializer(::io::Sink* stream) : sink_(stream) {
Expand Down

0 comments on commit 1c19d68

Please sign in to comment.