sequence_file_reader.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <atomic>
  11. #include <string>
  12. #include "port/port.h"
  13. #include "rocksdb/env.h"
  14. #include "rocksdb/file_system.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. // SequentialFileReader is a wrapper on top of Env::SequentialFile. It handles
  17. // Buffered (i.e when page cache is enabled) and Direct (with O_DIRECT / page
  18. // cache disabled) reads appropriately, and also updates the IO stats.
  19. class SequentialFileReader {
  20. private:
  21. std::unique_ptr<FSSequentialFile> file_;
  22. std::string file_name_;
  23. std::atomic<size_t> offset_{0}; // read offset
  24. public:
  25. explicit SequentialFileReader(std::unique_ptr<FSSequentialFile>&& _file,
  26. const std::string& _file_name)
  27. : file_(std::move(_file)), file_name_(_file_name) {}
  28. explicit SequentialFileReader(std::unique_ptr<FSSequentialFile>&& _file,
  29. const std::string& _file_name,
  30. size_t _readahead_size)
  31. : file_(NewReadaheadSequentialFile(std::move(_file), _readahead_size)),
  32. file_name_(_file_name) {}
  33. SequentialFileReader(SequentialFileReader&& o) ROCKSDB_NOEXCEPT {
  34. *this = std::move(o);
  35. }
  36. SequentialFileReader& operator=(SequentialFileReader&& o) ROCKSDB_NOEXCEPT {
  37. file_ = std::move(o.file_);
  38. return *this;
  39. }
  40. SequentialFileReader(const SequentialFileReader&) = delete;
  41. SequentialFileReader& operator=(const SequentialFileReader&) = delete;
  42. Status Read(size_t n, Slice* result, char* scratch);
  43. Status Skip(uint64_t n);
  44. FSSequentialFile* file() { return file_.get(); }
  45. std::string file_name() { return file_name_; }
  46. bool use_direct_io() const { return file_->use_direct_io(); }
  47. private:
  48. // NewReadaheadSequentialFile provides a wrapper over SequentialFile to
  49. // always prefetch additional data with every read.
  50. static std::unique_ptr<FSSequentialFile> NewReadaheadSequentialFile(
  51. std::unique_ptr<FSSequentialFile>&& file, size_t readahead_size);
  52. };
  53. } // namespace ROCKSDB_NAMESPACE