This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new d436fe2  Use MakeUnique and MakeShared (#781)
d436fe2 is described below

commit d436fe291424de6d234badc64be178201f9d2fb4
Author: FurudoErika <[email protected]>
AuthorDate: Mon Aug 22 00:13:02 2022 +0800

    Use MakeUnique and MakeShared (#781)
    
    Co-authored-by: hulk <[email protected]>
    Co-authored-by: Twice <[email protected]>
---
 src/cluster.cc                    |  9 +++++----
 src/redis_set.cc                  |  2 +-
 src/redis_stream.cc               |  8 ++++----
 src/redis_zset.cc                 |  8 ++++----
 src/server.cc                     | 12 ++++++------
 src/slot_migrate.cc               |  5 +++--
 src/storage.cc                    |  4 ++--
 src/table_properties_collector.cc |  5 +++--
 src/util.h                        |  4 ++++
 9 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/src/cluster.cc b/src/cluster.cc
index c837806..e6200a1 100644
--- a/src/cluster.cc
+++ b/src/cluster.cc
@@ -21,6 +21,7 @@
 #include <cassert>
 #include <cstring>
 #include <algorithm>
+#include <memory>
 
 #include "util.h"
 #include "server.h"
@@ -552,8 +553,8 @@ Status Cluster::ParseClusterNodes(const std::string 
&nodes_str, ClusterNodes *no
         return Status(Status::ClusterInvalidInfo, "Invalid cluster nodes 
info");
       } else {
         // Create slave node
-        (*nodes)[id] = std::shared_ptr<ClusterNode>(
-          new ClusterNode(id, host, port, role, master_id, slots));
+        (*nodes)[id] = Util::MakeShared<ClusterNode>(
+          id, host, port, role, master_id, slots);
         continue;
       }
     }
@@ -597,8 +598,8 @@ Status Cluster::ParseClusterNodes(const std::string 
&nodes_str, ClusterNodes *no
     }
 
     // Create master node
-    (*nodes)[id] = std::shared_ptr<ClusterNode>(
-        new ClusterNode(id, host, port, role, master_id, slots));
+    (*nodes)[id] = Util::MakeShared<ClusterNode>(
+        id, host, port, role, master_id, slots);
   }
   return Status::OK();
 }
diff --git a/src/redis_set.cc b/src/redis_set.cc
index 923a01b..8402149 100644
--- a/src/redis_set.cc
+++ b/src/redis_set.cc
@@ -208,7 +208,7 @@ rocksdb::Status Set::Take(const Slice &user_key, 
std::vector<std::string> *membe
   AppendNamespacePrefix(user_key, &ns_key);
 
   std::unique_ptr<LockGuard> lock_guard;
-  if (pop) lock_guard = std::unique_ptr<LockGuard>(new 
LockGuard(storage_->GetLockManager(), ns_key));
+  if (pop) lock_guard = 
Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
 
   SetMetadata metadata(false);
   rocksdb::Status s = GetMetadata(ns_key, &metadata);
diff --git a/src/redis_stream.cc b/src/redis_stream.cc
index 6c1dab3..3d5d65e 100644
--- a/src/redis_stream.cc
+++ b/src/redis_stream.cc
@@ -429,16 +429,16 @@ rocksdb::Status Stream::GetStreamInfo(const 
rocksdb::Slice &stream_name, bool fu
     if (!s.ok()) {
       return s;
     }
-    info->first_entry = std::unique_ptr<StreamEntry>(
-          new StreamEntry{metadata.first_entry_id.ToString(), 
DecodeRawStreamEntryValue(first_value)});
+    info->first_entry = Util::MakeUnique<StreamEntry>(
+          metadata.first_entry_id.ToString(), 
DecodeRawStreamEntryValue(first_value));
 
     std::string last_value;
     s = getEntryRawValue(ns_key, metadata, metadata.last_entry_id, 
&last_value);
     if (!s.ok()) {
       return s;
     }
-    info->last_entry = std::unique_ptr<StreamEntry>(
-          new StreamEntry{metadata.last_entry_id.ToString(), 
DecodeRawStreamEntryValue(last_value)});
+    info->last_entry = Util::MakeUnique<StreamEntry>(
+          metadata.last_entry_id.ToString(), 
DecodeRawStreamEntryValue(last_value));
   }
 
   return rocksdb::Status::OK();
diff --git a/src/redis_zset.cc b/src/redis_zset.cc
index 112f3a4..f29e4dc 100644
--- a/src/redis_zset.cc
+++ b/src/redis_zset.cc
@@ -218,7 +218,7 @@ rocksdb::Status ZSet::Range(const Slice &user_key, int 
start, int stop, uint8_t
   bool reversed = (flags & (uint8_t)ZSET_REVERSED) != 0;
 
   std::unique_ptr<LockGuard> lock_guard;
-  if (removed) lock_guard = std::unique_ptr<LockGuard>(new 
LockGuard(storage_->GetLockManager(), ns_key));
+  if (removed) lock_guard = 
Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
   ZSetMetadata metadata(false);
   rocksdb::Status s = GetMetadata(ns_key, &metadata);
   if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
@@ -296,7 +296,7 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
   AppendNamespacePrefix(user_key, &ns_key);
 
   std::unique_ptr<LockGuard> lock_guard;
-  if (spec.removed) lock_guard = std::unique_ptr<LockGuard>(new 
LockGuard(storage_->GetLockManager(), ns_key));
+  if (spec.removed) lock_guard = 
Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
   ZSetMetadata metadata(false);
   rocksdb::Status s = GetMetadata(ns_key, &metadata);
   if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
@@ -420,8 +420,8 @@ rocksdb::Status ZSet::RangeByLex(const Slice &user_key,
 
   std::unique_ptr<LockGuard> lock_guard;
   if (spec.removed) {
-    lock_guard = std::unique_ptr<LockGuard>(
-      new LockGuard(storage_->GetLockManager(), ns_key));
+    lock_guard = Util::MakeUnique<LockGuard>(
+      storage_->GetLockManager(), ns_key);
   }
   ZSetMetadata metadata(false);
   rocksdb::Status s = GetMetadata(ns_key, &metadata);
diff --git a/src/server.cc b/src/server.cc
index 5a49d87..a90bbb2 100644
--- a/src/server.cc
+++ b/src/server.cc
@@ -24,8 +24,8 @@
 #include <sys/statvfs.h>
 #include <sys/utsname.h>
 #include <sys/resource.h>
-#include <utility>
 #include <memory>
+#include <utility>
 #include <glog/logging.h>
 #include <rocksdb/convenience.h>
 
@@ -51,7 +51,7 @@ Server::Server(Engine::Storage *storage, Config *config) :
   }
 
   // Init cluster
-  cluster_ = std::unique_ptr<Cluster>(new Cluster(this, config_->binds, 
config_->port));
+  cluster_ = Util::MakeUnique<Cluster>(this, config_->binds, config_->port);
 
   for (int i = 0; i < config->workers; i++) {
     auto worker = Util::MakeUnique<Worker>(this, config);
@@ -119,8 +119,8 @@ Status Server::Start() {
 
   if (config_->cluster_enabled) {
     // Create objects used for slot migration
-    slot_migrate_ = std::unique_ptr<SlotMigrate>(new SlotMigrate(this, 
config_->migrate_speed,
-                                    config_->pipeline_size, 
config_->sequence_gap));
+    slot_migrate_ = Util::MakeUnique<SlotMigrate>(this, config_->migrate_speed,
+                                    config_->pipeline_size, 
config_->sequence_gap);
     slot_import_ = new SlotImport(this);
     // Create migrating thread
     auto s = slot_migrate_->CreateMigrateHandleThread();
@@ -594,11 +594,11 @@ int Server::DecrBlockedClientNum() {
 }
 
 std::unique_ptr<RWLock::ReadLock> Server::WorkConcurrencyGuard() {
-  return std::unique_ptr<RWLock::ReadLock>(new 
RWLock::ReadLock(works_concurrency_rw_lock_));
+  return Util::MakeUnique<RWLock::ReadLock>(works_concurrency_rw_lock_);
 }
 
 std::unique_ptr<RWLock::WriteLock> Server::WorkExclusivityGuard() {
-  return std::unique_ptr<RWLock::WriteLock>(new 
RWLock::WriteLock(works_concurrency_rw_lock_));
+  return Util::MakeUnique<RWLock::WriteLock>(works_concurrency_rw_lock_);
 }
 
 std::atomic<uint64_t> *Server::GetClientID() {
diff --git a/src/slot_migrate.cc b/src/slot_migrate.cc
index 4e6ec41..14a4b79 100644
--- a/src/slot_migrate.cc
+++ b/src/slot_migrate.cc
@@ -20,6 +20,7 @@
 
 #include "slot_migrate.h"
 
+#include <memory>
 #include <utility>
 
 #include "batch_extractor.h"
@@ -108,8 +109,8 @@ Status SlotMigrate::MigrateStart(Server *svr, const 
std::string &node_id, const
   dst_node_ = node_id;
 
   // Create migration job
-  auto job = std::unique_ptr<SlotMigrateJob>(new SlotMigrateJob(slot, dst_ip, 
dst_port,
-                                           speed, pipeline_size, seq_gap));
+  auto job = Util::MakeUnique<SlotMigrateJob>(slot, dst_ip, dst_port,
+                                           speed, pipeline_size, seq_gap);
   {
     std::lock_guard<std::mutex> guard(job_mutex_);
     slot_job_ = std::move(job);
diff --git a/src/storage.cc b/src/storage.cc
index 5c58efd..6ae9323 100644
--- a/src/storage.cc
+++ b/src/storage.cc
@@ -730,11 +730,11 @@ std::string Storage::GetReplIdFromDbEngine(void) {
 }
 
 std::unique_ptr<RWLock::ReadLock> Storage::ReadLockGuard() {
-  return std::unique_ptr<RWLock::ReadLock>(new RWLock::ReadLock(db_rw_lock_));
+  return Util::MakeUnique<RWLock::ReadLock>(db_rw_lock_);
 }
 
 std::unique_ptr<RWLock::WriteLock> Storage::WriteLockGuard() {
-  return std::unique_ptr<RWLock::WriteLock>(new 
RWLock::WriteLock(db_rw_lock_));
+  return Util::MakeUnique<RWLock::WriteLock>(db_rw_lock_);
 }
 
 Status Storage::ReplDataManager::GetFullReplDataInfo(Storage *storage, 
std::string *files) {
diff --git a/src/table_properties_collector.cc 
b/src/table_properties_collector.cc
index 6d8dc45..1f264c5 100644
--- a/src/table_properties_collector.cc
+++ b/src/table_properties_collector.cc
@@ -19,6 +19,7 @@
  */
 
 #include "table_properties_collector.h"
+#include <memory>
 #include <utility>
 #include "encoding.h"
 #include "redis_metadata.h"
@@ -97,6 +98,6 @@ 
CompactOnExpiredTableCollectorFactory::CreateTablePropertiesCollector(
 std::shared_ptr<CompactOnExpiredTableCollectorFactory>
 NewCompactOnExpiredTableCollectorFactory(const std::string &cf_name,
                                          float trigger_threshold) {
-  return std::shared_ptr<CompactOnExpiredTableCollectorFactory>(
-      new CompactOnExpiredTableCollectorFactory(cf_name, trigger_threshold));
+  return Util::MakeShared<CompactOnExpiredTableCollectorFactory>(
+      cf_name, trigger_threshold);
 }
diff --git a/src/util.h b/src/util.h
index 625e16e..ad3691b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -75,4 +75,8 @@ std::unique_ptr<T> MakeUnique(Args&& ... args) {
   return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
 }
 
+template <typename T, typename... Args>
+std::shared_ptr<T> MakeShared(Args&& ... args) {
+  return std::make_shared<T>(std::forward<Args>(args)...);
+}
 }  // namespace Util

Reply via email to