Skip to content

Commit

Permalink
Start work on kv example (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius authored Mar 2, 2024
1 parent a7934d7 commit 6cb4b40
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ add_executable(test_raft test/test_raft.cpp)
add_executable(test_read_write test/test_read_write.cpp)
add_executable(server server/server.cpp)
add_executable(client client/client.cpp)
add_executable(kv examples/kv.cpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/coroio)

target_link_libraries(server miniraft coroio)
target_link_libraries(client miniraft coroio)
target_link_libraries(kv miniraft coroio)

target_include_directories(test_raft PRIVATE ${CMOCKA_INCLUDE_DIRS})
target_link_directories(test_raft PRIVATE ${CMOCKA_LIBRARY_DIRS})
Expand Down
46 changes: 46 additions & 0 deletions examples/kv.cpp
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;
}
16 changes: 16 additions & 0 deletions examples/kv.h
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;
};
9 changes: 9 additions & 0 deletions src/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ struct INode {
virtual void Drain() = 0;
};

// CommandRequest -> Write? -> LogEntry -> Append -> Committed As Index -> Applied As Index (Same) -> Index -> CommandResponse
// CommandRequest -> Read? -> CurrentIndex (fixate) >= CommittedIndex -> CommandResponse
struct IRsm {
virtual ~IRsm() = default;
virtual TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message) = 0;
virtual void Write(TMessageHolder<TLogEntry> message) = 0;
virtual TMessageHolder<TLogEntry> Prepare(TMessageHolder<TCommandRequest> message, uint64_t term) = 0;
};

using TNodeDict = std::unordered_map<uint32_t, std::shared_ptr<INode>>;

struct TState {
Expand Down

0 comments on commit 6cb4b40

Please sign in to comment.