C++ Client-Side SDK
LaunchDarkly SDK
flag_persistence.hpp
1 #pragma once
2 
3 #include <chrono>
4 #include <functional>
5 #include <mutex>
6 #include <optional>
7 #include <unordered_map>
8 
9 #include "../data_sources/data_source_update_sink.hpp"
10 #include "context_index.hpp"
11 #include "flag_store.hpp"
12 
13 #include <launchdarkly/context.hpp>
14 #include <launchdarkly/logging/logger.hpp>
15 #include <launchdarkly/persistence/persistence.hpp>
16 
17 namespace launchdarkly::client_side::flag_manager {
18 
19 std::string PersistenceEncodeKey(std::string const& input);
20 
28  public:
29  using TimeStampsource =
30  std::function<std::chrono::time_point<std::chrono::system_clock>()>;
31 
33  std::string const& sdk_key,
35  FlagStore& flag_store,
36  std::shared_ptr<IPersistence> persistence,
37  Logger& logger,
38  std::size_t max_cached_contexts,
39  TimeStampsource time_stamper = []() {
40  return std::chrono::system_clock::now();
41  });
42 
43  void Init(Context const& context,
44  std::unordered_map<std::string, ItemDescriptor> data) override;
45 
46  void Upsert(Context const& context,
47  std::string key,
48  ItemDescriptor item) override;
49 
50  void Apply(Context const& context,
51  FlagChangeSet change_set,
52  bool from_cache) override;
53 
54  void LoadCached(Context const& context);
55 
62  [[nodiscard]] std::optional<std::unordered_map<std::string, ItemDescriptor>>
63  ReadCached(Context const& context);
64 
73  [[nodiscard]] std::optional<
74  std::chrono::time_point<std::chrono::system_clock>>
75  ReadFreshness(Context const& context);
76 
77  private:
78  inline static std::string global_namespace_ = "LaunchDarkly";
79  inline static std::string index_key_ = "ContextIndex";
80  inline static std::string freshness_key_ = "ContextFreshness";
81 
82  Logger& logger_;
83  std::size_t max_cached_contexts_;
84 
85  IDataSourceUpdateSink& sink_;
86  FlagStore& flag_store_;
87 
88  // Serializes the read-modify-write of the stored index and flag data, so
89  // that two contexts being cached at once cannot lose an index entry.
90  mutable std::recursive_mutex persistence_mutex_;
91  std::shared_ptr<IPersistence> persistence_;
92 
93  std::string environment_namespace_;
94  TimeStampsource time_stamper_;
95 
96  void StoreCache(std::string const& context_id);
97 
98  // Records that the service just confirmed this context's data is current.
99  void RecordFreshness(Context const& context);
100 
101  // Reads the stored index at the given key, or an empty index if none is
102  // stored. Must be called with persistence_mutex_ held.
103  ContextIndex ReadIndexAt(std::string const& key);
104 };
105 
106 } // namespace launchdarkly::client_side::flag_manager
Definition: context.hpp:29
Definition: data_source_update_sink.hpp:38
void Apply(Context const &context, FlagChangeSet change_set, bool from_cache) override
Definition: flag_persistence.cpp:56
std::optional< std::chrono::time_point< std::chrono::system_clock > > ReadFreshness(Context const &context)
Definition: flag_persistence.cpp:162
std::optional< std::unordered_map< std::string, ItemDescriptor > > ReadCached(Context const &context)
Definition: flag_persistence.cpp:76