• 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 #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 
PyRecordWriter()28 PyRecordWriter::PyRecordWriter() {}
29 
New(const string & filename,const io::RecordWriterOptions & options,TF_Status * out_status)30 PyRecordWriter* PyRecordWriter::New(const string& filename,
31                                     const io::RecordWriterOptions& options,
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   writer->writer_.reset(new RecordWriter(writer->file_.get(), options));
42   return writer;
43 }
44 
~PyRecordWriter()45 PyRecordWriter::~PyRecordWriter() {
46   // Writer depends on file during close for zlib flush, so destruct first.
47   writer_.reset();
48   file_.reset();
49 }
50 
WriteRecord(tensorflow::StringPiece record,TF_Status * out_status)51 void PyRecordWriter::WriteRecord(tensorflow::StringPiece record,
52                                  TF_Status* out_status) {
53   if (writer_ == nullptr) {
54     TF_SetStatus(out_status, TF_FAILED_PRECONDITION,
55                  "Writer not initialized or previously closed");
56     return;
57   }
58   Status s = writer_->WriteRecord(record);
59   if (!s.ok()) {
60     Set_TF_Status_from_Status(out_status, s);
61   }
62 }
63 
Flush(TF_Status * out_status)64 void PyRecordWriter::Flush(TF_Status* out_status) {
65   if (writer_ == nullptr) {
66     TF_SetStatus(out_status, TF_FAILED_PRECONDITION,
67                  "Writer not initialized or previously closed");
68     return;
69   }
70   Status s = writer_->Flush();
71   if (s.ok()) {
72     // Per the RecordWriter contract, flushing the RecordWriter does not
73     // flush the underlying file.  Here we need to do both.
74     s = file_->Flush();
75   }
76   if (!s.ok()) {
77     Set_TF_Status_from_Status(out_status, s);
78     return;
79   }
80 }
81 
Close(TF_Status * out_status)82 void PyRecordWriter::Close(TF_Status* out_status) {
83   if (writer_ != nullptr) {
84     Status s = writer_->Close();
85     if (!s.ok()) {
86       Set_TF_Status_from_Status(out_status, s);
87       return;
88     }
89     writer_.reset(nullptr);
90   }
91   if (file_ != nullptr) {
92     Status s = file_->Close();
93     if (!s.ok()) {
94       Set_TF_Status_from_Status(out_status, s);
95       return;
96     }
97     file_.reset(nullptr);
98   }
99 }
100 
101 }  // namespace io
102 }  // namespace tensorflow
103