GRPC C++  1.30.0
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 #include <map>
23 
24 #include <grpc/impl/codegen/log.h>
26 
27 namespace grpc {
28 
29 namespace internal {
30 
31 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin";
32 
33 class MetadataMap {
34  public:
35  MetadataMap() { Setup(); }
36 
37  ~MetadataMap() { Destroy(); }
38 
40  // if filled_, extract from the multimap for O(log(n))
41  if (filled_) {
42  auto iter = map_.find(kBinaryErrorDetailsKey);
43  if (iter != map_.end()) {
44  return grpc::string(iter->second.begin(), iter->second.length());
45  }
46  }
47  // if not yet filled, take the O(n) lookup to avoid allocating the
48  // multimap until it is requested.
49  // TODO(ncteisen): plumb this through core as a first class object, just
50  // like code and message.
51  else {
52  for (size_t i = 0; i < arr_.count; i++) {
53  if (strncmp(reinterpret_cast<const char*>(
56  GRPC_SLICE_LENGTH(arr_.metadata[i].key)) == 0) {
57  return grpc::string(reinterpret_cast<const char*>(
60  }
61  }
62  }
63  return grpc::string();
64  }
65 
66  std::multimap<grpc::string_ref, grpc::string_ref>* map() {
67  FillMap();
68  return &map_;
69  }
70  grpc_metadata_array* arr() { return &arr_; }
71 
72  void Reset() {
73  filled_ = false;
74  map_.clear();
75  Destroy();
76  Setup();
77  }
78 
79  private:
80  bool filled_ = false;
82  std::multimap<grpc::string_ref, grpc::string_ref> map_;
83 
84  void Destroy() {
86  }
87 
88  void Setup() { memset(&arr_, 0, sizeof(arr_)); }
89 
90  void FillMap() {
91  if (filled_) return;
92  filled_ = true;
93  for (size_t i = 0; i < arr_.count; i++) {
94  // TODO(yangg) handle duplicates?
95  map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
97  StringRefFromSlice(&arr_.metadata[i].value)));
98  }
99  }
100 };
101 } // namespace internal
102 
103 } // namespace grpc
104 
105 #endif // GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
grpc::StringRefFromSlice
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: slice.h:120
log.h
grpc::internal::MetadataMap::GetBinaryErrorDetails
grpc::string GetBinaryErrorDetails()
Definition: metadata_map.h:39
grpc_metadata_array::metadata
grpc_metadata * metadata
Definition: grpc_types.h:549
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:99
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc_metadata_array
Definition: grpc_types.h:546
grpc::internal::MetadataMap::arr
grpc_metadata_array * arr()
Definition: metadata_map.h:70
grpc_metadata_array::count
size_t count
Definition: grpc_types.h:547
grpc::internal::MetadataMap
Definition: metadata_map.h:33
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:66
grpc::internal::MetadataMap::~MetadataMap
~MetadataMap()
Definition: metadata_map.h:37
grpc::internal::kBinaryErrorDetailsKey
const char kBinaryErrorDetailsKey[]
Definition: metadata_map.h:31
grpc_metadata::value
grpc_slice value
Definition: grpc_types.h:506
grpc::CoreCodegenInterface::grpc_metadata_array_destroy
virtual void grpc_metadata_array_destroy(grpc_metadata_array *array)=0
grpc::string
std::string string
Definition: config.h:35
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:96
grpc::internal::MetadataMap::MetadataMap
MetadataMap()
Definition: metadata_map.h:35
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:93
slice.h
grpc_metadata::key
grpc_slice key
the key, value values are expected to line up with grpc_mdelem: if changing them, update metadata....
Definition: grpc_types.h:505
grpc::internal::MetadataMap::Reset
void Reset()
Definition: metadata_map.h:72