Home | History | Annotate | Download | only in io
      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_LIB_IO_RECORD_WRITER_H_
     17 #define TENSORFLOW_LIB_IO_RECORD_WRITER_H_
     18 
     19 #include "tensorflow/core/lib/core/status.h"
     20 #include "tensorflow/core/lib/core/stringpiece.h"
     21 #if !defined(IS_SLIM_BUILD)
     22 #include "tensorflow/core/lib/io/zlib_compression_options.h"
     23 #include "tensorflow/core/lib/io/zlib_outputbuffer.h"
     24 #endif  // IS_SLIM_BUILD
     25 #include "tensorflow/core/platform/macros.h"
     26 #include "tensorflow/core/platform/types.h"
     27 
     28 namespace tensorflow {
     29 
     30 class WritableFile;
     31 
     32 namespace io {
     33 
     34 class RecordWriterOptions {
     35  public:
     36   enum CompressionType { NONE = 0, ZLIB_COMPRESSION = 1 };
     37   CompressionType compression_type = NONE;
     38 
     39   static RecordWriterOptions CreateRecordWriterOptions(
     40       const string& compression_type);
     41 
     42 // Options specific to zlib compression.
     43 #if !defined(IS_SLIM_BUILD)
     44   ZlibCompressionOptions zlib_options;
     45 #endif  // IS_SLIM_BUILD
     46 };
     47 
     48 class RecordWriter {
     49  public:
     50   // Create a writer that will append data to "*dest".
     51   // "*dest" must be initially empty.
     52   // "*dest" must remain live while this Writer is in use.
     53   RecordWriter(WritableFile* dest,
     54                const RecordWriterOptions& options = RecordWriterOptions());
     55 
     56   // Calls Close() and logs if an error occurs.
     57   //
     58   // TODO(jhseu): Require that callers explicitly call Close() and remove the
     59   // implicit Close() call in the destructor.
     60   ~RecordWriter();
     61 
     62   Status WriteRecord(StringPiece slice);
     63 
     64   // Flushes any buffered data held by underlying containers of the
     65   // RecordWriter to the WritableFile. Does *not* flush the
     66   // WritableFile.
     67   Status Flush();
     68 
     69   // Writes all output to the file. Does *not* close the WritableFile.
     70   //
     71   // After calling Close(), any further calls to `WriteRecord()` or `Flush()`
     72   // are invalid.
     73   Status Close();
     74 
     75  private:
     76   WritableFile* dest_;
     77   RecordWriterOptions options_;
     78 
     79   TF_DISALLOW_COPY_AND_ASSIGN(RecordWriter);
     80 };
     81 
     82 }  // namespace io
     83 }  // namespace tensorflow
     84 
     85 #endif  // TENSORFLOW_LIB_IO_RECORD_WRITER_H_
     86