Repository navigation
Expand file tree
/
Copy pathrow_streamer.cc
More file actions
98 lines (91 loc) · 3.21 KB
/
Copy pathrow_streamer.cc
File metadata and controls
98 lines (91 loc) · 3.21 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
// Copyright 2024 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.
#include "row_streamer.h"
#include "absl/types/optional.h"
#include "cell_view.h"
#include <google/bigtable/v2/bigtable.pb.h>
#include <chrono>
#include <utility>
namespace google {
namespace cloud {
namespace bigtable {
namespace emulator {
namespace btproto = ::google::bigtable::v2;
RowStreamer::RowStreamer(grpc::ServerWriter<btproto::ReadRowsResponse>& writer)
: writer_(writer) {}
bool RowStreamer::Stream(CellView const& cell) {
btproto::ReadRowsResponse::CellChunk chunk;
if (!current_row_key_ || current_row_key_ != cell.row_key()) {
if (!pending_chunks_.empty()) {
pending_chunks_.back().set_commit_row(true);
}
current_row_key_ = cell.row_key();
current_column_family_ = cell.column_family();
current_column_qualifier_ = cell.column_qualifier();
chunk.set_row_key(cell.row_key());
chunk.mutable_family_name()->set_value(cell.column_family());
chunk.mutable_qualifier()->set_value(cell.column_qualifier());
}
if (current_row_key_ != cell.column_family()) {
current_column_family_ = cell.column_family();
current_column_qualifier_ = cell.column_qualifier();
chunk.mutable_family_name()->set_value(cell.column_family());
chunk.mutable_qualifier()->set_value(cell.column_qualifier());
}
if (current_row_key_ != cell.column_qualifier()) {
current_column_qualifier_ = cell.column_qualifier();
chunk.mutable_qualifier()->set_value(cell.column_qualifier());
}
chunk.set_timestamp_micros(
std::chrono::duration_cast<std::chrono::microseconds>(cell.timestamp())
.count());
chunk.set_value(cell.value());
if (cell.HasLabel()) {
*chunk.add_labels() = cell.label();
}
pending_chunks_.emplace_back(std::move(chunk));
if (pending_chunks_.size() > 200) {
return Flush(false);
}
return true;
}
bool RowStreamer::Flush(bool stream_finished) {
absl::optional<btproto::ReadRowsResponse::CellChunk> dont_flush_this;
if (stream_finished) {
if (!pending_chunks_.empty()) {
pending_chunks_.back().set_commit_row(true);
}
current_row_key_.reset();
current_column_family_.reset();
current_column_qualifier_.reset();
} else {
if (!pending_chunks_.empty()) {
dont_flush_this = std::move(pending_chunks_.back());
pending_chunks_.resize(pending_chunks_.size() - 1);
}
}
btproto::ReadRowsResponse resp;
for (auto& chunk : pending_chunks_) {
*resp.add_chunks() = std::move(chunk);
}
pending_chunks_.resize(0);
if (dont_flush_this) {
pending_chunks_.emplace_back(*std::move(dont_flush_this));
}
return writer_.Write(resp);
}
} // namespace emulator
} // namespace bigtable
} // namespace cloud
} // namespace google