]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/file/file_prefetch_buffer.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / file / file_prefetch_buffer.h
CommitLineData
f67539c2
TL
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 <sstream>
13#include <string>
20effc67 14
f67539c2
TL
15#include "file/random_access_file_reader.h"
16#include "port/port.h"
17#include "rocksdb/env.h"
20effc67 18#include "rocksdb/options.h"
f67539c2
TL
19#include "util/aligned_buffer.h"
20
21namespace ROCKSDB_NAMESPACE {
22
23// FilePrefetchBuffer is a smart buffer to store and read data from a file.
24class FilePrefetchBuffer {
25 public:
26 // Constructor.
27 //
28 // All arguments are optional.
29 // file_reader : the file reader to use. Can be a nullptr.
30 // readahead_size : the initial readahead size.
31 // max_readahead_size : the maximum readahead size.
32 // If max_readahead_size > readahead_size, the readahead size will be
33 // doubled on every IO until max_readahead_size is hit.
34 // Typically this is set as a multiple of readahead_size.
35 // max_readahead_size should be greater than equal to readahead_size.
36 // enable : controls whether reading from the buffer is enabled.
37 // If false, TryReadFromCache() always return false, and we only take stats
38 // for the minimum offset if track_min_offset = true.
39 // track_min_offset : Track the minimum offset ever read and collect stats on
40 // it. Used for adaptable readahead of the file footer/metadata.
41 //
42 // Automatic readhead is enabled for a file if file_reader, readahead_size,
43 // and max_readahead_size are passed in.
44 // If file_reader is a nullptr, setting readadhead_size and max_readahead_size
45 // does not make any sense. So it does nothing.
46 // A user can construct a FilePrefetchBuffer without any arguments, but use
47 // `Prefetch` to load data into the buffer.
48 FilePrefetchBuffer(RandomAccessFileReader* file_reader = nullptr,
49 size_t readadhead_size = 0, size_t max_readahead_size = 0,
50 bool enable = true, bool track_min_offset = false)
51 : buffer_offset_(0),
52 file_reader_(file_reader),
53 readahead_size_(readadhead_size),
54 max_readahead_size_(max_readahead_size),
55 min_offset_read_(port::kMaxSizet),
56 enable_(enable),
57 track_min_offset_(track_min_offset) {}
58
59 // Load data into the buffer from a file.
60 // reader : the file reader.
61 // offset : the file offset to start reading from.
62 // n : the number of bytes to read.
63 // for_compaction : if prefetch is done for compaction read.
20effc67
TL
64 Status Prefetch(const IOOptions& opts, RandomAccessFileReader* reader,
65 uint64_t offset, size_t n, bool for_compaction = false);
f67539c2
TL
66
67 // Tries returning the data for a file raed from this buffer, if that data is
68 // in the buffer.
69 // It handles tracking the minimum read offset if track_min_offset = true.
70 // It also does the exponential readahead when readadhead_size is set as part
71 // of the constructor.
72 //
73 // offset : the file offset.
74 // n : the number of bytes.
75 // result : output buffer to put the data into.
76 // for_compaction : if cache read is done for compaction read.
20effc67
TL
77 bool TryReadFromCache(const IOOptions& opts, uint64_t offset, size_t n,
78 Slice* result, bool for_compaction = false);
f67539c2
TL
79
80 // The minimum `offset` ever passed to TryReadFromCache(). This will nly be
81 // tracked if track_min_offset = true.
82 size_t min_offset_read() const { return min_offset_read_; }
83
84 private:
85 AlignedBuffer buffer_;
86 uint64_t buffer_offset_;
87 RandomAccessFileReader* file_reader_;
88 size_t readahead_size_;
89 size_t max_readahead_size_;
90 // The minimum `offset` ever passed to TryReadFromCache().
91 size_t min_offset_read_;
92 // if false, TryReadFromCache() always return false, and we only take stats
93 // for track_min_offset_ if track_min_offset_ = true
94 bool enable_;
95 // If true, track minimum `offset` ever passed to TryReadFromCache(), which
96 // can be fetched from min_offset_read().
97 bool track_min_offset_;
98};
99} // namespace ROCKSDB_NAMESPACE