• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_CORE_LIB_IO_RECORD_WRITER_H_
17 #define TENSORFLOW_CORE_LIB_IO_RECORD_WRITER_H_
18 
19 #include "tensorflow/core/lib/core/coding.h"
20 #include "tensorflow/core/lib/core/status.h"
21 #include "tensorflow/core/lib/core/stringpiece.h"
22 #include "tensorflow/core/lib/hash/crc32c.h"
23 #if !defined(IS_SLIM_BUILD)
24 #include "tensorflow/core/lib/io/zlib_compression_options.h"
25 #include "tensorflow/core/lib/io/zlib_outputbuffer.h"
26 #endif  // IS_SLIM_BUILD
27 #include "tensorflow/core/platform/cord.h"
28 #include "tensorflow/core/platform/macros.h"
29 #include "tensorflow/core/platform/types.h"
30 
31 namespace tensorflow {
32 
33 class WritableFile;
34 
35 namespace io {
36 
37 class RecordWriterOptions {
38  public:
39   enum CompressionType { NONE = 0, ZLIB_COMPRESSION = 1 };
40   CompressionType compression_type = NONE;
41 
42   static RecordWriterOptions CreateRecordWriterOptions(
43       const string& compression_type);
44 
45 // Options specific to zlib compression.
46 #if !defined(IS_SLIM_BUILD)
47   tensorflow::io::ZlibCompressionOptions zlib_options;
48 #endif  // IS_SLIM_BUILD
49 };
50 
51 class RecordWriter {
52  public:
53   // Format of a single record:
54   //  uint64    length
55   //  uint32    masked crc of length
56   //  byte      data[length]
57   //  uint32    masked crc of data
58   static const size_t kHeaderSize = sizeof(uint64) + sizeof(uint32);
59   static const size_t kFooterSize = sizeof(uint32);
60 
61   // Create a writer that will append data to "*dest".
62   // "*dest" must be initially empty.
63   // "*dest" must remain live while this Writer is in use.
64   RecordWriter(WritableFile* dest,
65                const RecordWriterOptions& options = RecordWriterOptions());
66 
67   // Calls Close() and logs if an error occurs.
68   //
69   // TODO(jhseu): Require that callers explicitly call Close() and remove the
70   // implicit Close() call in the destructor.
71   ~RecordWriter();
72 
73   Status WriteRecord(StringPiece slice);
74 
75 #if defined(PLATFORM_GOOGLE)
76   Status WriteRecord(const absl::Cord& data);
77 #endif
78 
79   // Flushes any buffered data held by underlying containers of the
80   // RecordWriter to the WritableFile. Does *not* flush the
81   // WritableFile.
82   Status Flush();
83 
84   // Writes all output to the file. Does *not* close the WritableFile.
85   //
86   // After calling Close(), any further calls to `WriteRecord()` or `Flush()`
87   // are invalid.
88   Status Close();
89 
90   // Utility method to populate TFRecord headers.  Populates record-header in
91   // "header[0,kHeaderSize-1]".  The record-header is based on data[0, n-1].
92   inline static void PopulateHeader(char* header, const char* data, size_t n);
93 
94   // Utility method to populate TFRecord footers.  Populates record-footer in
95   // "footer[0,kFooterSize-1]".  The record-footer is based on data[0, n-1].
96   inline static void PopulateFooter(char* footer, const char* data, size_t n);
97 
98 #if defined(PLATFORM_GOOGLE)
99   inline static void PopulateHeader(char* header, const absl::Cord& data);
100   inline static void PopulateFooter(char* footer, const absl::Cord& data);
101 #endif
102 
103  private:
104   WritableFile* dest_;
105   RecordWriterOptions options_;
106 
MaskedCrc(const char * data,size_t n)107   inline static uint32 MaskedCrc(const char* data, size_t n) {
108     return crc32c::Mask(crc32c::Value(data, n));
109   }
110 
111 #if defined(PLATFORM_GOOGLE)
MaskedCrc(const absl::Cord & data)112   inline static uint32 MaskedCrc(const absl::Cord& data) {
113     return crc32c::Mask(crc32c::Value(data));
114   }
115 #endif
116 
117   TF_DISALLOW_COPY_AND_ASSIGN(RecordWriter);
118 };
119 
PopulateHeader(char * header,const char * data,size_t n)120 void RecordWriter::PopulateHeader(char* header, const char* data, size_t n) {
121   core::EncodeFixed64(header + 0, n);
122   core::EncodeFixed32(header + sizeof(uint64),
123                       MaskedCrc(header, sizeof(uint64)));
124 }
125 
PopulateFooter(char * footer,const char * data,size_t n)126 void RecordWriter::PopulateFooter(char* footer, const char* data, size_t n) {
127   core::EncodeFixed32(footer, MaskedCrc(data, n));
128 }
129 
130 #if defined(PLATFORM_GOOGLE)
PopulateHeader(char * header,const absl::Cord & data)131 void RecordWriter::PopulateHeader(char* header, const absl::Cord& data) {
132   core::EncodeFixed64(header + 0, data.size());
133   core::EncodeFixed32(header + sizeof(uint64),
134                       MaskedCrc(header, sizeof(uint64)));
135 }
136 
PopulateFooter(char * footer,const absl::Cord & data)137 void RecordWriter::PopulateFooter(char* footer, const absl::Cord& data) {
138   core::EncodeFixed32(footer, MaskedCrc(data));
139 }
140 #endif
141 
142 }  // namespace io
143 }  // namespace tensorflow
144 
145 #endif  // TENSORFLOW_CORE_LIB_IO_RECORD_WRITER_H_
146