Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace launchdarkly::server_side::data_components {

void ExpirationTracker::Add(std::string const& key,
ExpirationTracker::TimePoint expiration) {
void ExpirationTracker::Add(std::string const& key, TimePoint expiration) {
unscoped_.insert({key, expiration});
}

Expand All @@ -15,18 +14,18 @@ void ExpirationTracker::Remove(std::string const& key) {

ExpirationTracker::TrackState ExpirationTracker::State(
std::string const& key,
ExpirationTracker::TimePoint current_time) const {
TimePoint current_time) const {
auto item = unscoped_.find(key);
if (item != unscoped_.end()) {
return State(item->second, current_time);
}

return ExpirationTracker::TrackState::kNotTracked;
return TrackState::kNotTracked;
}

void ExpirationTracker::Add(DataKind kind,
std::string const& key,
ExpirationTracker::TimePoint expiration) {
TimePoint expiration) {
scoped_.Set(kind, key, expiration);
}

Expand All @@ -37,33 +36,31 @@ void ExpirationTracker::Remove(DataKind kind, std::string const& key) {
ExpirationTracker::TrackState ExpirationTracker::State(
DataKind kind,
std::string const& key,
ExpirationTracker::TimePoint current_time) const {
TimePoint current_time) const {
auto expiration = scoped_.Get(kind, key);
if (expiration.has_value()) {
return State(expiration.value(), current_time);
}
return ExpirationTracker::TrackState::kNotTracked;
return TrackState::kNotTracked;
}

void ExpirationTracker::Clear() {
scoped_.Clear();
unscoped_.clear();
}
std::vector<std::pair<std::optional<DataKind>, std::string>>
ExpirationTracker::Prune(ExpirationTracker::TimePoint current_time) {
ExpirationTracker::Prune(TimePoint current_time) {
std::vector<std::pair<std::optional<DataKind>, std::string>> pruned;

// Determine everything to be pruned.
for (auto const& item : unscoped_) {
if (State(item.second, current_time) ==
ExpirationTracker::TrackState::kStale) {
if (State(item.second, current_time) == TrackState::kStale) {
pruned.emplace_back(std::nullopt, item.first);
}
}
for (auto const& scope : scoped_) {
for (auto const& item : scope.Data()) {
if (State(item.second, current_time) ==
ExpirationTracker::TrackState::kStale) {
if (State(item.second, current_time) == TrackState::kStale) {
pruned.emplace_back(scope.Kind(), item.first);
}
}
Expand All @@ -79,19 +76,17 @@ ExpirationTracker::Prune(ExpirationTracker::TimePoint current_time) {
}
return pruned;
}
ExpirationTracker::TrackState ExpirationTracker::State(
ExpirationTracker::TimePoint expiration,
ExpirationTracker::TimePoint current_time) {
ExpirationTracker::TrackState ExpirationTracker::State(TimePoint expiration,
TimePoint current_time) {
if (expiration > current_time) {
return ExpirationTracker::TrackState::kFresh;
return TrackState::kFresh;
}
return ExpirationTracker::TrackState::kStale;
return TrackState::kStale;
}

void ExpirationTracker::ScopedTtls::Set(
DataKind kind,
std::string const& key,
ExpirationTracker::TimePoint expiration) {
void ExpirationTracker::ScopedTtls::Set(DataKind kind,
std::string const& key,
TimePoint expiration) {
data_[static_cast<std::underlying_type_t<DataKind>>(kind)].Data().insert(
{key, expiration});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ class ExpirationTracker {
* @param key The key to track.
* @param expiration The time that the key expires.
*/
void Add(data_components::DataKind kind,
std::string const& key,
TimePoint expiration);
void Add(DataKind kind, std::string const& key, TimePoint expiration);

/**
* Remove a scoped key from the tracker.
*
* @param kind The scope (kind) of the key.
* @param key The key to stop tracking.
*/
void Remove(data_components::DataKind kind, std::string const& key);
void Remove(DataKind kind, std::string const& key);

/**
* Check the state of a scoped key.
Expand All @@ -87,7 +85,7 @@ class ExpirationTracker {
* @param key The key to check.
* @return The state of the key.
*/
TrackState State(data_components::DataKind kind,
TrackState State(DataKind kind,
std::string const& key,
TimePoint current_time) const;

Expand All @@ -110,9 +108,7 @@ class ExpirationTracker {

TtlMap unscoped_;

static ExpirationTracker::TrackState State(
ExpirationTracker::TimePoint expiration,
ExpirationTracker::TimePoint current_time);
static TrackState State(TimePoint expiration, TimePoint current_time);

class ScopedTtls {
public:
Expand Down