From f99fb4fc06c4f150e7d22f13e3cf9032aa00a53b Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 20 Dec 2022 16:37:22 +0300 Subject: [PATCH] Delete events from callback before forgetting them in TQueue. --- tddb/td/db/TQueue.cpp | 23 ++++++++++++++++++++++- test/tqueue.cpp | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tddb/td/db/TQueue.cpp b/tddb/td/db/TQueue.cpp index c94d7defa..0e6fd8057 100644 --- a/tddb/td/db/TQueue.cpp +++ b/tddb/td/db/TQueue.cpp @@ -236,6 +236,26 @@ class TQueueImpl final : public TQueue { for (size_t i = 0; i < keep_count; i++) { --end_it; } + + if (callback_ != nullptr) { + vector deleted_log_event_ids; + auto callback_end_it = end_it; + if (keep_count == 0) { + --callback_end_it; + } + for (auto it = q.events.begin(); it != callback_end_it; ++it) { + auto &event = it->second; + if (event.log_event_id != 0) { + deleted_log_event_ids.push_back(event.log_event_id); + event.log_event_id = 0; + } + } + for (auto log_event_id : deleted_log_event_ids) { + callback_->pop(log_event_id); + } + } + auto callback_clear_time = Time::now() - start_time; + for (auto it = q.events.begin(); it != end_it;) { pop(q, queue_id, it, q.tail_id); } @@ -243,7 +263,8 @@ class TQueueImpl final : public TQueue { auto clear_time = Time::now() - start_time; if (clear_time > 0.1) { LOG(WARNING) << "Cleared " << (size - keep_count) << " TQueue events with total size " - << (total_event_length - q.total_event_length) << " in " << clear_time << " seconds"; + << (total_event_length - q.total_event_length) << " in " << clear_time - callback_clear_time + << " seconds and deleted them from callback in " << callback_clear_time << " seconds"; } } diff --git a/test/tqueue.cpp b/test/tqueue.cpp index b1bf94a09..4c71bd738 100644 --- a/test/tqueue.cpp +++ b/test/tqueue.cpp @@ -234,7 +234,7 @@ TEST(TQueue, clear) { td::int32 now = 0; td::vector ids; td::Random::Xorshift128plus rnd(123); - for (size_t i = 0; i < 1000000; i++) { + for (size_t i = 0; i < 100000; i++) { tqueue->push(1, td::string(td::Random::fast(100, 500), 'a'), now + 600000, 0, {}).ensure(); } auto tail_id = tqueue->get_tail(1);