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