Skip to content

Commit 7e299ea

Browse files
ArrayRecord Teamcopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 715812031
1 parent 64637c5 commit 7e299ea

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

cpp/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ cc_library(
115115
"@abseil-cpp//absl/status",
116116
"@abseil-cpp//absl/status:statusor",
117117
"@abseil-cpp//absl/strings",
118+
"@abseil-cpp//absl/strings:cord",
118119
"@abseil-cpp//absl/synchronization",
119120
"@abseil-cpp//absl/types:span",
120121
"@protobuf//:protobuf_lite",
@@ -235,6 +236,8 @@ cc_test(
235236
":test_utils",
236237
":thread_pool",
237238
"@abseil-cpp//absl/strings",
239+
"@abseil-cpp//absl/strings:cord",
240+
"@abseil-cpp//absl/strings:cord_test_helpers",
238241
"@googletest//:gtest_main",
239242
"@riegeli//riegeli/base:initializer",
240243
"@riegeli//riegeli/bytes:string_reader",

cpp/array_record_writer.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ limitations under the License.
3131
#include "absl/log/check.h"
3232
#include "absl/status/status.h"
3333
#include "absl/status/statusor.h"
34+
#include "absl/strings/cord.h"
3435
#include "absl/strings/match.h"
3536
#include "absl/strings/str_cat.h"
3637
#include "absl/strings/string_view.h"
@@ -39,8 +40,8 @@ limitations under the License.
3940
#include "cpp/common.h"
4041
#include "cpp/layout.pb.h"
4142
#include "cpp/sequenced_chunk_writer.h"
42-
#include "cpp/tri_state_ptr.h"
4343
#include "cpp/thread_pool.h"
44+
#include "cpp/tri_state_ptr.h"
4445
#include "google/protobuf/message_lite.h"
4546
#include "riegeli/base/object.h"
4647
#include "riegeli/base/options_parser.h"
@@ -418,6 +419,16 @@ bool ArrayRecordWriterBase::WriteRecord(absl::string_view record) {
418419
return WriteRecordImpl(std::move(record));
419420
}
420421

422+
bool ArrayRecordWriterBase::WriteRecord(const absl::Cord& record) {
423+
if (auto flat = record.TryFlat(); flat.has_value()) {
424+
return WriteRecord(*flat);
425+
}
426+
427+
std::string cord_string;
428+
absl::AppendCordToString(record, &cord_string);
429+
return WriteRecord(cord_string);
430+
}
431+
421432
bool ArrayRecordWriterBase::WriteRecord(const void* data, size_t num_bytes) {
422433
auto view = absl::string_view(reinterpret_cast<const char*>(data), num_bytes);
423434
return WriteRecordImpl(std::move(view));

cpp/array_record_writer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ limitations under the License.
6666
#include <utility>
6767

6868
#include "absl/status/statusor.h"
69+
#include "absl/strings/cord.h"
6970
#include "absl/strings/string_view.h"
7071
#include "absl/types/span.h"
7172
#include "cpp/common.h"
7273
#include "cpp/sequenced_chunk_writer.h"
73-
#include "cpp/tri_state_ptr.h"
7474
#include "cpp/thread_pool.h"
75+
#include "cpp/tri_state_ptr.h"
7576
#include "riegeli/base/initializer.h"
7677
#include "riegeli/base/object.h"
7778
#include "riegeli/bytes/writer.h"
@@ -304,6 +305,7 @@ class ArrayRecordWriterBase : public riegeli::Object {
304305
// Write records of various types.
305306
bool WriteRecord(const google::protobuf::MessageLite& record);
306307
bool WriteRecord(absl::string_view record);
308+
bool WriteRecord(const absl::Cord& record);
307309
bool WriteRecord(const void* data, size_t num_bytes);
308310
template <typename T>
309311
bool WriteRecord(absl::Span<const T> record) {

cpp/array_record_writer_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ limitations under the License.
2525
#include <vector>
2626

2727
#include "gtest/gtest.h"
28+
#include "absl/strings/cord.h"
29+
#include "absl/strings/cord_test_helpers.h"
2830
#include "absl/strings/string_view.h"
2931
#include "cpp/common.h"
3032
#include "cpp/layout.pb.h"
@@ -116,6 +118,39 @@ TEST_P(ArrayRecordWriterTest, MoveTest) {
116118
}
117119
}
118120

121+
TEST_P(ArrayRecordWriterTest, CordTest) {
122+
std::string encoded;
123+
ARThreadPool* pool = nullptr;
124+
if (std::get<3>(GetParam())) {
125+
pool = ArrayRecordGlobalPool();
126+
}
127+
auto options = GetOptions();
128+
options.set_group_size(2);
129+
auto writer = ArrayRecordWriter(
130+
riegeli::Maker<riegeli::StringWriter>(&encoded), options, pool);
131+
132+
absl::Cord flat_cord("test");
133+
// Empty string should not crash the writer.
134+
absl::Cord empty_cord("");
135+
absl::Cord fragmented_cord = absl::MakeFragmentedCord({"aaa ", "", "c"});
136+
137+
EXPECT_TRUE(writer.WriteRecord(flat_cord));
138+
EXPECT_TRUE(writer.WriteRecord(empty_cord));
139+
EXPECT_TRUE(writer.WriteRecord(fragmented_cord));
140+
ASSERT_TRUE(writer.Close());
141+
142+
// Empty string should not crash the reader.
143+
std::vector<std::string> expected_strings{"test", "", "aaa c"};
144+
145+
auto reader =
146+
riegeli::RecordReader(riegeli::Maker<riegeli::StringReader>(encoded));
147+
for (const auto& expected : expected_strings) {
148+
std::string result;
149+
reader.ReadRecord(result);
150+
EXPECT_EQ(result, expected);
151+
}
152+
}
153+
119154
TEST_P(ArrayRecordWriterTest, RandomDatasetTest) {
120155
std::mt19937 bitgen;
121156
constexpr uint32_t kGroupSize = 100;

0 commit comments

Comments
 (0)