|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/bigtable/query_row.h" |
| 16 | +#include "google/cloud/internal/make_status.h" |
| 17 | +#include "google/cloud/log.h" |
| 18 | +#include "google/cloud/status.h" |
| 19 | +#include "google/cloud/status_or.h" |
| 20 | +#include <algorithm> |
| 21 | +#include <iterator> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +namespace google { |
| 25 | +namespace cloud { |
| 26 | +namespace bigtable_internal { |
| 27 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 28 | +bigtable::QueryRow QueryRowFriend::MakeQueryRow( |
| 29 | + std::vector<bigtable::Value> values, |
| 30 | + std::shared_ptr<std::vector<std::string> const> columns) { |
| 31 | + return bigtable::QueryRow(std::move(values), std::move(columns)); |
| 32 | +} |
| 33 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 34 | +} // namespace bigtable_internal |
| 35 | + |
| 36 | +namespace bigtable { |
| 37 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 38 | + |
| 39 | +QueryRow::QueryRow() |
| 40 | + : QueryRow({}, std::make_shared<std::vector<std::string>>()) {} |
| 41 | + |
| 42 | +QueryRow::QueryRow(std::vector<Value> values, |
| 43 | + std::shared_ptr<std::vector<std::string> const> columns) |
| 44 | + : values_(std::move(values)), columns_(std::move(columns)) { |
| 45 | + if (values_.size() != columns_->size()) { |
| 46 | + GCP_LOG(FATAL) << "QueryRow's value and column sizes do not match: " |
| 47 | + << values_.size() << " vs " << columns_->size(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +StatusOr<Value> QueryRow::get(std::size_t pos) const { |
| 52 | + if (pos < values_.size()) return values_[pos]; |
| 53 | + return internal::InvalidArgumentError("position out of range", |
| 54 | + GCP_ERROR_INFO()); |
| 55 | +} |
| 56 | + |
| 57 | +StatusOr<Value> QueryRow::get(std::string const& name) const { |
| 58 | + auto it = std::find(columns_->begin(), columns_->end(), name); |
| 59 | + if (it != columns_->end()) return get(std::distance(columns_->begin(), it)); |
| 60 | + return internal::InvalidArgumentError("column name not found", |
| 61 | + GCP_ERROR_INFO()); |
| 62 | +} |
| 63 | + |
| 64 | +bool operator==(QueryRow const& a, QueryRow const& b) { |
| 65 | + return a.values_ == b.values_ && *a.columns_ == *b.columns_; |
| 66 | +} |
| 67 | + |
| 68 | +// |
| 69 | +// QueryRowStreamIterator |
| 70 | +// |
| 71 | + |
| 72 | +QueryRowStreamIterator::QueryRowStreamIterator() = default; |
| 73 | + |
| 74 | +QueryRowStreamIterator::QueryRowStreamIterator(Source source) |
| 75 | + : source_(std::move(source)) { |
| 76 | + ++*this; |
| 77 | +} |
| 78 | + |
| 79 | +QueryRowStreamIterator& QueryRowStreamIterator::operator++() { |
| 80 | + if (!row_ok_) { |
| 81 | + source_ = nullptr; // Last row was an error; become "end" |
| 82 | + return *this; |
| 83 | + } |
| 84 | + row_ = source_(); |
| 85 | + row_ok_ = row_.ok(); |
| 86 | + if (row_ && row_->size() == 0) { |
| 87 | + source_ = nullptr; // No more Rows to consume; become "end" |
| 88 | + return *this; |
| 89 | + } |
| 90 | + return *this; |
| 91 | +} |
| 92 | + |
| 93 | +QueryRowStreamIterator QueryRowStreamIterator::operator++(int) { |
| 94 | + auto old = *this; |
| 95 | + ++*this; |
| 96 | + return old; |
| 97 | +} |
| 98 | + |
| 99 | +bool operator==(QueryRowStreamIterator const& a, |
| 100 | + QueryRowStreamIterator const& b) { |
| 101 | + // Input iterators may only be compared to (copies of) themselves and end. |
| 102 | + // See https://en.cppreference.com/w/cpp/named_req/InputIterator. Therefore, |
| 103 | + // by definition, all input iterators are equal unless one is end and the |
| 104 | + // other is not. |
| 105 | + return !a.source_ == !b.source_; |
| 106 | +} |
| 107 | + |
| 108 | +bool operator!=(QueryRowStreamIterator const& a, |
| 109 | + QueryRowStreamIterator const& b) { |
| 110 | + return !(a == b); |
| 111 | +} |
| 112 | + |
| 113 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 114 | +} // namespace bigtable |
| 115 | +} // namespace cloud |
| 116 | +} // namespace google |
0 commit comments