1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 // 6 #pragma once 7 8 #ifndef ROCKSDB_LITE 9 10 #include <memory> 11 #include <string> 12 13 #include "file/random_access_file_reader.h" 14 #include "rocksdb/env.h" 15 #include "rocksdb/slice.h" 16 #include "rocksdb/statistics.h" 17 #include "rocksdb/status.h" 18 #include "utilities/blob_db/blob_log_format.h" 19 20 namespace ROCKSDB_NAMESPACE { 21 22 class SequentialFileReader; 23 class Logger; 24 25 namespace blob_db { 26 27 /** 28 * Reader is a general purpose log stream reader implementation. The actual job 29 * of reading from the device is implemented by the SequentialFile interface. 30 * 31 * Please see Writer for details on the file and record layout. 32 */ 33 class Reader { 34 public: 35 enum ReadLevel { 36 kReadHeader, 37 kReadHeaderKey, 38 kReadHeaderKeyBlob, 39 }; 40 41 // Create a reader that will return log records from "*file". 42 // "*file" must remain live while this Reader is in use. 43 Reader(std::unique_ptr<RandomAccessFileReader>&& file_reader, Env* env, 44 Statistics* statistics); 45 // No copying allowed 46 Reader(const Reader&) = delete; 47 Reader& operator=(const Reader&) = delete; 48 49 ~Reader() = default; 50 51 Status ReadHeader(BlobLogHeader* header); 52 53 // Read the next record into *record. Returns true if read 54 // successfully, false if we hit end of the input. May use 55 // "*scratch" as temporary storage. The contents filled in *record 56 // will only be valid until the next mutating operation on this 57 // reader or the next mutation to *scratch. 58 // If blob_offset is non-null, return offset of the blob through it. 59 Status ReadRecord(BlobLogRecord* record, ReadLevel level = kReadHeader, 60 uint64_t* blob_offset = nullptr); 61 ResetNextByte()62 void ResetNextByte() { next_byte_ = 0; } 63 GetNextByte()64 uint64_t GetNextByte() const { return next_byte_; } 65 66 private: 67 Status ReadSlice(uint64_t size, Slice* slice, char* buf); 68 69 const std::unique_ptr<RandomAccessFileReader> file_; 70 Env* env_; 71 Statistics* statistics_; 72 73 Slice buffer_; 74 char header_buf_[BlobLogRecord::kHeaderSize]; 75 76 // which byte to read next. For asserting proper usage 77 uint64_t next_byte_; 78 }; 79 80 } // namespace blob_db 81 } // namespace ROCKSDB_NAMESPACE 82 #endif // ROCKSDB_LITE 83