|
| 1 | + |
| 2 | +// Copyright 2025 Google LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_QUERY_PLAN_H |
| 17 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_QUERY_PLAN_H |
| 18 | + |
| 19 | +#include "google/cloud/bigtable/version.h" |
| 20 | +#include "google/cloud/completion_queue.h" |
| 21 | +#include <google/bigtable/v2/bigtable.pb.h> |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +namespace google { |
| 26 | +namespace cloud { |
| 27 | +namespace bigtable_internal { |
| 28 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 29 | + |
| 30 | +class QueryPlan : public std::enable_shared_from_this<QueryPlan> { |
| 31 | + public: |
| 32 | + // Typically, a lambda capturing the original PrepareQueryRequest and |
| 33 | + // DataConnection pointer necessary to call the PrepareQuery RPC. |
| 34 | + using RefreshFn = std::function<google::bigtable::v2::PrepareQueryResponse()>; |
| 35 | + |
| 36 | + // Calls the constructor and then Initialize. |
| 37 | + static std::shared_ptr<QueryPlan> Create( |
| 38 | + 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 | + } |
| 43 | + |
| 44 | + // Accessor for the prepared_query field in response_. |
| 45 | + std::string const& prepared_query() const; |
| 46 | + |
| 47 | + // Accessor for the metadata field in response_. |
| 48 | + google::bigtable::v2::ResultSetMetadata const& metadata() const; |
| 49 | + |
| 50 | + private: |
| 51 | + QueryPlan(CompletionQueue cq, |
| 52 | + google::bigtable::v2::PrepareQueryResponse response, RefreshFn fn) |
| 53 | + : cq_(std::move(cq)), |
| 54 | + response_(std::move(response)), |
| 55 | + fn_(std::move(fn)) {} |
| 56 | + |
| 57 | + // Performs the first call to ScheduleRefresh and any other initialization not |
| 58 | + // possible in the constructor. |
| 59 | + void Initialize() {} |
| 60 | + |
| 61 | + // Calls MakeDeadlineTimer on the CompletionQueue with a continuation lambda |
| 62 | + // capturing a std::weak_ptr to this that calls RefreshQueryPlan. |
| 63 | + void ScheduleRefresh() {} |
| 64 | + |
| 65 | + // Performs the synchronization around calling RefreshFn and updating |
| 66 | + // response_. |
| 67 | + void RefreshQueryPlan() {} |
| 68 | + |
| 69 | + CompletionQueue cq_; |
| 70 | + future<void> refresh_timer_; |
| 71 | + mutable std::mutex mu_; |
| 72 | + std::condition_variable cond_; |
| 73 | + google::bigtable::v2::PrepareQueryResponse response_; // GUARDED_BY(mu_) |
| 74 | + RefreshFn fn_; |
| 75 | +}; |
| 76 | + |
| 77 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 78 | +} // namespace bigtable_internal |
| 79 | +} // namespace cloud |
| 80 | +} // namespace google |
| 81 | + |
| 82 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_QUERY_PLAN_H |
0 commit comments