C++ Server-Side SDK
LaunchDarkly SDK
memory_store.hpp
1 #pragma once
2 
3 #include "../../data_interfaces/destination/itransactional_destination.hpp"
4 #include "../../data_interfaces/item_change.hpp"
5 #include "../../data_interfaces/store/istore.hpp"
6 
7 #include <launchdarkly/data_model/change_set.hpp>
8 
9 #include <memory>
10 #include <mutex>
11 #include <string>
12 #include <unordered_map>
13 
14 namespace launchdarkly::server_side::data_components {
15 
16 class MemoryStore final : public data_interfaces::IStore,
18  public:
19  [[nodiscard]] std::shared_ptr<data_model::FlagDescriptor> GetFlag(
20  std::string const& key) const override;
21 
22  [[nodiscard]] std::shared_ptr<data_model::SegmentDescriptor> GetSegment(
23  std::string const& key) const override;
24 
25  [[nodiscard]] std::
26  unordered_map<std::string, std::shared_ptr<data_model::FlagDescriptor>>
27  AllFlags() const override;
28 
29  [[nodiscard]] std::unordered_map<
30  std::string,
31  std::shared_ptr<data_model::SegmentDescriptor>>
32  AllSegments() const override;
33 
34  [[nodiscard]] bool Initialized() const override;
35 
36  [[nodiscard]] std::string const& Identity() const override;
37 
38  void Init(data_model::SDKDataSet dataSet) override;
39 
40  void Upsert(std::string const& key,
41  data_model::FlagDescriptor flag) override;
42 
43  void Upsert(std::string const& key,
44  data_model::SegmentDescriptor segment) override;
45 
46  bool RemoveFlag(std::string const& key);
47 
48  bool RemoveSegment(std::string const& key);
49 
50  void Apply(data_model::ChangeSet<data_interfaces::ChangeSetData> changeSet)
51  override;
52 
53  MemoryStore() = default;
54  ~MemoryStore() override = default;
55 
56  MemoryStore(MemoryStore const& item) = delete;
57  MemoryStore(MemoryStore&& item) = delete;
58  MemoryStore& operator=(MemoryStore const&) = delete;
59  MemoryStore& operator=(MemoryStore&&) = delete;
60 
61  private:
62  static inline std::string const description_ = "memory";
63  std::unordered_map<std::string, std::shared_ptr<data_model::FlagDescriptor>>
64  flags_;
65  std::unordered_map<std::string,
66  std::shared_ptr<data_model::SegmentDescriptor>>
67  segments_;
68  bool initialized_ = false;
69  mutable std::mutex data_mutex_;
70 };
71 
72 } // namespace launchdarkly::server_side::data_components
std::unordered_map< std::string, std::shared_ptr< data_model::SegmentDescriptor > > AllSegments() const override
Get a map of all segments.
Definition: memory_store.cpp:34
void Init(data_model::SDKDataSet dataSet) override
Initialize the destination with a base set of data.
Definition: memory_store.cpp:48
std::unordered_map< std::string, std::shared_ptr< data_model::FlagDescriptor > > AllFlags() const override
Get a map of all flags.
Definition: memory_store.cpp:28
std::shared_ptr< data_model::SegmentDescriptor > GetSegment(std::string const &key) const override
Get the segment named by key. Returns nullptr if no such flag exists.
Definition: memory_store.cpp:17
std::shared_ptr< data_model::FlagDescriptor > GetFlag(std::string const &key) const override
Get the flag named by key. Returns nullptr if no such flag exists.
Definition: memory_store.cpp:7
std::string const & Identity() const override
Definition: memory_store.cpp:44
void Upsert(std::string const &key, data_model::FlagDescriptor flag) override
Upsert a flag named by key.
Definition: memory_store.cpp:64
bool Initialized() const override
Definition: memory_store.cpp:39
IStore provides shared ownership of flag and segment domain objects.
Definition: istore.hpp:15