Skip to content

Commit 570927e

Browse files
authored
chore(spanner): migrate to std::optional (#16201)
1 parent 1540e6e commit 570927e

43 files changed

Lines changed: 362 additions & 364 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

google/cloud/spanner/client.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include "google/cloud/internal/retry_loop.h"
2828
#include "google/cloud/log.h"
2929
#include "google/cloud/status.h"
30-
#include "absl/types/optional.h"
3130
#include <grpcpp/grpcpp.h>
3231
#include <chrono>
32+
#include <optional>
3333
#include <thread>
3434

3535
namespace google {
@@ -59,7 +59,7 @@ RowStream Client::Read(std::string table, KeySet keys,
5959
return conn_->Read({spanner_internal::MakeSingleUseTransaction(
6060
Transaction::ReadOnlyOptions()),
6161
std::move(table), std::move(keys), std::move(columns),
62-
ToReadOptions(internal::CurrentOptions()), absl::nullopt,
62+
ToReadOptions(internal::CurrentOptions()), std::nullopt,
6363
false, std::move(directed_read_option),
6464
std::move(order_by), std::move(lock_hint)});
6565
}
@@ -75,7 +75,7 @@ RowStream Client::Read(Transaction::SingleUseOptions transaction_options,
7575
return conn_->Read({spanner_internal::MakeSingleUseTransaction(
7676
std::move(transaction_options)),
7777
std::move(table), std::move(keys), std::move(columns),
78-
ToReadOptions(internal::CurrentOptions()), absl::nullopt,
78+
ToReadOptions(internal::CurrentOptions()), std::nullopt,
7979
false, std::move(directed_read_option),
8080
std::move(order_by), std::move(lock_hint)});
8181
}
@@ -89,7 +89,7 @@ RowStream Client::Read(Transaction transaction, std::string table, KeySet keys,
8989
internal::OptionsSpan span(std::move(opts));
9090
return conn_->Read({std::move(transaction), std::move(table), std::move(keys),
9191
std::move(columns),
92-
ToReadOptions(internal::CurrentOptions()), absl::nullopt,
92+
ToReadOptions(internal::CurrentOptions()), std::nullopt,
9393
false, std::move(directed_read_option),
9494
std::move(order_by), std::move(lock_hint)});
9595
}
@@ -112,7 +112,7 @@ StatusOr<std::vector<ReadPartition>> Client::PartitionRead(
112112
return conn_->PartitionRead(
113113
{{std::move(transaction), std::move(table), std::move(keys),
114114
std::move(columns), ToReadOptions(internal::CurrentOptions()),
115-
absl::nullopt, false, DirectedReadOption::Type{},
115+
std::nullopt, false, DirectedReadOption::Type{},
116116
OrderBy::kOrderByUnspecified, LockHint::kLockHintUnspecified},
117117
ToPartitionOptions(internal::CurrentOptions())});
118118
}
@@ -125,7 +125,7 @@ RowStream Client::ExecuteQuery(SqlStatement statement, Options opts) {
125125
{spanner_internal::MakeSingleUseTransaction(
126126
Transaction::ReadOnlyOptions()),
127127
std::move(statement), QueryOptions(internal::CurrentOptions()),
128-
absl::nullopt, false, std::move(directed_read_option)});
128+
std::nullopt, false, std::move(directed_read_option)});
129129
}
130130

131131
RowStream Client::ExecuteQuery(
@@ -138,7 +138,7 @@ RowStream Client::ExecuteQuery(
138138
{spanner_internal::MakeSingleUseTransaction(
139139
std::move(transaction_options)),
140140
std::move(statement), QueryOptions(internal::CurrentOptions()),
141-
absl::nullopt, false, std::move(directed_read_option)});
141+
std::nullopt, false, std::move(directed_read_option)});
142142
}
143143

144144
RowStream Client::ExecuteQuery(Transaction transaction, SqlStatement statement,
@@ -148,7 +148,7 @@ RowStream Client::ExecuteQuery(Transaction transaction, SqlStatement statement,
148148
internal::OptionsSpan span(std::move(opts));
149149
return conn_->ExecuteQuery({std::move(transaction), std::move(statement),
150150
QueryOptions(internal::CurrentOptions()),
151-
absl::nullopt, false,
151+
std::nullopt, false,
152152
std::move(directed_read_option)});
153153
}
154154

@@ -169,7 +169,7 @@ ProfileQueryResult Client::ProfileQuery(SqlStatement statement, Options opts) {
169169
{spanner_internal::MakeSingleUseTransaction(
170170
Transaction::ReadOnlyOptions()),
171171
std::move(statement), QueryOptions(internal::CurrentOptions()),
172-
absl::nullopt, false, std::move(directed_read_option)});
172+
std::nullopt, false, std::move(directed_read_option)});
173173
}
174174

175175
ProfileQueryResult Client::ProfileQuery(
@@ -182,7 +182,7 @@ ProfileQueryResult Client::ProfileQuery(
182182
{spanner_internal::MakeSingleUseTransaction(
183183
std::move(transaction_options)),
184184
std::move(statement), QueryOptions(internal::CurrentOptions()),
185-
absl::nullopt, false, std::move(directed_read_option)});
185+
std::nullopt, false, std::move(directed_read_option)});
186186
}
187187

188188
ProfileQueryResult Client::ProfileQuery(Transaction transaction,
@@ -192,7 +192,7 @@ ProfileQueryResult Client::ProfileQuery(Transaction transaction,
192192
internal::OptionsSpan span(std::move(opts));
193193
return conn_->ProfileQuery({std::move(transaction), std::move(statement),
194194
QueryOptions(internal::CurrentOptions()),
195-
absl::nullopt, false,
195+
std::nullopt, false,
196196
std::move(directed_read_option)});
197197
}
198198

@@ -209,7 +209,7 @@ StatusOr<DmlResult> Client::ExecuteDml(Transaction transaction,
209209
internal::OptionsSpan span(internal::MergeOptions(std::move(opts), opts_));
210210
return conn_->ExecuteDml({std::move(transaction), std::move(statement),
211211
QueryOptions(internal::CurrentOptions()),
212-
absl::nullopt, false, DirectedReadOption::Type{}});
212+
std::nullopt, false, DirectedReadOption::Type{}});
213213
}
214214

215215
StatusOr<ProfileDmlResult> Client::ProfileDml(Transaction transaction,
@@ -218,7 +218,7 @@ StatusOr<ProfileDmlResult> Client::ProfileDml(Transaction transaction,
218218
internal::OptionsSpan span(internal::MergeOptions(std::move(opts), opts_));
219219
return conn_->ProfileDml({std::move(transaction), std::move(statement),
220220
QueryOptions(internal::CurrentOptions()),
221-
absl::nullopt, false, DirectedReadOption::Type{}});
221+
std::nullopt, false, DirectedReadOption::Type{}});
222222
}
223223

224224
StatusOr<ExecutionPlan> Client::AnalyzeSql(Transaction transaction,
@@ -227,7 +227,7 @@ StatusOr<ExecutionPlan> Client::AnalyzeSql(Transaction transaction,
227227
internal::OptionsSpan span(internal::MergeOptions(std::move(opts), opts_));
228228
return conn_->AnalyzeSql({std::move(transaction), std::move(statement),
229229
QueryOptions(internal::CurrentOptions()),
230-
absl::nullopt, false, DirectedReadOption::Type{}});
230+
std::nullopt, false, DirectedReadOption::Type{}});
231231
}
232232

233233
StatusOr<BatchDmlResult> Client::ExecuteBatchDml(

google/cloud/spanner/client_test.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#include "google/cloud/testing_util/is_proto_equal.h"
2727
#include "google/cloud/testing_util/scoped_environment.h"
2828
#include "google/cloud/testing_util/status_matchers.h"
29-
#include "absl/types/optional.h"
3029
#include <google/protobuf/text_format.h>
3130
#include <gmock/gmock.h>
3231
#include <array>
3332
#include <chrono>
3433
#include <cstdint>
34+
#include <optional>
3535
#include <string>
3636
#include <utility>
3737
#include <vector>
@@ -481,7 +481,7 @@ TEST(ClientTest, CommitMutatorSuccess) {
481481
Return(ByMove(RowStream(std::move(source))))));
482482
EXPECT_CALL(*conn, Commit)
483483
.WillOnce(DoAll(SaveArg<0>(&actual_commit_params),
484-
Return(CommitResult{*timestamp, absl::nullopt})));
484+
Return(CommitResult{*timestamp, std::nullopt})));
485485

486486
Client client(conn);
487487
auto mutation = MakeDeleteMutation("table", KeySet::All());
@@ -668,7 +668,7 @@ TEST(ClientTest, CommitMutatorRerunTransientFailures) {
668668
return Status(StatusCode::kAborted, "Aborted transaction");
669669
})
670670
.WillOnce([&timestamp](Connection::CommitParams const&) {
671-
return CommitResult{*timestamp, absl::nullopt};
671+
return CommitResult{*timestamp, std::nullopt};
672672
});
673673

674674
auto mutator = [](Transaction const&) -> StatusOr<Mutations> {
@@ -740,7 +740,7 @@ TEST(ClientTest, CommitMutations) {
740740
EXPECT_CALL(*conn, Commit)
741741
.WillOnce([&mutation, &timestamp](Connection::CommitParams const& cp) {
742742
EXPECT_EQ(cp.mutations, Mutations{mutation});
743-
return CommitResult{*timestamp, absl::nullopt};
743+
return CommitResult{*timestamp, std::nullopt};
744744
});
745745

746746
Client client(conn);
@@ -886,7 +886,7 @@ TEST(ClientTest, CommitMutatorWithTags) {
886886
.WillOnce([&](Connection::CommitParams const& params) {
887887
EXPECT_EQ(params.options.transaction_tag(), transaction_tag);
888888
EXPECT_THAT(params.transaction, HasTag(transaction_tag));
889-
return CommitResult{*timestamp, absl::nullopt};
889+
return CommitResult{*timestamp, std::nullopt};
890890
});
891891

892892
Client client(conn);
@@ -928,7 +928,7 @@ TEST(ClientTest, CommitMutatorSessionAffinity) {
928928
EXPECT_THAT(cp.transaction, HasSession(session_name));
929929
EXPECT_THAT(cp.transaction, HasBegin());
930930
SetTransactionId(cp.transaction, "last-transaction-id");
931-
return CommitResult{*timestamp, absl::nullopt};
931+
return CommitResult{*timestamp, std::nullopt};
932932
});
933933
// But only after some aborts, the first of which sets the session.
934934
EXPECT_CALL(*conn, Commit)
@@ -967,7 +967,7 @@ TEST(ClientTest, CommitMutatorSessionNotFound) {
967967
EXPECT_CALL(*conn, Commit)
968968
.WillOnce([&timestamp](Connection::CommitParams const& cp) {
969969
EXPECT_THAT(cp.transaction, HasSession("session-3"));
970-
return CommitResult{*timestamp, absl::nullopt};
970+
return CommitResult{*timestamp, std::nullopt};
971971
});
972972

973973
int n = 0;
@@ -998,7 +998,7 @@ TEST(ClientTest, CommitSessionNotFound) {
998998
})
999999
.WillOnce([&timestamp](Connection::CommitParams const& cp) {
10001000
EXPECT_THAT(cp.transaction, HasSession("session-2"));
1001-
return CommitResult{*timestamp, absl::nullopt};
1001+
return CommitResult{*timestamp, std::nullopt};
10021002
});
10031003

10041004
int n = 0;
@@ -1046,7 +1046,7 @@ TEST(ClientTest, MaxCommitDelay) {
10461046
.WillOnce([&timestamp](Connection::CommitParams const& cp) {
10471047
EXPECT_EQ(cp.options.max_commit_delay(),
10481048
std::chrono::milliseconds(100));
1049-
return CommitResult{*timestamp, absl::nullopt};
1049+
return CommitResult{*timestamp, std::nullopt};
10501050
});
10511051

10521052
Client client(conn);
@@ -1074,7 +1074,7 @@ TEST(ClientTest, CommitAtLeastOnce) {
10741074
EXPECT_FALSE(cp.options.request_priority().has_value());
10751075
EXPECT_FALSE(cp.options.max_commit_delay().has_value());
10761076
EXPECT_EQ(cp.options.transaction_tag(), transaction_tag);
1077-
return CommitResult{*timestamp, absl::nullopt};
1077+
return CommitResult{*timestamp, std::nullopt};
10781078
});
10791079

10801080
Client client(conn);

google/cloud/spanner/commit_options.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "google/cloud/spanner/request_priority.h"
1919
#include "google/cloud/spanner/version.h"
2020
#include "google/cloud/options.h"
21-
#include "absl/types/optional.h"
2221
#include <chrono>
22+
#include <optional>
2323
#include <string>
2424

2525
namespace google {
@@ -70,13 +70,13 @@ class CommitOptions {
7070

7171
/// Set the priority of the `spanner::Client::Commit()` call.
7272
CommitOptions& set_request_priority(
73-
absl::optional<RequestPriority> request_priority) {
73+
std::optional<RequestPriority> request_priority) {
7474
request_priority_ = std::move(request_priority);
7575
return *this;
7676
}
7777

7878
/// The priority of the `spanner::Client::Commit()` call.
79-
absl::optional<RequestPriority> request_priority() const {
79+
std::optional<RequestPriority> request_priority() const {
8080
return request_priority_;
8181
}
8282

@@ -85,35 +85,35 @@ class CommitOptions {
8585
* Ignored for the overload that already takes a `spanner::Transaction`.
8686
*/
8787
CommitOptions& set_transaction_tag(
88-
absl::optional<std::string> transaction_tag) {
88+
std::optional<std::string> transaction_tag) {
8989
transaction_tag_ = std::move(transaction_tag);
9090
return *this;
9191
}
9292

9393
/// The transaction tag for the `spanner::Client::Commit()` call.
94-
absl::optional<std::string> const& transaction_tag() const {
94+
std::optional<std::string> const& transaction_tag() const {
9595
return transaction_tag_;
9696
}
9797

9898
// Set the max commit delay of the `spanner::Client::Commit()` call.
9999
CommitOptions& set_max_commit_delay(
100-
absl::optional<std::chrono::milliseconds> max_commit_delay) {
100+
std::optional<std::chrono::milliseconds> max_commit_delay) {
101101
max_commit_delay_ = std::move(max_commit_delay);
102102
return *this;
103103
}
104104

105105
// The max commit delay for the `spanner::Client::Commit()` call.
106-
absl::optional<std::chrono::milliseconds> const& max_commit_delay() const {
106+
std::optional<std::chrono::milliseconds> const& max_commit_delay() const {
107107
return max_commit_delay_;
108108
}
109109

110110
private:
111111
// Note that CommitRequest.request_options.request_tag is ignored,
112112
// so we do not even provide a mechanism to specify one.
113113
bool return_stats_ = false;
114-
absl::optional<RequestPriority> request_priority_;
115-
absl::optional<std::string> transaction_tag_;
116-
absl::optional<std::chrono::milliseconds> max_commit_delay_;
114+
std::optional<RequestPriority> request_priority_;
115+
std::optional<std::string> transaction_tag_;
116+
std::optional<std::chrono::milliseconds> max_commit_delay_;
117117
};
118118

119119
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

google/cloud/spanner/commit_result.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "google/cloud/spanner/version.h"
2020
#include "google/cloud/status_or.h"
2121
#include "google/cloud/stream_range.h"
22-
#include "absl/types/optional.h"
2322
#include <cstddef>
2423
#include <cstdint>
24+
#include <optional>
2525
#include <vector>
2626

2727
namespace google {
@@ -44,7 +44,7 @@ struct CommitResult {
4444
Timestamp commit_timestamp;
4545

4646
/// Additional statistics about the committed transaction.
47-
absl::optional<CommitStats> commit_stats;
47+
std::optional<CommitStats> commit_stats;
4848
};
4949

5050
/**

google/cloud/spanner/connection.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class StatusOnlyResultSetSource : public ResultSourceInterface {
2929
: status_(std::move(status)) {}
3030

3131
StatusOr<Row> NextRow() override { return status_; }
32-
absl::optional<google::spanner::v1::ResultSetMetadata> Metadata() override {
32+
std::optional<google::spanner::v1::ResultSetMetadata> Metadata() override {
3333
return {};
3434
}
35-
absl::optional<google::spanner::v1::ResultSetStats> Stats() const override {
35+
std::optional<google::spanner::v1::ResultSetStats> Stats() const override {
3636
return {};
3737
}
3838

google/cloud/spanner/connection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "google/cloud/optional.h"
3535
#include "google/cloud/options.h"
3636
#include "google/cloud/status_or.h"
37-
#include "absl/types/optional.h"
37+
#include <optional>
3838
#include <string>
3939
#include <vector>
4040

@@ -80,7 +80,7 @@ class Connection {
8080
KeySet keys;
8181
std::vector<std::string> columns;
8282
ReadOptions read_options;
83-
absl::optional<std::string> partition_token;
83+
std::optional<std::string> partition_token;
8484
bool partition_data_boost = false; // when partition_token
8585
DirectedReadOption::Type directed_read_option;
8686
OrderBy order_by;
@@ -99,7 +99,7 @@ class Connection {
9999
Transaction transaction;
100100
SqlStatement statement;
101101
QueryOptions query_options;
102-
absl::optional<std::string> partition_token;
102+
std::optional<std::string> partition_token;
103103
bool partition_data_boost = false; // when partition_token
104104
DirectedReadOption::Type directed_read_option;
105105
};

google/cloud/spanner/directed_read_replicas.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_DIRECTED_READ_REPLICAS_H
1717

1818
#include "google/cloud/spanner/version.h"
19-
#include "absl/types/optional.h"
2019
#include <initializer_list>
20+
#include <optional>
2121
#include <string>
2222
#include <utility>
2323
#include <vector>
@@ -53,19 +53,19 @@ class ReplicaSelection {
5353
// Replicas in the location, of any available type, will be used to
5454
// process the request.
5555
explicit ReplicaSelection(std::string location)
56-
: location_(std::move(location)), type_(absl::nullopt) {}
56+
: location_(std::move(location)), type_(std::nullopt) {}
5757

5858
// Replicas of the given type, in the nearest available location, will
5959
// be used to process the request.
6060
explicit ReplicaSelection(ReplicaType type)
61-
: location_(absl::nullopt), type_(type) {}
61+
: location_(std::nullopt), type_(type) {}
6262

63-
absl::optional<std::string> const& location() const { return location_; }
64-
absl::optional<ReplicaType> const& type() const { return type_; }
63+
std::optional<std::string> const& location() const { return location_; }
64+
std::optional<ReplicaType> const& type() const { return type_; }
6565

6666
private:
67-
absl::optional<std::string> location_;
68-
absl::optional<ReplicaType> type_;
67+
std::optional<std::string> location_;
68+
std::optional<ReplicaType> type_;
6969
};
7070

7171
inline bool operator==(ReplicaSelection const& a, ReplicaSelection const& b) {

google/cloud/spanner/doc/spanner-mocking.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ returned one value at a time, even if a row contains multiple values.
5050

5151
@snippet mock_execute_query.cc simulate-streaming-results
5252

53-
Note that the last value in the stream is indicated by an absl::optional
53+
Note that the last value in the stream is indicated by an std::optional
5454
without a value:
5555

5656
@snippet mock_execute_query.cc simulate-streaming-end

0 commit comments

Comments
 (0)