CCF
Loading...
Searching...
No Matches
strategy.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/pal/locking.h"
7#include "ccf/tx_id.h"
8
9#include <optional>
10#include <string>
11
12namespace ccf::indexing
13{
24 {
25 const std::string name;
26
27 public:
28 Strategy(const std::string& name) : name(name) {}
29 virtual ~Strategy() = default;
30
31 std::string get_name() const
32 {
33 return name;
34 }
35
42 const ccf::TxID& tx_id, const ccf::kv::ReadOnlyStorePtr& store) = 0;
43
44 virtual void tick() {}
45
49 virtual std::optional<ccf::SeqNo> next_requested() = 0;
50
51 virtual nlohmann::json describe()
52 {
53 auto j = nlohmann::json::object();
54 j["name"] = get_name();
55
56 const auto nr = next_requested();
57 if (nr.has_value())
58 {
59 j["next_requested_seqno"] = *nr;
60 }
61
62 return j;
63 }
64 };
65
66 using StrategyPtr = std::shared_ptr<Strategy>;
67
68 template <typename Base>
69 class LazyStrategy : public Base
70 {
71 protected:
74
75 public:
76 using Base::Base;
77
78 std::optional<ccf::SeqNo> next_requested() override
79 {
80 const auto base = Base::next_requested();
81 if (base.has_value())
82 {
83 std::lock_guard<ccf::pal::Mutex> guard(lock);
84 if (*base <= max_requested_seqno)
85 {
86 return base;
87 }
88 }
89
90 return std::nullopt;
91 }
92
94 {
95 std::lock_guard<ccf::pal::Mutex> guard(lock);
96 if (to_txid.seqno > max_requested_seqno)
97 {
98 max_requested_seqno = to_txid.seqno;
99 }
100 }
101 };
102}
Definition strategy.h:70
ccf::pal::Mutex lock
Definition strategy.h:72
std::optional< ccf::SeqNo > next_requested() override
Definition strategy.h:78
ccf::SeqNo max_requested_seqno
Definition strategy.h:73
void extend_index_to(ccf::TxID to_txid)
Definition strategy.h:93
Definition strategy.h:24
virtual ~Strategy()=default
Strategy(const std::string &name)
Definition strategy.h:28
virtual void tick()
Definition strategy.h:44
virtual std::optional< ccf::SeqNo > next_requested()=0
virtual void handle_committed_transaction(const ccf::TxID &tx_id, const ccf::kv::ReadOnlyStorePtr &store)=0
std::string get_name() const
Definition strategy.h:31
virtual nlohmann::json describe()
Definition strategy.h:51
Definition indexer_interface.h:14
std::shared_ptr< Strategy > StrategyPtr
Definition strategy.h:66
std::shared_ptr< ReadOnlyStore > ReadOnlyStorePtr
Definition read_only_store.h:23
std::mutex Mutex
Definition locking.h:12
uint64_t SeqNo
Definition tx_id.h:36
Definition tx_id.h:44
SeqNo seqno
Definition tx_id.h:46