diff --git a/tddb/td/db/BinlogKeyValue.h b/tddb/td/db/BinlogKeyValue.h index 26e0a2b3..dda3684e 100644 --- a/tddb/td/db/BinlogKeyValue.h +++ b/tddb/td/db/BinlogKeyValue.h @@ -29,6 +29,7 @@ #include namespace td { + template class BinlogKeyValue : public KeyValueSyncInterface { public: @@ -234,17 +235,21 @@ class BinlogKeyValue : public KeyValueSyncInterface { RwMutex rw_mutex_; int32 magic_ = magic; }; + template <> inline void BinlogKeyValue::add_event(uint64 seq_no, BufferSlice &&event) { binlog_->add_raw_event(std::move(event), BinlogDebugInfo{__FILE__, __LINE__}); } + template <> inline void BinlogKeyValue::force_sync(Promise<> &&promise) { binlog_->sync(); promise.set_value(Unit()); } + template <> inline void BinlogKeyValue::lazy_sync(Promise<> &&promise) { force_sync(std::move(promise)); } + } // namespace td diff --git a/tddb/td/db/DbKey.h b/tddb/td/db/DbKey.h index 633e3b61..0c857660 100644 --- a/tddb/td/db/DbKey.h +++ b/tddb/td/db/DbKey.h @@ -10,6 +10,7 @@ #include "td/utils/Slice.h" namespace td { + class DbKey { public: enum Type { Empty, RawKey, Password }; @@ -49,4 +50,5 @@ class DbKey { Type type_{Empty}; string data_; }; + } // namespace td diff --git a/tddb/td/db/binlog/BinlogEvent.cpp b/tddb/td/db/binlog/BinlogEvent.cpp index 3cadbda0..fcce7b86 100644 --- a/tddb/td/db/binlog/BinlogEvent.cpp +++ b/tddb/td/db/binlog/BinlogEvent.cpp @@ -6,9 +6,13 @@ // #include "td/db/binlog/BinlogEvent.h" +#include "td/utils/crypto.h" +#include "td/utils/misc.h" #include "td/utils/tl_parsers.h" +#include "td/utils/tl_storers.h" namespace td { + int32 VERBOSITY_NAME(binlog) = VERBOSITY_NAME(DEBUG) + 8; Status BinlogEvent::init(BufferSlice &&raw_event, bool check_crc) { @@ -47,4 +51,23 @@ Status BinlogEvent::validate() const { return event.init(raw_event_.clone(), true); } +BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { + auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE}; + + TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin()); + tl_storer.store_int(narrow_cast(raw_event.size())); + tl_storer.store_long(id); + tl_storer.store_int(type); + tl_storer.store_int(flags); + tl_storer.store_long(0); + + CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE); + tl_storer.store_storer(storer); + + CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE); + tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE))); + + return raw_event; +} + } // namespace td diff --git a/tddb/td/db/binlog/BinlogEvent.h b/tddb/td/db/binlog/BinlogEvent.h index 8b1f2d9b..7fd9948c 100644 --- a/tddb/td/db/binlog/BinlogEvent.h +++ b/tddb/td/db/binlog/BinlogEvent.h @@ -8,17 +8,15 @@ #include "td/utils/buffer.h" #include "td/utils/common.h" -#include "td/utils/crypto.h" #include "td/utils/format.h" #include "td/utils/logging.h" -#include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/Storer.h" #include "td/utils/StringBuilder.h" -#include "td/utils/tl_storers.h" namespace td { + struct EmptyStorerImpl { EmptyStorerImpl() { } @@ -54,7 +52,6 @@ inline StringBuilder &operator<<(StringBuilder &sb, const BinlogDebugInfo &info) return sb << "[" << info.file << ":" << info.line << "]"; } -// TODO: smaller BinlogEvent struct BinlogEvent { int64 offset_; @@ -102,6 +99,7 @@ struct BinlogEvent { Status init(BufferSlice &&raw_event, bool check_crc = true) TD_WARN_UNUSED_RESULT; static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer); + std::string public_to_string() const { return PSTRING() << "LogEvent[" << tag("id", format::as_hex(id_)) << tag("type", type_) << tag("flags", flags_) << tag("data", data_.size()) << "]" << debug_info_; @@ -116,23 +114,4 @@ inline StringBuilder &operator<<(StringBuilder &sb, const BinlogEvent &event) { << event.debug_info_; } -// Implementation -inline BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { - auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE}; - - TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin()); - tl_storer.store_int(narrow_cast(raw_event.size())); - tl_storer.store_long(id); - tl_storer.store_int(type); - tl_storer.store_int(flags); - tl_storer.store_long(0); - - CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE); - tl_storer.store_storer(storer); - - CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE); - tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE))); - - return raw_event; -} } // namespace td