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 #include "tensorflow/python/lib/io/py_record_writer.h"
     17 
     18 #include "tensorflow/c/tf_status_helper.h"
     19 #include "tensorflow/core/lib/core/stringpiece.h"
     20 #include "tensorflow/core/lib/io/record_writer.h"
     21 #include "tensorflow/core/lib/io/zlib_compression_options.h"
     22 #include "tensorflow/core/platform/env.h"
     23 #include "tensorflow/core/platform/types.h"
     24 
     25 namespace tensorflow {
     26 namespace io {
     27 
     28 PyRecordWriter::PyRecordWriter() {}
     29 
     30 PyRecordWriter* PyRecordWriter::New(const string& filename,
     31                                     const string& compression_type_string,
     32                                     TF_Status* out_status) {
     33   std::unique_ptr<WritableFile> file;
     34   Status s = Env::Default()->NewWritableFile(filename, &file);
     35   if (!s.ok()) {
     36     Set_TF_Status_from_Status(out_status, s);
     37     return nullptr;
     38   }
     39   PyRecordWriter* writer = new PyRecordWriter;
     40   writer->file_ = std::move(file);
     41 
     42   RecordWriterOptions options =
     43       RecordWriterOptions::CreateRecordWriterOptions(compression_type_string);
     44 
     45   writer->writer_.reset(new RecordWriter(writer->file_.get(), options));
     46   return writer;
     47 }
     48 
     49 PyRecordWriter::~PyRecordWriter() {
     50 }
     51 
     52 bool PyRecordWriter::WriteRecord(tensorflow::StringPiece record) {
     53   if (writer_ == nullptr) return false;
     54   Status s = writer_->WriteRecord(record);
     55   return s.ok();
     56 }
     57 
     58 void PyRecordWriter::Flush(TF_Status* out_status) {
     59   Status s = writer_->Flush();
     60   if (!s.ok()) {
     61     Set_TF_Status_from_Status(out_status, s);
     62     return;
     63   }
     64 }
     65 
     66 void PyRecordWriter::Close(TF_Status* out_status) {
     67   Status s = writer_->Close();
     68   if (!s.ok()) {
     69     Set_TF_Status_from_Status(out_status, s);
     70     return;
     71   }
     72   writer_.reset(nullptr);
     73   s = file_->Close();
     74   if (!s.ok()) {
     75     Set_TF_Status_from_Status(out_status, s);
     76     return;
     77   }
     78   file_.reset(nullptr);
     79 }
     80 
     81 }  // namespace io
     82 }  // namespace tensorflow
     83