1
#pragma once
2

            
3
#include <chrono>
4

            
5
#include "envoy/common/pure.h"
6
#include "envoy/config/typed_config.h"
7
#include "envoy/event/dispatcher.h"
8
#include "envoy/filesystem/filesystem.h"
9
#include "envoy/protobuf/message_validator.h"
10

            
11
#include "absl/strings/string_view.h"
12
#include "absl/types/optional.h"
13

            
14
namespace Envoy {
15

            
16
// A key value store, designed to periodically flush key value pairs to long
17
// term storage (disk or otherwise)
18
class KeyValueStore {
19
public:
20
156
  virtual ~KeyValueStore() = default;
21

            
22
  /**
23
   * Adds or updates a key:value pair in the store.
24
   * @param key supplies a key to add or update.
25
   * @param value supplies the value to set for that key.
26
   * @param ttl optionally specifies a lifetime after which this entry will be removed.
27
   * ttl must be greater than 0.
28
   */
29
  virtual void addOrUpdate(absl::string_view key, absl::string_view value,
30
                           absl::optional<std::chrono::seconds> ttl) PURE;
31

            
32
  /**
33
   * Removes a key:value pair from the store. This is a no-op if the key is not present.
34
   * @param key supplies a key to remove from the store.
35
   */
36
  virtual void remove(absl::string_view key) PURE;
37

            
38
  /**
39
   * Returns the value of the key provided.
40
   * @param key supplies a key to return the value of.
41
   * @return the value, if the key is in the store, absl::nullopt otherwise.
42
   */
43
  virtual absl::optional<absl::string_view> get(absl::string_view key) PURE;
44

            
45
  /**
46
   * Flushes the store to long term storage.
47
   */
48
  virtual void flush() PURE;
49

            
50
  // Returns for the iterate function.
51
  enum class Iterate { Continue, Break };
52

            
53
  /**
54
   * Callback when calling iterate() in a key value store.
55
   * @param key is the key for a given entry
56
   * @param value is the value for a given entry
57
   * @return Iterate::Continue to continue iteration, or Iterate::Break to stop.
58
   */
59
  using ConstIterateCb = std::function<Iterate(const std::string& key, const std::string& value)>;
60

            
61
  /**
62
   * Iterate over a key value store.
63
   * @param cb supplies the iteration callback.
64
   */
65
  virtual void iterate(ConstIterateCb cb) const PURE;
66
};
67

            
68
using KeyValueStorePtr = std::unique_ptr<KeyValueStore>;
69

            
70
// A factory for creating key value stores.
71
class KeyValueStoreFactory : public Envoy::Config::TypedFactory {
72
public:
73
  /**
74
   * Function to create KeyValueStores from the specified config.
75
   * @param cb supplies the key value store configuration
76
   * @param validation_visitor the configuration validator
77
   * @dispatcher the dispatcher for the thread, for flush alarms.
78
   * @file_system the file system.
79
   * @return a new key value store.
80
   */
81
  virtual KeyValueStorePtr createStore(const Protobuf::Message& config,
82
                                       ProtobufMessage::ValidationVisitor& validation_visitor,
83
                                       Event::Dispatcher& dispatcher,
84
                                       Filesystem::Instance& file_system) PURE;
85

            
86
  // @brief the category of the key value store for factory registration.
87
26
  std::string category() const override { return "envoy.common.key_value"; }
88
};
89

            
90
} // namespace Envoy