Skip to content

Commit 34ab9cb

Browse files
authored
feat(util): Introduce map utilities (#2076)
When using C++ standard map-like containers, it's inconvenient to use the find*() functions, we have to compare the found iterator to the `end()` element, using the `second` to access its value, etc. It will be convenient to introduce some map utilities. The facebook/folly implementation [1] is limited, it lacks of some useful functions (for example, ContainsKey(), InsertIfNotPresent()). The original "gutil" implementation maybe from google/supersonic [2], but it has been archived and not hasn't been well maintainanced for years. The apache/kudu and impala/kudu implementation [3][4] is copied from some google implementation, but they lack of meaningful licenses. Another evoluted implementation is google/zetasql [5], and it contains clear license. It's a SQL analyzer framework but not a lightweight infrastructure library, it's too heavy to introduce the whole project. This patch introduces the necessary source code of map-util and its unit tests (under src/gutil/*), and give a few examples to show how to use. 1. https://github.com/facebook/folly/blob/main/folly/container/MapUtil.h 2. https://github.com/google/supersonic/blob/master/supersonic/utils/map_util.h 3. https://github.com/apache/kudu/blob/master/src/kudu/gutil/map-util.h 4. https://github.com/apache/impala/blob/master/be/src/gutil/map-util.h 5. https://github.com/google/zetasql/blob/master/zetasql/base/map_util.h
1 parent 722af2e commit 34ab9cb

30 files changed

+2424
-125
lines changed

.licenserc.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,5 +667,13 @@ header:
667667
- 'src/zookeeper/zookeeper_session.h'
668668
- 'src/zookeeper/zookeeper_session_mgr.cpp'
669669
- 'src/zookeeper/zookeeper_session_mgr.h'
670+
# Apache License, Version 2.0, Copyright 2018 Google LLC
671+
- 'src/gutil/test/map_traits_test.cpp'
672+
- 'src/gutil/test/map_util_test.h'
673+
- 'src/gutil/test/map_util_unittest.cpp'
674+
- 'src/gutil/test/no_destructor_test.cpp'
675+
- 'src/gutil/map_traits.h'
676+
- 'src/gutil/map_util.h'
677+
- 'src/gutil/no_destructor.h'
670678

671679
comment: on-failure

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,26 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
570570
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
571571
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
572572

573+
--------------------------------------------------------------------------------
574+
575+
src/gutil/test/map_traits_test.cpp
576+
src/gutil/test/map_util_test.h
577+
src/gutil/test/map_util_unittest.cpp
578+
src/gutil/test/no_destructor_test.cpp
579+
src/gutil/map_traits.h
580+
src/gutil/map_util.h
581+
src/gutil/no_destructor.h
582+
583+
Copyright 2018 Google LLC
584+
585+
Licensed under the Apache License, Version 2.0 (the "License");
586+
you may not use this file except in compliance with the License.
587+
You may obtain a copy of the License at
588+
589+
http://www.apache.org/licenses/LICENSE-2.0
590+
591+
Unless required by applicable law or agreed to in writing, software
592+
distributed under the License is distributed on an "AS IS" BASIS,
593+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
594+
See the License for the specific language governing permissions and
595+
limitations under the License.

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_subdirectory(client_lib)
4141
add_subdirectory(common)
4242
add_subdirectory(failure_detector)
4343
add_subdirectory(geo)
44+
add_subdirectory(gutil)
4445
add_subdirectory(http)
4546
add_subdirectory(meta)
4647
add_subdirectory(nfs)

src/base/idl_utils.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@
2323

2424
#include "rrdb/rrdb_types.h"
2525
#include "utils/fmt_utils.h"
26+
#include "gutil/map_util.h"
2627

2728
namespace pegasus {
2829

2930
inline std::string cas_check_type_to_string(dsn::apps::cas_check_type::type type)
3031
{
31-
auto it = dsn::apps::_cas_check_type_VALUES_TO_NAMES.find(type);
32-
if (it == dsn::apps::_cas_check_type_VALUES_TO_NAMES.end()) {
33-
return std::string("INVALID=") + std::to_string(int(type));
32+
const auto *name = gutil::FindOrNull(dsn::apps::_cas_check_type_VALUES_TO_NAMES, type);
33+
if (dsn_unlikely(name == nullptr)) {
34+
return fmt::format("INVALID={}", type);
3435
}
35-
return it->second;
36+
return *name;
3637
}
3738

3839
inline bool cas_is_check_operand_needed(dsn::apps::cas_check_type::type type)

src/common/duplication_common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class duplication_group_registry : public utils::singleton<duplication_group_reg
122122
_group.size(),
123123
"there might be duplicate cluster_name in configuration");
124124

125+
// TODO(yingchun): add InsertValuesFromMap to src/gutil/map_util.h, then use it here.
125126
for (const auto &kv : _group) {
126127
_distinct_cids.insert(kv.second);
127128
}

src/gutil/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# TODO(yingchun): add the project after new *.cpp files have been added.
19+
#set(MY_PROJ_NAME pgs_gutil)
20+
21+
add_subdirectory(test)

src/gutil/map_traits.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// Copyright 2018 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#pragma once
18+
19+
// Traits classes for performing uniform lookup on different map value types.
20+
//
21+
// The access is computed as follows:
22+
//
23+
// 1. If T has a `first` or `second` field, use them.
24+
// 2. Otherwise if it has `key()` or `value()` methods, use them.
25+
// 3. Otherwise the program is ill-formed.
26+
27+
#include <utility>
28+
29+
namespace gutil {
30+
namespace subtle {
31+
namespace internal_map_traits {
32+
struct Rank1
33+
{
34+
};
35+
struct Rank0 : Rank1
36+
{
37+
};
38+
39+
template <class V>
40+
auto GetKey(V &&v, Rank0) -> decltype((std::forward<V>(v).first))
41+
{
42+
return std::forward<V>(v).first;
43+
}
44+
template <class V>
45+
auto GetKey(V &&v, Rank1) -> decltype(std::forward<V>(v).key())
46+
{
47+
return std::forward<V>(v).key();
48+
}
49+
50+
template <class V>
51+
auto GetMapped(V &&v, Rank0) -> decltype((std::forward<V>(v).second))
52+
{
53+
return std::forward<V>(v).second;
54+
}
55+
template <class V>
56+
auto GetMapped(V &&v, Rank1) -> decltype(std::forward<V>(v).value())
57+
{
58+
return std::forward<V>(v).value();
59+
}
60+
61+
} // namespace internal_map_traits
62+
63+
// Accesses the `key_type` from a `value_type`.
64+
template <typename V>
65+
auto GetKey(V &&v)
66+
-> decltype(internal_map_traits::GetKey(std::forward<V>(v), internal_map_traits::Rank0()))
67+
{
68+
return internal_map_traits::GetKey(std::forward<V>(v), internal_map_traits::Rank0());
69+
}
70+
71+
// Accesses the `mapped_type` from a `value_type`.
72+
template <typename V>
73+
auto GetMapped(V &&v)
74+
-> decltype(internal_map_traits::GetMapped(std::forward<V>(v), internal_map_traits::Rank0()))
75+
{
76+
return internal_map_traits::GetMapped(std::forward<V>(v), internal_map_traits::Rank0());
77+
}
78+
79+
} // namespace subtle
80+
} // namespace gutil

0 commit comments

Comments
 (0)