Skip to content

Commit

Permalink
feat: add RemoveFlag/RemoveSegment API to memory store (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld authored Nov 29, 2023
1 parent 5eac208 commit 9ce95b6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std::string const& MemoryStore::Identity() const {
return description_;
}

void MemoryStore::Init(launchdarkly::data_model::SDKDataSet dataSet) {
void MemoryStore::Init(data_model::SDKDataSet dataSet) {
std::lock_guard lock{data_mutex_};
initialized_ = true;
flags_.clear();
Expand Down Expand Up @@ -72,4 +72,14 @@ void MemoryStore::Upsert(std::string const& key,
std::make_shared<data_model::SegmentDescriptor>(std::move(segment));
}

bool MemoryStore::RemoveFlag(std::string const& key) {
std::lock_guard lock{data_mutex_};
return flags_.erase(key) == 1;
}

bool MemoryStore::RemoveSegment(std::string const& key) {
std::lock_guard lock{data_mutex_};
return segments_.erase(key) == 1;
}

} // namespace launchdarkly::server_side::data_components
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ class MemoryStore final : public data_interfaces::IStore,

[[nodiscard]] std::string const& Identity() const override;

void Init(launchdarkly::data_model::SDKDataSet dataSet) override;
void Init(data_model::SDKDataSet dataSet) override;

void Upsert(std::string const& key,
data_model::FlagDescriptor flag) override;

void Upsert(std::string const& key,
data_model::SegmentDescriptor segment) override;

bool RemoveFlag(std::string const& key);

bool RemoveSegment(std::string const& key);

MemoryStore() = default;
~MemoryStore() override = default;

Expand Down
41 changes: 41 additions & 0 deletions libs/server-sdk/tests/memory_store_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,44 @@ TEST(MemoryStoreTest, OriginalFlagValidAfterUpsertOfFlag) {
EXPECT_EQ(std::string("potato"),
fetched_flag_after->item->variations[0].AsString());
}

TEST(MemoryStoreTest, CanDeleteExistingSegment) {
MemoryStore store;

Segment segment_a;
segment_a.version = 1;
segment_a.key = "segmentA";

store.Init(SDKDataSet{
std::unordered_map<std::string, FlagDescriptor>{},
std::unordered_map<std::string, SegmentDescriptor>{
{"segmentA", SegmentDescriptor(segment_a)}},
});

ASSERT_TRUE(store.GetSegment("segmentA"));
ASSERT_TRUE(store.RemoveSegment("segmentA"));

ASSERT_FALSE(store.GetSegment("segmentA"));
ASSERT_FALSE(store.RemoveSegment("segmentA"));
}

TEST(MemoryStoreTest, CanDeleteExistingFlag) {
MemoryStore store;

Flag flag_a;
flag_a.version = 1;
flag_a.key = "flagA";
flag_a.variations = std::vector<Value>{"potato", "ham"};

store.Init(SDKDataSet{
std::unordered_map<std::string, FlagDescriptor>{
{"flagA", FlagDescriptor(flag_a)}},
std::unordered_map<std::string, SegmentDescriptor>(),
});

ASSERT_TRUE(store.GetFlag("flagA"));
ASSERT_TRUE(store.RemoveFlag("flagA"));

ASSERT_FALSE(store.GetFlag("flagA"));
ASSERT_FALSE(store.RemoveFlag("flagA"));
}

0 comments on commit 9ce95b6

Please sign in to comment.