aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/containers/expiring_set.h
blob: f8a1e336309e73a32b4e1b0745805e9b10d1ccc0 (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
#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 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_