aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/common/feedtoken.h
blob: 8ccb4863878a22ce2a4b166436b07dc1c65fca1e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/idestructorcallback.h>
#include <atomic>

namespace storage::spi { class Result; }
namespace proton {

typedef std::unique_ptr<storage::spi::Result> ResultUP;

namespace feedtoken {

/**
 * This class is used by the FeedEngine to encapsulate the necessary information
 * for an IFeedHandler to perform an async reply to an operation. A unique
 * instance of this class is passed to every invokation of the IFeedHandler.
 */
class ITransport {
public:
    virtual ~ITransport() { }
    virtual void send(ResultUP result, bool documentWasFound) = 0;
};

/**
 * This holds the result of the feed operation until it is either failed or acked.
 * Guarantees that the result is propagated back to the invoker via ITransport interface.
 */
class State : public vespalib::IDestructorCallback {
public:
    State(const State &) = delete;
    State & operator = (const State &) = delete;
    State(ITransport & transport);
    ~State() override;
    void fail();
    void setResult(ResultUP result, bool documentWasFound);
    const storage::spi::Result &getResult() { return *_result; }
protected:
    void ack();
private:
    ITransport           &_transport;
    ResultUP              _result;
    bool                  _documentWasFound;
    std::atomic<bool>     _alreadySent;
};

/**
 * This takes ownership ov the transport object, so that it can be used fully asynchronous
 * without invoker needing to hold any state.
 */
class OwningState : public State {
public:
    OwningState(std::shared_ptr<ITransport> transport)
        : State(*transport),
          _owned(std::move(transport))
    {}
    ~OwningState() override;
private:
    std::shared_ptr<ITransport> _owned;
};

inline std::shared_ptr<State>
make(ITransport & latch) {
    return std::make_shared<State>(latch);
}

inline std::shared_ptr<State>
make(std::shared_ptr<ITransport> transport) {
    return std::make_shared<OwningState>(std::move(transport));
}

}

using FeedToken = std::shared_ptr<feedtoken::State>;

} // namespace proton