Google Cloud Bigtable C++ Client 2.13.0
A C++ Client Library for Google Cloud Bigtable
Loading...
Searching...
No Matches
cell.h
1// Copyright 2017 Google Inc.
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#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H
17
18#include "google/cloud/bigtable/internal/google_bytes_traits.h"
19#include "google/cloud/bigtable/row_key.h"
20#include "google/cloud/bigtable/version.h"
21#include "google/cloud/status_or.h"
22#include <chrono>
23#include <string>
24#include <type_traits>
25#include <vector>
26
27namespace google {
28namespace cloud {
29namespace bigtable {
30GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31class Cell;
32struct Mutation;
34
35/**
36 * Defines the type for column qualifiers.
37 *
38 * Inside Google some protobuf fields of type `bytes` are mapped to a different
39 * type than `std::string`. This is the case for column qualifiers. We use this
40 * type to automatically detect what is the representation for this field and
41 * use the correct mapping.
42 *
43 * External users of the Cloud Bigtable C++ client library should treat this as
44 * a complicated `typedef` for `std::string`. We have no plans to change the
45 * type in the external version of the C++ client library for the foreseeable
46 * future. In the eventuality that we do decide to change the type, this would
47 * be a reason update the library major version number, and we would give users
48 * time to migrate.
49 *
50 * In other words, external users of the Cloud Bigtable C++ client should simply
51 * write `std::string` where this type appears. For Google projects that must
52 * compile both inside and outside Google, this alias may be convenient.
53 */
54using ColumnQualifierType = std::decay<
55 decltype(std::declval<google::bigtable::v2::Column>().qualifier())>::type;
56
57/**
58 * Defines the type for cell values.
59 *
60 * Inside Google some protobuf fields of type `bytes` are mapped to a different
61 * type than `std::string`. This is the case for column qualifiers. We use this
62 * type to automatically detect what is the representation for this field and
63 * use the correct mapping.
64 *
65 * External users of the Cloud Bigtable C++ client library should treat this as
66 * a complicated `typedef` for `std::string`. We have no plans to change the
67 * type in the external version of the C++ client library for the foreseeable
68 * future. In the eventuality that we do decide to change the type, this would
69 * be a reason update the library major version number, and we would give users
70 * time to migrate.
71 *
72 * In other words, external users of the Cloud Bigtable C++ client should simply
73 * write `std::string` where this type appears. For Google projects that must
74 * compile both inside and outside Google, this alias may be convenient.
75 */
76using CellValueType = std::decay<
77 decltype(std::declval<google::bigtable::v2::Cell>().value())>::type;
78
79/**
80 * The in-memory representation of a Bigtable cell.
81 *
82 * Bigtable stores data in rows, indexes by row keys. Each row may contain
83 * multiple column families, each column family might contain multiple columns,
84 * and each column has multiple cells indexed by timestamp. Notice that the
85 * storage is sparse, column families, columns, and timestamps might contain
86 * zero cells.
87 *
88 * The Cell class owns all its data.
89 */
90class Cell {
91 public:
92 /**
93 * Creates a Cell and fill it with data.
94 *
95 * This function does not participate in overload resolution if @p ValueType
96 * is not an integral type. The case for integral types is handled by a
97 * separate overload.
98 */
99 template <typename KeyType, typename ColumnType, typename ValueType,
100 /// @cond implementation_details
101 typename std::enable_if<!std::is_integral<ValueType>::value,
102 int>::type = 0
103 /// @endcond
104 >
105 Cell(KeyType&& row_key, std::string family_name,
106 ColumnType&& column_qualifier, std::int64_t timestamp, ValueType&& value,
107 std::vector<std::string> labels)
108 : row_key_(std::forward<KeyType>(row_key)),
109 family_name_(std::move(family_name)),
110 column_qualifier_(std::forward<ColumnType>(column_qualifier)),
111 timestamp_(timestamp),
112 value_(std::forward<ValueType>(value)),
113 labels_(std::move(labels)) {}
114
115 /// Create a Cell and fill it with a 64-bit value encoded as big endian.
116 template <typename KeyType, typename ColumnType>
117 Cell(KeyType&& row_key, std::string family_name,
118 ColumnType&& column_qualifier, std::int64_t timestamp,
119 std::int64_t value, std::vector<std::string> labels)
120 : Cell(std::forward<KeyType>(row_key), std::move(family_name),
121 std::forward<ColumnType>(column_qualifier), timestamp,
122 google::cloud::internal::EncodeBigEndian(value),
123 std::move(labels)) {}
124
125 /// Create a cell and fill it with data, but with empty labels.
126 template <typename KeyType, typename ColumnType, typename ValueType>
127 Cell(KeyType&& row_key, std::string family_name,
128 ColumnType&& column_qualifier, std::int64_t timestamp, ValueType&& value)
129 : Cell(std::forward<KeyType>(row_key), std::move(family_name),
130 std::forward<ColumnType>(column_qualifier), timestamp,
131 std::forward<ValueType>(value), std::vector<std::string>{}) {}
132
133 /// Return the row key this cell belongs to. The returned value is not valid
134 /// after this object is deleted.
135 RowKeyType const& row_key() const { return row_key_; }
136
137 /// Return the family this cell belongs to. The returned value is not valid
138 /// after this object is deleted.
139 std::string const& family_name() const { return family_name_; }
140
141 /// Return the column this cell belongs to. The returned value is not valid
142 /// after this object is deleted.
143 ColumnQualifierType const& column_qualifier() const {
144 return column_qualifier_;
145 }
146
147 /// Return the timestamp of this cell.
148 std::chrono::microseconds timestamp() const {
149 return std::chrono::microseconds(timestamp_);
150 }
151
152 /// Return the contents of this cell. The returned value is not valid after
153 /// this object is deleted.
154 CellValueType const& value() const& { return value_; }
155 /// Return the contents of this cell.
156 CellValueType&& value() && { return std::move(value_); }
157
158 /**
159 * Interpret the value as a big-endian encoded `T` and return it.
160 *
161 * Google Cloud Bigtable stores arbitrary blobs in each cell. Some
162 * applications interpret these blobs as strings, other as encoded protos,
163 * and sometimes as big-endian integers. This is a helper function to convert
164 * the blob into a T value.
165 */
166 template <typename T>
167 StatusOr<T> decode_big_endian_integer() const {
168 return internal::DecodeBigEndianCellValue<T>(value_);
169 }
170
171 /// Return the labels applied to this cell by label transformer read filters.
172 std::vector<std::string> const& labels() const { return labels_; }
173
174 private:
175 RowKeyType row_key_;
176 std::string family_name_;
177 ColumnQualifierType column_qualifier_;
178 std::int64_t timestamp_;
179 CellValueType value_;
180 std::vector<std::string> labels_;
181
182 friend Mutation SetCell(Cell);
183};
184
185GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
186} // namespace bigtable
187} // namespace cloud
188} // namespace google
189
190#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H
The in-memory representation of a Bigtable cell.
Definition: cell.h:90
CellValueType const & value() const &
Return the contents of this cell.
Definition: cell.h:154
RowKeyType const & row_key() const
Return the row key this cell belongs to.
Definition: cell.h:135
StatusOr< T > decode_big_endian_integer() const
Interpret the value as a big-endian encoded T and return it.
Definition: cell.h:167
Cell(KeyType &&row_key, std::string family_name, ColumnType &&column_qualifier, std::int64_t timestamp, ValueType &&value)
Create a cell and fill it with data, but with empty labels.
Definition: cell.h:127
std::vector< std::string > const & labels() const
Return the labels applied to this cell by label transformer read filters.
Definition: cell.h:172
std::string const & family_name() const
Return the family this cell belongs to.
Definition: cell.h:139
Cell(KeyType &&row_key, std::string family_name, ColumnType &&column_qualifier, std::int64_t timestamp, std::int64_t value, std::vector< std::string > labels)
Create a Cell and fill it with a 64-bit value encoded as big endian.
Definition: cell.h:117
CellValueType && value() &&
Return the contents of this cell.
Definition: cell.h:156
Cell(KeyType &&row_key, std::string family_name, ColumnType &&column_qualifier, std::int64_t timestamp, ValueType &&value, std::vector< std::string > labels)
Creates a Cell and fill it with data.
Definition: cell.h:105
ColumnQualifierType const & column_qualifier() const
Return the column this cell belongs to.
Definition: cell.h:143
std::chrono::microseconds timestamp() const
Return the timestamp of this cell.
Definition: cell.h:148
Contains all the Cloud Bigtable C++ client APIs.
Definition: admin_client.h:28
Mutation SetCell(Cell)
Create a mutation to set a cell value based on a bigtable::Cell.
Represent a single change to a specific row in a Table.
Definition: mutations.h:45