diff options
| author | Per Larsson <[email protected]> | 2021-12-07 11:06:22 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-12-07 11:06:22 +0100 |
| commit | fdcd769a821adb03b3cf79bb873fe46dfeb02e55 (patch) | |
| tree | c1fea6b5a9a94403226bc43874a4c65778ddb5a9 /zenstore | |
| parent | Fixed bug in container GC. (diff) | |
| download | zen-fdcd769a821adb03b3cf79bb873fe46dfeb02e55.tar.xz zen-fdcd769a821adb03b3cf79bb873fe46dfeb02e55.zip | |
Added support for time based eviction policy in structured cache.
Diffstat (limited to 'zenstore')
| -rw-r--r-- | zenstore/gc.cpp | 31 | ||||
| -rw-r--r-- | zenstore/include/zenstore/gc.h | 24 |
2 files changed, 49 insertions, 6 deletions
diff --git a/zenstore/gc.cpp b/zenstore/gc.cpp index b18a577bf..5aff20e7a 100644 --- a/zenstore/gc.cpp +++ b/zenstore/gc.cpp @@ -47,14 +47,17 @@ SaveCompactBinaryObject(const std::filesystem::path& Path, const CbObject& Objec struct GcContext::GcState { - CasChunkSet m_CasChunks; - CasChunkSet m_CidChunks; - bool m_DeletionMode = true; - bool m_ContainerGcEnabled = false; + CasChunkSet m_CasChunks; + CasChunkSet m_CidChunks; + GcClock::TimePoint m_GcTime; + GcClock::Duration m_MaxCacheDuration = std::chrono::hours(24); + bool m_DeletionMode = true; + bool m_ContainerGcEnabled = false; }; -GcContext::GcContext() : m_State(std::make_unique<GcState>()) +GcContext::GcContext(GcClock::TimePoint Time) : m_State(std::make_unique<GcState>()) { + m_State->m_GcTime = Time; } GcContext::~GcContext() @@ -115,6 +118,24 @@ GcContext::SetContainerGcEnabled(bool NewState) m_State->m_ContainerGcEnabled = NewState; } +GcClock::TimePoint +GcContext::Time() const +{ + return m_State->m_GcTime; +} + +GcClock::Duration +GcContext::MaxCacheDuration() const +{ + return m_State->m_MaxCacheDuration; +} + +void +GcContext::MaxCacheDuration(GcClock::Duration Duration) +{ + m_State->m_MaxCacheDuration = Duration; +} + ////////////////////////////////////////////////////////////////////////// GcContributor::GcContributor(CasGc& Gc) : m_Gc(Gc) diff --git a/zenstore/include/zenstore/gc.h b/zenstore/include/zenstore/gc.h index 94f3c32ac..b30c70b1f 100644 --- a/zenstore/include/zenstore/gc.h +++ b/zenstore/include/zenstore/gc.h @@ -25,13 +25,28 @@ class CasGc; class CidStore; struct IoHash; +/** GC clock + */ +class GcClock +{ +public: + using Clock = std::chrono::system_clock; + using TimePoint = Clock::time_point; + using Duration = Clock::duration; + using Tick = int64_t; + + static Tick TickCount() { return Now().time_since_epoch().count(); } + static TimePoint Now() { return Clock::now(); } + static TimePoint TimePointFromTick(const Tick TickCount) { return TimePoint{Duration{TickCount}}; } +}; + /** Garbage Collection context object */ class GcContext { public: - GcContext(); + GcContext(GcClock::TimePoint Time = GcClock::Now()); ~GcContext(); void ContributeCids(std::span<const IoHash> Cid); @@ -48,6 +63,13 @@ public: bool IsContainerGcEnabled() const; void SetContainerGcEnabled(bool NewState); + GcClock::TimePoint Time() const; + + GcClock::Duration MaxCacheDuration() const; + void MaxCacheDuration(GcClock::Duration Duration); + + inline bool Expired(GcClock::Tick TickCount) { return Time() - GcClock::TimePointFromTick(TickCount) > MaxCacheDuration(); } + private: struct GcState; |