diff --git a/include/grpcpp/impl/codegen/client_context.h b/include/grpcpp/impl/codegen/client_context.h index cf1d9018885..c6c95409504 100644 --- a/include/grpcpp/impl/codegen/client_context.h +++ b/include/grpcpp/impl/codegen/client_context.h @@ -202,7 +202,6 @@ class ClientContext { const std::multimap& GetServerInitialMetadata() const { GPR_CODEGEN_ASSERT(initial_metadata_received_); - recv_initial_metadata_.FillMap(); return *recv_initial_metadata_.map(); } @@ -215,7 +214,6 @@ class ClientContext { const std::multimap& GetServerTrailingMetadata() const { // TODO(yangg) check finished - trailing_metadata_.FillMap(); return *trailing_metadata_.map(); } diff --git a/include/grpcpp/impl/codegen/metadata_map.h b/include/grpcpp/impl/codegen/metadata_map.h index 6e0c2a96372..5e480bc7ace 100644 --- a/include/grpcpp/impl/codegen/metadata_map.h +++ b/include/grpcpp/impl/codegen/metadata_map.h @@ -19,6 +19,7 @@ #ifndef GRPCPP_IMPL_CODEGEN_METADATA_MAP_H #define GRPCPP_IMPL_CODEGEN_METADATA_MAP_H +#include #include #include @@ -37,8 +38,8 @@ class MetadataMap { } grpc::string GetBinaryErrorDetails() { - // if filled, extract from the multimap for O(log(n)) - if (filled) { + // if filled_, extract from the multimap for O(log(n)) + if (filled_) { auto iter = map_.find(kBinaryErrorDetailsKey); if (iter != map_.end()) { return grpc::string(iter->second.begin(), iter->second.length()); @@ -46,9 +47,12 @@ class MetadataMap { } // if not yet filled, take the O(n) lookup to avoid allocating the // multimap until it is requested. + // TODO(ncteisen): plumb this through core as a first class object, just + // like code and message. else { for (size_t i = 0; i < arr_.count; i++) { - if (grpc_slice_str_cmp(arr_.metadata[i].key, kBinaryErrorDetailsKey)) { + if (grpc_slice_str_cmp(arr_.metadata[i].key, kBinaryErrorDetailsKey) == + 0) { return grpc::string(reinterpret_cast( GRPC_SLICE_START_PTR(arr_.metadata[i].value)), GRPC_SLICE_LENGTH(arr_.metadata[i].value)); @@ -58,9 +62,20 @@ class MetadataMap { return grpc::string(); } + std::multimap* map() { + FillMap(); + return &map_; + } + grpc_metadata_array* arr() { return &arr_; } + + private: + bool filled_ = false; + grpc_metadata_array arr_; + std::multimap map_; + void FillMap() { - if (filled) return; - filled = true; + if (filled_) return; + filled_ = true; for (size_t i = 0; i < arr_.count; i++) { // TODO(yangg) handle duplicates? map_.insert(std::pair( @@ -68,17 +83,6 @@ class MetadataMap { StringRefFromSlice(&arr_.metadata[i].value))); } } - - std::multimap* map() { return &map_; } - const std::multimap* map() const { - return &map_; - } - grpc_metadata_array* arr() { return &arr_; } - - private: - bool filled = false; - grpc_metadata_array arr_; - std::multimap map_; }; } // namespace internal diff --git a/include/grpcpp/impl/codegen/server_context.h b/include/grpcpp/impl/codegen/server_context.h index 506c51a5d96..b58f029de93 100644 --- a/include/grpcpp/impl/codegen/server_context.h +++ b/include/grpcpp/impl/codegen/server_context.h @@ -169,7 +169,6 @@ class ServerContext { /// \return A multimap of initial metadata key-value pairs from the server. const std::multimap& client_metadata() const { - client_metadata_.FillMap(); return *client_metadata_.map(); }