|
| 1 | +// Copyright 2022, DragonflyDB authors. All rights reserved. |
| 2 | +// See LICENSE for licensing terms. |
| 3 | +// |
| 4 | + |
| 5 | +#include "server/journal/serializer.h" |
| 6 | + |
| 7 | +#include "base/io_buf.h" |
| 8 | +#include "base/logging.h" |
| 9 | +#include "io/io.h" |
| 10 | +#include "server/common.h" |
| 11 | +#include "server/error.h" |
| 12 | +#include "server/journal/types.h" |
| 13 | +#include "server/main_service.h" |
| 14 | +#include "server/serializer_commons.h" |
| 15 | +#include "server/transaction.h" |
| 16 | + |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +namespace dfly { |
| 20 | + |
| 21 | +JournalWriter::JournalWriter(io::Sink* sink, std::optional<DbIndex> dbid) |
| 22 | + : sink_{sink}, cur_dbid_{dbid} { |
| 23 | +} |
| 24 | + |
| 25 | +error_code JournalWriter::Write(uint64_t v) { |
| 26 | + uint8_t buf[10]; |
| 27 | + unsigned len = WritePackedUInt(v, buf); |
| 28 | + return sink_->Write(io::Bytes{buf, len}); |
| 29 | +} |
| 30 | + |
| 31 | +error_code JournalWriter::Write(std::string_view sv) { |
| 32 | + RETURN_ON_ERR(Write(sv.size())); |
| 33 | + return sink_->Write(io::Buffer(sv)); |
| 34 | +} |
| 35 | + |
| 36 | +error_code JournalWriter::Write(CmdArgList args) { |
| 37 | + RETURN_ON_ERR(Write(args.size())); |
| 38 | + for (auto v : args) |
| 39 | + RETURN_ON_ERR(Write(facade::ToSV(v))); |
| 40 | + |
| 41 | + return std::error_code{}; |
| 42 | +} |
| 43 | + |
| 44 | +error_code JournalWriter::Write(std::pair<std::string_view, ArgSlice> args) { |
| 45 | + auto [cmd, tail_args] = args; |
| 46 | + |
| 47 | + RETURN_ON_ERR(Write(1 + tail_args.size())); |
| 48 | + RETURN_ON_ERR(Write(cmd)); |
| 49 | + for (auto v : tail_args) |
| 50 | + RETURN_ON_ERR(Write(v)); |
| 51 | + |
| 52 | + return std::error_code{}; |
| 53 | +} |
| 54 | + |
| 55 | +error_code JournalWriter::Write(std::monostate) { |
| 56 | + return std::error_code{}; |
| 57 | +} |
| 58 | + |
| 59 | +error_code JournalWriter::Write(const journal::EntryNew& entry) { |
| 60 | + // Check if entry has a new db index and we need to emit a SELECT entry. |
| 61 | + if (entry.opcode != journal::Op::SELECT && (!cur_dbid_ || entry.dbid != *cur_dbid_)) { |
| 62 | + RETURN_ON_ERR(Write(journal::EntryNew{journal::Op::SELECT, entry.dbid})); |
| 63 | + cur_dbid_ = entry.dbid; |
| 64 | + } |
| 65 | + |
| 66 | + RETURN_ON_ERR(Write(uint8_t(entry.opcode))); |
| 67 | + |
| 68 | + switch (entry.opcode) { |
| 69 | + case journal::Op::SELECT: |
| 70 | + return Write(entry.dbid); |
| 71 | + case journal::Op::VAL: |
| 72 | + RETURN_ON_ERR(Write(entry.txid)); |
| 73 | + return std::visit([this](const auto& payload) { return Write(payload); }, entry.payload); |
| 74 | + default: |
| 75 | + break; |
| 76 | + }; |
| 77 | + return std::error_code{}; |
| 78 | +} |
| 79 | + |
| 80 | +JournalReader::JournalReader(io::Source* source, DbIndex dbid) |
| 81 | + : source_{source}, buf_{}, dbid_{dbid} { |
| 82 | +} |
| 83 | + |
| 84 | +template <typename UT> io::Result<UT> ReadPackedUIntTyped(io::Source* source) { |
| 85 | + uint64_t v; |
| 86 | + SET_OR_UNEXPECT(ReadPackedUInt(source), v); |
| 87 | + if (v > std::numeric_limits<UT>::max()) |
| 88 | + return make_unexpected(make_error_code(errc::result_out_of_range)); |
| 89 | + return static_cast<UT>(v); |
| 90 | +} |
| 91 | + |
| 92 | +io::Result<uint8_t> JournalReader::ReadU8() { |
| 93 | + return ReadPackedUIntTyped<uint8_t>(source_); |
| 94 | +} |
| 95 | + |
| 96 | +io::Result<uint16_t> JournalReader::ReadU16() { |
| 97 | + return ReadPackedUIntTyped<uint16_t>(source_); |
| 98 | +} |
| 99 | + |
| 100 | +io::Result<uint64_t> JournalReader::ReadU64() { |
| 101 | + return ReadPackedUIntTyped<uint64_t>(source_); |
| 102 | +} |
| 103 | + |
| 104 | +io::Result<size_t> JournalReader::ReadString() { |
| 105 | + size_t size = 0; |
| 106 | + SET_OR_UNEXPECT(ReadU64(), size); |
| 107 | + |
| 108 | + buf_.EnsureCapacity(size); |
| 109 | + auto dest = buf_.AppendBuffer().first(size); |
| 110 | + uint64_t read = 0; |
| 111 | + SET_OR_UNEXPECT(source_->Read(dest), read); |
| 112 | + |
| 113 | + buf_.CommitWrite(read); |
| 114 | + if (read != size) |
| 115 | + return make_unexpected(std::make_error_code(std::errc::message_size)); |
| 116 | + |
| 117 | + return size; |
| 118 | +} |
| 119 | + |
| 120 | +std::error_code JournalReader::Read(CmdArgVec* vec) { |
| 121 | + buf_.ConsumeInput(buf_.InputBuffer().size()); |
| 122 | + |
| 123 | + size_t size = 0; |
| 124 | + SET_OR_RETURN(ReadU64(), size); |
| 125 | + |
| 126 | + vec->resize(size); |
| 127 | + for (auto& span : *vec) { |
| 128 | + size_t len; |
| 129 | + SET_OR_RETURN(ReadString(), len); |
| 130 | + span = MutableSlice{nullptr, len}; |
| 131 | + } |
| 132 | + |
| 133 | + size_t offset = 0; |
| 134 | + for (auto& span : *vec) { |
| 135 | + size_t len = span.size(); |
| 136 | + auto ptr = buf_.InputBuffer().subspan(offset).data(); |
| 137 | + span = MutableSlice{reinterpret_cast<char*>(ptr), len}; |
| 138 | + offset += len; |
| 139 | + } |
| 140 | + |
| 141 | + return std::error_code{}; |
| 142 | +} |
| 143 | + |
| 144 | +io::Result<journal::ParsedEntry> JournalReader::ReadEntry() { |
| 145 | + uint8_t opcode; |
| 146 | + SET_OR_UNEXPECT(ReadU8(), opcode); |
| 147 | + |
| 148 | + journal::ParsedEntry entry{static_cast<journal::Op>(opcode), dbid_}; |
| 149 | + |
| 150 | + switch (entry.opcode) { |
| 151 | + case journal::Op::VAL: |
| 152 | + SET_OR_UNEXPECT(ReadU64(), entry.txid); |
| 153 | + entry.payload = CmdArgVec{}; |
| 154 | + if (auto ec = Read(&*entry.payload); ec) |
| 155 | + return make_unexpected(ec); |
| 156 | + break; |
| 157 | + case journal::Op::SELECT: |
| 158 | + SET_OR_UNEXPECT(ReadU16(), dbid_); |
| 159 | + return ReadEntry(); |
| 160 | + default: |
| 161 | + break; |
| 162 | + }; |
| 163 | + return entry; |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace dfly |
0 commit comments