From b6ce963200ba00bb5beddb8d8bceea6fde5549f6 Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Thu, 8 Jun 2017 11:29:06 +0200 Subject: [PATCH] partition_version: Introduce partition_entry::with_detached_versions() --- partition_version.cc | 26 ++++++++++++++++++++++++++ partition_version.hh | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/partition_version.cc b/partition_version.cc index ffa67ae8f7..86bdc9e3d1 100644 --- a/partition_version.cc +++ b/partition_version.cc @@ -20,6 +20,7 @@ */ #include +#include #include "partition_version.hh" @@ -316,6 +317,31 @@ void partition_entry::apply(const schema& s, partition_entry&& pe, const schema& } } + +template +void partition_entry::with_detached_versions(Func&& func) { + partition_version* current = &*_version; + auto snapshot = _snapshot; + if (snapshot) { + snapshot->_version = std::move(_version); + snapshot->_entry = nullptr; + _snapshot = nullptr; + } + _version = { }; + + auto revert = defer([&] { + if (snapshot) { + _snapshot = snapshot; + snapshot->_entry = this; + _version = std::move(snapshot->_version); + } else { + _version = partition_version_ref(*current); + } + }); + + func(current); +} + mutation_partition partition_entry::squashed(schema_ptr from, schema_ptr to) { mutation_partition mp(to); diff --git a/partition_version.hh b/partition_version.hh index 9c0fffba68..29407d4a78 100644 --- a/partition_version.hh +++ b/partition_version.hh @@ -245,6 +245,11 @@ class partition_entry { friend class partition_snapshot; friend class cache_entry; private: + // Detaches all versions temporarily around execution of the function. + // The function receives partition_version* pointing to the latest version. + template + void with_detached_versions(Func&&); + void set_version(partition_version*); void apply(const schema& s, partition_version* pv, const schema& pv_schema);