Low-level interface to read TFRecord files. If using compression or buffering, consider using SequentialRecordReader. Note: this class is not thread safe; external synchronization required.
| 57 | // |
| 58 | // Note: this class is not thread safe; external synchronization required. |
| 59 | class RecordReader { |
| 60 | public: |
| 61 | // Format of a single record: |
| 62 | // uint64 length |
| 63 | // uint32 masked crc of length |
| 64 | // byte data[length] |
| 65 | // uint32 masked crc of data |
| 66 | static const size_t kHeaderSize = sizeof(uint64) + sizeof(uint32); |
| 67 | static const size_t kFooterSize = sizeof(uint32); |
| 68 | |
| 69 | // Statistics (sizes are in units of bytes) |
| 70 | struct Stats { |
| 71 | int64 file_size = -1; |
| 72 | int64 data_size = -1; |
| 73 | int64 entries = -1; // Number of values |
| 74 | }; |
| 75 | |
| 76 | // Metadata for the TFRecord file. |
| 77 | struct Metadata { |
| 78 | Stats stats; |
| 79 | }; |
| 80 | |
| 81 | // Create a reader that will return log records from "*file". |
| 82 | // "*file" must remain live while this Reader is in use. |
| 83 | explicit RecordReader( |
| 84 | RandomAccessFile* file, |
| 85 | const RecordReaderOptions& options = RecordReaderOptions()); |
| 86 | |
| 87 | virtual ~RecordReader() = default; |
| 88 | |
| 89 | // Read the record at "*offset" into *record and update *offset to |
| 90 | // point to the offset of the next record. Returns OK on success, |
| 91 | // OUT_OF_RANGE for end of file, or something else for an error. |
| 92 | Status ReadRecord(uint64* offset, string* record); |
| 93 | |
| 94 | // Return the metadata of the Record file. |
| 95 | // |
| 96 | // The current implementation scans the file to completion, |
| 97 | // skipping over the data regions, to extract the metadata once |
| 98 | // on the first call to GetStats(). An improved implementation |
| 99 | // would change RecordWriter to write the metadata into TFRecord |
| 100 | // so that GetMetadata() could be a const method. |
| 101 | // |
| 102 | // 'metadata' must not be nullptr. |
| 103 | Status GetMetadata(Metadata* md); |
| 104 | |
| 105 | private: |
| 106 | Status ReadChecksummed(uint64 offset, size_t n, string* result); |
| 107 | |
| 108 | RecordReaderOptions options_; |
| 109 | std::unique_ptr<InputStreamInterface> input_stream_; |
| 110 | bool last_read_failed_; |
| 111 | |
| 112 | std::unique_ptr<Metadata> cached_metadata_; |
| 113 | |
| 114 | TF_DISALLOW_COPY_AND_ASSIGN(RecordReader); |
| 115 | }; |
| 116 |
nothing calls this directly
no test coverage detected