aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/cache/structuredcachestore.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-04-20 23:38:04 +0200
committerStefan Boberg <[email protected]>2026-04-20 23:38:04 +0200
commit56b485d7d291950a4edff1d25e8594dca891c0ce (patch)
treeaf63ceafea1ac77a4a5d5a0b6e238d4cc76f3137 /src/zenstore/cache/structuredcachestore.cpp
parentUse eastl::deque for queues with many small elements (#991) (diff)
downloadarchived-zen-sb/fix-batch-ptr.tar.xz
archived-zen-sb/fix-batch-ptr.zip
structured cache: return unique_ptr from Create*Batch factoriessb/fix-batch-ptr
- ZenCacheNamespace::CreatePutBatch/CreateGetBatch now return std::unique_ptr so ownership is explicit at the call site - ZenCacheNamespace::PutBatchHandle/GetBatchHandle own their disk-layer handle and clean it up in the destructor, so the public DeletePutBatch/DeleteGetBatch entry points on ZenCacheNamespace are removed - ZenCacheStore::PutBatch/GetBatch store the handle as unique_ptr and their destructors collapse to `= default`; the try/catch wrappers are no longer needed since destruction is driven by the destructors of types that do not throw - Disk-layer public API (CreatePutBatch, DeletePutBatch, etc.) is untouched because its inner batch-handle structs live in the .cpp and exposing them to the header to satisfy std::unique_ptr's completeness requirement would leak implementation details
Diffstat (limited to 'src/zenstore/cache/structuredcachestore.cpp')
-rw-r--r--src/zenstore/cache/structuredcachestore.cpp89
1 files changed, 30 insertions, 59 deletions
diff --git a/src/zenstore/cache/structuredcachestore.cpp b/src/zenstore/cache/structuredcachestore.cpp
index 97b793083..1cb310026 100644
--- a/src/zenstore/cache/structuredcachestore.cpp
+++ b/src/zenstore/cache/structuredcachestore.cpp
@@ -138,15 +138,23 @@ ZenCacheNamespace::~ZenCacheNamespace()
struct ZenCacheNamespace::PutBatchHandle
{
+ PutBatchHandle(ZenCacheDiskLayer& InDiskLayer, ZenCacheDiskLayer::PutBatchHandle* InDiskLayerHandle)
+ : DiskLayer(InDiskLayer)
+ , DiskLayerHandle(InDiskLayerHandle)
+ {
+ }
+ ~PutBatchHandle() noexcept { DiskLayer.DeletePutBatch(DiskLayerHandle); }
+ PutBatchHandle(const PutBatchHandle&) = delete;
+ PutBatchHandle& operator=(const PutBatchHandle&) = delete;
+
+ ZenCacheDiskLayer& DiskLayer;
ZenCacheDiskLayer::PutBatchHandle* DiskLayerHandle = nullptr;
};
-ZenCacheNamespace::PutBatchHandle*
+std::unique_ptr<ZenCacheNamespace::PutBatchHandle>
ZenCacheNamespace::CreatePutBatch(ZenCachePutResultVec_t& OutResult)
{
- ZenCacheNamespace::PutBatchHandle* Handle = new ZenCacheNamespace::PutBatchHandle;
- Handle->DiskLayerHandle = m_DiskLayer.CreatePutBatch(OutResult);
- return Handle;
+ return std::make_unique<ZenCacheNamespace::PutBatchHandle>(m_DiskLayer, m_DiskLayer.CreatePutBatch(OutResult));
}
void
@@ -156,27 +164,27 @@ ZenCacheNamespace::CommitPutBatch(PutBatchHandle* Batch)
m_DiskLayer.CommitPutBatch(Batch->DiskLayerHandle);
}
-void
-ZenCacheNamespace::DeletePutBatch(PutBatchHandle* Batch) noexcept
-{
- ZEN_ASSERT(Batch);
- m_DiskLayer.DeletePutBatch(Batch->DiskLayerHandle);
- delete Batch;
-}
-
struct ZenCacheNamespace::GetBatchHandle
{
- GetBatchHandle(ZenCacheValueVec_t& OutResult) : Results(OutResult) {}
+ GetBatchHandle(ZenCacheDiskLayer& InDiskLayer, ZenCacheValueVec_t& OutResult, ZenCacheDiskLayer::GetBatchHandle* InDiskLayerHandle)
+ : DiskLayer(InDiskLayer)
+ , Results(OutResult)
+ , DiskLayerHandle(InDiskLayerHandle)
+ {
+ }
+ ~GetBatchHandle() noexcept { DiskLayer.DeleteGetBatch(DiskLayerHandle); }
+ GetBatchHandle(const GetBatchHandle&) = delete;
+ GetBatchHandle& operator=(const GetBatchHandle&) = delete;
+
+ ZenCacheDiskLayer& DiskLayer;
ZenCacheValueVec_t& Results;
ZenCacheDiskLayer::GetBatchHandle* DiskLayerHandle = nullptr;
};
-ZenCacheNamespace::GetBatchHandle*
+std::unique_ptr<ZenCacheNamespace::GetBatchHandle>
ZenCacheNamespace::CreateGetBatch(ZenCacheValueVec_t& OutResult)
{
- ZenCacheNamespace::GetBatchHandle* Handle = new ZenCacheNamespace::GetBatchHandle(OutResult);
- Handle->DiskLayerHandle = m_DiskLayer.CreateGetBatch(OutResult);
- return Handle;
+ return std::make_unique<ZenCacheNamespace::GetBatchHandle>(m_DiskLayer, OutResult, m_DiskLayer.CreateGetBatch(OutResult));
}
void
@@ -200,14 +208,6 @@ ZenCacheNamespace::CommitGetBatch(GetBatchHandle* Batch)
}
}
-void
-ZenCacheNamespace::DeleteGetBatch(GetBatchHandle* Batch) noexcept
-{
- ZEN_ASSERT(Batch);
- m_DiskLayer.DeleteGetBatch(Batch->DiskLayerHandle);
- delete Batch;
-}
-
bool
ZenCacheNamespace::Get(std::string_view InBucket, const IoHash& HashKey, ZenCacheValue& OutValue)
{
@@ -560,25 +560,11 @@ ZenCacheStore::PutBatch::Commit()
{
ZEN_MEMSCOPE(GetCacheStoreTag());
ZEN_ASSERT(m_NamespaceBatchHandle);
- m_Store->CommitPutBatch(m_NamespaceBatchHandle);
+ m_Store->CommitPutBatch(m_NamespaceBatchHandle.get());
}
}
-ZenCacheStore::PutBatch::~PutBatch()
-{
- try
- {
- if (m_Store)
- {
- ZEN_ASSERT(m_NamespaceBatchHandle);
- m_Store->DeletePutBatch(m_NamespaceBatchHandle);
- }
- }
- catch (const std::exception& Ex)
- {
- ZEN_ERROR("Exception in cache store when ending batch put operation: '{}'", Ex.what());
- }
-}
+ZenCacheStore::PutBatch::~PutBatch() = default;
ZenCacheStore::GetBatch::GetBatch(ZenCacheStore& CacheStore, std::string_view InNamespace, ZenCacheValueVec_t& OutResult)
: m_CacheStore(CacheStore)
@@ -598,7 +584,7 @@ ZenCacheStore::GetBatch::Commit()
{
ZEN_MEMSCOPE(GetCacheStoreTag());
ZEN_ASSERT(m_NamespaceBatchHandle);
- m_Store->CommitGetBatch(m_NamespaceBatchHandle);
+ m_Store->CommitGetBatch(m_NamespaceBatchHandle.get());
metrics::RequestStats::Scope OpScope(m_CacheStore.m_GetOps, 0);
for (const ZenCacheValue& Result : Results)
@@ -616,22 +602,7 @@ ZenCacheStore::GetBatch::Commit()
}
}
-ZenCacheStore::GetBatch::~GetBatch()
-{
- try
- {
- if (m_Store)
- {
- ZEN_MEMSCOPE(GetCacheStoreTag());
- ZEN_ASSERT(m_NamespaceBatchHandle);
- m_Store->DeleteGetBatch(m_NamespaceBatchHandle);
- }
- }
- catch (const std::exception& Ex)
- {
- ZEN_ERROR("Exception in cache store when ending batch get operation: '{}'", Ex.what());
- }
-}
+ZenCacheStore::GetBatch::~GetBatch() = default;
bool
ZenCacheStore::Get(const CacheRequestContext& Context,
@@ -776,7 +747,7 @@ ZenCacheStore::Put(const CacheRequestContext& Context,
if (ZenCacheNamespace* Store = GetNamespace(Namespace); Store)
{
- ZenCacheNamespace::PutBatchHandle* BatchHandle = OptionalBatchHandle ? OptionalBatchHandle->m_NamespaceBatchHandle : nullptr;
+ ZenCacheNamespace::PutBatchHandle* BatchHandle = OptionalBatchHandle ? OptionalBatchHandle->m_NamespaceBatchHandle.get() : nullptr;
PutResult RetVal = Store->Put(Bucket, HashKey, Value, References, Overwrite, BatchHandle);
if (RetVal.Status == zen::PutStatus::Success)
{