Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions google/cloud/spanner/mocks/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "google/cloud/spanner/row.h"
#include "google/cloud/spanner/value.h"
#include "google/cloud/version.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -27,9 +28,6 @@ namespace cloud {
namespace spanner_mocks {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

// TODO(#9086): Delete this when the MakeRow() implementation is moved here.
#include "google/cloud/internal/disable_deprecation_warnings.inc"

/**
* Creates a `spanner::Row` with the specified column names and values.
*
Expand All @@ -41,7 +39,14 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
*/
inline spanner::Row MakeRow(
std::vector<std::pair<std::string, spanner::Value>> pairs) {
return spanner::MakeTestRow(std::move(pairs));
auto values = std::vector<spanner::Value>{};
auto columns = std::make_shared<std::vector<std::string>>();
Comment on lines +42 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve performance by avoiding potential reallocations, it's good practice to reserve space for the vectors since their final size is known in advance.

  auto values = std::vector<spanner::Value>{};
  values.reserve(pairs.size());
  auto columns = std::make_shared<std::vector<std::string>>();
  columns->reserve(pairs.size());

for (auto& p : pairs) {
values.emplace_back(std::move(p.second));
columns->emplace_back(std::move(p.first));
}
return spanner_internal::RowFriend::MakeRow(std::move(values),
std::move(columns));
}

/**
Expand All @@ -58,12 +63,14 @@ inline spanner::Row MakeRow(
*/
template <typename... Ts>
spanner::Row MakeRow(Ts&&... ts) {
return spanner::MakeTestRow(std::forward<Ts>(ts)...);
auto columns = std::make_shared<std::vector<std::string>>();
for (std::size_t i = 0; i < sizeof...(ts); ++i) {
columns->emplace_back(std::to_string(i));
}
Comment on lines +66 to +69

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve performance by avoiding potential reallocations, it's good practice to reserve space for the columns vector since its final size is known in advance.

  auto columns = std::make_shared<std::vector<std::string>>();
  columns->reserve(sizeof...(ts));
  for (std::size_t i = 0; i < sizeof...(ts); ++i) {
    columns->emplace_back(std::to_string(i));
  }

std::vector<spanner::Value> v{spanner::Value(std::forward<Ts>(ts))...};
return spanner_internal::RowFriend::MakeRow(std::move(v), std::move(columns));
}

// TODO(#9086): Delete this when the MakeRow() implementation is moved here.
#include "google/cloud/internal/diagnostics_pop.inc"

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace spanner_mocks
} // namespace cloud
Expand Down
10 changes: 0 additions & 10 deletions google/cloud/spanner/row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

namespace spanner {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
Row MakeTestRow(std::vector<std::pair<std::string, Value>> pairs) {
auto values = std::vector<Value>{};
auto columns = std::make_shared<std::vector<std::string>>();
for (auto& p : pairs) {
values.emplace_back(std::move(p.second));
columns->emplace_back(std::move(p.first));
}
return spanner_internal::RowFriend::MakeRow(std::move(values),
std::move(columns));
}

Row::Row() : Row({}, std::make_shared<std::vector<std::string>>()) {}

Expand Down
35 changes: 0 additions & 35 deletions google/cloud/spanner/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,41 +200,6 @@ class Row {
std::shared_ptr<std::vector<std::string> const> columns_;
};

/**
* Creates a `Row` with the specified column names and values.
*
* This overload accepts a vector of pairs, allowing the caller to specify both
* the column names and the `Value` that goes in each column.
*
* This function is intended for application developers who are mocking the
* results of a `Client::ExecuteQuery` call.
*/
GOOGLE_CLOUD_CPP_SPANNER_MAKE_TEST_ROW_DEPRECATED()
Row MakeTestRow(std::vector<std::pair<std::string, Value>> pairs);

/**
* Creates a `Row` with `Value`s created from the given arguments and with
* auto-generated column names.
*
* This overload accepts a variadic list of arguments that will be used to
* create the `Value`s in the row. The column names will be implicitly
* generated, the first column being "0", the second "1", and so on,
* corresponding to the argument's position.
*
* This function is intended for application developers who are mocking the
* results of a `Client::ExecuteQuery` call.
*/
template <typename... Ts>
GOOGLE_CLOUD_CPP_SPANNER_MAKE_TEST_ROW_DEPRECATED()
Row MakeTestRow(Ts&&... ts) {
auto columns = std::make_shared<std::vector<std::string>>();
for (std::size_t i = 0; i < sizeof...(ts); ++i) {
columns->emplace_back(std::to_string(i));
}
std::vector<Value> v{Value(std::forward<Ts>(ts))...};
return spanner_internal::RowFriend::MakeRow(std::move(v), std::move(columns));
}

/**
* A `RowStreamIterator` is an _Input Iterator_ (see below) that returns a
* sequence of `StatusOr<Row>` objects.
Expand Down