aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/io/record_writer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/core/lib/io/record_writer.cc')
-rw-r--r--tensorflow/core/lib/io/record_writer.cc42
1 files changed, 42 insertions, 0 deletions
diff --git a/tensorflow/core/lib/io/record_writer.cc b/tensorflow/core/lib/io/record_writer.cc
new file mode 100644
index 0000000000..3d7f1509ab
--- /dev/null
+++ b/tensorflow/core/lib/io/record_writer.cc
@@ -0,0 +1,42 @@
+#include "tensorflow/core/lib/io/record_writer.h"
+
+#include "tensorflow/core/public/env.h"
+#include "tensorflow/core/lib/core/coding.h"
+#include "tensorflow/core/lib/hash/crc32c.h"
+
+namespace tensorflow {
+namespace io {
+
+RecordWriter::RecordWriter(WritableFile* dest) : dest_(dest) {}
+
+RecordWriter::~RecordWriter() {}
+
+static uint32 MaskedCrc(const char* data, size_t n) {
+ return crc32c::Mask(crc32c::Value(data, n));
+}
+
+Status RecordWriter::WriteRecord(StringPiece data) {
+ // Format of a single record:
+ // uint64 length
+ // uint32 masked crc of length
+ // byte data[length]
+ // uint32 masked crc of data
+ char header[sizeof(uint64) + sizeof(uint32)];
+ core::EncodeFixed64(header + 0, data.size());
+ core::EncodeFixed32(header + sizeof(uint64),
+ MaskedCrc(header, sizeof(uint64)));
+ Status s = dest_->Append(StringPiece(header, sizeof(header)));
+ if (!s.ok()) {
+ return s;
+ }
+ s = dest_->Append(data);
+ if (!s.ok()) {
+ return s;
+ }
+ char footer[sizeof(uint32)];
+ core::EncodeFixed32(footer, MaskedCrc(data.data(), data.size()));
+ return dest_->Append(StringPiece(footer, sizeof(footer)));
+}
+
+} // namespace io
+} // namespace tensorflow