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).
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.
10 // Thread-safe (provides internal synchronization)
17 #include "db/dbformat.h"
18 #include "db/range_del_aggregator.h"
19 #include "options/cf_options.h"
20 #include "port/port.h"
21 #include "rocksdb/cache.h"
22 #include "rocksdb/env.h"
23 #include "rocksdb/options.h"
24 #include "rocksdb/table.h"
25 #include "table/table_reader.h"
31 struct FileDescriptor
;
37 TableCache(const ImmutableCFOptions
& ioptions
,
38 const EnvOptions
& storage_options
, Cache
* cache
);
41 // Return an iterator for the specified file number (the corresponding
42 // file length must be exactly "file_size" bytes). If "tableptr" is
43 // non-nullptr, also sets "*tableptr" to point to the Table object
44 // underlying the returned iterator, or nullptr if no Table object underlies
45 // the returned iterator. The returned "*tableptr" object is owned by
46 // the cache and should not be deleted, and is valid for as long as the
47 // returned iterator is live.
48 // @param range_del_agg If non-nullptr, adds range deletions to the
49 // aggregator. If an error occurs, returns it in a NewErrorInternalIterator
50 // @param skip_filters Disables loading/accessing the filter block
51 // @param level The level this table is at, -1 for "not set / don't know"
52 InternalIterator
* NewIterator(
53 const ReadOptions
& options
, const EnvOptions
& toptions
,
54 const InternalKeyComparator
& internal_comparator
,
55 const FileMetaData
& file_meta
, RangeDelAggregator
* range_del_agg
,
56 const SliceTransform
* prefix_extractor
= nullptr,
57 TableReader
** table_reader_ptr
= nullptr,
58 HistogramImpl
* file_read_hist
= nullptr, bool for_compaction
= false,
59 Arena
* arena
= nullptr, bool skip_filters
= false, int level
= -1,
60 const InternalKey
* smallest_compaction_key
= nullptr,
61 const InternalKey
* largest_compaction_key
= nullptr);
63 // If a seek to internal key "k" in specified file finds an entry,
64 // call (*handle_result)(arg, found_key, found_value) repeatedly until
66 // @param get_context State for get operation. If its range_del_agg() returns
67 // non-nullptr, adds range deletions to the aggregator. If an error occurs,
68 // returns non-ok status.
69 // @param skip_filters Disables loading/accessing the filter block
70 // @param level The level this table is at, -1 for "not set / don't know"
71 Status
Get(const ReadOptions
& options
,
72 const InternalKeyComparator
& internal_comparator
,
73 const FileMetaData
& file_meta
, const Slice
& k
,
74 GetContext
* get_context
,
75 const SliceTransform
* prefix_extractor
= nullptr,
76 HistogramImpl
* file_read_hist
= nullptr, bool skip_filters
= false,
79 // Evict any entry for the specified file number
80 static void Evict(Cache
* cache
, uint64_t file_number
);
82 // Clean table handle and erase it from the table cache
83 // Used in DB close, or the file is not live anymore.
84 void EraseHandle(const FileDescriptor
& fd
, Cache::Handle
* handle
);
87 // @param skip_filters Disables loading/accessing the filter block
88 // @param level == -1 means not specified
89 Status
FindTable(const EnvOptions
& toptions
,
90 const InternalKeyComparator
& internal_comparator
,
91 const FileDescriptor
& file_fd
, Cache::Handle
**,
92 const SliceTransform
* prefix_extractor
= nullptr,
93 const bool no_io
= false, bool record_read_stats
= true,
94 HistogramImpl
* file_read_hist
= nullptr,
95 bool skip_filters
= false, int level
= -1,
96 bool prefetch_index_and_filter_in_cache
= true);
98 // Get TableReader from a cache handle.
99 TableReader
* GetTableReaderFromHandle(Cache::Handle
* handle
);
101 // Get the table properties of a given table.
102 // @no_io: indicates if we should load table to the cache if it is not present
103 // in table cache yet.
104 // @returns: `properties` will be reset on success. Please note that we will
105 // return Status::Incomplete() if table is not present in cache and
106 // we set `no_io` to be true.
107 Status
GetTableProperties(const EnvOptions
& toptions
,
108 const InternalKeyComparator
& internal_comparator
,
109 const FileDescriptor
& file_meta
,
110 std::shared_ptr
<const TableProperties
>* properties
,
111 const SliceTransform
* prefix_extractor
= nullptr,
114 // Return total memory usage of the table reader of the file.
115 // 0 if table reader of the file is not loaded.
116 size_t GetMemoryUsageByTableReader(
117 const EnvOptions
& toptions
,
118 const InternalKeyComparator
& internal_comparator
,
119 const FileDescriptor
& fd
,
120 const SliceTransform
* prefix_extractor
= nullptr);
122 // Release the handle from a cache
123 void ReleaseHandle(Cache::Handle
* handle
);
125 Cache
* get_cache() const { return cache_
; }
127 // Capacity of the backing Cache that indicates inifinite TableCache capacity.
128 // For example when max_open_files is -1 we set the backing Cache to this.
129 static const int kInfiniteCapacity
= 0x400000;
131 // The tables opened with this TableCache will be immortal, i.e., their
132 // lifetime is as long as that of the DB.
133 void SetTablesAreImmortal() {
134 if (cache_
->GetCapacity() >= kInfiniteCapacity
) {
135 immortal_tables_
= true;
140 // Build a table reader
141 Status
GetTableReader(const EnvOptions
& env_options
,
142 const InternalKeyComparator
& internal_comparator
,
143 const FileDescriptor
& fd
, bool sequential_mode
,
144 size_t readahead
, bool record_read_stats
,
145 HistogramImpl
* file_read_hist
,
146 std::unique_ptr
<TableReader
>* table_reader
,
147 const SliceTransform
* prefix_extractor
= nullptr,
148 bool skip_filters
= false, int level
= -1,
149 bool prefetch_index_and_filter_in_cache
= true,
150 bool for_compaction
= false);
152 const ImmutableCFOptions
& ioptions_
;
153 const EnvOptions
& env_options_
;
155 std::string row_cache_id_
;
156 bool immortal_tables_
;
159 } // namespace rocksdb