-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <string_view> | ||
|
||
#include "kv.h" | ||
|
||
struct TWriteKv: public TLogEntry { | ||
uint16_t KeySize; | ||
uint16_t ValSize; | ||
char Data[0]; | ||
}; | ||
|
||
struct TReadKv: public TCommandRequest { | ||
uint16_t KeySize; | ||
char Data[0]; | ||
}; | ||
|
||
TMessageHolder<TMessage> TKv::Read(TMessageHolder<TCommandRequest> message) { | ||
auto readKv = message.Cast<TReadKv>(); | ||
std::string_view k(readKv->Data, readKv->KeySize); | ||
auto it = H.find(std::string(k)); | ||
if (it == H.end()) { | ||
// TODO | ||
} else { | ||
// TODO | ||
} | ||
return {}; | ||
} | ||
|
||
void TKv::Write(TMessageHolder<TLogEntry> message) { | ||
auto writeKv = message.Cast<TWriteKv>(); | ||
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); | ||
return; | ||
} | ||
|
||
TMessageHolder<TLogEntry> TKv::Prepare(TMessageHolder<TCommandRequest> command, uint64_t term) { | ||
auto dataSize = command->Len - sizeof(TCommandRequest); | ||
auto entry = NewHoldedMessage<TLogEntry>(sizeof(TLogEntry)+dataSize); | ||
memcpy(entry->Data, command->Data, dataSize); | ||
entry->Term = term; | ||
return entry; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
|
||
#include <raft.h> | ||
|
||
class TKv: public IRsm { | ||
public: | ||
TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message) override; | ||
void Write(TMessageHolder<TLogEntry> message) override; | ||
TMessageHolder<TLogEntry> Prepare(TMessageHolder<TCommandRequest> message, uint64_t term) override; | ||
|
||
private: | ||
std::unordered_map<std::string, std::string> H; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters