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_PYTHON_LIB_IO_PY_RECORD_READER_H_ 17 #define TENSORFLOW_PYTHON_LIB_IO_PY_RECORD_READER_H_ 18 19 #include "tensorflow/c/c_api.h" 20 #include "tensorflow/core/lib/core/stringpiece.h" 21 #include "tensorflow/core/platform/macros.h" 22 #include "tensorflow/core/platform/types.h" 23 24 namespace tensorflow { 25 26 class RandomAccessFile; 27 28 namespace io { 29 30 class RecordReader; 31 32 // A wrapper around io::RecordReader that is more easily SWIG wrapped for 33 // Python. An instance of this class is not safe for concurrent access 34 // by multiple threads. 35 class PyRecordReader { 36 public: 37 // TODO(vrv): make this take a shared proto to configure 38 // the compression options. 39 static PyRecordReader* New(const string& filename, uint64 start_offset, 40 const string& compression_type_string, 41 TF_Status* out_status); 42 43 ~PyRecordReader(); 44 45 // Attempt to get the next record at "current_offset()". Populates status 46 // with OK on success, OUT_OF_RANGE for end of file, DATA_LOSS for some 47 // kinds of truncated reads, or another code for other errors 48 // (e.g., filesystem errors). 49 void GetNext(TF_Status* status); 50 51 // Return the current record contents. Only valid after the preceding call 52 // to GetNext() returned true record()53 string record() const { return record_; } 54 // Return the current offset in the file. offset()55 uint64 offset() const { return offset_; } 56 57 // Close the underlying file and release its resources. 58 void Close(); 59 60 private: 61 PyRecordReader(); 62 63 uint64 offset_; 64 RandomAccessFile* file_; // Owned 65 io::RecordReader* reader_; // Owned 66 string record_; 67 TF_DISALLOW_COPY_AND_ASSIGN(PyRecordReader); 68 }; 69 70 } // namespace io 71 } // namespace tensorflow 72 73 #endif // TENSORFLOW_PYTHON_LIB_IO_PY_RECORD_READER_H_ 74