blob_log_reader.h 2.3 KB

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