aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/common/handlermap.hpp
blob: e762ac527f09cfa052ff97557618e228bc6762e0 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "doctypename.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/sequence.h>
#include <vespa/vespalib/stllike/string.h>
#include <map>
#include <vector>

namespace proton {

/**
 * This template defines a mapping from a document type name to a shared pointer
 * to the template argument type.
 */
template <typename T>
class HandlerMap {
private:
    using HandlerSP = typename std::shared_ptr<T>;
    using StdMap = std::map<DocTypeName, HandlerSP>;

    StdMap _handlers;

public:
    /**
     * A snapshot of the currently registered handlers. This
     * implementation simply takes a copy of all the shared pointers
     * in the map to keep the handlers alive. However, the current
     * abstraction allows as to make a snapshot based on bald pointers
     * using event barriers to resolve visibility constraints in the
     * future without changing the external API.
     **/
    class Snapshot : public vespalib::Sequence<T*>
    {
    private:
        std::vector<HandlerSP> _handlers;
        size_t                 _offset;

    public:
        Snapshot() : _handlers(), _offset(0) { }
        Snapshot(const StdMap &map) : _handlers(), _offset(0) {
            _handlers.reserve(map.size());
            for (auto itr : map) {
                _handlers.push_back(itr.second);
            }
        }
        Snapshot(std::vector<HandlerSP> &&handlers) : _handlers(std::move(handlers)), _offset(0) {}
        Snapshot(Snapshot &&) noexcept = default;
        Snapshot & operator = (Snapshot &&) noexcept = default;
        Snapshot(const Snapshot &) = delete;
        Snapshot & operator = (const Snapshot &) = delete;

        bool valid() const override { return (_offset < _handlers.size()); }
        T *get() const override { return _handlers[_offset].get(); }
        void next() override { ++_offset; }
    };

    /**
     * Convenience typedefs.
     */
    typedef typename StdMap::iterator       iterator;
    typedef typename StdMap::const_iterator const_iterator;

    /**
     * Constructs a new instance of this class.
     */
    HandlerMap() = default;

    /**
     * Registers a new handler for the given document type. If another handler
     * was already registered under the same type, this method will return a
     * pointer to that handler.
     *
     * @param docType The document type to register a handler for.
     * @param handler The handler to register.
     * @return The replaced handler, if any.
     */
    HandlerSP
    putHandler(const DocTypeName &docTypeNameVer,
               const HandlerSP &handler)
    {
        if ( ! handler) {
            throw vespalib::IllegalArgumentException(vespalib::make_string(
                            "Handler is null for docType '%s'",
                            docTypeNameVer.toString().c_str()));
        }
        iterator it = _handlers.find(docTypeNameVer);
        if (it == _handlers.end()) {
            _handlers[docTypeNameVer] = handler;
            return HandlerSP();
        }
        HandlerSP ret = it->second;
        it->second = handler;
        return ret;
    }

    /**
     * Returns the handler for the given document type. If no handler was
     * registered, this method returns an empty shared pointer.
     *
     * @param docType The document type whose handler to return.
     * @return The registered handler, if any.
     */
    HandlerSP
    getHandler(const DocTypeName &docTypeNameVer) const
    {
        const_iterator it = _handlers.find(docTypeNameVer);
        if (it != _handlers.end()) {
            return it->second;
        }
        return HandlerSP();
    }

    /**
     * Returns the handler for the given document type. If no handler was
     * registered, this method returns a null pointer.
     *
     * @param docType The document type whose handler to return.
     * @return The registered handler, if any.
     */
    T *
    getHandlerPtr(const DocTypeName &docTypeNameVer) const
    {
        const_iterator it = _handlers.find(docTypeNameVer);
        return (it != _handlers.end()) ? it->second.get() : nullptr;
    }

    bool hasHandler(const HandlerSP &handler) const {
        bool found = false;
        for (const auto &kv : _handlers) {
            if (handler == kv.second) {
                found = true;
                break;
            }
        }
        return found;
    }

    /**
     * Removes and returns the handler for the given document type. If no
     * handler was registered, this method returns an empty shared pointer.
     *
     * @param docType The document type whose handler to remove.
     * @return The removed handler, if any.
     */
    HandlerSP
    removeHandler(const DocTypeName &docTypeNameVer)
    {
        iterator it = _handlers.find(docTypeNameVer);
        if (it == _handlers.end()) {
            return HandlerSP();
        }
        HandlerSP ret = it->second;
        _handlers.erase(it);
        return ret;
    }

    /**
     * Clear all handlers.
     */
    void clear() { _handlers.clear(); }

    /**
     * Create a snapshot of the handlers currently contained in this
     * map and return it as a sequence. The returned sequence will
     * ensure that all object pointers stay valid until it is deleted.
     *
     * @return handler sequence
     **/
    Snapshot snapshot() const { return Snapshot(_handlers); }

// we want to use snapshots rather than direct iteration to reduce locking;
// the below functions should be deprecated when possible.

    iterator begin() { return _handlers.begin(); }
    const_iterator begin() const { return _handlers.begin(); }
    iterator end() { return _handlers.end(); }
    const_iterator end() const { return _handlers.end(); }
    size_t size() const { return _handlers.size(); }
};

} // namespace proton