Move BinlogEvent::create_raw implementation to cpp.

GitOrigin-RevId: 1e68c4d39044e6563c481d05a1bb1eb341b61c7c
This commit is contained in:
levlam 2018-07-22 01:03:21 +03:00
parent e8f4c31586
commit 3afcad7a5e
4 changed files with 32 additions and 23 deletions

View File

@ -29,6 +29,7 @@
#include <utility>
namespace td {
template <class BinlogT>
class BinlogKeyValue : public KeyValueSyncInterface {
public:
@ -234,17 +235,21 @@ class BinlogKeyValue : public KeyValueSyncInterface {
RwMutex rw_mutex_;
int32 magic_ = magic;
};
template <>
inline void BinlogKeyValue<Binlog>::add_event(uint64 seq_no, BufferSlice &&event) {
binlog_->add_raw_event(std::move(event), BinlogDebugInfo{__FILE__, __LINE__});
}
template <>
inline void BinlogKeyValue<Binlog>::force_sync(Promise<> &&promise) {
binlog_->sync();
promise.set_value(Unit());
}
template <>
inline void BinlogKeyValue<Binlog>::lazy_sync(Promise<> &&promise) {
force_sync(std::move(promise));
}
} // namespace td

View File

@ -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

View File

@ -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<int32>(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

View File

@ -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<int32>(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