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

Add list and del commands #13

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Changes from all 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
72 changes: 50 additions & 22 deletions examples/kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,62 @@

#include "kv.h"

struct TWriteKvEntry: public TLogEntry {
uint16_t KeySize;
uint16_t ValSize;
struct TKvEntry {
uint16_t KeySize = 0;
uint16_t ValSize = 0;
char Data[0];
};

struct TWriteKv: public TCommandRequest {
uint16_t KeySize;
uint16_t ValSize;
char Data[0];
struct TKvLogEntry: public TLogEntry, public TKvEntry
{
};

struct TWriteKv: public TCommandRequest, public TKvEntry
{
};

struct TReadKv: public TCommandRequest {
uint16_t KeySize;
uint16_t KeySize = 0;
char Data[0];
};

TMessageHolder<TMessage> TKv::Read(TMessageHolder<TCommandRequest> message, uint64_t index) {
auto readKv = message.Cast<TReadKv>();
std::string_view k(readKv->Data, readKv->KeySize);
auto it = H.find(std::string(k));
if (it == H.end()) {
auto res = NewHoldedMessage<TCommandResponse>(sizeof(TCommandResponse));
if (readKv->KeySize == 0) {
std::string data;
for (auto& [k, v] : H) {
data += k + "=" + v + ",";
}
auto res = NewHoldedMessage<TCommandResponse>(sizeof(TCommandResponse) + data.size());
memcpy(res->Data, data.data(), data.size());
res->Index = index;
return res;
} else {
auto res = NewHoldedMessage<TCommandResponse>(sizeof(TCommandResponse)+it->second.size());
res->Index = index;
memcpy(res->Data, it->second.data(), it->second.size());
return res;
std::string_view k(readKv->Data, readKv->KeySize);
auto it = H.find(std::string(k));
if (it == H.end()) {
auto res = NewHoldedMessage<TCommandResponse>(sizeof(TCommandResponse));
res->Index = index;
return res;
} else {
auto res = NewHoldedMessage<TCommandResponse>(sizeof(TCommandResponse)+it->second.size());
res->Index = index;
memcpy(res->Data, it->second.data(), it->second.size());
return res;
}
}
}

void TKv::Write(TMessageHolder<TLogEntry> message, uint64_t index) {
if (LastAppliedIndex < index) {
auto writeKv = message.Cast<TWriteKvEntry>();
std::string_view k(writeKv->Data, writeKv->KeySize);
std::string_view v(writeKv->Data + writeKv->KeySize, writeKv->ValSize);
H[std::string(k)] = std::string(v);
auto entry = message.Cast<TKvLogEntry>();
std::string_view k(entry->TKvEntry::Data, entry->KeySize);
if (entry->ValSize) {
std::string_view v(entry->TKvEntry::Data + entry->KeySize, entry->ValSize);
H[std::string(k)] = std::string(v);
} else {
H.erase(std::string(k));
}
LastAppliedIndex = index;
}
}
Expand Down Expand Up @@ -87,8 +104,8 @@ NNet::TVoidTask Client(TPoller& poller, TSocket socket) {
mes->Flags = TCommandRequest::EWrite;
mes->KeySize = keySize;
mes->ValSize = valSize;
memcpy(mes->Data, key, keySize);
memcpy(mes->Data+keySize, val, valSize);
memcpy(mes->TKvEntry::Data, key, keySize);
memcpy(mes->TKvEntry::Data+keySize, val, valSize);
req = mes;
} else if (!strcmp(prefix, "get")) {
auto key = strtok(nullptr, sep);
Expand All @@ -97,6 +114,17 @@ NNet::TVoidTask Client(TPoller& poller, TSocket socket) {
mes->KeySize = size;
memcpy(mes->Data, key, size);
req = mes;
} else if (!strcmp(prefix, "list")) {
req = NewHoldedMessage<TReadKv>(sizeof(TReadKv));
} else if (!strcmp(prefix, "del")) {
auto key = strtok(nullptr, sep);
auto size = strlen(key);
auto mes = NewHoldedMessage<TWriteKv>(sizeof(TWriteKv) + size);
mes->Flags = TCommandRequest::EWrite;
mes->KeySize = size;
mes->ValSize = 0;
memcpy(mes->TKvEntry::Data, key, size);
req = mes;
} else {
std::cout << "Cannot parse command: " << strLine << "\n";
}
Expand Down
Loading