1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
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 http://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
16 #include "tensorflow/core/lib/io/record_writer.h"
17
18 #include "tensorflow/core/lib/core/coding.h"
19 #include "tensorflow/core/lib/hash/crc32c.h"
20 #include "tensorflow/core/lib/io/compression.h"
21 #include "tensorflow/core/platform/env.h"
22
23 namespace tensorflow {
24 namespace io {
25 namespace {
IsZlibCompressed(const RecordWriterOptions & options)26 bool IsZlibCompressed(const RecordWriterOptions& options) {
27 return options.compression_type == RecordWriterOptions::ZLIB_COMPRESSION;
28 }
29
IsSnappyCompressed(const RecordWriterOptions & options)30 bool IsSnappyCompressed(const RecordWriterOptions& options) {
31 return options.compression_type == RecordWriterOptions::SNAPPY_COMPRESSION;
32 }
33 } // namespace
34
CreateRecordWriterOptions(const string & compression_type)35 RecordWriterOptions RecordWriterOptions::CreateRecordWriterOptions(
36 const string& compression_type) {
37 RecordWriterOptions options;
38 #if defined(IS_SLIM_BUILD)
39 if (compression_type != compression::kNone) {
40 LOG(ERROR) << "Compression is not supported but compression_type is set."
41 << " No compression will be used.";
42 }
43 #else
44 if (compression_type == compression::kZlib) {
45 options.compression_type = io::RecordWriterOptions::ZLIB_COMPRESSION;
46 options.zlib_options = io::ZlibCompressionOptions::DEFAULT();
47 } else if (compression_type == compression::kGzip) {
48 options.compression_type = io::RecordWriterOptions::ZLIB_COMPRESSION;
49 options.zlib_options = io::ZlibCompressionOptions::GZIP();
50 } else if (compression_type == compression::kSnappy) {
51 options.compression_type = io::RecordWriterOptions::SNAPPY_COMPRESSION;
52 } else if (compression_type != compression::kNone) {
53 LOG(ERROR) << "Unsupported compression_type:" << compression_type
54 << ". No compression will be used.";
55 }
56 #endif
57 return options;
58 }
59
RecordWriter(WritableFile * dest,const RecordWriterOptions & options)60 RecordWriter::RecordWriter(WritableFile* dest,
61 const RecordWriterOptions& options)
62 : dest_(dest), options_(options) {
63 #if defined(IS_SLIM_BUILD)
64 if (options.compression_type != RecordWriterOptions::NONE) {
65 LOG(FATAL) << "Compression is unsupported on mobile platforms.";
66 }
67 #else
68 if (IsZlibCompressed(options)) {
69 ZlibOutputBuffer* zlib_output_buffer = new ZlibOutputBuffer(
70 dest, options.zlib_options.input_buffer_size,
71 options.zlib_options.output_buffer_size, options.zlib_options);
72 Status s = zlib_output_buffer->Init();
73 if (!s.ok()) {
74 LOG(FATAL) << "Failed to initialize Zlib inputbuffer. Error: "
75 << s.ToString();
76 }
77 dest_ = zlib_output_buffer;
78 } else if (IsSnappyCompressed(options)) {
79 dest_ =
80 new SnappyOutputBuffer(dest, options.snappy_options.input_buffer_size,
81 options.snappy_options.output_buffer_size);
82 } else if (options.compression_type == RecordWriterOptions::NONE) {
83 // Nothing to do
84 } else {
85 LOG(FATAL) << "Unspecified compression type :" << options.compression_type;
86 }
87 #endif
88 }
89
~RecordWriter()90 RecordWriter::~RecordWriter() {
91 if (dest_ != nullptr) {
92 Status s = Close();
93 if (!s.ok()) {
94 LOG(ERROR) << "Could not finish writing file: " << s;
95 }
96 }
97 }
98
WriteRecord(StringPiece data)99 Status RecordWriter::WriteRecord(StringPiece data) {
100 if (dest_ == nullptr) {
101 return Status(::tensorflow::error::FAILED_PRECONDITION,
102 "Writer not initialized or previously closed");
103 }
104 // Format of a single record:
105 // uint64 length
106 // uint32 masked crc of length
107 // byte data[length]
108 // uint32 masked crc of data
109 char header[kHeaderSize];
110 char footer[kFooterSize];
111 PopulateHeader(header, data.data(), data.size());
112 PopulateFooter(footer, data.data(), data.size());
113 TF_RETURN_IF_ERROR(dest_->Append(StringPiece(header, sizeof(header))));
114 TF_RETURN_IF_ERROR(dest_->Append(data));
115 return dest_->Append(StringPiece(footer, sizeof(footer)));
116 }
117
118 #if defined(TF_CORD_SUPPORT)
WriteRecord(const absl::Cord & data)119 Status RecordWriter::WriteRecord(const absl::Cord& data) {
120 if (dest_ == nullptr) {
121 return Status(::tensorflow::error::FAILED_PRECONDITION,
122 "Writer not initialized or previously closed");
123 }
124 // Format of a single record:
125 // uint64 length
126 // uint32 masked crc of length
127 // byte data[length]
128 // uint32 masked crc of data
129 char header[kHeaderSize];
130 char footer[kFooterSize];
131 PopulateHeader(header, data);
132 PopulateFooter(footer, data);
133 TF_RETURN_IF_ERROR(dest_->Append(StringPiece(header, sizeof(header))));
134 TF_RETURN_IF_ERROR(dest_->Append(data));
135 return dest_->Append(StringPiece(footer, sizeof(footer)));
136 }
137 #endif
138
Close()139 Status RecordWriter::Close() {
140 if (dest_ == nullptr) return Status::OK();
141 if (IsZlibCompressed(options_) || IsSnappyCompressed(options_)) {
142 Status s = dest_->Close();
143 delete dest_;
144 dest_ = nullptr;
145 return s;
146 }
147 return Status::OK();
148 }
149
Flush()150 Status RecordWriter::Flush() {
151 if (dest_ == nullptr) {
152 return Status(::tensorflow::error::FAILED_PRECONDITION,
153 "Writer not initialized or previously closed");
154 }
155 return dest_->Flush();
156 }
157
158 } // namespace io
159 } // namespace tensorflow
160