grpcpp/impl/codegen/metadata_map.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
20 #define GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
21 
22 // IWYU pragma: private
23 
24 #include <map>
25 
26 #include <grpc/impl/codegen/log.h>
28 
29 namespace grpc {
30 
31 namespace internal {
32 
33 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin";
34 
35 class MetadataMap {
36  public:
37  MetadataMap() { Setup(); }
38 
40 
42  // if filled_, extract from the multimap for O(log(n))
43  if (filled_) {
44  auto iter = map_.find(kBinaryErrorDetailsKey);
45  if (iter != map_.end()) {
46  return std::string(iter->second.begin(), iter->second.length());
47  }
48  }
49  // if not yet filled, take the O(n) lookup to avoid allocating the
50  // multimap until it is requested.
51  // TODO(ncteisen): plumb this through core as a first class object, just
52  // like code and message.
53  else {
54  for (size_t i = 0; i < arr_.count; i++) {
55  if (strncmp(reinterpret_cast<const char*>(
59  return std::string(reinterpret_cast<const char*>(
62  }
63  }
64  }
65  return std::string();
66  }
67 
68  std::multimap<grpc::string_ref, grpc::string_ref>* map() {
69  FillMap();
70  return &map_;
71  }
72  grpc_metadata_array* arr() { return &arr_; }
73 
74  void Reset() {
75  filled_ = false;
76  map_.clear();
77  Destroy();
78  Setup();
79  }
80 
81  private:
82  bool filled_ = false;
84  std::multimap<grpc::string_ref, grpc::string_ref> map_;
85 
86  void Destroy() {
88  }
89 
90  void Setup() { memset(&arr_, 0, sizeof(arr_)); }
91 
92  void FillMap() {
93  if (filled_) return;
94  filled_ = true;
95  for (size_t i = 0; i < arr_.count; i++) {
96  // TODO(yangg) handle duplicates?
97  map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
100  }
101  }
102 };
103 } // namespace internal
104 
105 } // namespace grpc
106 
107 #endif // GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
grpc::StringRefFromSlice
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: include/grpcpp/impl/codegen/slice.h:133
grpc_metadata_array::metadata
grpc_metadata * metadata
Definition: grpc_types.h:582
grpc::internal::MetadataMap::map_
std::multimap< grpc::string_ref, grpc::string_ref > map_
Definition: grpcpp/impl/codegen/metadata_map.h:84
memset
return memset(p, 0, total)
grpc
Definition: grpcpp/alarm.h:33
grpc_metadata_array
Definition: grpc_types.h:579
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::internal::MetadataMap::~MetadataMap
~MetadataMap()
Definition: grpcpp/impl/codegen/metadata_map.h:39
slice.h
log.h
grpc_metadata_array::count
size_t count
Definition: grpc_types.h:580
grpc::internal::MetadataMap::arr
grpc_metadata_array * arr()
Definition: grpcpp/impl/codegen/metadata_map.h:72
grpc::internal::MetadataMap::FillMap
void FillMap()
Definition: grpcpp/impl/codegen/metadata_map.h:92
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::CoreCodegenInterface::grpc_metadata_array_destroy
virtual void grpc_metadata_array_destroy(grpc_metadata_array *array)=0
grpc::internal::MetadataMap::Setup
void Setup()
Definition: grpcpp/impl/codegen/metadata_map.h:90
grpc::internal::kBinaryErrorDetailsKey
const char kBinaryErrorDetailsKey[]
Definition: grpcpp/impl/codegen/metadata_map.h:33
grpc_metadata::value
grpc_slice value
Definition: grpc_types.h:541
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc::internal::MetadataMap
Definition: grpcpp/impl/codegen/metadata_map.h:35
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
grpc::internal::MetadataMap::Reset
void Reset()
Definition: grpcpp/impl/codegen/metadata_map.h:74
grpc::internal::MetadataMap::GetBinaryErrorDetails
std::string GetBinaryErrorDetails()
Definition: grpcpp/impl/codegen/metadata_map.h:41
grpc::internal::MetadataMap::MetadataMap
MetadataMap()
Definition: grpcpp/impl/codegen/metadata_map.h:37
grpc::internal::MetadataMap::filled_
bool filled_
Definition: grpcpp/impl/codegen/metadata_map.h:82
internal
Definition: benchmark/test/output_test_helper.cc:20
iter
Definition: test_winkernel.cpp:47
grpc::internal::MetadataMap::Destroy
void Destroy()
Definition: grpcpp/impl/codegen/metadata_map.h:86
grpc::internal::MetadataMap::arr_
grpc_metadata_array arr_
Definition: grpcpp/impl/codegen/metadata_map.h:83
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: grpcpp/impl/codegen/metadata_map.h:68
grpc_metadata::key
grpc_slice key
Definition: grpc_types.h:540
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:39