Skip to content

Commit f149191

Browse files
authored
feat(bigtable): add expiration logic to query plan implementation (#15647)
* feat(bigtable): add expiration logic to query plan implementation
1 parent 04f7a48 commit f149191

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

google/cloud/bigtable/internal/query_plan.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,30 @@ namespace cloud {
2121
namespace bigtable_internal {
2222
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2323

24-
std::string const& QueryPlan::prepared_query() const {
24+
std::shared_ptr<QueryPlan> QueryPlan::Create(
25+
CompletionQueue cq, google::bigtable::v2::PrepareQueryResponse response,
26+
RefreshFn fn) {
27+
auto plan = std::shared_ptr<QueryPlan>(
28+
new QueryPlan(std::move(cq), std::move(response), std::move(fn)));
29+
plan->Initialize();
30+
return plan;
31+
}
32+
33+
bool QueryPlan::IsExpired() { return false; }
34+
35+
StatusOr<std::string> QueryPlan::prepared_query() const {
36+
std::lock_guard<std::mutex> lock(mu_);
37+
if (IsExpired()) {
38+
return Status(StatusCode::kUnavailable, "Query plan has expired");
39+
}
2540
return response_.prepared_query();
2641
}
2742

28-
google::bigtable::v2::ResultSetMetadata const& QueryPlan::metadata() const {
43+
StatusOr<google::bigtable::v2::ResultSetMetadata> QueryPlan::metadata() const {
44+
std::lock_guard<std::mutex> lock(mu_);
45+
if (IsExpired()) {
46+
return Status(StatusCode::kUnavailable, "Query plan has expired");
47+
}
2948
return response_.metadata();
3049
}
3150

google/cloud/bigtable/internal/query_plan.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,21 @@ class QueryPlan : public std::enable_shared_from_this<QueryPlan> {
3636
// Calls the constructor and then Initialize.
3737
static std::shared_ptr<QueryPlan> Create(
3838
CompletionQueue cq, google::bigtable::v2::PrepareQueryResponse response,
39-
RefreshFn fn) {
40-
return std::shared_ptr<QueryPlan>(
41-
new QueryPlan(std::move(cq), std::move(response), std::move(fn)));
42-
}
39+
RefreshFn fn);
4340

4441
// Accessor for the prepared_query field in response_.
45-
std::string const& prepared_query() const;
42+
StatusOr<std::string> prepared_query() const;
4643

4744
// Accessor for the metadata field in response_.
48-
google::bigtable::v2::ResultSetMetadata const& metadata() const;
45+
StatusOr<google::bigtable::v2::ResultSetMetadata> metadata() const;
4946

5047
private:
5148
QueryPlan(CompletionQueue cq,
5249
google::bigtable::v2::PrepareQueryResponse response, RefreshFn fn)
5350
: cq_(std::move(cq)),
5451
response_(std::move(response)),
5552
fn_(std::move(fn)) {}
53+
static bool IsExpired();
5654

5755
// Performs the first call to ScheduleRefresh and any other initialization not
5856
// possible in the constructor.

google/cloud/bigtable/internal/query_plan_test.cc

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,55 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/bigtable/internal/query_plan.h"
16-
#include "google/cloud/completion_queue.h"
16+
#include "google/cloud/testing_util/is_proto_equal.h"
17+
#include "google/cloud/testing_util/mock_completion_queue_impl.h"
18+
#include "google/cloud/testing_util/status_matchers.h"
1719
#include <google/bigtable/v2/data.pb.h>
20+
#include <google/protobuf/text_format.h>
21+
#include <google/protobuf/util/time_util.h>
1822
#include <gmock/gmock.h>
23+
#include <gtest/gtest.h>
1924

2025
namespace google {
2126
namespace cloud {
2227
namespace bigtable_internal {
2328
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2429
namespace {
2530

26-
/// @test Basic test to confirm compilation and dummy behavior.
27-
/// Remove this after implementing QueryPlan.
28-
TEST(QueryPlanTest, TestDummyMethods) {
29-
auto cq = CompletionQueue();
30-
auto response = google::bigtable::v2::PrepareQueryResponse();
31-
auto fn = QueryPlan::RefreshFn();
32-
auto created = QueryPlan::Create(cq, response, fn);
33-
ASSERT_EQ("", (*created).prepared_query());
34-
ASSERT_EQ(0, created->metadata().ByteSizeLong());
31+
using ::google::cloud::testing_util::IsProtoEqual;
32+
using ::google::cloud::testing_util::MockCompletionQueueImpl;
33+
using ::google::protobuf::util::TimeUtil;
34+
35+
TEST(QueryPlanTest, Accessors) {
36+
auto mock_cq = std::make_shared<MockCompletionQueueImpl>();
37+
CompletionQueue cq(mock_cq);
38+
google::bigtable::v2::PrepareQueryResponse response;
39+
response.set_prepared_query("test-query");
40+
auto constexpr kResultMetadataText = R"pb(
41+
proto_schema {
42+
columns {
43+
name: "user_id"
44+
type { string_type {} }
45+
}
46+
}
47+
)pb";
48+
google::bigtable::v2::ResultSetMetadata metadata;
49+
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kResultMetadataText,
50+
&metadata));
51+
*response.mutable_metadata() = metadata;
52+
*response.mutable_valid_until() =
53+
TimeUtil::GetCurrentTime() + TimeUtil::SecondsToDuration(300);
54+
55+
auto plan = QueryPlan::Create(cq, response, [] {
56+
return google::bigtable::v2::PrepareQueryResponse{};
57+
});
58+
59+
auto prepared_query = plan->prepared_query();
60+
ASSERT_STATUS_OK(prepared_query);
61+
EXPECT_EQ(*prepared_query, "test-query");
62+
auto actual_metadata = plan->metadata();
63+
ASSERT_STATUS_OK(actual_metadata);
64+
EXPECT_THAT(*actual_metadata, IsProtoEqual(metadata));
3565
}
3666

3767
} // namespace

0 commit comments

Comments
 (0)