feat(server): Use new journal format (#563)

This commit is contained in:
Vladislav 2022-12-20 16:38:19 +03:00 committed by GitHub
parent cdc31fc98c
commit 2386b02234
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 302 additions and 179 deletions

View file

@ -56,10 +56,10 @@ error_code JournalWriter::Write(std::monostate) {
return std::error_code{};
}
error_code JournalWriter::Write(const journal::EntryNew& entry) {
error_code JournalWriter::Write(const journal::Entry& entry) {
// Check if entry has a new db index and we need to emit a SELECT entry.
if (entry.opcode != journal::Op::SELECT && (!cur_dbid_ || entry.dbid != *cur_dbid_)) {
RETURN_ON_ERR(Write(journal::EntryNew{journal::Op::SELECT, entry.dbid}));
RETURN_ON_ERR(Write(journal::Entry{journal::Op::SELECT, entry.dbid}));
cur_dbid_ = entry.dbid;
}
@ -68,7 +68,7 @@ error_code JournalWriter::Write(const journal::EntryNew& entry) {
switch (entry.opcode) {
case journal::Op::SELECT:
return Write(entry.dbid);
case journal::Op::VAL:
case journal::Op::COMMAND:
RETURN_ON_ERR(Write(entry.txid));
return std::visit([this](const auto& payload) { return Write(payload); }, entry.payload);
default:
@ -77,8 +77,11 @@ error_code JournalWriter::Write(const journal::EntryNew& entry) {
return std::error_code{};
}
JournalReader::JournalReader(io::Source* source, DbIndex dbid)
: source_{source}, buf_{}, dbid_{dbid} {
JournalReader::JournalReader(DbIndex dbid) : buf_{}, dbid_{dbid} {
}
void JournalReader::SetDb(DbIndex dbid) {
dbid_ = dbid;
}
template <typename UT> io::Result<UT> ReadPackedUIntTyped(io::Source* source) {
@ -89,26 +92,26 @@ template <typename UT> io::Result<UT> ReadPackedUIntTyped(io::Source* source) {
return static_cast<UT>(v);
}
io::Result<uint8_t> JournalReader::ReadU8() {
return ReadPackedUIntTyped<uint8_t>(source_);
io::Result<uint8_t> JournalReader::ReadU8(io::Source* source) {
return ReadPackedUIntTyped<uint8_t>(source);
}
io::Result<uint16_t> JournalReader::ReadU16() {
return ReadPackedUIntTyped<uint16_t>(source_);
io::Result<uint16_t> JournalReader::ReadU16(io::Source* source) {
return ReadPackedUIntTyped<uint16_t>(source);
}
io::Result<uint64_t> JournalReader::ReadU64() {
return ReadPackedUIntTyped<uint64_t>(source_);
io::Result<uint64_t> JournalReader::ReadU64(io::Source* source) {
return ReadPackedUIntTyped<uint64_t>(source);
}
io::Result<size_t> JournalReader::ReadString() {
io::Result<size_t> JournalReader::ReadString(io::Source* source) {
size_t size = 0;
SET_OR_UNEXPECT(ReadU64(), size);
SET_OR_UNEXPECT(ReadU64(source), size);
buf_.EnsureCapacity(size);
auto dest = buf_.AppendBuffer().first(size);
uint64_t read = 0;
SET_OR_UNEXPECT(source_->Read(dest), read);
SET_OR_UNEXPECT(source->Read(dest), read);
buf_.CommitWrite(read);
if (read != size)
@ -117,16 +120,16 @@ io::Result<size_t> JournalReader::ReadString() {
return size;
}
std::error_code JournalReader::Read(CmdArgVec* vec) {
std::error_code JournalReader::Read(io::Source* source, CmdArgVec* vec) {
buf_.ConsumeInput(buf_.InputBuffer().size());
size_t size = 0;
SET_OR_RETURN(ReadU64(), size);
SET_OR_RETURN(ReadU64(source), size);
vec->resize(size);
for (auto& span : *vec) {
size_t len;
SET_OR_RETURN(ReadString(), len);
SET_OR_RETURN(ReadString(source), len);
span = MutableSlice{nullptr, len};
}
@ -141,22 +144,22 @@ std::error_code JournalReader::Read(CmdArgVec* vec) {
return std::error_code{};
}
io::Result<journal::ParsedEntry> JournalReader::ReadEntry() {
io::Result<journal::ParsedEntry> JournalReader::ReadEntry(io::Source* source) {
uint8_t opcode;
SET_OR_UNEXPECT(ReadU8(), opcode);
SET_OR_UNEXPECT(ReadU8(source), opcode);
journal::ParsedEntry entry{static_cast<journal::Op>(opcode), dbid_};
switch (entry.opcode) {
case journal::Op::VAL:
SET_OR_UNEXPECT(ReadU64(), entry.txid);
case journal::Op::COMMAND:
SET_OR_UNEXPECT(ReadU64(source), entry.txid);
entry.payload = CmdArgVec{};
if (auto ec = Read(&*entry.payload); ec)
if (auto ec = Read(source, &*entry.payload); ec)
return make_unexpected(ec);
break;
case journal::Op::SELECT:
SET_OR_UNEXPECT(ReadU16(), dbid_);
return ReadEntry();
SET_OR_UNEXPECT(ReadU16(source), dbid_);
return ReadEntry(source);
default:
break;
};