Make MessageId layout private.

GitOrigin-RevId: a123b1bd7fe02110e699878c6c60dcc41b90c960
This commit is contained in:
levlam 2019-11-26 03:32:05 +03:00
parent b4e45a2852
commit 9da6ecaf1f
3 changed files with 28 additions and 13 deletions

View File

@ -63,15 +63,14 @@ class MessageId {
int64 id = 0;
static constexpr int32 SERVER_ID_SHIFT = 20;
friend StringBuilder &operator<<(StringBuilder &string_builder, MessageId message_id);
public:
static constexpr int32 TYPE_MASK = (1 << 3) - 1;
static constexpr int32 FULL_TYPE_MASK = (1 << SERVER_ID_SHIFT) - 1;
static constexpr int32 TYPE_YET_UNSENT = 1;
static constexpr int32 TYPE_LOCAL = 2;
friend StringBuilder &operator<<(StringBuilder &string_builder, MessageId message_id);
public:
MessageId() = default;
explicit MessageId(ServerMessageId server_message_id)
@ -152,6 +151,21 @@ class MessageId {
return MessageId((id + FULL_TYPE_MASK) & ~FULL_TYPE_MASK);
}
MessageId get_next_message_id(MessageType type) const {
switch (type) {
case MessageType::Server:
return get_next_server_message_id();
case MessageType::Local:
return MessageId(((id + TYPE_MASK + 1 - TYPE_LOCAL) & ~TYPE_MASK) + TYPE_LOCAL);
case MessageType::YetUnsent:
return MessageId(((id + TYPE_MASK + 1 - TYPE_YET_UNSENT) & ~TYPE_MASK) + TYPE_YET_UNSENT);
case MessageType::None:
default:
UNREACHABLE();
return MessageId();
}
}
bool operator==(const MessageId &other) const {
return id == other.id;
}

View File

@ -21853,23 +21853,22 @@ void MessagesManager::on_send_message_fail(int64 random_id, Status error) {
fail_send_message(full_message_id, error_code, error_message);
}
MessageId MessagesManager::get_next_message_id(Dialog *d, int32 type) {
MessageId MessagesManager::get_next_message_id(Dialog *d, MessageType type) {
CHECK(d != nullptr);
int64 last =
std::max({d->last_message_id.get(), d->last_new_message_id.get(), d->last_database_message_id.get(),
d->last_assigned_message_id.get(), d->last_clear_history_message_id.get(),
d->deleted_last_message_id.get(), d->max_unavailable_message_id.get(), d->max_added_message_id.get()});
if (last < d->last_read_inbox_message_id.get() &&
d->last_read_inbox_message_id.get() <= (d->last_new_message_id.get() | MessageId::FULL_TYPE_MASK)) {
d->last_read_inbox_message_id.get() < d->last_new_message_id.get_next_server_message_id().get()) {
last = d->last_read_inbox_message_id.get();
}
if (last < d->last_read_outbox_message_id.get() &&
d->last_read_outbox_message_id.get() <= (d->last_new_message_id.get() | MessageId::FULL_TYPE_MASK)) {
d->last_read_outbox_message_id.get() < d->last_new_message_id.get_next_server_message_id().get()) {
last = d->last_read_outbox_message_id.get();
}
int64 base = (last + MessageId::TYPE_MASK + 1) & ~MessageId::TYPE_MASK;
d->last_assigned_message_id = MessageId(base + type);
d->last_assigned_message_id = MessageId(last).get_next_message_id(type);
if (d->last_assigned_message_id.get() > MessageId::max().get()) {
LOG(FATAL) << "Force restart because of message_id overflow: " << d->last_assigned_message_id;
}
@ -21878,11 +21877,11 @@ MessageId MessagesManager::get_next_message_id(Dialog *d, int32 type) {
}
MessageId MessagesManager::get_next_yet_unsent_message_id(Dialog *d) {
return get_next_message_id(d, MessageId::TYPE_YET_UNSENT);
return get_next_message_id(d, MessageType::YetUnsent);
}
MessageId MessagesManager::get_next_local_message_id(Dialog *d) {
return get_next_message_id(d, MessageId::TYPE_LOCAL);
return get_next_message_id(d, MessageType::Local);
}
void MessagesManager::fail_send_message(FullMessageId full_message_id, int error_code, const string &error_message) {
@ -21907,10 +21906,12 @@ void MessagesManager::fail_send_message(FullMessageId full_message_id, int error
// dump_debug_message_op(d, 5);
}
auto new_message_id = MessageId(old_message_id.get() - MessageId::TYPE_YET_UNSENT + MessageId::TYPE_LOCAL);
auto new_message_id = old_message_id.get_next_message_id(MessageType::Local); // trying to not change message place
if (get_message_force(d, new_message_id, "fail_send_message") != nullptr ||
d->deleted_message_ids.count(new_message_id) || new_message_id.get() <= d->last_clear_history_message_id.get()) {
new_message_id = get_next_local_message_id(d);
} else if (new_message_id.get() > d->last_assigned_message_id.get()) {
d->last_assigned_message_id = new_message_id;
}
message->message_id = new_message_id;

View File

@ -2237,7 +2237,7 @@ class MessagesManager : public Actor {
Dialog *get_service_notifications_dialog();
static MessageId get_next_message_id(Dialog *d, int32 type);
static MessageId get_next_message_id(Dialog *d, MessageType type);
static MessageId get_next_local_message_id(Dialog *d);