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);
61 // If a seek to internal key "k" in specified file finds an entry,
62 // call (*handle_result)(arg, found_key, found_value) repeatedly until
64 // @param get_context State for get operation. If its range_del_agg() returns
65 // non-nullptr, adds range deletions to the aggregator. If an error occurs,
66 // returns non-ok status.
67 // @param skip_filters Disables loading/accessing the filter block
68 // @param level The level this table is at, -1 for "not set / don't know"
69 Status
Get(const ReadOptions
& options
,
70 const InternalKeyComparator
& internal_comparator
,
71 const FileMetaData
& file_meta
, const Slice
& k
,
72 GetContext
* get_context
,
73 const SliceTransform
* prefix_extractor
= nullptr,
74 HistogramImpl
* file_read_hist
= nullptr, bool skip_filters
= false,
77 // Evict any entry for the specified file number
78 static void Evict(Cache
* cache
, uint64_t file_number
);
80 // Clean table handle and erase it from the table cache
81 // Used in DB close, or the file is not live anymore.
82 void EraseHandle(const FileDescriptor
& fd
, Cache::Handle
* handle
);
85 // @param skip_filters Disables loading/accessing the filter block
86 // @param level == -1 means not specified
87 Status
FindTable(const EnvOptions
& toptions
,
88 const InternalKeyComparator
& internal_comparator
,
89 const FileDescriptor
& file_fd
, Cache::Handle
**,
90 const SliceTransform
* prefix_extractor
= nullptr,
91 const bool no_io
= false, bool record_read_stats
= true,
92 HistogramImpl
* file_read_hist
= nullptr,
93 bool skip_filters
= false, int level
= -1,
94 bool prefetch_index_and_filter_in_cache
= true);
96 // Get TableReader from a cache handle.
97 TableReader
* GetTableReaderFromHandle(Cache::Handle
* handle
);
99 // Get the table properties of a given table.
100 // @no_io: indicates if we should load table to the cache if it is not present
101 // in table cache yet.
102 // @returns: `properties` will be reset on success. Please note that we will
103 // return Status::Incomplete() if table is not present in cache and
104 // we set `no_io` to be true.
105 Status
GetTableProperties(const EnvOptions
& toptions
,
106 const InternalKeyComparator
& internal_comparator
,
107 const FileDescriptor
& file_meta
,
108 std::shared_ptr
<const TableProperties
>* properties
,
109 const SliceTransform
* prefix_extractor
= nullptr,
112 // Return total memory usage of the table reader of the file.
113 // 0 if table reader of the file is not loaded.
114 size_t GetMemoryUsageByTableReader(
115 const EnvOptions
& toptions
,
116 const InternalKeyComparator
& internal_comparator
,
117 const FileDescriptor
& fd
,
118 const SliceTransform
* prefix_extractor
= nullptr);
120 // Release the handle from a cache
121 void ReleaseHandle(Cache::Handle
* handle
);
123 // Capacity of the backing Cache that indicates inifinite TableCache capacity.
124 // For example when max_open_files is -1 we set the backing Cache to this.
125 static const int kInfiniteCapacity
= 0x400000;
127 // The tables opened with this TableCache will be immortal, i.e., their
128 // lifetime is as long as that of the DB.
129 void SetTablesAreImmortal() {
130 if (cache_
->GetCapacity() >= kInfiniteCapacity
) {
131 immortal_tables_
= true;
136 // Build a table reader
137 Status
GetTableReader(const EnvOptions
& env_options
,
138 const InternalKeyComparator
& internal_comparator
,
139 const FileDescriptor
& fd
, bool sequential_mode
,
140 size_t readahead
, bool record_read_stats
,
141 HistogramImpl
* file_read_hist
,
142 unique_ptr
<TableReader
>* table_reader
,
143 const SliceTransform
* prefix_extractor
= nullptr,
144 bool skip_filters
= false, int level
= -1,
145 bool prefetch_index_and_filter_in_cache
= true,
146 bool for_compaction
= false);
148 const ImmutableCFOptions
& ioptions_
;
149 const EnvOptions
& env_options_
;
151 std::string row_cache_id_
;
152 bool immortal_tables_
;
155 } // namespace rocksdb