aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/containers/expiring_set.h
blob: 44e9d7e07d4051ec9cbd5ac9413d24cf27d3df65 (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
#pragma once

#include <util/generic/hash.h>

#include <util/datetime/base.h>

#include <queue>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

//! Maintains a set of items that expire after a certain time.
template <class TItem, class THash = THash<TItem>, class TEqual = TEqualTo<TItem>>
class TExpiringSet
{
public:
    void SetTtl(TDuration ttl);

    void Insert(TInstant now, const TItem& item);
    template <class TItems>
    void InsertMany(TInstant now, const TItems& items);

    void Remove(const TItem& item);

    void Expire(TInstant now);

    void Clear();

    template <class TItemLike>
    bool Contains(const TItemLike& item) const;

    int GetSize() const;

private:
    TDuration Ttl_;

    struct TItemPack
    {
        std::vector<TItem> Items;
        TInstant Deadline;

        bool operator<(const TItemPack& other) const;
    };

    THashMap<TItem, TInstant, THash, TEqual> ItemToDeadline_;
    std::priority_queue<TItemPack> ExpirationQueue_;
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define EXPIRING_SET_INL_H_
#include "expiring_set-inl.h"
#undef EXPIRING_SET_INL_H_