-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathcell.h
More file actions
189 lines (170 loc) · 7.47 KB
/
cell.h
File metadata and controls
189 lines (170 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H
#include "google/cloud/bigtable/internal/google_bytes_traits.h"
#include "google/cloud/bigtable/row_key.h"
#include "google/cloud/bigtable/version.h"
#include "google/cloud/status_or.h"
#include <chrono>
#include <string>
#include <type_traits>
#include <vector>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
class Cell;
struct Mutation;
Mutation SetCell(Cell);
/**
* Defines the type for column qualifiers.
*
* Inside Google some protobuf fields of type `bytes` are mapped to a different
* type than `std::string`. This is the case for column qualifiers. We use this
* type to automatically detect what is the representation for this field and
* use the correct mapping.
*
* External users of the Cloud Bigtable C++ client library should treat this as
* a complicated `typedef` for `std::string`. We have no plans to change the
* type in the external version of the C++ client library for the foreseeable
* future. In the eventuality that we do decide to change the type, this would
* be a reason update the library major version number, and we would give users
* time to migrate.
*
* In other words, external users of the Cloud Bigtable C++ client should simply
* write `std::string` where this type appears. For Google projects that must
* compile both inside and outside Google, this alias may be convenient.
*/
using ColumnQualifierType = std::decay_t<
decltype(std::declval<google::bigtable::v2::Column>().qualifier())>;
/**
* Defines the type for cell values.
*
* Inside Google some protobuf fields of type `bytes` are mapped to a different
* type than `std::string`. This is the case for column qualifiers. We use this
* type to automatically detect what is the representation for this field and
* use the correct mapping.
*
* External users of the Cloud Bigtable C++ client library should treat this as
* a complicated `typedef` for `std::string`. We have no plans to change the
* type in the external version of the C++ client library for the foreseeable
* future. In the eventuality that we do decide to change the type, this would
* be a reason update the library major version number, and we would give users
* time to migrate.
*
* In other words, external users of the Cloud Bigtable C++ client should simply
* write `std::string` where this type appears. For Google projects that must
* compile both inside and outside Google, this alias may be convenient.
*/
using CellValueType =
std::decay_t<decltype(std::declval<google::bigtable::v2::Cell>().value())>;
/**
* The in-memory representation of a Bigtable cell.
*
* Bigtable stores data in rows, indexes by row keys. Each row may contain
* multiple column families, each column family might contain multiple columns,
* and each column has multiple cells indexed by timestamp. Notice that the
* storage is sparse, column families, columns, and timestamps might contain
* zero cells.
*
* The Cell class owns all its data.
*/
class Cell {
public:
/**
* Creates a Cell and fill it with data.
*
* This function does not participate in overload resolution if @p ValueType
* is not an integral type. The case for integral types is handled by a
* separate overload.
*/
template <typename KeyType, typename ColumnType, typename ValueType,
/// @cond implementation_details
std::enable_if_t<!std::is_integral<ValueType>::value, int> = 0
/// @endcond
>
Cell(KeyType&& row_key, std::string family_name,
ColumnType&& column_qualifier, std::int64_t timestamp, ValueType&& value,
std::vector<std::string> labels)
: row_key_(std::forward<KeyType>(row_key)),
family_name_(std::move(family_name)),
column_qualifier_(std::forward<ColumnType>(column_qualifier)),
timestamp_(timestamp),
value_(std::forward<ValueType>(value)),
labels_(std::move(labels)) {}
/// Create a Cell and fill it with a 64-bit value encoded as big endian.
template <typename KeyType, typename ColumnType>
Cell(KeyType&& row_key, std::string family_name,
ColumnType&& column_qualifier, std::int64_t timestamp,
std::int64_t value, std::vector<std::string> labels)
: Cell(std::forward<KeyType>(row_key), std::move(family_name),
std::forward<ColumnType>(column_qualifier), timestamp,
google::cloud::internal::EncodeBigEndian(value),
std::move(labels)) {}
/// Create a cell and fill it with data, but with empty labels.
template <typename KeyType, typename ColumnType, typename ValueType>
Cell(KeyType&& row_key, std::string family_name,
ColumnType&& column_qualifier, std::int64_t timestamp, ValueType&& value)
: Cell(std::forward<KeyType>(row_key), std::move(family_name),
std::forward<ColumnType>(column_qualifier), timestamp,
std::forward<ValueType>(value), std::vector<std::string>{}) {}
/// Return the row key this cell belongs to. The returned value is not valid
/// after this object is deleted.
RowKeyType const& row_key() const { return row_key_; }
/// Return the family this cell belongs to. The returned value is not valid
/// after this object is deleted.
std::string const& family_name() const { return family_name_; }
/// Return the column this cell belongs to. The returned value is not valid
/// after this object is deleted.
ColumnQualifierType const& column_qualifier() const {
return column_qualifier_;
}
/// Return the timestamp of this cell.
std::chrono::microseconds timestamp() const {
return std::chrono::microseconds(timestamp_);
}
/// Return the contents of this cell. The returned value is not valid after
/// this object is deleted.
CellValueType const& value() const& { return value_; }
/// Return the contents of this cell.
CellValueType&& value() && { return std::move(value_); }
/**
* Interpret the value as a big-endian encoded `T` and return it.
*
* Google Cloud Bigtable stores arbitrary blobs in each cell. Some
* applications interpret these blobs as strings, other as encoded protos,
* and sometimes as big-endian integers. This is a helper function to convert
* the blob into a T value.
*/
template <typename T>
StatusOr<T> decode_big_endian_integer() const {
return internal::DecodeBigEndianCellValue<T>(value_);
}
/// Return the labels applied to this cell by label transformer read filters.
std::vector<std::string> const& labels() const { return labels_; }
private:
RowKeyType row_key_;
std::string family_name_;
ColumnQualifierType column_qualifier_;
std::int64_t timestamp_;
CellValueType value_;
std::vector<std::string> labels_;
friend Mutation SetCell(Cell);
};
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_CELL_H