You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by dr...@apache.org on 2016/07/11 18:25:12 UTC

[4/4] incubator-kudu git commit: Make EraseKeyReturnValuePtr in map-util work with smart pointers.

Make EraseKeyReturnValuePtr in map-util work with smart pointers.

This changes the function so that it works with smart pointers and
adds a small test that covers both smart and raw pointer cases.

Change-Id: I65d17dd26317f77c4c0aa4b8ec00fbe51f8d1ced
Reviewed-on: http://gerrit.cloudera.org:8080/3595
Reviewed-by: Todd Lipcon <to...@apache.org>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/2e34e315
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/2e34e315
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/2e34e315

Branch: refs/heads/master
Commit: 2e34e315e677df76b2365bd76f01252478d67326
Parents: 48fc399
Author: dralves <dr...@apache.org>
Authored: Fri Jul 8 09:30:15 2016 -0700
Committer: David Ribeiro Alves <dr...@apache.org>
Committed: Mon Jul 11 18:18:49 2016 +0000

----------------------------------------------------------------------
 src/kudu/gutil/map-util.h      |  6 ++++--
 src/kudu/util/map-util-test.cc | 23 +++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/2e34e315/src/kudu/gutil/map-util.h
----------------------------------------------------------------------
diff --git a/src/kudu/gutil/map-util.h b/src/kudu/gutil/map-util.h
index 61ffd4f..9e6e46c 100644
--- a/src/kudu/gutil/map-util.h
+++ b/src/kudu/gutil/map-util.h
@@ -677,15 +677,17 @@ void ReverseMap(const Collection& collection,
 //     if (value_ptr.get())
 //       value_ptr->DoSomething();
 //
+// Note: if 'collection' is a multimap, this will only erase and return the
+// first value.
 template <class Collection>
 typename Collection::value_type::second_type EraseKeyReturnValuePtr(
     Collection* const collection,
     const typename Collection::value_type::first_type& key) {
   auto it = collection->find(key);
   if (it == collection->end()) {
-    return NULL;
+    return typename Collection::value_type::second_type();
   }
-  typename Collection::value_type::second_type v = it->second;
+  typename Collection::value_type::second_type v = std::move(it->second);
   collection->erase(it);
   return v;
 }

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/2e34e315/src/kudu/util/map-util-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/map-util-test.cc b/src/kudu/util/map-util-test.cc
index ef07e58..1e818a2 100644
--- a/src/kudu/util/map-util-test.cc
+++ b/src/kudu/util/map-util-test.cc
@@ -25,6 +25,7 @@
 
 using std::map;
 using std::string;
+using std::shared_ptr;
 using std::unique_ptr;
 
 namespace kudu {
@@ -77,4 +78,26 @@ TEST(FindPointeeOrNullTest, TestFindPointeeOrNull) {
   ASSERT_TRUE(value == nullptr);
 }
 
+TEST(EraseKeyReturnValuePtrTest, TestRawAndSmartSmartPointers) {
+  map<string, unique_ptr<string>> my_map;
+  unique_ptr<string> value = EraseKeyReturnValuePtr(&my_map, "key");
+  ASSERT_TRUE(value.get() == nullptr);
+  my_map.emplace("key", unique_ptr<string>(new string("hello_world")));
+  value = EraseKeyReturnValuePtr(&my_map, "key");
+  ASSERT_EQ(*value, "hello_world");
+  value.reset();
+  value = EraseKeyReturnValuePtr(&my_map, "key");
+  ASSERT_TRUE(value.get() == nullptr);
+  map<string, shared_ptr<string>> my_map2;
+  shared_ptr<string> value2 = EraseKeyReturnValuePtr(&my_map2, "key");
+  ASSERT_TRUE(value2.get() == nullptr);
+  my_map2.emplace("key", shared_ptr<string>(new string("hello_world")));
+  value2 = EraseKeyReturnValuePtr(&my_map2, "key");
+  ASSERT_EQ(*value2, "hello_world");
+  map<string, string*> my_map_raw;
+  my_map_raw.emplace("key", new string("hello_world"));
+  value.reset(EraseKeyReturnValuePtr(&my_map_raw, "key"));
+  ASSERT_EQ(*value, "hello_world");
+}
+
 } // namespace kudu