Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move assign and reset for ObserverOrUnique #1299

Merged
merged 2 commits into from
Mar 7, 2023
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
18 changes: 17 additions & 1 deletion src/common/ptr_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ struct ObserverOrUniquePtr : private D {
explicit ObserverOrUniquePtr(T* ptr, ObserverOrUnique own) : ptr_(ptr), own_(own) {}

ObserverOrUniquePtr(ObserverOrUniquePtr&& p) : ptr_(p.ptr_), own_(p.own_) { p.Release(); }
ObserverOrUniquePtr& operator=(ObserverOrUniquePtr&& p) {
Reset(p.ptr_);
own_ = p.own_;
p.Release();

return *this;
}

ObserverOrUniquePtr(const ObserverOrUniquePtr&) = delete;
ObserverOrUniquePtr& operator=(const ObserverOrUniquePtr&) = delete;

Expand All @@ -45,7 +53,15 @@ struct ObserverOrUniquePtr : private D {
return ptr_;
}

void Reset(T* ptr = nullptr) {
if (own_ == ObserverOrUnique::Unique) {
(*this)(ptr_);
}

ptr_ = ptr;
}

private:
T* ptr_;
ObserverOrUnique own_;
};
};
9 changes: 4 additions & 5 deletions src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,15 @@ rocksdb::Status Database::Expire(const Slice &user_key, int timestamp) {
}
if (metadata.expire == timestamp) return rocksdb::Status::OK();

char *buf = new char[value.size()];
memcpy(buf, value.data(), value.size());
auto buf = std::make_unique<char[]>(value.size());
memcpy(buf.get(), value.data(), value.size());
// +1 to skip the flags
EncodeFixed32(buf + 1, (uint32_t)timestamp);
EncodeFixed32(buf.get() + 1, (uint32_t)timestamp);
auto batch = storage_->GetWriteBatchBase();
WriteBatchLogData log_data(kRedisNone, {std::to_string(kRedisCmdExpire)});
batch->PutLogData(log_data.Encode());
batch->Put(metadata_cf_handle_, ns_key, Slice(buf, value.size()));
batch->Put(metadata_cf_handle_, ns_key, Slice(buf.get(), value.size()));
s = storage_->Write(storage_->DefaultWriteOptions(), batch->GetWriteBatch());
delete[] buf;
return s;
}

Expand Down
17 changes: 12 additions & 5 deletions tests/cppunit/ptr_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
#include "common/ptr_util.h"

#include <gtest/gtest.h>
struct Counter {
Counter(int* i) : i_(i) { ++*i_; }
struct Counter { // NOLINT
explicit Counter(int* i) : i_(i) { ++*i_; }
~Counter() { --*i_; }

int* i_;
};

TEST(ObserverOrUniquePtr, Unique) {
int v = 0;
{
Expand All @@ -38,8 +39,14 @@ TEST(ObserverOrUniquePtr, Unique) {

TEST(CompositePtr, Observer) {
int v = 0;
ObserverOrUniquePtr<Counter> observer(new Counter{&v}, ObserverOrUnique::Observer);
Counter* c = nullptr;
{
ObserverOrUniquePtr<Counter> observer(new Counter{&v}, ObserverOrUnique::Observer);
ASSERT_EQ(v, 1);

c = observer.Get();
}
ASSERT_EQ(v, 1);
delete observer.Release();
delete c;
ASSERT_EQ(v, 0);
}
}