aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/reference/gid_to_lid_change_handler.h
blob: 07a22fd7ce2bcb838887206de8b77a222cba6d0c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "i_gid_to_lid_change_handler.h"
#include "pending_gid_to_lid_change.h"
#include <vespa/vespalib/stllike/hash_map.h>
#include <vector>
#include <mutex>

namespace searchcorespi { namespace index { struct IThreadService; } }

namespace proton {

/*
 * Class for registering listeners that get notification when
 * gid to lid mapping changes. Also handles notifications which have
 * to be executed by master thread service.
 */
class GidToLidChangeHandler : public std::enable_shared_from_this<GidToLidChangeHandler>,
                              public IGidToLidChangeHandler
{
    using lock_guard = std::lock_guard<std::mutex>;
    using Listeners = std::vector<std::unique_ptr<IGidToLidChangeListener>>;
    struct PendingRemoveEntry {
        SerialNum removeSerialNum;
        SerialNum putSerialNum;
        uint32_t  refCount;

        PendingRemoveEntry(SerialNum removeSerialNum_)
            : removeSerialNum(removeSerialNum_),
              putSerialNum(0),
              refCount(1)
        {
        }

        PendingRemoveEntry()
            : PendingRemoveEntry(0)
        {
        }
    };

    std::mutex _lock;
    Listeners  _listeners;
    bool       _closed;
    vespalib::hash_map<GlobalId, PendingRemoveEntry, GlobalId::hash> _pendingRemove;
    std::vector<PendingGidToLidChange> _pending_changes;

    void notifyPutDone(IDestructorCallbackSP context, GlobalId gid, uint32_t lid);
    void notifyRemove(IDestructorCallbackSP context, GlobalId gid);
public:
    GidToLidChangeHandler();
    ~GidToLidChangeHandler() override;

    void notifyPut(IDestructorCallbackSP context, GlobalId gid, uint32_t lid, SerialNum serial_num) override;
    void notifyPutDone(IDestructorCallbackSP context, GlobalId gid, uint32_t lid, SerialNum serialNum);
    void notifyRemoves(IDestructorCallbackSP context, const std::vector<GlobalId> & gids, SerialNum serialNum) override;
    void notifyRemoveDone(GlobalId gid, SerialNum serialNum);
    std::unique_ptr<IPendingGidToLidChanges> grab_pending_changes() override;

    /**
     * Close handler, further notifications are blocked.
     */
    void close();

    void addListener(std::unique_ptr<IGidToLidChangeListener> listener) override;
    void removeListeners(const vespalib::string &docTypeName, const std::set<vespalib::string> &keepNames) override;
};

} // namespace proton