aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/storage.cpp
blob: 402ec63c939e9c28a8c0ddc075b235a3e8fea1cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "storage.h"

#include <typeinfo>

namespace NBus {
    namespace NPrivate {
        TTimedMessages::TTimedMessages() {
        }

        TTimedMessages::~TTimedMessages() {
            Y_ABORT_UNLESS(Items.empty());
        }

        void TTimedMessages::PushBack(TNonDestroyingAutoPtr<TBusMessage> m) {
            TItem i;
            i.Message.Reset(m.Release());
            Items.push_back(i);
        }

        TNonDestroyingAutoPtr<TBusMessage> TTimedMessages::PopFront() {
            TBusMessage* r = nullptr;
            if (!Items.empty()) {
                r = Items.front()->Message.Release();
                Items.pop_front();
            }
            return r;
        }

        bool TTimedMessages::Empty() const {
            return Items.empty();
        }

        size_t TTimedMessages::Size() const {
            return Items.size();
        }

        void TTimedMessages::Timeout(TInstant before, TMessagesPtrs* r) {
            // shortcut
            if (before == TInstant::Max()) {
                Clear(r);
                return;
            }

            while (!Items.empty()) {
                TItem& i = *Items.front();
                if (TInstant::MilliSeconds(i.Message->GetHeader()->SendTime) > before) {
                    break;
                }
                r->push_back(i.Message.Release());
                Items.pop_front();
            }
        }

        void TTimedMessages::Clear(TMessagesPtrs* r) {
            while (!Items.empty()) {
                r->push_back(Items.front()->Message.Release());
                Items.pop_front();
            }
        }

        TSyncAckMessages::TSyncAckMessages() {
            KeyToMessage.set_empty_key(0);
            KeyToMessage.set_deleted_key(1);
        }

        TSyncAckMessages::~TSyncAckMessages() {
            Y_ABORT_UNLESS(KeyToMessage.empty());
            Y_ABORT_UNLESS(TimedItems.empty());
        }

        void TSyncAckMessages::Push(TBusMessagePtrAndHeader& m) {
            // Perform garbage collection if `TimedMessages` contain too many junk data
            if (TimedItems.size() > 1000 && TimedItems.size() > KeyToMessage.size() * 4) {
                Gc();
            }

            TValue value = {m.MessagePtr.Release()};

            std::pair<TKeyToMessage::iterator, bool> p = KeyToMessage.insert(TKeyToMessage::value_type(m.Header.Id, value));
            Y_ABORT_UNLESS(p.second, "non-unique id; %s", value.Message->Describe().data());

            TTimedItem item = {m.Header.Id, m.Header.SendTime};
            TimedItems.push_back(item);
        }

        TBusMessage* TSyncAckMessages::Pop(TBusKey id) {
            TKeyToMessage::iterator it = KeyToMessage.find(id);
            if (it == KeyToMessage.end()) {
                return nullptr;
            }
            TValue v = it->second;
            KeyToMessage.erase(it);

            // `TimedMessages` still contain record about this message

            return v.Message;
        }

        void TSyncAckMessages::Timeout(TInstant before, TMessagesPtrs* r) {
            // shortcut
            if (before == TInstant::Max()) {
                Clear(r);
                return;
            }

            Y_ASSERT(r->empty());

            while (!TimedItems.empty()) {
                TTimedItem i = TimedItems.front();
                if (TInstant::MilliSeconds(i.SendTime) > before) {
                    break;
                }

                TKeyToMessage::iterator itMessage = KeyToMessage.find(i.Key);

                if (itMessage != KeyToMessage.end()) {
                    r->push_back(itMessage->second.Message);
                    KeyToMessage.erase(itMessage);
                }

                TimedItems.pop_front();
            }
        }

        void TSyncAckMessages::Clear(TMessagesPtrs* r) {
            for (TKeyToMessage::const_iterator i = KeyToMessage.begin(); i != KeyToMessage.end(); ++i) {
                r->push_back(i->second.Message);
            }

            KeyToMessage.clear();
            TimedItems.clear();
        }

        void TSyncAckMessages::Gc() {
            TDeque<TTimedItem> tmp;

            for (auto& timedItem : TimedItems) {
                if (KeyToMessage.find(timedItem.Key) == KeyToMessage.end()) {
                    continue;
                }
                tmp.push_back(timedItem);
            }

            TimedItems.swap(tmp);
        }

        void TSyncAckMessages::RemoveAll(const TMessagesPtrs& messages) {
            for (auto message : messages) {
                TKeyToMessage::iterator it = KeyToMessage.find(message->GetHeader()->Id);
                Y_ABORT_UNLESS(it != KeyToMessage.end(), "delete non-existent message");
                KeyToMessage.erase(it);
            }
        }

        void TSyncAckMessages::DumpState() {
            Cerr << TimedItems.size() << Endl;
            Cerr << KeyToMessage.size() << Endl;
        }

    }
}