Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions google/cloud/bigtable/internal/query_plan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ namespace bigtable_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

std::string const& QueryPlan::prepared_query() const {
return response_.prepared_query();
std::lock_guard<std::mutex> lock(mu_);
auto valid_until = std::chrono::system_clock::time_point(
std::chrono::seconds(response_.valid_until().seconds()) +
std::chrono::nanoseconds(response_.valid_until().nanos()));
if (valid_until > std::chrono::system_clock::now()) {
return response_.prepared_query();
}
static std::string const kEmpty;
return kEmpty;
}

google::bigtable::v2::ResultSetMetadata const& QueryPlan::metadata() const {
return response_.metadata();
std::lock_guard<std::mutex> lock(mu_);
auto valid_until = std::chrono::system_clock::time_point(
std::chrono::seconds(response_.valid_until().seconds()) +
std::chrono::nanoseconds(response_.valid_until().nanos()));
if (valid_until > std::chrono::system_clock::now()) {
return response_.metadata();
}
static google::bigtable::v2::ResultSetMetadata const kEmpty;
return kEmpty;
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/bigtable/internal/query_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class QueryPlan : public std::enable_shared_from_this<QueryPlan> {
static std::shared_ptr<QueryPlan> Create(
CompletionQueue cq, google::bigtable::v2::PrepareQueryResponse response,
RefreshFn fn) {
return std::shared_ptr<QueryPlan>(
auto plan = std::shared_ptr<QueryPlan>(
Copy link
Member

Choose a reason for hiding this comment

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

Let's go ahead and move the implementation of this function into the .cc file along with the other member functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moving this implementation into the .cc file, thanks for catching this. Additionally, the implementations of the other member functions are currently present in .cc.

new QueryPlan(std::move(cq), std::move(response), std::move(fn)));
plan->Initialize();
return plan;
}

// Accessor for the prepared_query field in response_.
Expand Down
68 changes: 58 additions & 10 deletions google/cloud/bigtable/internal/query_plan_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,73 @@
// limitations under the License.

#include "google/cloud/bigtable/internal/query_plan.h"
#include "google/cloud/completion_queue.h"
#include "google/cloud/testing_util/is_proto_equal.h"
#include "google/cloud/testing_util/mock_completion_queue_impl.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <google/bigtable/v2/data.pb.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/time_util.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace google {
namespace cloud {
namespace bigtable_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

/// @test Basic test to confirm compilation and dummy behavior.
/// Remove this after implementing QueryPlan.
TEST(QueryPlanTest, TestDummyMethods) {
auto cq = CompletionQueue();
auto response = google::bigtable::v2::PrepareQueryResponse();
auto fn = QueryPlan::RefreshFn();
auto created = QueryPlan::Create(cq, response, fn);
ASSERT_EQ("", (*created).prepared_query());
ASSERT_EQ(0, created->metadata().ByteSizeLong());
using ::google::cloud::testing_util::MockCompletionQueueImpl;
using ::google::protobuf::TextFormat;
using ::google::protobuf::util::TimeUtil;
using ::testing::IsEmpty;
using ::testing::Not;

TEST(QueryPlanTest, Accessors) {
auto mock_cq = std::make_shared<MockCompletionQueueImpl>();
CompletionQueue cq(mock_cq);
google::bigtable::v2::PrepareQueryResponse response;
response.set_prepared_query("test-query");
auto constexpr kResultMetadataText = R"pb(
proto_schema {
columns {
name: "user_id"
type { string_type {} }
}
}
)pb";
google::bigtable::v2::ResultSetMetadata metadata;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kResultMetadataText,
&metadata));
*response.mutable_metadata() = metadata;
*response.mutable_valid_until() =
TimeUtil::GetCurrentTime() + TimeUtil::SecondsToDuration(300);

auto plan = QueryPlan::Create(cq, response, [] {
return google::bigtable::v2::PrepareQueryResponse{};
});

EXPECT_EQ(plan->prepared_query(), "test-query");
EXPECT_EQ(plan->metadata().DebugString(), metadata.DebugString());
}

TEST(QueryPlanTest, Expired) {
auto mock_cq = std::make_shared<MockCompletionQueueImpl>();
CompletionQueue cq(mock_cq);
google::bigtable::v2::PrepareQueryResponse response;
response.set_prepared_query("test-query");
*response.mutable_metadata() = google::bigtable::v2::ResultSetMetadata{};

// Set the expiration time to the past.
*response.mutable_valid_until() =
TimeUtil::GetCurrentTime() - TimeUtil::SecondsToDuration(1);

auto plan = QueryPlan::Create(cq, response, [] {
return google::bigtable::v2::PrepareQueryResponse{};
});

EXPECT_THAT(plan->prepared_query(), IsEmpty());
EXPECT_EQ(plan->metadata().DebugString(),
google::bigtable::v2::ResultSetMetadata{}.DebugString());
}

} // namespace
Expand Down
Loading