blob_log_sequential_reader.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include <memory>
  8. #include "db/blob/blob_log_format.h"
  9. #include "rocksdb/slice.h"
  10. #define MAX_HEADER_SIZE(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
  11. namespace ROCKSDB_NAMESPACE {
  12. class RandomAccessFileReader;
  13. class Env;
  14. class Statistics;
  15. class Status;
  16. class SystemClock;
  17. /**
  18. * BlobLogSequentialReader is a general purpose log stream reader
  19. * implementation. The actual job of reading from the device is implemented by
  20. * the RandomAccessFileReader interface.
  21. *
  22. * Please see BlobLogWriter for details on the file and record layout.
  23. */
  24. class BlobLogSequentialReader {
  25. public:
  26. enum ReadLevel {
  27. kReadHeader,
  28. kReadHeaderKey,
  29. kReadHeaderKeyBlob,
  30. };
  31. // Create a reader that will return log records from "*file_reader".
  32. BlobLogSequentialReader(std::unique_ptr<RandomAccessFileReader>&& file_reader,
  33. SystemClock* clock, Statistics* statistics);
  34. // No copying allowed
  35. BlobLogSequentialReader(const BlobLogSequentialReader&) = delete;
  36. BlobLogSequentialReader& operator=(const BlobLogSequentialReader&) = delete;
  37. ~BlobLogSequentialReader();
  38. Status ReadHeader(BlobLogHeader* header);
  39. // Read the next record into *record. Returns true if read
  40. // successfully, false if we hit end of the input. The contents filled in
  41. // *record will only be valid until the next mutating operation on this
  42. // reader.
  43. // If blob_offset is non-null, return offset of the blob through it.
  44. Status ReadRecord(BlobLogRecord* record, ReadLevel level = kReadHeader,
  45. uint64_t* blob_offset = nullptr);
  46. Status ReadFooter(BlobLogFooter* footer);
  47. void ResetNextByte() { next_byte_ = 0; }
  48. uint64_t GetNextByte() const { return next_byte_; }
  49. private:
  50. Status ReadSlice(uint64_t size, Slice* slice, char* buf);
  51. const std::unique_ptr<RandomAccessFileReader> file_;
  52. SystemClock* clock_;
  53. Statistics* statistics_;
  54. Slice buffer_;
  55. char header_buf_[MAX_HEADER_SIZE(BlobLogHeader::kSize, BlobLogFooter::kSize,
  56. BlobLogRecord::kHeaderSize)];
  57. // which byte to read next
  58. uint64_t next_byte_;
  59. };
  60. } // namespace ROCKSDB_NAMESPACE
  61. #undef MAX_HEADER_SIZE