Skip to content

Commit

Permalink
Merge pull request grpc#3065 from yang-g/string_ref
Browse files Browse the repository at this point in the history
Use string_ref for incoming metadata
  • Loading branch information
ctiller committed Aug 26, 2015
2 parents f1c7674 + 9f8c100 commit afbbaf9
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 88 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4794,6 +4794,7 @@ LIBGRPC++_TEST_UTIL_SRC = \
$(GENDIR)/test/cpp/util/echo_duplicate.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.grpc.pb.cc \
test/cpp/util/cli_call.cc \
test/cpp/util/create_test_channel.cc \
test/cpp/util/string_ref_helper.cc \
test/cpp/util/subprocess.cc \


Expand Down Expand Up @@ -4840,6 +4841,7 @@ endif
endif
$(OBJDIR)/$(CONFIG)/test/cpp/util/cli_call.o: $(GENDIR)/test/cpp/util/messages.pb.cc $(GENDIR)/test/cpp/util/messages.grpc.pb.cc $(GENDIR)/test/cpp/util/echo.pb.cc $(GENDIR)/test/cpp/util/echo.grpc.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.grpc.pb.cc
$(OBJDIR)/$(CONFIG)/test/cpp/util/create_test_channel.o: $(GENDIR)/test/cpp/util/messages.pb.cc $(GENDIR)/test/cpp/util/messages.grpc.pb.cc $(GENDIR)/test/cpp/util/echo.pb.cc $(GENDIR)/test/cpp/util/echo.grpc.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.grpc.pb.cc
$(OBJDIR)/$(CONFIG)/test/cpp/util/string_ref_helper.o: $(GENDIR)/test/cpp/util/messages.pb.cc $(GENDIR)/test/cpp/util/messages.grpc.pb.cc $(GENDIR)/test/cpp/util/echo.pb.cc $(GENDIR)/test/cpp/util/echo.grpc.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.grpc.pb.cc
$(OBJDIR)/$(CONFIG)/test/cpp/util/subprocess.o: $(GENDIR)/test/cpp/util/messages.pb.cc $(GENDIR)/test/cpp/util/messages.grpc.pb.cc $(GENDIR)/test/cpp/util/echo.pb.cc $(GENDIR)/test/cpp/util/echo.grpc.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.pb.cc $(GENDIR)/test/cpp/util/echo_duplicate.grpc.pb.cc


Expand Down Expand Up @@ -20702,6 +20704,7 @@ test/cpp/qps/timer.cc: $(OPENSSL_DEP)
test/cpp/util/benchmark_config.cc: $(OPENSSL_DEP)
test/cpp/util/cli_call.cc: $(OPENSSL_DEP)
test/cpp/util/create_test_channel.cc: $(OPENSSL_DEP)
test/cpp/util/string_ref_helper.cc: $(OPENSSL_DEP)
test/cpp/util/subprocess.cc: $(OPENSSL_DEP)
test/cpp/util/test_config.cc: $(OPENSSL_DEP)
endif
Expand Down
2 changes: 2 additions & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@
"headers": [
"test/cpp/util/cli_call.h",
"test/cpp/util/create_test_channel.h",
"test/cpp/util/string_ref_helper.h",
"test/cpp/util/subprocess.h"
],
"src": [
Expand All @@ -671,6 +672,7 @@
"test/cpp/util/echo_duplicate.proto",
"test/cpp/util/cli_call.cc",
"test/cpp/util/create_test_channel.cc",
"test/cpp/util/string_ref_helper.cc",
"test/cpp/util/subprocess.cc"
],
"deps": [
Expand Down
11 changes: 7 additions & 4 deletions include/grpc++/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <grpc++/support/auth_context.h>
#include <grpc++/support/config.h>
#include <grpc++/support/status.h>
#include <grpc++/support/string_ref.h>
#include <grpc++/support/time.h>

struct census_context;
Expand Down Expand Up @@ -138,12 +139,14 @@ class ClientContext {
void AddMetadata(const grpc::string& meta_key,
const grpc::string& meta_value);

const std::multimap<grpc::string, grpc::string>& GetServerInitialMetadata() {
const std::multimap<grpc::string_ref, grpc::string_ref>&
GetServerInitialMetadata() {
GPR_ASSERT(initial_metadata_received_);
return recv_initial_metadata_;
}

const std::multimap<grpc::string, grpc::string>& GetServerTrailingMetadata() {
const std::multimap<grpc::string_ref, grpc::string_ref>&
GetServerTrailingMetadata() {
// TODO(yangg) check finished
return trailing_metadata_;
}
Expand Down Expand Up @@ -234,8 +237,8 @@ class ClientContext {
mutable std::shared_ptr<const AuthContext> auth_context_;
struct census_context* census_context_;
std::multimap<grpc::string, grpc::string> send_initial_metadata_;
std::multimap<grpc::string, grpc::string> recv_initial_metadata_;
std::multimap<grpc::string, grpc::string> trailing_metadata_;
std::multimap<grpc::string_ref, grpc::string_ref> recv_initial_metadata_;
std::multimap<grpc::string_ref, grpc::string_ref> trailing_metadata_;

grpc_call* propagate_from_call_;
PropagationOptions propagation_options_;
Expand Down
9 changes: 5 additions & 4 deletions include/grpc++/impl/call.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ namespace grpc {
class ByteBuffer;
class Call;

void FillMetadataMap(grpc_metadata_array* arr,
std::multimap<grpc::string, grpc::string>* metadata);
void FillMetadataMap(
grpc_metadata_array* arr,
std::multimap<grpc::string_ref, grpc::string_ref>* metadata);
grpc_metadata* FillMetadataArray(
const std::multimap<grpc::string, grpc::string>& metadata);

Expand Down Expand Up @@ -418,7 +419,7 @@ class CallOpRecvInitialMetadata {
}

private:
std::multimap<grpc::string, grpc::string>* recv_initial_metadata_;
std::multimap<grpc::string_ref, grpc::string_ref>* recv_initial_metadata_;
grpc_metadata_array recv_initial_metadata_arr_;
};

Expand Down Expand Up @@ -461,7 +462,7 @@ class CallOpClientRecvStatus {
}

private:
std::multimap<grpc::string, grpc::string>* recv_trailing_metadata_;
std::multimap<grpc::string_ref, grpc::string_ref>* recv_trailing_metadata_;
Status* recv_status_;
grpc_metadata_array recv_trailing_metadata_arr_;
grpc_status_code status_code_;
Expand Down
5 changes: 3 additions & 2 deletions include/grpc++/server_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <grpc/support/time.h>
#include <grpc++/support/auth_context.h>
#include <grpc++/support/config.h>
#include <grpc++/support/string_ref.h>
#include <grpc++/support/time.h>

struct gpr_timespec;
Expand Down Expand Up @@ -103,7 +104,7 @@ class ServerContext {

bool IsCancelled() const;

const std::multimap<grpc::string, grpc::string>& client_metadata() {
const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() {
return client_metadata_;
}

Expand Down Expand Up @@ -185,7 +186,7 @@ class ServerContext {
CompletionQueue* cq_;
bool sent_initial_metadata_;
mutable std::shared_ptr<const AuthContext> auth_context_;
std::multimap<grpc::string, grpc::string> client_metadata_;
std::multimap<grpc::string_ref, grpc::string_ref> client_metadata_;
std::multimap<grpc::string, grpc::string> initial_metadata_;
std::multimap<grpc::string, grpc::string> trailing_metadata_;

Expand Down
28 changes: 13 additions & 15 deletions include/grpc++/support/string_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ class string_ref {
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

// constants
static constexpr size_t npos = size_t(-1);
const static size_t npos = size_t(-1);

// construct/copy.
constexpr string_ref() : data_(nullptr), length_(0) {}
constexpr string_ref(const string_ref& other)
string_ref() : data_(nullptr), length_(0) {}
string_ref(const string_ref& other)
: data_(other.data_), length_(other.length_) {}
string_ref& operator=(const string_ref& rhs);
string_ref(const char* s);
constexpr string_ref(const char* s, size_t l) : data_(s), length_(l) {}
string_ref(const char* s, size_t l) : data_(s), length_(l) {}
string_ref(const grpc::string& s) : data_(s.data()), length_(s.length()) {}

// iterators
constexpr const_iterator begin() const { return data_; }
constexpr const_iterator end() const { return data_ + length_; }
constexpr const_iterator cbegin() const { return data_; }
constexpr const_iterator cend() const { return data_ + length_; }
const_iterator begin() const { return data_; }
const_iterator end() const { return data_ + length_; }
const_iterator cbegin() const { return data_; }
const_iterator cend() const { return data_ + length_; }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
Expand All @@ -80,10 +80,10 @@ class string_ref {
}

// capacity
constexpr size_t size() const { return length_; }
constexpr size_t length() const { return length_; }
constexpr size_t max_size() const { return length_; }
constexpr bool empty() const { return length_ == 0; }
size_t size() const { return length_; }
size_t length() const { return length_; }
size_t max_size() const { return length_; }
bool empty() const { return length_ == 0; }

// element access
const char* data() const { return data_; }
Expand All @@ -95,9 +95,7 @@ class string_ref {
size_t find(string_ref s) const;
size_t find(char c) const;

// Defined as constexpr in n3442 but C++11 constexpr semantics do not allow
// the implementation of this function to comply.
/* constrexpr */ string_ref substr(size_t pos, size_t n = npos) const;
string_ref substr(size_t pos, size_t n = npos) const;

private:
const char* data_;
Expand Down
11 changes: 6 additions & 5 deletions src/cpp/common/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@

namespace grpc {

void FillMetadataMap(grpc_metadata_array* arr,
std::multimap<grpc::string, grpc::string>* metadata) {
void FillMetadataMap(
grpc_metadata_array* arr,
std::multimap<grpc::string_ref, grpc::string_ref>* metadata) {
for (size_t i = 0; i < arr->count; i++) {
// TODO(yangg) handle duplicates?
metadata->insert(std::pair<grpc::string, grpc::string>(
arr->metadata[i].key,
grpc::string(arr->metadata[i].value, arr->metadata[i].value_length)));
metadata->insert(std::pair<grpc::string_ref, grpc::string_ref>(
arr->metadata[i].key, grpc::string_ref(arr->metadata[i].value,
arr->metadata[i].value_length)));
}
grpc_metadata_array_destroy(arr);
grpc_metadata_array_init(arr);
Expand Down
11 changes: 6 additions & 5 deletions src/cpp/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,12 @@ Server::BaseAsyncRequest::~BaseAsyncRequest() {}
bool Server::BaseAsyncRequest::FinalizeResult(void** tag, bool* status) {
if (*status) {
for (size_t i = 0; i < initial_metadata_array_.count; i++) {
context_->client_metadata_.insert(std::make_pair(
grpc::string(initial_metadata_array_.metadata[i].key),
grpc::string(initial_metadata_array_.metadata[i].value,
initial_metadata_array_.metadata[i].value +
initial_metadata_array_.metadata[i].value_length)));
context_->client_metadata_.insert(
std::pair<grpc::string_ref, grpc::string_ref>(
initial_metadata_array_.metadata[i].key,
grpc::string_ref(
initial_metadata_array_.metadata[i].value,
initial_metadata_array_.metadata[i].value_length)));
}
}
grpc_metadata_array_destroy(&initial_metadata_array_);
Expand Down
7 changes: 3 additions & 4 deletions src/cpp/server/server_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
cq_(nullptr),
sent_initial_metadata_(false) {
for (size_t i = 0; i < metadata_count; i++) {
client_metadata_.insert(std::make_pair(
grpc::string(metadata[i].key),
grpc::string(metadata[i].value,
metadata[i].value + metadata[i].value_length)));
client_metadata_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
metadata[i].key,
grpc::string_ref(metadata[i].value, metadata[i].value_length)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/util/string_ref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

namespace grpc {

constexpr size_t string_ref::npos;
const size_t string_ref::npos;

string_ref& string_ref::operator=(const string_ref& rhs) {
data_ = rhs.data_;
Expand Down
37 changes: 25 additions & 12 deletions test/cpp/end2end/async_end2end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "test/core/util/test_config.h"
#include "test/cpp/util/echo_duplicate.grpc.pb.h"
#include "test/cpp/util/echo.grpc.pb.h"
#include "test/cpp/util/string_ref_helper.h"

#ifdef GPR_POSIX_SOCKET
#include "src/core/iomgr/pollset_posix.h"
Expand Down Expand Up @@ -484,8 +485,10 @@ TEST_P(AsyncEnd2endTest, ClientInitialMetadataRpc) {
Verifier(GetParam()).Expect(2, true).Verify(cq_.get());
EXPECT_EQ(send_request.message(), recv_request.message());
auto client_initial_metadata = srv_ctx.client_metadata();
EXPECT_EQ(meta1.second, client_initial_metadata.find(meta1.first)->second);
EXPECT_EQ(meta2.second, client_initial_metadata.find(meta2.first)->second);
EXPECT_EQ(meta1.second,
ToString(client_initial_metadata.find(meta1.first)->second));
EXPECT_EQ(meta2.second,
ToString(client_initial_metadata.find(meta2.first)->second));
EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));

send_response.set_message(recv_request.message());
Expand Down Expand Up @@ -532,8 +535,10 @@ TEST_P(AsyncEnd2endTest, ServerInitialMetadataRpc) {
response_reader->ReadInitialMetadata(tag(4));
Verifier(GetParam()).Expect(4, true).Verify(cq_.get());
auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
EXPECT_EQ(meta1.second, server_initial_metadata.find(meta1.first)->second);
EXPECT_EQ(meta2.second, server_initial_metadata.find(meta2.first)->second);
EXPECT_EQ(meta1.second,
ToString(server_initial_metadata.find(meta1.first)->second));
EXPECT_EQ(meta2.second,
ToString(server_initial_metadata.find(meta2.first)->second));
EXPECT_EQ(static_cast<size_t>(2), server_initial_metadata.size());

send_response.set_message(recv_request.message());
Expand Down Expand Up @@ -586,8 +591,10 @@ TEST_P(AsyncEnd2endTest, ServerTrailingMetadataRpc) {
EXPECT_EQ(send_response.message(), recv_response.message());
EXPECT_TRUE(recv_status.ok());
auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
EXPECT_EQ(meta1.second, server_trailing_metadata.find(meta1.first)->second);
EXPECT_EQ(meta2.second, server_trailing_metadata.find(meta2.first)->second);
EXPECT_EQ(meta1.second,
ToString(server_trailing_metadata.find(meta1.first)->second));
EXPECT_EQ(meta2.second,
ToString(server_trailing_metadata.find(meta2.first)->second));
EXPECT_EQ(static_cast<size_t>(2), server_trailing_metadata.size());
}

Expand Down Expand Up @@ -631,8 +638,10 @@ TEST_P(AsyncEnd2endTest, MetadataRpc) {
Verifier(GetParam()).Expect(2, true).Verify(cq_.get());
EXPECT_EQ(send_request.message(), recv_request.message());
auto client_initial_metadata = srv_ctx.client_metadata();
EXPECT_EQ(meta1.second, client_initial_metadata.find(meta1.first)->second);
EXPECT_EQ(meta2.second, client_initial_metadata.find(meta2.first)->second);
EXPECT_EQ(meta1.second,
ToString(client_initial_metadata.find(meta1.first)->second));
EXPECT_EQ(meta2.second,
ToString(client_initial_metadata.find(meta2.first)->second));
EXPECT_GE(client_initial_metadata.size(), static_cast<size_t>(2));

srv_ctx.AddInitialMetadata(meta3.first, meta3.second);
Expand All @@ -642,8 +651,10 @@ TEST_P(AsyncEnd2endTest, MetadataRpc) {
response_reader->ReadInitialMetadata(tag(4));
Verifier(GetParam()).Expect(4, true).Verify(cq_.get());
auto server_initial_metadata = cli_ctx.GetServerInitialMetadata();
EXPECT_EQ(meta3.second, server_initial_metadata.find(meta3.first)->second);
EXPECT_EQ(meta4.second, server_initial_metadata.find(meta4.first)->second);
EXPECT_EQ(meta3.second,
ToString(server_initial_metadata.find(meta3.first)->second));
EXPECT_EQ(meta4.second,
ToString(server_initial_metadata.find(meta4.first)->second));
EXPECT_GE(server_initial_metadata.size(), static_cast<size_t>(2));

send_response.set_message(recv_request.message());
Expand All @@ -658,8 +669,10 @@ TEST_P(AsyncEnd2endTest, MetadataRpc) {
EXPECT_EQ(send_response.message(), recv_response.message());
EXPECT_TRUE(recv_status.ok());
auto server_trailing_metadata = cli_ctx.GetServerTrailingMetadata();
EXPECT_EQ(meta5.second, server_trailing_metadata.find(meta5.first)->second);
EXPECT_EQ(meta6.second, server_trailing_metadata.find(meta6.first)->second);
EXPECT_EQ(meta5.second,
ToString(server_trailing_metadata.find(meta5.first)->second));
EXPECT_EQ(meta6.second,
ToString(server_trailing_metadata.find(meta6.first)->second));
EXPECT_GE(server_trailing_metadata.size(), static_cast<size_t>(2));
}

Expand Down
27 changes: 15 additions & 12 deletions test/cpp/end2end/end2end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "test/core/util/test_config.h"
#include "test/cpp/util/echo_duplicate.grpc.pb.h"
#include "test/cpp/util/echo.grpc.pb.h"
#include "test/cpp/util/string_ref_helper.h"

using grpc::cpp::test::util::EchoRequest;
using grpc::cpp::test::util::EchoResponse;
Expand Down Expand Up @@ -152,12 +153,13 @@ class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
}

if (request->has_param() && request->param().echo_metadata()) {
const std::multimap<grpc::string, grpc::string>& client_metadata =
const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata =
context->client_metadata();
for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
client_metadata.begin();
for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
iter = client_metadata.begin();
iter != client_metadata.end(); ++iter) {
context->AddTrailingMetadata((*iter).first, (*iter).second);
context->AddTrailingMetadata(ToString(iter->first),
ToString(iter->second));
}
}
if (request->has_param() && request->param().check_auth_context()) {
Expand All @@ -182,12 +184,12 @@ class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
EchoRequest request;
response->set_message("");
int cancel_after_reads = 0;
const std::multimap<grpc::string, grpc::string> client_initial_metadata =
context->client_metadata();
const std::multimap<grpc::string_ref, grpc::string_ref>&
client_initial_metadata = context->client_metadata();
if (client_initial_metadata.find(kServerCancelAfterReads) !=
client_initial_metadata.end()) {
std::istringstream iss(
client_initial_metadata.find(kServerCancelAfterReads)->second);
std::istringstream iss(ToString(
client_initial_metadata.find(kServerCancelAfterReads)->second));
iss >> cancel_after_reads;
gpr_log(GPR_INFO, "cancel_after_reads %d", cancel_after_reads);
}
Expand Down Expand Up @@ -721,14 +723,15 @@ TEST_F(End2endTest, RpcMaxMessageSize) {
EXPECT_FALSE(s.ok());
}

bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata,
const grpc::string& key, const grpc::string& value) {
bool MetadataContains(
const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
const grpc::string& key, const grpc::string& value) {
int count = 0;

for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator iter =
metadata.begin();
iter != metadata.end(); ++iter) {
if ((*iter).first == key && (*iter).second == value) {
if (ToString(iter->first) == key && ToString(iter->second) == value) {
count++;
}
}
Expand Down
Loading

0 comments on commit afbbaf9

Please sign in to comment.