grpc 1.20.0 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +500 -29
  3. data/etc/roots.pem +146 -0
  4. data/include/grpc/grpc_security.h +1 -1
  5. data/include/grpc/impl/codegen/grpc_types.h +10 -7
  6. data/include/grpc/impl/codegen/port_platform.h +11 -1
  7. data/include/grpc/impl/codegen/slice.h +1 -21
  8. data/include/grpc/impl/codegen/status.h +2 -1
  9. data/include/grpc/slice.h +1 -1
  10. data/src/core/ext/filters/client_channel/backup_poller.cc +19 -13
  11. data/src/core/ext/filters/client_channel/backup_poller.h +3 -0
  12. data/src/core/ext/filters/client_channel/channel_connectivity.cc +1 -1
  13. data/src/core/ext/filters/client_channel/client_channel.cc +2084 -1673
  14. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +2 -3
  15. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +4 -0
  16. data/src/core/ext/filters/client_channel/health/health_check_client.cc +54 -49
  17. data/src/core/ext/filters/client_channel/health/health_check_client.h +20 -9
  18. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +1 -2
  19. data/src/core/ext/filters/client_channel/http_connect_handshaker.h +1 -1
  20. data/src/core/ext/filters/client_channel/lb_policy.cc +3 -30
  21. data/src/core/ext/filters/client_channel/lb_policy.h +16 -25
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +106 -81
  23. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.cc +6 -2
  24. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +8 -12
  25. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +2 -2
  26. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +1 -1
  27. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +57 -49
  28. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +47 -41
  29. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +24 -20
  30. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +989 -284
  31. data/src/core/ext/filters/client_channel/lb_policy_factory.h +4 -1
  32. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +105 -2
  33. data/src/core/ext/filters/client_channel/lb_policy_registry.h +9 -2
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +79 -36
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +84 -2
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +3 -0
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +179 -0
  38. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +15 -3
  39. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +80 -4
  40. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +7 -13
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +2 -2
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv.cc +39 -0
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc +0 -6
  44. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +2 -64
  45. data/src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.cc +28 -0
  46. data/src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h +29 -0
  47. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +4 -4
  48. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +367 -232
  49. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +55 -76
  50. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +50 -39
  51. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +18 -12
  52. data/src/core/ext/filters/client_channel/service_config.cc +247 -27
  53. data/src/core/ext/filters/client_channel/service_config.h +119 -166
  54. data/src/core/ext/filters/client_channel/subchannel.cc +46 -84
  55. data/src/core/ext/filters/client_channel/subchannel.h +7 -7
  56. data/src/core/ext/filters/deadline/deadline_filter.cc +3 -4
  57. data/src/core/ext/filters/deadline/deadline_filter.h +3 -2
  58. data/src/core/ext/filters/http/client/http_client_filter.cc +7 -5
  59. data/src/core/ext/filters/http/client/http_client_filter.h +1 -1
  60. data/src/core/ext/filters/http/client_authority_filter.cc +1 -1
  61. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +4 -3
  62. data/src/core/ext/filters/http/server/http_server_filter.cc +18 -12
  63. data/src/core/ext/filters/message_size/message_size_filter.cc +118 -76
  64. data/src/core/ext/filters/message_size/message_size_filter.h +33 -0
  65. data/src/core/ext/transport/chttp2/alpn/alpn.h +1 -1
  66. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.cc +9 -7
  67. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +93 -60
  68. data/src/core/ext/transport/chttp2/transport/flow_control.h +1 -1
  69. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +4 -3
  70. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +3 -3
  71. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +8 -2
  72. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +2 -2
  73. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +1 -1
  74. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +3 -2
  75. data/src/core/ext/transport/chttp2/transport/internal.h +35 -23
  76. data/src/core/ext/transport/chttp2/transport/parsing.cc +4 -4
  77. data/src/core/ext/transport/chttp2/transport/stream_lists.cc +3 -3
  78. data/src/core/ext/transport/chttp2/transport/writing.cc +61 -27
  79. data/src/core/ext/transport/inproc/inproc_transport.cc +18 -18
  80. data/src/core/lib/channel/channel_args.cc +0 -101
  81. data/src/core/lib/channel/channel_args.h +0 -37
  82. data/src/core/lib/channel/channel_stack.h +9 -5
  83. data/src/core/lib/channel/channelz_registry.cc +1 -1
  84. data/src/core/lib/channel/connected_channel.cc +2 -2
  85. data/src/core/lib/channel/context.h +3 -0
  86. data/src/core/lib/channel/handshaker.cc +4 -4
  87. data/src/core/lib/channel/handshaker.h +1 -1
  88. data/src/core/lib/compression/compression_args.cc +127 -0
  89. data/src/core/lib/compression/compression_args.h +55 -0
  90. data/src/core/lib/debug/trace.cc +13 -7
  91. data/src/core/lib/debug/trace.h +12 -0
  92. data/src/core/lib/gpr/arena.h +13 -9
  93. data/src/core/lib/gpr/env.h +2 -5
  94. data/src/core/lib/gpr/env_linux.cc +6 -1
  95. data/src/core/lib/gpr/env_posix.cc +5 -0
  96. data/src/core/lib/gpr/env_windows.cc +7 -5
  97. data/src/core/lib/gpr/log.cc +9 -13
  98. data/src/core/lib/gpr/string.cc +12 -6
  99. data/src/core/lib/gpr/string.h +4 -2
  100. data/src/core/lib/gpr/time_posix.cc +13 -0
  101. data/src/core/lib/gprpp/arena.cc +103 -0
  102. data/src/core/lib/gprpp/arena.h +121 -0
  103. data/src/core/lib/gprpp/fork.cc +12 -29
  104. data/src/core/lib/gprpp/global_config.h +87 -0
  105. data/src/core/lib/gprpp/global_config_custom.h +29 -0
  106. data/src/core/lib/gprpp/global_config_env.cc +135 -0
  107. data/src/core/lib/gprpp/global_config_env.h +131 -0
  108. data/src/core/lib/gprpp/global_config_generic.h +44 -0
  109. data/src/core/lib/gprpp/map.h +419 -0
  110. data/src/core/lib/gprpp/optional.h +1 -0
  111. data/src/core/lib/gprpp/orphanable.h +2 -2
  112. data/src/core/lib/gprpp/{mutex_lock.h → pair.h} +15 -19
  113. data/src/core/lib/gprpp/ref_counted.h +18 -2
  114. data/src/core/lib/gprpp/sync.h +126 -0
  115. data/src/core/lib/http/parser.cc +1 -1
  116. data/src/core/lib/iomgr/call_combiner.cc +84 -90
  117. data/src/core/lib/iomgr/call_combiner.h +75 -82
  118. data/src/core/lib/iomgr/cfstream_handle.cc +202 -0
  119. data/src/core/lib/iomgr/cfstream_handle.h +82 -0
  120. data/src/core/lib/iomgr/combiner.h +1 -1
  121. data/src/core/lib/iomgr/endpoint_cfstream.cc +375 -0
  122. data/src/core/lib/iomgr/endpoint_cfstream.h +49 -0
  123. data/src/core/lib/iomgr/endpoint_pair_windows.cc +2 -2
  124. data/src/core/lib/iomgr/error.h +23 -0
  125. data/src/core/lib/iomgr/error_cfstream.cc +52 -0
  126. data/src/core/lib/iomgr/error_cfstream.h +31 -0
  127. data/src/core/lib/iomgr/ev_epoll1_linux.cc +34 -27
  128. data/src/core/lib/iomgr/ev_epollex_linux.cc +33 -33
  129. data/src/core/lib/iomgr/ev_poll_posix.cc +7 -7
  130. data/src/core/lib/iomgr/ev_posix.cc +15 -13
  131. data/src/core/lib/iomgr/ev_posix.h +4 -1
  132. data/src/core/lib/iomgr/executor.cc +13 -9
  133. data/src/core/lib/iomgr/fork_posix.cc +0 -1
  134. data/src/core/lib/iomgr/internal_errqueue.cc +1 -1
  135. data/src/core/lib/iomgr/iomgr.cc +6 -5
  136. data/src/core/lib/iomgr/iomgr_custom.cc +3 -0
  137. data/src/core/lib/iomgr/iomgr_custom.h +2 -0
  138. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +93 -0
  139. data/src/core/lib/iomgr/iomgr_windows.cc +1 -0
  140. data/src/core/lib/iomgr/lockfree_event.cc +3 -3
  141. data/src/core/lib/iomgr/port.h +11 -0
  142. data/src/core/lib/iomgr/resource_quota.cc +40 -37
  143. data/src/core/lib/iomgr/socket_utils_common_posix.cc +6 -2
  144. data/src/core/lib/iomgr/socket_windows.cc +19 -0
  145. data/src/core/lib/iomgr/socket_windows.h +8 -0
  146. data/src/core/lib/iomgr/tcp_client_cfstream.cc +216 -0
  147. data/src/core/lib/iomgr/tcp_client_custom.cc +2 -2
  148. data/src/core/lib/iomgr/tcp_client_posix.cc +3 -3
  149. data/src/core/lib/iomgr/tcp_client_windows.cc +1 -1
  150. data/src/core/lib/iomgr/tcp_custom.cc +9 -9
  151. data/src/core/lib/iomgr/tcp_posix.cc +41 -41
  152. data/src/core/lib/iomgr/tcp_server_custom.cc +3 -3
  153. data/src/core/lib/iomgr/tcp_server_posix.cc +14 -1
  154. data/src/core/lib/iomgr/tcp_server_windows.cc +2 -2
  155. data/src/core/lib/iomgr/tcp_windows.cc +7 -9
  156. data/src/core/lib/iomgr/timer_generic.cc +16 -16
  157. data/src/core/lib/iomgr/timer_manager.cc +12 -11
  158. data/src/core/lib/profiling/basic_timers.cc +10 -4
  159. data/src/core/lib/security/context/security_context.cc +6 -7
  160. data/src/core/lib/security/context/security_context.h +3 -4
  161. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +1 -1
  162. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -3
  163. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +1 -1
  164. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +7 -7
  165. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +7 -5
  166. data/src/core/lib/security/security_connector/security_connector.cc +0 -1
  167. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +3 -2
  168. data/src/core/lib/security/security_connector/ssl_utils.cc +30 -26
  169. data/src/core/lib/security/security_connector/ssl_utils.h +5 -1
  170. data/src/core/lib/security/transport/client_auth_filter.cc +7 -11
  171. data/src/core/lib/security/transport/secure_endpoint.cc +4 -4
  172. data/src/core/lib/security/transport/server_auth_filter.cc +2 -3
  173. data/src/core/lib/slice/slice.cc +99 -116
  174. data/src/core/lib/slice/slice_buffer.cc +5 -0
  175. data/src/core/lib/slice/slice_intern.cc +38 -95
  176. data/src/core/lib/slice/slice_internal.h +200 -2
  177. data/src/core/lib/surface/api_trace.h +1 -1
  178. data/src/core/lib/surface/call.cc +41 -35
  179. data/src/core/lib/surface/call.h +7 -2
  180. data/src/core/lib/surface/call_details.cc +0 -1
  181. data/src/core/lib/surface/completion_queue.cc +36 -27
  182. data/src/core/lib/surface/init.cc +3 -4
  183. data/src/core/lib/surface/lame_client.cc +1 -1
  184. data/src/core/lib/surface/server.cc +18 -25
  185. data/src/core/lib/surface/version.cc +1 -1
  186. data/src/core/lib/transport/bdp_estimator.cc +3 -3
  187. data/src/core/lib/transport/bdp_estimator.h +2 -2
  188. data/src/core/lib/transport/connectivity_state.cc +10 -40
  189. data/src/core/lib/transport/connectivity_state.h +0 -8
  190. data/src/core/lib/transport/error_utils.cc +12 -0
  191. data/src/core/lib/transport/metadata.cc +206 -278
  192. data/src/core/lib/transport/metadata.h +205 -10
  193. data/src/core/lib/transport/static_metadata.cc +108 -116
  194. data/src/core/lib/transport/static_metadata.h +1 -2
  195. data/src/core/lib/transport/status_metadata.cc +3 -3
  196. data/src/core/lib/transport/transport.cc +29 -66
  197. data/src/core/lib/transport/transport.h +36 -8
  198. data/src/core/lib/transport/transport_impl.h +1 -1
  199. data/src/core/tsi/fake_transport_security.cc +4 -4
  200. data/src/core/tsi/ssl/session_cache/ssl_session_cache.cc +1 -1
  201. data/src/core/tsi/ssl_transport_security.cc +1 -1
  202. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  203. data/src/ruby/lib/grpc/errors.rb +22 -3
  204. data/src/ruby/lib/grpc/generic/bidi_call.rb +1 -1
  205. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  206. data/src/ruby/lib/grpc/version.rb +1 -1
  207. data/src/ruby/spec/errors_spec.rb +141 -0
  208. metadata +57 -33
  209. data/src/core/lib/gpr/arena.cc +0 -192
@@ -0,0 +1,44 @@
1
+ /*
2
+ *
3
+ * Copyright 2019 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * 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, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H
20
+ #define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include "src/core/lib/gprpp/memory.h"
25
+
26
+ #include <stdint.h>
27
+
28
+ #define GPR_GLOBAL_CONFIG_GET(name) gpr_global_config_get_##name()
29
+
30
+ #define GPR_GLOBAL_CONFIG_SET(name, value) gpr_global_config_set_##name(value)
31
+
32
+ #define GPR_GLOBAL_CONFIG_DECLARE_BOOL(name) \
33
+ extern bool gpr_global_config_get_##name(); \
34
+ extern void gpr_global_config_set_##name(bool value)
35
+
36
+ #define GPR_GLOBAL_CONFIG_DECLARE_INT32(name) \
37
+ extern int32_t gpr_global_config_get_##name(); \
38
+ extern void gpr_global_config_set_##name(int32_t value)
39
+
40
+ #define GPR_GLOBAL_CONFIG_DECLARE_STRING(name) \
41
+ extern grpc_core::UniquePtr<char> gpr_global_config_get_##name(); \
42
+ extern void gpr_global_config_set_##name(const char* value)
43
+
44
+ #endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H */
@@ -0,0 +1,419 @@
1
+ /*
2
+ *
3
+ * Copyright 2017 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * 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, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_CORE_LIB_GPRPP_MAP_H
20
+ #define GRPC_CORE_LIB_GPRPP_MAP_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include <string.h>
25
+ #include <functional>
26
+ #include <iterator>
27
+ #include "src/core/lib/gpr/useful.h"
28
+ #include "src/core/lib/gprpp/memory.h"
29
+ #include "src/core/lib/gprpp/pair.h"
30
+
31
+ namespace grpc_core {
32
+ struct StringLess {
33
+ bool operator()(const char* a, const char* b) const {
34
+ return strcmp(a, b) < 0;
35
+ }
36
+ bool operator()(const UniquePtr<char>& k1, const UniquePtr<char>& k2) {
37
+ return strcmp(k1.get(), k2.get()) < 0;
38
+ }
39
+ };
40
+
41
+ namespace testing {
42
+ class MapTest;
43
+ }
44
+
45
+ // Alternative map implementation for grpc_core
46
+ template <class Key, class T, class Compare = std::less<Key>>
47
+ class Map {
48
+ public:
49
+ typedef Key key_type;
50
+ typedef T mapped_type;
51
+ typedef Pair<key_type, mapped_type> value_type;
52
+ typedef Compare key_compare;
53
+ class iterator;
54
+
55
+ ~Map() { clear(); }
56
+
57
+ T& operator[](key_type&& key);
58
+ T& operator[](const key_type& key);
59
+ iterator find(const key_type& k);
60
+ size_t erase(const key_type& key);
61
+ // Removes the current entry and points to the next one
62
+ iterator erase(iterator iter);
63
+
64
+ size_t size() { return size_; }
65
+
66
+ template <class... Args>
67
+ Pair<iterator, bool> emplace(Args&&... args);
68
+
69
+ Pair<iterator, bool> insert(value_type&& pair) {
70
+ return emplace(std::move(pair));
71
+ }
72
+
73
+ Pair<iterator, bool> insert(const value_type& pair) { return emplace(pair); }
74
+
75
+ bool empty() const { return root_ == nullptr; }
76
+
77
+ void clear() {
78
+ auto iter = begin();
79
+ while (!empty()) {
80
+ iter = erase(iter);
81
+ }
82
+ }
83
+
84
+ iterator begin() {
85
+ Entry* curr = GetMinEntry(root_);
86
+ return iterator(this, curr);
87
+ }
88
+
89
+ iterator end() { return iterator(this, nullptr); }
90
+
91
+ private:
92
+ friend class testing::MapTest;
93
+ struct Entry {
94
+ explicit Entry(value_type&& pair) : pair(std::move(pair)) {}
95
+ value_type pair;
96
+ Entry* left = nullptr;
97
+ Entry* right = nullptr;
98
+ int32_t height = 1;
99
+ };
100
+
101
+ static int32_t EntryHeight(const Entry* e) {
102
+ return e == nullptr ? 0 : e->height;
103
+ }
104
+
105
+ static Entry* GetMinEntry(Entry* e);
106
+ Entry* InOrderSuccessor(const Entry* e) const;
107
+ static Entry* RotateLeft(Entry* e);
108
+ static Entry* RotateRight(Entry* e);
109
+ static Entry* RebalanceTreeAfterInsertion(Entry* root, const key_type& k);
110
+ static Entry* RebalanceTreeAfterDeletion(Entry* root);
111
+ // Returns a pair with the first value being an iterator pointing to the
112
+ // inserted entry and the second value being the new root of the subtree
113
+ // after a rebalance
114
+ Pair<iterator, Entry*> InsertRecursive(Entry* root, value_type&& p);
115
+ static Entry* RemoveRecursive(Entry* root, const key_type& k);
116
+ // Return 0 if lhs = rhs
117
+ // 1 if lhs > rhs
118
+ // -1 if lhs < rhs
119
+ static int CompareKeys(const Key& lhs, const Key& rhs);
120
+
121
+ Entry* root_ = nullptr;
122
+ size_t size_ = 0;
123
+ };
124
+
125
+ template <class Key, class T, class Compare>
126
+ class Map<Key, T, Compare>::iterator
127
+ : public std::iterator<std::input_iterator_tag, Pair<Key, T>, int32_t,
128
+ Pair<Key, T>*, Pair<Key, T>&> {
129
+ public:
130
+ iterator(const iterator& iter) : curr_(iter.curr_), map_(iter.map_) {}
131
+ bool operator==(const iterator& rhs) const { return (curr_ == rhs.curr_); }
132
+ bool operator!=(const iterator& rhs) const { return (curr_ != rhs.curr_); }
133
+
134
+ iterator& operator++() {
135
+ curr_ = map_->InOrderSuccessor(curr_);
136
+ return *this;
137
+ }
138
+
139
+ iterator operator++(int) {
140
+ Entry* prev = curr_;
141
+ curr_ = map_->InOrderSuccessor(curr_);
142
+ return iterator(map_, prev);
143
+ }
144
+
145
+ iterator& operator=(const iterator& other) {
146
+ if (this != &other) {
147
+ this->curr_ = other.curr_;
148
+ this->map_ = other.map_;
149
+ }
150
+ return *this;
151
+ }
152
+
153
+ // operator*()
154
+ value_type& operator*() { return curr_->pair; }
155
+ const value_type& operator*() const { return curr_->pair; }
156
+
157
+ // operator->()
158
+ value_type* operator->() { return &curr_->pair; }
159
+ value_type const* operator->() const { return &curr_->pair; }
160
+
161
+ private:
162
+ friend class Map<key_type, mapped_type, key_compare>;
163
+ using GrpcMap = typename ::grpc_core::Map<Key, T, Compare>;
164
+ iterator(GrpcMap* map, Entry* curr) : curr_(curr), map_(map) {}
165
+ Entry* curr_;
166
+ GrpcMap* map_;
167
+ };
168
+
169
+ template <class Key, class T, class Compare>
170
+ T& Map<Key, T, Compare>::operator[](key_type&& key) {
171
+ auto iter = find(key);
172
+ if (iter == end()) {
173
+ return emplace(std::move(key), T()).first->second;
174
+ }
175
+ return iter->second;
176
+ }
177
+
178
+ template <class Key, class T, class Compare>
179
+ T& Map<Key, T, Compare>::operator[](const key_type& key) {
180
+ auto iter = find(key);
181
+ if (iter == end()) {
182
+ return emplace(key, T()).first->second;
183
+ }
184
+ return iter->second;
185
+ }
186
+
187
+ template <class Key, class T, class Compare>
188
+ typename Map<Key, T, Compare>::iterator Map<Key, T, Compare>::find(
189
+ const key_type& k) {
190
+ Entry* iter = root_;
191
+ while (iter != nullptr) {
192
+ int comp = CompareKeys(iter->pair.first, k);
193
+ if (comp == 0) {
194
+ return iterator(this, iter);
195
+ } else if (comp < 0) {
196
+ iter = iter->right;
197
+ } else {
198
+ iter = iter->left;
199
+ }
200
+ }
201
+ return end();
202
+ }
203
+
204
+ template <class Key, class T, class Compare>
205
+ template <class... Args>
206
+ typename ::grpc_core::Pair<typename Map<Key, T, Compare>::iterator, bool>
207
+ Map<Key, T, Compare>::emplace(Args&&... args) {
208
+ Pair<key_type, mapped_type> pair(std::forward<Args>(args)...);
209
+ iterator ret = find(pair.first);
210
+ bool insertion = false;
211
+ if (ret == end()) {
212
+ Pair<iterator, Entry*> p = InsertRecursive(root_, std::move(pair));
213
+ root_ = p.second;
214
+ ret = p.first;
215
+ insertion = true;
216
+ size_++;
217
+ }
218
+ return MakePair(ret, insertion);
219
+ }
220
+
221
+ template <class Key, class T, class Compare>
222
+ size_t Map<Key, T, Compare>::erase(const key_type& key) {
223
+ iterator it = find(key);
224
+ if (it == end()) return 0;
225
+ erase(it);
226
+ return 1;
227
+ }
228
+
229
+ // TODO(mhaidry): Modify erase to use the iterator location
230
+ // to create an efficient erase method
231
+ template <class Key, class T, class Compare>
232
+ typename Map<Key, T, Compare>::iterator Map<Key, T, Compare>::erase(
233
+ iterator iter) {
234
+ if (iter == end()) return iter;
235
+ key_type& del_key = iter->first;
236
+ iter++;
237
+ root_ = RemoveRecursive(root_, del_key);
238
+ size_--;
239
+ return iter;
240
+ }
241
+
242
+ template <class Key, class T, class Compare>
243
+ typename Map<Key, T, Compare>::Entry* Map<Key, T, Compare>::InOrderSuccessor(
244
+ const Entry* e) const {
245
+ if (e->right != nullptr) {
246
+ return GetMinEntry(e->right);
247
+ }
248
+ Entry* successor = nullptr;
249
+ Entry* iter = root_;
250
+ while (iter != nullptr) {
251
+ int comp = CompareKeys(iter->pair.first, e->pair.first);
252
+ if (comp > 0) {
253
+ successor = iter;
254
+ iter = iter->left;
255
+ } else if (comp < 0) {
256
+ iter = iter->right;
257
+ } else
258
+ break;
259
+ }
260
+ return successor;
261
+ }
262
+
263
+ // Returns a pair with the first value being an iterator pointing to the
264
+ // inserted entry and the second value being the new root of the subtree
265
+ // after a rebalance
266
+ template <class Key, class T, class Compare>
267
+ typename ::grpc_core::Pair<typename Map<Key, T, Compare>::iterator,
268
+ typename Map<Key, T, Compare>::Entry*>
269
+ Map<Key, T, Compare>::InsertRecursive(Entry* root, value_type&& p) {
270
+ if (root == nullptr) {
271
+ Entry* e = New<Entry>(std::move(p));
272
+ return MakePair(iterator(this, e), e);
273
+ }
274
+ int comp = CompareKeys(root->pair.first, p.first);
275
+ if (comp > 0) {
276
+ Pair<iterator, Entry*> ret = InsertRecursive(root->left, std::move(p));
277
+ root->left = ret.second;
278
+ ret.second = RebalanceTreeAfterInsertion(root, ret.first->first);
279
+ return ret;
280
+ } else if (comp < 0) {
281
+ Pair<iterator, Entry*> ret = InsertRecursive(root->right, std::move(p));
282
+ root->right = ret.second;
283
+ ret.second = RebalanceTreeAfterInsertion(root, ret.first->first);
284
+ return ret;
285
+ } else {
286
+ root->pair = std::move(p);
287
+ return MakePair(iterator(this, root), root);
288
+ }
289
+ }
290
+
291
+ template <class Key, class T, class Compare>
292
+ typename Map<Key, T, Compare>::Entry* Map<Key, T, Compare>::GetMinEntry(
293
+ Entry* e) {
294
+ if (e != nullptr) {
295
+ while (e->left != nullptr) {
296
+ e = e->left;
297
+ }
298
+ }
299
+ return e;
300
+ }
301
+
302
+ template <class Key, class T, class Compare>
303
+ typename Map<Key, T, Compare>::Entry* Map<Key, T, Compare>::RotateLeft(
304
+ Entry* e) {
305
+ Entry* rightChild = e->right;
306
+ Entry* rightLeftChild = rightChild->left;
307
+ rightChild->left = e;
308
+ e->right = rightLeftChild;
309
+ e->height = 1 + GPR_MAX(EntryHeight(e->left), EntryHeight(e->right));
310
+ rightChild->height = 1 + GPR_MAX(EntryHeight(rightChild->left),
311
+ EntryHeight(rightChild->right));
312
+ return rightChild;
313
+ }
314
+
315
+ template <class Key, class T, class Compare>
316
+ typename Map<Key, T, Compare>::Entry* Map<Key, T, Compare>::RotateRight(
317
+ Entry* e) {
318
+ Entry* leftChild = e->left;
319
+ Entry* leftRightChild = leftChild->right;
320
+ leftChild->right = e;
321
+ e->left = leftRightChild;
322
+ e->height = 1 + GPR_MAX(EntryHeight(e->left), EntryHeight(e->right));
323
+ leftChild->height =
324
+ 1 + GPR_MAX(EntryHeight(leftChild->left), EntryHeight(leftChild->right));
325
+ return leftChild;
326
+ }
327
+
328
+ template <class Key, class T, class Compare>
329
+ typename Map<Key, T, Compare>::Entry*
330
+ Map<Key, T, Compare>::RebalanceTreeAfterInsertion(Entry* root,
331
+ const key_type& k) {
332
+ root->height = 1 + GPR_MAX(EntryHeight(root->left), EntryHeight(root->right));
333
+ int32_t heightDifference = EntryHeight(root->left) - EntryHeight(root->right);
334
+ if (heightDifference > 1 && CompareKeys(root->left->pair.first, k) > 0) {
335
+ return RotateRight(root);
336
+ }
337
+ if (heightDifference < -1 && CompareKeys(root->right->pair.first, k) < 0) {
338
+ return RotateLeft(root);
339
+ }
340
+ if (heightDifference > 1 && CompareKeys(root->left->pair.first, k) < 0) {
341
+ root->left = RotateLeft(root->left);
342
+ return RotateRight(root);
343
+ }
344
+ if (heightDifference < -1 && CompareKeys(root->right->pair.first, k) > 0) {
345
+ root->right = RotateRight(root->right);
346
+ return RotateLeft(root);
347
+ }
348
+ return root;
349
+ }
350
+
351
+ template <class Key, class T, class Compare>
352
+ typename Map<Key, T, Compare>::Entry*
353
+ Map<Key, T, Compare>::RebalanceTreeAfterDeletion(Entry* root) {
354
+ root->height = 1 + GPR_MAX(EntryHeight(root->left), EntryHeight(root->right));
355
+ int32_t heightDifference = EntryHeight(root->left) - EntryHeight(root->right);
356
+ if (heightDifference > 1) {
357
+ int leftHeightDifference =
358
+ EntryHeight(root->left->left) - EntryHeight(root->left->right);
359
+ if (leftHeightDifference < 0) {
360
+ root->left = RotateLeft(root->left);
361
+ }
362
+ return RotateRight(root);
363
+ }
364
+ if (heightDifference < -1) {
365
+ int rightHeightDifference =
366
+ EntryHeight(root->right->left) - EntryHeight(root->right->right);
367
+ if (rightHeightDifference > 0) {
368
+ root->right = RotateRight(root->right);
369
+ }
370
+ return RotateLeft(root);
371
+ }
372
+ return root;
373
+ }
374
+
375
+ template <class Key, class T, class Compare>
376
+ typename Map<Key, T, Compare>::Entry* Map<Key, T, Compare>::RemoveRecursive(
377
+ Entry* root, const key_type& k) {
378
+ if (root == nullptr) return root;
379
+ int comp = CompareKeys(root->pair.first, k);
380
+ if (comp > 0) {
381
+ root->left = RemoveRecursive(root->left, k);
382
+ } else if (comp < 0) {
383
+ root->right = RemoveRecursive(root->right, k);
384
+ } else {
385
+ Entry* ret;
386
+ if (root->left == nullptr) {
387
+ ret = root->right;
388
+ Delete(root);
389
+ return ret;
390
+ } else if (root->right == nullptr) {
391
+ ret = root->left;
392
+ Delete(root);
393
+ return ret;
394
+ } else {
395
+ ret = root->right;
396
+ while (ret->left != nullptr) {
397
+ ret = ret->left;
398
+ }
399
+ root->pair.swap(ret->pair);
400
+ root->right = RemoveRecursive(root->right, ret->pair.first);
401
+ }
402
+ }
403
+ return RebalanceTreeAfterDeletion(root);
404
+ }
405
+
406
+ template <class Key, class T, class Compare>
407
+ int Map<Key, T, Compare>::CompareKeys(const key_type& lhs,
408
+ const key_type& rhs) {
409
+ key_compare compare;
410
+ bool left_comparison = compare(lhs, rhs);
411
+ bool right_comparison = compare(rhs, lhs);
412
+ // Both values are equal
413
+ if (!left_comparison && !right_comparison) {
414
+ return 0;
415
+ }
416
+ return left_comparison ? -1 : 1;
417
+ }
418
+ } // namespace grpc_core
419
+ #endif /* GRPC_CORE_LIB_GPRPP_MAP_H */