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 
10 #pragma once
11 #include <atomic>
12 #include <string>
13 
14 #include "env/file_system_tracer.h"
15 #include "port/port.h"
16 #include "rocksdb/env.h"
17 #include "rocksdb/file_system.h"
18 
19 namespace ROCKSDB_NAMESPACE {
20 
21 // SequentialFileReader is a wrapper on top of Env::SequentialFile. It handles
22 // Buffered (i.e when page cache is enabled) and Direct (with O_DIRECT / page
23 // cache disabled) reads appropriately, and also updates the IO stats.
24 class SequentialFileReader {
25  private:
26   std::string file_name_;
27   FSSequentialFilePtr file_;
28   std::atomic<size_t> offset_{0};  // read offset
29 
30  public:
31   explicit SequentialFileReader(
32       std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
33       const std::shared_ptr<IOTracer>& io_tracer = nullptr)
file_name_(_file_name)34       : file_name_(_file_name),
35         file_(std::move(_file), io_tracer, _file_name) {}
36 
37   explicit SequentialFileReader(
38       std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
39       size_t _readahead_size,
40       const std::shared_ptr<IOTracer>& io_tracer = nullptr)
file_name_(_file_name)41       : file_name_(_file_name),
42         file_(NewReadaheadSequentialFile(std::move(_file), _readahead_size),
43               io_tracer, _file_name) {}
44   static Status Create(const std::shared_ptr<FileSystem>& fs,
45                        const std::string& fname, const FileOptions& file_opts,
46                        std::unique_ptr<SequentialFileReader>* reader,
47                        IODebugContext* dbg);
48 
49   SequentialFileReader(const SequentialFileReader&) = delete;
50   SequentialFileReader& operator=(const SequentialFileReader&) = delete;
51 
52   Status Read(size_t n, Slice* result, char* scratch);
53 
54   Status Skip(uint64_t n);
55 
file()56   FSSequentialFile* file() { return file_.get(); }
57 
file_name()58   std::string file_name() { return file_name_; }
59 
use_direct_io()60   bool use_direct_io() const { return file_->use_direct_io(); }
61 
62  private:
63   // NewReadaheadSequentialFile provides a wrapper over SequentialFile to
64   // always prefetch additional data with every read.
65   static std::unique_ptr<FSSequentialFile> NewReadaheadSequentialFile(
66       std::unique_ptr<FSSequentialFile>&& file, size_t readahead_size);
67 };
68 }  // namespace ROCKSDB_NAMESPACE
69