Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit c9c3e78

Browse files
authored
feat!: refactor Read to return ReadResult; remove ResultSet (#935)
1 parent a84a137 commit c9c3e78

File tree

14 files changed

+255
-285
lines changed

14 files changed

+255
-285
lines changed

google/cloud/spanner/client.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,33 @@ namespace cloud {
2727
namespace spanner {
2828
inline namespace SPANNER_CLIENT_NS {
2929

30-
StatusOr<ResultSet> Client::Read(std::string table, KeySet keys,
31-
std::vector<std::string> columns,
32-
ReadOptions read_options) {
30+
ReadResult Client::Read(std::string table, KeySet keys,
31+
std::vector<std::string> columns,
32+
ReadOptions read_options) {
3333
return conn_->Read(
3434
{internal::MakeSingleUseTransaction(Transaction::ReadOnlyOptions()),
3535
std::move(table), std::move(keys), std::move(columns),
3636
std::move(read_options)});
3737
}
3838

39-
StatusOr<ResultSet> Client::Read(
40-
Transaction::SingleUseOptions transaction_options, std::string table,
41-
KeySet keys, std::vector<std::string> columns, ReadOptions read_options) {
39+
ReadResult Client::Read(Transaction::SingleUseOptions transaction_options,
40+
std::string table, KeySet keys,
41+
std::vector<std::string> columns,
42+
ReadOptions read_options) {
4243
return conn_->Read(
4344
{internal::MakeSingleUseTransaction(std::move(transaction_options)),
4445
std::move(table), std::move(keys), std::move(columns),
4546
std::move(read_options)});
4647
}
4748

48-
StatusOr<ResultSet> Client::Read(Transaction transaction, std::string table,
49-
KeySet keys, std::vector<std::string> columns,
50-
ReadOptions read_options) {
49+
ReadResult Client::Read(Transaction transaction, std::string table, KeySet keys,
50+
std::vector<std::string> columns,
51+
ReadOptions read_options) {
5152
return conn_->Read({std::move(transaction), std::move(table), std::move(keys),
5253
std::move(columns), std::move(read_options)});
5354
}
5455

55-
StatusOr<ResultSet> Client::Read(ReadPartition const& read_partition) {
56+
ReadResult Client::Read(ReadPartition const& read_partition) {
5657
return conn_->Read(internal::MakeReadParams(read_partition));
5758
}
5859

google/cloud/spanner/client.h

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,12 @@ inline namespace SPANNER_CLIENT_NS {
7575
*
7676
* @code
7777
* namespace cs = ::google::cloud::spanner;
78-
* using ::google::cloud::StatusOr;
7978
*
8079
* auto db = cs::Database("my_project", "my_instance", "my_db_id"));
8180
* auto conn = cs::MakeConnection(db);
8281
* auto client = cs::Client(conn);
8382
*
84-
* StatusOr<cs::ResultSet> result = client.Read(...);
85-
* if (!result) {
86-
* return result.status();
87-
* }
83+
* cs::ReadResult result = client.Read(...);
8884
* using RowType = Row<std::int64_t, std::string>;
8985
* for (auto const& row : result.Rows<RowType>()) {
9086
* // ...
@@ -143,31 +139,30 @@ class Client {
143139
* this request.
144140
* @param read_options `ReadOptions` used for this request.
145141
*
146-
* @return A `StatusOr` containing a `ResultSet` or error status on failure.
147-
* No individual row in the `ResultSet` can exceed 100 MiB, and no column
148-
* value can exceed 10 MiB.
142+
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
143+
* column value can exceed 10 MiB.
149144
*/
150-
StatusOr<ResultSet> Read(std::string table, KeySet keys,
151-
std::vector<std::string> columns,
152-
ReadOptions read_options = {});
145+
ReadResult Read(std::string table, KeySet keys,
146+
std::vector<std::string> columns,
147+
ReadOptions read_options = {});
153148
/**
154149
* @copydoc Read
155150
*
156151
* @param transaction_options Execute this read in a single-use transaction
157152
* with these options.
158153
*/
159-
StatusOr<ResultSet> Read(Transaction::SingleUseOptions transaction_options,
160-
std::string table, KeySet keys,
161-
std::vector<std::string> columns,
162-
ReadOptions read_options = {});
154+
ReadResult Read(Transaction::SingleUseOptions transaction_options,
155+
std::string table, KeySet keys,
156+
std::vector<std::string> columns,
157+
ReadOptions read_options = {});
163158
/**
164159
* @copydoc Read
165160
*
166161
* @param transaction Execute this read as part of an existing transaction.
167162
*/
168-
StatusOr<ResultSet> Read(Transaction transaction, std::string table,
169-
KeySet keys, std::vector<std::string> columns,
170-
ReadOptions read_options = {});
163+
ReadResult Read(Transaction transaction, std::string table, KeySet keys,
164+
std::vector<std::string> columns,
165+
ReadOptions read_options = {});
171166
//@}
172167

173168
/**
@@ -177,14 +172,13 @@ class Client {
177172
*
178173
* @param partition A `ReadPartition`, obtained by calling `PartitionRead`.
179174
*
180-
* @return A `StatusOr` containing a `ResultSet` or error status on failure.
181-
* No individual row in the `ResultSet` can exceed 100 MiB, and no column
182-
* value can exceed 10 MiB.
175+
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
176+
* column value can exceed 10 MiB.
183177
*
184178
* @par Example
185179
* @snippet samples.cc read-read-partition
186180
*/
187-
StatusOr<ResultSet> Read(ReadPartition const& partition);
181+
ReadResult Read(ReadPartition const& partition);
188182

189183
/**
190184
* Creates a set of partitions that can be used to execute a read
@@ -237,8 +231,8 @@ class Client {
237231
*
238232
* @param statement The SQL statement to execute.
239233
*
240-
* @note No individual row in the `ResultSet` can exceed 100 MiB, and no
241-
* column value can exceed 10 MiB.
234+
* @note No individual row in the `ExecuteQueryResult` can exceed 100 MiB, and
235+
* no column value can exceed 10 MiB.
242236
*/
243237
ExecuteQueryResult ExecuteQuery(SqlStatement statement);
244238

@@ -266,8 +260,8 @@ class Client {
266260
*
267261
* @param partition A `QueryPartition`, obtained by calling `PartitionRead`.
268262
*
269-
* @note No individual row in the `ResultSet` can exceed 100 MiB, and no
270-
* column value can exceed 10 MiB.
263+
* @note No individual row in the `ExecuteQueryResult` can exceed 100 MiB, and
264+
* no column value can exceed 10 MiB.
271265
*
272266
* @par Example
273267
* @snippet samples.cc execute-sql-query-partition

google/cloud/spanner/client_test.cc

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,26 @@ TEST(ClientTest, ReadSuccess) {
9696
)pb",
9797
&metadata));
9898
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
99-
EXPECT_CALL(*source, Stats())
100-
.WillRepeatedly(Return(optional<spanner_proto::ResultSetStats>()));
10199
EXPECT_CALL(*source, NextValue())
102100
.WillOnce(Return(optional<Value>("Steve")))
103101
.WillOnce(Return(optional<Value>(12)))
104102
.WillOnce(Return(optional<Value>("Ann")))
105103
.WillOnce(Return(optional<Value>(42)))
106104
.WillOnce(Return(optional<Value>()));
107105

108-
ResultSet result_set(std::move(source));
106+
ReadResult result_set(std::move(source));
109107
EXPECT_CALL(*conn, Read(_)).WillOnce(Return(ByMove(std::move(result_set))));
110108

111109
KeySet keys = KeySet::All();
112110
auto result = client.Read("table", std::move(keys), {"column1", "column2"});
113-
EXPECT_STATUS_OK(result);
114111

115112
using RowType = Row<std::string, std::int64_t>;
116113
auto expected = std::vector<RowType>{
117114
RowType("Steve", 12),
118115
RowType("Ann", 42),
119116
};
120117
int row_number = 0;
121-
for (auto& row : result->Rows<RowType>()) {
118+
for (auto& row : result.Rows<RowType>()) {
122119
EXPECT_STATUS_OK(row);
123120
EXPECT_EQ(*row, expected[row_number]);
124121
++row_number;
@@ -143,21 +140,18 @@ TEST(ClientTest, ReadFailure) {
143140
)pb",
144141
&metadata));
145142
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
146-
EXPECT_CALL(*source, Stats())
147-
.WillRepeatedly(Return(optional<spanner_proto::ResultSetStats>()));
148143
EXPECT_CALL(*source, NextValue())
149144
.WillOnce(Return(optional<Value>("Steve")))
150145
.WillOnce(Return(optional<Value>("Ann")))
151146
.WillOnce(Return(Status(StatusCode::kDeadlineExceeded, "deadline!")));
152147

153-
ResultSet result_set(std::move(source));
148+
ReadResult result_set(std::move(source));
154149
EXPECT_CALL(*conn, Read(_)).WillOnce(Return(ByMove(std::move(result_set))));
155150

156151
KeySet keys = KeySet::All();
157152
auto result = client.Read("table", std::move(keys), {"column1"});
158-
EXPECT_STATUS_OK(result);
159153

160-
auto rows = result->Rows<Row<std::string>>();
154+
auto rows = result.Rows<Row<std::string>>();
161155
auto iter = rows.begin();
162156
EXPECT_NE(iter, rows.end());
163157
EXPECT_STATUS_OK(*iter);
@@ -194,8 +188,6 @@ TEST(ClientTest, ExecuteQuerySuccess) {
194188
)pb",
195189
&metadata));
196190
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
197-
EXPECT_CALL(*source, Stats())
198-
.WillRepeatedly(Return(optional<spanner_proto::ResultSetStats>()));
199191
EXPECT_CALL(*source, NextValue())
200192
.WillOnce(Return(optional<Value>("Steve")))
201193
.WillOnce(Return(optional<Value>(12)))
@@ -241,8 +233,6 @@ TEST(ClientTest, ExecuteQueryFailure) {
241233
)pb",
242234
&metadata));
243235
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
244-
EXPECT_CALL(*source, Stats())
245-
.WillRepeatedly(Return(optional<spanner_proto::ResultSetStats>()));
246236
EXPECT_CALL(*source, NextValue())
247237
.WillOnce(Return(optional<Value>("Steve")))
248238
.WillOnce(Return(optional<Value>("Ann")))
@@ -329,8 +319,6 @@ TEST(ClientTest, ExecutePartitionedDml_Success) {
329319
auto source = make_unique<MockResultSetSource>();
330320
spanner_proto::ResultSetMetadata metadata;
331321
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
332-
EXPECT_CALL(*source, Stats())
333-
.WillRepeatedly(Return(optional<spanner_proto::ResultSetStats>()));
334322
EXPECT_CALL(*source, NextValue()).WillRepeatedly(Return(optional<Value>()));
335323

336324
std::string const sql_statement = "UPDATE Singers SET MarketingBudget = 1000";
@@ -418,17 +406,38 @@ TEST(ClientTest, RunTransactionCommit) {
418406
Transaction txn = MakeReadWriteTransaction(); // dummy
419407
Connection::ReadParams actual_read_params{txn, {}, {}, {}, {}};
420408
Connection::CommitParams actual_commit_params{txn, {}};
409+
410+
auto source = make_unique<MockResultSetSource>();
411+
spanner_proto::ResultSetMetadata metadata;
412+
ASSERT_TRUE(TextFormat::ParseFromString(
413+
R"pb(
414+
row_type: {
415+
fields: {
416+
name: "Name",
417+
type: { code: STRING }
418+
}
419+
}
420+
)pb",
421+
&metadata));
422+
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
423+
EXPECT_CALL(*source, NextValue())
424+
.WillOnce(Return(optional<Value>("Bob")))
425+
.WillOnce(Return(optional<Value>()));
426+
ReadResult result_set(std::move(source));
427+
421428
EXPECT_CALL(*conn, Read(_))
422-
.WillOnce(
423-
DoAll(SaveArg<0>(&actual_read_params), Return(ByMove(ResultSet{}))));
429+
.WillOnce(DoAll(SaveArg<0>(&actual_read_params),
430+
Return(ByMove(std::move(result_set)))));
424431
EXPECT_CALL(*conn, Commit(_))
425432
.WillOnce(DoAll(SaveArg<0>(&actual_commit_params),
426433
Return(CommitResult{*timestamp})));
427434

428435
auto mutation = MakeDeleteMutation("table", KeySet::All());
429436
auto f = [&mutation](Client client, Transaction txn) -> StatusOr<Mutations> {
430437
auto read = client.Read(std::move(txn), "T", KeySet::All(), {"C"});
431-
if (!read) return read.status();
438+
for (auto& row : read.Rows<Row<std::string>>()) {
439+
if (!row) return row.status();
440+
}
432441
return Mutations{mutation};
433442
};
434443

@@ -447,16 +456,35 @@ TEST(ClientTest, RunTransactionRollback) {
447456
auto conn = std::make_shared<MockConnection>();
448457
Transaction txn = MakeReadWriteTransaction(); // dummy
449458
Connection::ReadParams actual_read_params{txn, {}, {}, {}, {}};
459+
460+
auto source = make_unique<MockResultSetSource>();
461+
spanner_proto::ResultSetMetadata metadata;
462+
ASSERT_TRUE(TextFormat::ParseFromString(
463+
R"pb(
464+
row_type: {
465+
fields: {
466+
name: "Name",
467+
type: { code: INT64 }
468+
}
469+
}
470+
)pb",
471+
&metadata));
472+
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
473+
EXPECT_CALL(*source, NextValue())
474+
.WillOnce(Return(Status(StatusCode::kInvalidArgument, "blah")));
475+
ReadResult result_set(std::move(source));
476+
450477
EXPECT_CALL(*conn, Read(_))
451-
.WillOnce(
452-
DoAll(SaveArg<0>(&actual_read_params),
453-
Return(ByMove(Status(StatusCode::kInvalidArgument, "blah")))));
478+
.WillOnce(DoAll(SaveArg<0>(&actual_read_params),
479+
Return(ByMove(std::move(result_set)))));
454480
EXPECT_CALL(*conn, Rollback(_)).WillOnce(Return(Status()));
455481

456482
auto mutation = MakeDeleteMutation("table", KeySet::All());
457483
auto f = [&mutation](Client client, Transaction txn) -> StatusOr<Mutations> {
458484
auto read = client.Read(std::move(txn), "T", KeySet::All(), {"C"});
459-
if (!read) return read.status();
485+
for (auto& row : read.Rows<Row<std::string>>()) {
486+
if (!row) return row.status();
487+
}
460488
return Mutations{mutation};
461489
};
462490

@@ -475,17 +503,36 @@ TEST(ClientTest, RunTransactionRollbackError) {
475503
auto conn = std::make_shared<MockConnection>();
476504
Transaction txn = MakeReadWriteTransaction(); // dummy
477505
Connection::ReadParams actual_read_params{txn, {}, {}, {}, {}};
506+
507+
auto source = make_unique<MockResultSetSource>();
508+
spanner_proto::ResultSetMetadata metadata;
509+
ASSERT_TRUE(TextFormat::ParseFromString(
510+
R"pb(
511+
row_type: {
512+
fields: {
513+
name: "Name",
514+
type: { code: INT64 }
515+
}
516+
}
517+
)pb",
518+
&metadata));
519+
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
520+
EXPECT_CALL(*source, NextValue())
521+
.WillOnce(Return(Status(StatusCode::kInvalidArgument, "blah")));
522+
ReadResult result_set(std::move(source));
523+
478524
EXPECT_CALL(*conn, Read(_))
479-
.WillOnce(
480-
DoAll(SaveArg<0>(&actual_read_params),
481-
Return(ByMove(Status(StatusCode::kInvalidArgument, "blah")))));
525+
.WillOnce(DoAll(SaveArg<0>(&actual_read_params),
526+
Return(ByMove(std::move(result_set)))));
482527
EXPECT_CALL(*conn, Rollback(_))
483528
.WillOnce(Return(Status(StatusCode::kInternal, "oops")));
484529

485530
auto mutation = MakeDeleteMutation("table", KeySet::All());
486531
auto f = [&mutation](Client client, Transaction txn) -> StatusOr<Mutations> {
487532
auto read = client.Read(std::move(txn), "T", KeySet::All(), {"C"});
488-
if (!read) return read.status();
533+
for (auto& row : read.Rows<Row<std::string>>()) {
534+
if (!row) return row.status();
535+
}
489536
return Mutations{mutation};
490537
};
491538

@@ -503,14 +550,33 @@ TEST(ClientTest, RunTransactionRollbackError) {
503550
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
504551
TEST(ClientTest, RunTransactionException) {
505552
auto conn = std::make_shared<MockConnection>();
506-
EXPECT_CALL(*conn, Read(_))
507-
.WillOnce(Return(ByMove(Status(StatusCode::kInvalidArgument, "blah"))));
553+
554+
auto source = make_unique<MockResultSetSource>();
555+
spanner_proto::ResultSetMetadata metadata;
556+
ASSERT_TRUE(TextFormat::ParseFromString(
557+
R"pb(
558+
row_type: {
559+
fields: {
560+
name: "Name",
561+
type: { code: INT64 }
562+
}
563+
}
564+
)pb",
565+
&metadata));
566+
EXPECT_CALL(*source, Metadata()).WillRepeatedly(Return(metadata));
567+
EXPECT_CALL(*source, NextValue())
568+
.WillOnce(Return(Status(StatusCode::kInvalidArgument, "blah")));
569+
ReadResult result_set(std::move(source));
570+
571+
EXPECT_CALL(*conn, Read(_)).WillOnce(Return(ByMove(std::move(result_set))));
508572
EXPECT_CALL(*conn, Rollback(_)).WillOnce(Return(Status()));
509573

510574
auto mutation = MakeDeleteMutation("table", KeySet::All());
511575
auto f = [&mutation](Client client, Transaction txn) -> StatusOr<Mutations> {
512576
auto read = client.Read(std::move(txn), "T", KeySet::All(), {"C"});
513-
if (!read) throw "Read() error";
577+
for (auto& row : read.Rows<Row<std::string>>()) {
578+
if (!row) throw "Read() error";
579+
}
514580
return Mutations{mutation};
515581
};
516582

google/cloud/spanner/connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Connection {
133133
//@}
134134

135135
/// Define the interface for a google.spanner.v1.Spanner.Read RPC
136-
virtual StatusOr<ResultSet> Read(ReadParams) = 0;
136+
virtual ReadResult Read(ReadParams) = 0;
137137

138138
/// Define the interface for a google.spanner.v1.Spanner.PartitionRead RPC
139139
virtual StatusOr<std::vector<ReadPartition>> PartitionRead(

0 commit comments

Comments
 (0)