mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 10:25:47 +02:00
Revert to use CachePrevValueIfNeeded(), but let it call GetString() if needed. (#1080)
I believe this is a better API as it's harder to mistakenly use. Signed-off-by: chakaz <chakaz@chakaz> Co-authored-by: ashotland <ari@dragonflydb.io>
This commit is contained in:
parent
5fb5f54885
commit
f013699613
1 changed files with 8 additions and 13 deletions
|
@ -482,14 +482,11 @@ class SetResultBuilder {
|
||||||
explicit SetResultBuilder(bool return_prev_value) : return_prev_value_(return_prev_value) {
|
explicit SetResultBuilder(bool return_prev_value) : return_prev_value_(return_prev_value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calling GetString() copies `it`'s value, which is unnecessary in most case
|
void CachePrevValueIfNeeded(EngineShard* shard, const PrimeValue& pv) {
|
||||||
bool ShouldReturnPrevValue() const {
|
if (return_prev_value_) {
|
||||||
return return_prev_value_;
|
// We call lazily call GetString() here to save string copying when not needed.
|
||||||
}
|
prev_value_ = GetString(shard, pv);
|
||||||
|
}
|
||||||
void CachePrevValue(string_view value) {
|
|
||||||
DCHECK(return_prev_value_);
|
|
||||||
prev_value_ = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns either the previous value or `status`, depending on return_prev_value_.
|
// Returns either the previous value or `status`, depending on return_prev_value_.
|
||||||
|
@ -521,8 +518,8 @@ OpResult<optional<string>> SetCmd::Set(const SetParams& params, string_view key,
|
||||||
|
|
||||||
if (params.IsConditionalSet()) {
|
if (params.IsConditionalSet()) {
|
||||||
const auto [it, expire_it] = db_slice.FindExt(op_args_.db_cntx, key);
|
const auto [it, expire_it] = db_slice.FindExt(op_args_.db_cntx, key);
|
||||||
if (result_builder.ShouldReturnPrevValue() && IsValid(it)) {
|
if (IsValid(it)) {
|
||||||
result_builder.CachePrevValue(GetString(shard, it->second));
|
result_builder.CachePrevValueIfNeeded(shard, it->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that we have this key, and only add it if it does exists
|
// Make sure that we have this key, and only add it if it does exists
|
||||||
|
@ -550,9 +547,7 @@ OpResult<optional<string>> SetCmd::Set(const SetParams& params, string_view key,
|
||||||
|
|
||||||
PrimeIterator it = get<0>(add_res);
|
PrimeIterator it = get<0>(add_res);
|
||||||
if (!get<2>(add_res)) { // Existing.
|
if (!get<2>(add_res)) { // Existing.
|
||||||
if (result_builder.ShouldReturnPrevValue()) {
|
result_builder.CachePrevValueIfNeeded(shard, it->second);
|
||||||
result_builder.CachePrevValue(GetString(shard, it->second));
|
|
||||||
}
|
|
||||||
return std::move(result_builder).Return(SetExisting(params, it, get<1>(add_res), key, value));
|
return std::move(result_builder).Return(SetExisting(params, it, get<1>(add_res), key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue