/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/column_family.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #ifndef YB_ROCKSDB_DB_COLUMN_FAMILY_H |
25 | | #define YB_ROCKSDB_DB_COLUMN_FAMILY_H |
26 | | |
27 | | #pragma once |
28 | | |
29 | | #include <atomic> |
30 | | #include <string> |
31 | | #include <unordered_map> |
32 | | #include <vector> |
33 | | |
34 | | #include "yb/rocksdb/compaction_job_stats.h" |
35 | | #include "yb/rocksdb/db.h" |
36 | | #include "yb/rocksdb/db/memtable_list.h" |
37 | | #include "yb/rocksdb/db/table_properties_collector.h" |
38 | | #include "yb/rocksdb/db/write_batch_internal.h" |
39 | | #include "yb/rocksdb/db/write_controller.h" |
40 | | #include "yb/rocksdb/env.h" |
41 | | #include "yb/rocksdb/options.h" |
42 | | #include "yb/rocksdb/util/mutable_cf_options.h" |
43 | | #include "yb/rocksdb/util/thread_local.h" |
44 | | |
45 | | namespace rocksdb { |
46 | | |
47 | | class Version; |
48 | | class VersionSet; |
49 | | class MemTable; |
50 | | class MemTableListVersion; |
51 | | class CompactionPicker; |
52 | | class Compaction; |
53 | | class InternalKey; |
54 | | class InternalStats; |
55 | | class ColumnFamilyData; |
56 | | class DBImpl; |
57 | | class LogBuffer; |
58 | | class InstrumentedMutex; |
59 | | class InstrumentedMutexLock; |
60 | | |
61 | | extern const double kSlowdownRatio; |
62 | | |
63 | | // ColumnFamilyHandleImpl is the class that clients use to access different |
64 | | // column families. It has non-trivial destructor, which gets called when client |
65 | | // is done using the column family |
66 | | class ColumnFamilyHandleImpl : public ColumnFamilyHandle { |
67 | | public: |
68 | | // create while holding the mutex |
69 | | ColumnFamilyHandleImpl( |
70 | | ColumnFamilyData* cfd, DBImpl* db, InstrumentedMutex* mutex); |
71 | | // destroy without mutex |
72 | | virtual ~ColumnFamilyHandleImpl(); |
73 | 249M | virtual ColumnFamilyData* cfd() const { return cfd_; } |
74 | | virtual const Comparator* user_comparator() const; |
75 | | |
76 | | virtual uint32_t GetID() const override; |
77 | | virtual const std::string& GetName() const override; |
78 | | virtual Status GetDescriptor(ColumnFamilyDescriptor* desc) override; |
79 | | |
80 | | private: |
81 | | ColumnFamilyData* cfd_; |
82 | | DBImpl* db_; |
83 | | InstrumentedMutex* mutex_; |
84 | | }; |
85 | | |
86 | | // Does not ref-count ColumnFamilyData |
87 | | // We use this dummy ColumnFamilyHandleImpl because sometimes MemTableInserter |
88 | | // calls DBImpl methods. When this happens, MemTableInserter need access to |
89 | | // ColumnFamilyHandle (same as the client would need). In that case, we feed |
90 | | // MemTableInserter dummy ColumnFamilyHandle and enable it to call DBImpl |
91 | | // methods |
92 | | class ColumnFamilyHandleInternal : public ColumnFamilyHandleImpl { |
93 | | public: |
94 | | ColumnFamilyHandleInternal() |
95 | 489k | : ColumnFamilyHandleImpl(nullptr, nullptr, nullptr) {} |
96 | | |
97 | 49.4M | void SetCFD(ColumnFamilyData* _cfd) { internal_cfd_ = _cfd; } |
98 | 261 | virtual ColumnFamilyData* cfd() const override { return internal_cfd_; } |
99 | | |
100 | | private: |
101 | | ColumnFamilyData* internal_cfd_; |
102 | | }; |
103 | | |
104 | | // holds references to memtable, all immutable memtables and version |
105 | | struct SuperVersion { |
106 | | // Accessing members of this class is not thread-safe and requires external |
107 | | // synchronization (ie db mutex held or on write thread). |
108 | | MemTable* mem; |
109 | | MemTableListVersion* imm; |
110 | | Version* current; |
111 | | MutableCFOptions mutable_cf_options; |
112 | | // Version number of the current SuperVersion |
113 | | uint64_t version_number; |
114 | | |
115 | | InstrumentedMutex* db_mutex; |
116 | | |
117 | | // should be called outside the mutex |
118 | 1.32M | SuperVersion() = default; |
119 | | ~SuperVersion(); |
120 | | SuperVersion* Ref(); |
121 | | // If Unref() returns true, Cleanup() should be called with mutex held |
122 | | // before deleting this SuperVersion. |
123 | | bool Unref(); |
124 | | |
125 | | // call these two methods with db mutex held |
126 | | // Cleanup unrefs mem, imm and current. Also, it stores all memtables |
127 | | // that needs to be deleted in to_delete vector. Unrefing those |
128 | | // objects needs to be done in the mutex |
129 | | void Cleanup(); |
130 | | void Init(MemTable* new_mem, MemTableListVersion* new_imm, |
131 | | Version* new_current); |
132 | | |
133 | | // The value of dummy is not actually used. kSVInUse takes its address as a |
134 | | // mark in the thread local storage to indicate the SuperVersion is in use |
135 | | // by thread. This way, the value of kSVInUse is guaranteed to have no |
136 | | // conflict with SuperVersion object address and portable on different |
137 | | // platform. |
138 | | static int dummy; |
139 | | static void* const kSVInUse; |
140 | | static void* const kSVObsolete; |
141 | | |
142 | | private: |
143 | | std::atomic<uint32_t> refs; |
144 | | // We need to_delete because during Cleanup(), imm->Unref() returns |
145 | | // all memtables that we need to free through this vector. We then |
146 | | // delete all those memtables outside of mutex, during destruction |
147 | | autovector<MemTable*> to_delete; |
148 | | }; |
149 | | |
150 | | extern Status CheckCompressionSupported(const ColumnFamilyOptions& cf_options); |
151 | | |
152 | | extern Status CheckConcurrentWritesSupported( |
153 | | const ColumnFamilyOptions& cf_options); |
154 | | |
155 | | extern ColumnFamilyOptions SanitizeOptions(const DBOptions& db_options, |
156 | | const InternalKeyComparator* icmp, |
157 | | const ColumnFamilyOptions& src); |
158 | | // Wrap user defined table proproties collector factories `from cf_options` |
159 | | // into internal ones in int_tbl_prop_collector_factories. Add a system internal |
160 | | // one too. |
161 | | extern void GetIntTblPropCollectorFactory( |
162 | | const ColumnFamilyOptions& cf_options, |
163 | | IntTblPropCollectorFactories* int_tbl_prop_collector_factories); |
164 | | |
165 | | class ColumnFamilySet; |
166 | | |
167 | | // This class keeps all the data that a column family needs. |
168 | | // Most methods require DB mutex held, unless otherwise noted |
169 | | class ColumnFamilyData { |
170 | | public: |
171 | | ~ColumnFamilyData(); |
172 | | |
173 | | // thread-safe |
174 | 89.8M | uint32_t GetID() const { return id_; } |
175 | | // thread-safe |
176 | 4.62M | const std::string& GetName() const { return name_; } |
177 | | |
178 | | // Ref() can only be called from a context where the caller can guarantee |
179 | | // that ColumnFamilyData is alive (while holding a non-zero ref already, |
180 | | // holding a DB mutex, or as the leader in a write batch group). |
181 | 1.93M | void Ref() { refs_.fetch_add(1, std::memory_order_relaxed); } |
182 | | |
183 | | // Unref decreases the reference count, but does not handle deletion |
184 | | // when the count goes to 0. If this method returns true then the |
185 | | // caller should delete the instance immediately, or later, by calling |
186 | | // FreeDeadColumnFamilies(). Unref() can only be called while holding |
187 | | // a DB mutex, or during single-threaded recovery. |
188 | 1.81M | bool Unref() { |
189 | 1.81M | int old_refs = refs_.fetch_sub(1, std::memory_order_relaxed); |
190 | 1.81M | assert(old_refs > 0); |
191 | 0 | return old_refs == 1; |
192 | 1.81M | } |
193 | | |
194 | | // SetDropped() can only be called under following conditions: |
195 | | // 1) Holding a DB mutex, |
196 | | // 2) from single-threaded write thread, AND |
197 | | // 3) from single-threaded VersionSet::LogAndApply() |
198 | | // After dropping column family no other operation on that column family |
199 | | // will be executed. All the files and memory will be, however, kept around |
200 | | // until client drops the column family handle. That way, client can still |
201 | | // access data from dropped column family. |
202 | | // Column family can be dropped and still alive. In that state: |
203 | | // *) Compaction and flush is not executed on the dropped column family. |
204 | | // *) Client can continue reading from column family. Writes will fail unless |
205 | | // WriteOptions::ignore_missing_column_families is true |
206 | | // When the dropped column family is unreferenced, then we: |
207 | | // *) Remove column family from the linked list maintained by ColumnFamilySet |
208 | | // *) delete all memory associated with that column family |
209 | | // *) delete all the files associated with that column family |
210 | | void SetDropped(); |
211 | 94.6M | bool IsDropped() const { return dropped_; } |
212 | | |
213 | | // thread-safe |
214 | 44.2M | int NumberLevels() const { return ioptions_.num_levels; } |
215 | | |
216 | 944k | void SetLogNumber(uint64_t log_number) { log_number_ = log_number; } |
217 | 13.1M | uint64_t GetLogNumber() const { return log_number_; } |
218 | | |
219 | | // !!! To be deprecated! Please don't not use this function anymore! |
220 | 23.9M | const Options* options() const { return &options_; } |
221 | | |
222 | | // thread-safe |
223 | | const EnvOptions* soptions() const; |
224 | 59.4M | const ImmutableCFOptions* ioptions() const { return &ioptions_; } |
225 | | // REQUIRES: DB mutex held |
226 | | // This returns the MutableCFOptions used by current SuperVersion |
227 | | // You shoul use this API to reference MutableCFOptions most of the time. |
228 | 1.06k | const MutableCFOptions* GetCurrentMutableCFOptions() const { |
229 | 1.06k | return &(super_version_->mutable_cf_options); |
230 | 1.06k | } |
231 | | // REQUIRES: DB mutex held |
232 | | // This returns the latest MutableCFOptions, which may be not in effect yet. |
233 | 6.15M | const MutableCFOptions* GetLatestMutableCFOptions() const { |
234 | 6.15M | return &mutable_cf_options_; |
235 | 6.15M | } |
236 | | #ifndef ROCKSDB_LITE |
237 | | // REQUIRES: DB mutex held |
238 | | Status SetOptions( |
239 | | const std::unordered_map<std::string, std::string>& options_map); |
240 | | #endif // ROCKSDB_LITE |
241 | | |
242 | 29.3M | InternalStats* internal_stats() { return internal_stats_.get(); } |
243 | | |
244 | 80.7M | MemTableList* imm() { return &imm_; } |
245 | 189M | MemTable* mem() { return mem_; } |
246 | 75.4M | Version* current() const { return current_.load(); } |
247 | 6.73M | Version* dummy_versions() { return dummy_versions_; } |
248 | | void SetCurrent(Version* _current); |
249 | | uint64_t GetNumLiveVersions() const; // REQUIRE: DB mutex held |
250 | | uint64_t GetTotalSstFilesSize() const; // REQUIRE: DB mutex held |
251 | 474k | void SetMemtable(MemTable* new_mem) { mem_ = new_mem; } |
252 | | |
253 | | // See Memtable constructor for explanation of earliest_seq param. |
254 | | MemTable* ConstructNewMemtable(const MutableCFOptions& mutable_cf_options, |
255 | | SequenceNumber earliest_seq); |
256 | | void CreateNewMemtable(const MutableCFOptions& mutable_cf_options, |
257 | | SequenceNumber earliest_seq); |
258 | | |
259 | 38.1M | TableCache* table_cache() const { return table_cache_.get(); } |
260 | | |
261 | | // See documentation in compaction_picker.h |
262 | | // REQUIRES: DB mutex held |
263 | | bool NeedsCompaction() const; |
264 | | // REQUIRES: DB mutex held |
265 | | std::unique_ptr<Compaction> PickCompaction( |
266 | | const MutableCFOptions& mutable_options, LogBuffer* log_buffer); |
267 | | // A flag to tell a manual compaction is to compact all levels together |
268 | | // instad of for specific level. |
269 | | static const int kCompactAllLevels; |
270 | | // A flag to tell a manual compaction's output is base level. |
271 | | static const int kCompactToBaseLevel; |
272 | | // REQUIRES: DB mutex held |
273 | | std::unique_ptr<Compaction> CompactRange( |
274 | | const MutableCFOptions& mutable_cf_options, |
275 | | int input_level, |
276 | | int output_level, |
277 | | uint32_t output_path_id, |
278 | | const InternalKey* begin, |
279 | | const InternalKey* end, |
280 | | InternalKey** compaction_end, |
281 | | bool* manual_conflict); |
282 | | |
283 | 23.9k | CompactionPicker* compaction_picker() { return compaction_picker_.get(); } |
284 | | // thread-safe |
285 | 48.4M | const Comparator* user_comparator() const { |
286 | 48.4M | return internal_comparator_->user_comparator(); |
287 | 48.4M | } |
288 | | // thread-safe |
289 | 107M | const InternalKeyComparatorPtr& internal_comparator() const { |
290 | 107M | return internal_comparator_; |
291 | 107M | } |
292 | | |
293 | 55.1k | const IntTblPropCollectorFactories& int_tbl_prop_collector_factories() const { |
294 | 55.1k | return int_tbl_prop_collector_factories_; |
295 | 55.1k | } |
296 | | |
297 | 15.9M | SuperVersion* GetSuperVersion() { return super_version_; } |
298 | | // thread-safe |
299 | | // Return a already referenced SuperVersion to be used safely. |
300 | | SuperVersion* GetReferencedSuperVersion(InstrumentedMutex* db_mutex); |
301 | | // thread-safe |
302 | | // Get SuperVersion stored in thread local storage. If it does not exist, |
303 | | // get a reference from a current SuperVersion. |
304 | | SuperVersion* GetThreadLocalSuperVersion(InstrumentedMutex* db_mutex); |
305 | | // Try to return SuperVersion back to thread local storage. Retrun true on |
306 | | // success and false on failure. It fails when the thread local storage |
307 | | // contains anything other than SuperVersion::kSVInUse flag. |
308 | | bool ReturnThreadLocalSuperVersion(SuperVersion* sv); |
309 | | // thread-safe |
310 | 53.8k | uint64_t GetSuperVersionNumber() const { |
311 | 53.8k | return super_version_number_.load(); |
312 | 53.8k | } |
313 | | // will return a pointer to SuperVersion* if previous SuperVersion |
314 | | // if its reference count is zero and needs deletion or nullptr if not |
315 | | // As argument takes a pointer to allocated SuperVersion to enable |
316 | | // the clients to allocate SuperVersion outside of mutex. |
317 | | // IMPORTANT: Only call this from DBImpl::InstallSuperVersion() |
318 | | MUST_USE_RESULT std::unique_ptr<SuperVersion> InstallSuperVersion( |
319 | | SuperVersion* new_superversion, |
320 | | InstrumentedMutex* db_mutex, |
321 | | const MutableCFOptions& mutable_cf_options); |
322 | | MUST_USE_RESULT std::unique_ptr<SuperVersion> InstallSuperVersion( |
323 | | SuperVersion* new_superversion, |
324 | | InstrumentedMutex* db_mutex); |
325 | | |
326 | | void ResetThreadLocalSuperVersions(); |
327 | | |
328 | | // Protected by DB mutex |
329 | 119k | void set_pending_flush(bool value) { pending_flush_ = value; } |
330 | 35.5k | void set_pending_compaction(bool value) { pending_compaction_ = value; } |
331 | 1.11M | bool pending_flush() { return pending_flush_; } |
332 | 1.07M | bool pending_compaction() { return pending_compaction_; } |
333 | | |
334 | | // Recalculate some small conditions, which are changed only during |
335 | | // compaction, adding new memtable and/or |
336 | | // recalculation of compaction score. These values are used in |
337 | | // DBImpl::MakeRoomForWrite function to decide, if it need to make |
338 | | // a write stall |
339 | | void RecalculateWriteStallConditions( |
340 | | const MutableCFOptions& mutable_cf_options); |
341 | | |
342 | | private: |
343 | | friend class ColumnFamilySet; |
344 | | ColumnFamilyData(uint32_t id, const std::string& name, |
345 | | Version* dummy_versions, Cache* table_cache, |
346 | | WriteBuffer* write_buffer, |
347 | | const ColumnFamilyOptions& options, |
348 | | const DBOptions* db_options, const EnvOptions& env_options, |
349 | | ColumnFamilySet* column_family_set); |
350 | | |
351 | | uint32_t id_; |
352 | | const std::string name_; |
353 | | Version* dummy_versions_; // Head of circular doubly-linked list of versions. |
354 | | std::atomic<Version*> current_; // == dummy_versions->prev_ |
355 | | |
356 | | std::atomic<int> refs_; // outstanding references to ColumnFamilyData |
357 | | bool dropped_; // true if client dropped it |
358 | | |
359 | | InternalKeyComparatorPtr internal_comparator_; |
360 | | IntTblPropCollectorFactories int_tbl_prop_collector_factories_; |
361 | | |
362 | | const Options options_; |
363 | | const ImmutableCFOptions ioptions_; |
364 | | MutableCFOptions mutable_cf_options_; |
365 | | |
366 | | std::unique_ptr<TableCache> table_cache_; |
367 | | |
368 | | std::unique_ptr<InternalStats> internal_stats_; |
369 | | |
370 | | WriteBuffer* write_buffer_; |
371 | | |
372 | | MemTable* mem_; |
373 | | MemTableList imm_; |
374 | | SuperVersion* super_version_; |
375 | | |
376 | | // An ordinal representing the current SuperVersion. Updated by |
377 | | // InstallSuperVersion(), i.e. incremented every time super_version_ |
378 | | // changes. |
379 | | std::atomic<uint64_t> super_version_number_; |
380 | | |
381 | | // Thread's local copy of SuperVersion pointer |
382 | | // This needs to be destructed before mutex_ |
383 | | std::unique_ptr<ThreadLocalPtr> local_sv_; |
384 | | |
385 | | // pointers for a circular linked list. we use it to support iterations over |
386 | | // all column families that are alive (note: dropped column families can also |
387 | | // be alive as long as client holds a reference) |
388 | | ColumnFamilyData* next_; |
389 | | ColumnFamilyData* prev_; |
390 | | |
391 | | // This is the earliest log file number that contains data from this |
392 | | // Column Family. All earlier log files must be ignored and not |
393 | | // recovered from |
394 | | uint64_t log_number_; |
395 | | |
396 | | // An object that keeps all the compaction stats |
397 | | // and picks the next compaction |
398 | | std::unique_ptr<CompactionPicker> compaction_picker_; |
399 | | |
400 | | ColumnFamilySet* column_family_set_; |
401 | | |
402 | | std::unique_ptr<WriteControllerToken> write_controller_token_; |
403 | | |
404 | | // If true --> this ColumnFamily is currently present in DBImpl::flush_queue_ |
405 | | bool pending_flush_; |
406 | | |
407 | | // If true --> this ColumnFamily is currently present in |
408 | | // DBImpl::compaction_queue_ |
409 | | bool pending_compaction_; |
410 | | |
411 | | uint64_t prev_compaction_needed_bytes_; |
412 | | }; |
413 | | |
414 | | // ColumnFamilySet has interesting thread-safety requirements |
415 | | // * CreateColumnFamily() or RemoveColumnFamily() -- need to be protected by DB |
416 | | // mutex AND executed in the write thread. |
417 | | // CreateColumnFamily() should ONLY be called from VersionSet::LogAndApply() AND |
418 | | // single-threaded write thread. It is also called during Recovery and in |
419 | | // DumpManifest(). |
420 | | // RemoveColumnFamily() is only called from SetDropped(). DB mutex needs to be |
421 | | // held and it needs to be executed from the write thread. SetDropped() also |
422 | | // guarantees that it will be called only from single-threaded LogAndApply(), |
423 | | // but this condition is not that important. |
424 | | // * Iteration -- hold DB mutex, but you can release it in the body of |
425 | | // iteration. If you release DB mutex in body, reference the column |
426 | | // family before the mutex and unreference after you unlock, since the column |
427 | | // family might get dropped when the DB mutex is released |
428 | | // * GetDefault() -- thread safe |
429 | | // * GetColumnFamily() -- either inside of DB mutex or from a write thread |
430 | | // * GetNextColumnFamilyID(), GetMaxColumnFamily(), UpdateMaxColumnFamily(), |
431 | | // NumberOfColumnFamilies -- inside of DB mutex |
432 | | class ColumnFamilySet { |
433 | | public: |
434 | | // ColumnFamilySet supports iteration |
435 | | class iterator { |
436 | | public: |
437 | | explicit iterator(ColumnFamilyData* cfd) |
438 | 103M | : current_(cfd) {} |
439 | 53.1M | iterator& operator++() { |
440 | | // dropped column families might still be included in this iteration |
441 | | // (we're only removing them when client drops the last reference to the |
442 | | // column family). |
443 | | // dummy is never dead, so this will never be infinite |
444 | 53.1M | do { |
445 | 53.1M | current_ = current_->next_; |
446 | 53.1M | } while (current_->refs_.load(std::memory_order_relaxed) == 0); |
447 | 53.1M | return *this; |
448 | 53.1M | } |
449 | 105M | bool operator!=(const iterator& other) { |
450 | 105M | return this->current_ != other.current_; |
451 | 105M | } |
452 | 53.1M | ColumnFamilyData* operator*() { return current_; } |
453 | | |
454 | | private: |
455 | | ColumnFamilyData* current_; |
456 | | }; |
457 | | |
458 | | ColumnFamilySet(const std::string& dbname, const DBOptions* db_options, |
459 | | const EnvOptions& env_options, Cache* table_cache, |
460 | | WriteBuffer* write_buffer, WriteController* write_controller); |
461 | | ~ColumnFamilySet(); |
462 | | |
463 | | ColumnFamilyData* GetDefault() const; |
464 | | // GetColumnFamily() calls return nullptr if column family is not found |
465 | | ColumnFamilyData* GetColumnFamily(uint32_t id) const; |
466 | | ColumnFamilyData* GetColumnFamily(const std::string& name) const; |
467 | | // this call will return the next available column family ID. it guarantees |
468 | | // that there is no column family with id greater than or equal to the |
469 | | // returned value in the current running instance or anytime in RocksDB |
470 | | // instance history. |
471 | | uint32_t GetNextColumnFamilyID(); |
472 | | uint32_t GetMaxColumnFamily(); |
473 | | void UpdateMaxColumnFamily(uint32_t new_max_column_family); |
474 | | size_t NumberOfColumnFamilies() const; |
475 | | |
476 | | ColumnFamilyData* CreateColumnFamily(const std::string& name, uint32_t id, |
477 | | Version* dummy_version, |
478 | | const ColumnFamilyOptions& options); |
479 | | |
480 | 51.9M | iterator begin() { return iterator(dummy_cfd_->next_); } |
481 | 51.9M | iterator end() { return iterator(dummy_cfd_); } |
482 | | |
483 | | // REQUIRES: DB mutex held |
484 | | // Don't call while iterating over ColumnFamilySet |
485 | | void FreeDeadColumnFamilies(); |
486 | | |
487 | | private: |
488 | | friend class ColumnFamilyData; |
489 | | // helper function that gets called from cfd destructor |
490 | | // REQUIRES: DB mutex held |
491 | | void RemoveColumnFamily(ColumnFamilyData* cfd); |
492 | | |
493 | | // column_families_ and column_family_data_ need to be protected: |
494 | | // * when mutating both conditions have to be satisfied: |
495 | | // 1. DB mutex locked |
496 | | // 2. thread currently in single-threaded write thread |
497 | | // * when reading, at least one condition needs to be satisfied: |
498 | | // 1. DB mutex locked |
499 | | // 2. accessed from a single-threaded write thread |
500 | | std::unordered_map<std::string, uint32_t> column_families_; |
501 | | std::unordered_map<uint32_t, ColumnFamilyData*> column_family_data_; |
502 | | |
503 | | uint32_t max_column_family_; |
504 | | ColumnFamilyData* dummy_cfd_; |
505 | | // We don't hold the refcount here, since default column family always exists |
506 | | // We are also not responsible for cleaning up default_cfd_cache_. This is |
507 | | // just a cache that makes common case (accessing default column family) |
508 | | // faster |
509 | | ColumnFamilyData* default_cfd_cache_; |
510 | | |
511 | | const std::string db_name_; |
512 | | const DBOptions* const db_options_; |
513 | | const EnvOptions env_options_; |
514 | | Cache* table_cache_; |
515 | | WriteBuffer* write_buffer_; |
516 | | WriteController* write_controller_; |
517 | | }; |
518 | | |
519 | | // We use ColumnFamilyMemTablesImpl to provide WriteBatch a way to access |
520 | | // memtables of different column families (specified by ID in the write batch) |
521 | | class ColumnFamilyMemTablesImpl : public ColumnFamilyMemTables { |
522 | | public: |
523 | | explicit ColumnFamilyMemTablesImpl(ColumnFamilySet* column_family_set) |
524 | 489k | : column_family_set_(column_family_set), current_(nullptr) {} |
525 | | |
526 | | // Constructs a ColumnFamilyMemTablesImpl equivalent to one constructed |
527 | | // with the arguments used to construct *orig. |
528 | | explicit ColumnFamilyMemTablesImpl(ColumnFamilyMemTablesImpl* orig) |
529 | 0 | : column_family_set_(orig->column_family_set_), current_(nullptr) {} |
530 | | |
531 | | // sets current_ to ColumnFamilyData with column_family_id |
532 | | // returns false if column family doesn't exist |
533 | | // REQUIRES: use this function of DBImpl::column_family_memtables_ should be |
534 | | // under a DB mutex OR from a write thread |
535 | | bool Seek(uint32_t column_family_id) override; |
536 | | |
537 | | // Returns log number of the selected column family |
538 | | // REQUIRES: under a DB mutex OR from a write thread |
539 | | uint64_t GetLogNumber() const override; |
540 | | |
541 | | // REQUIRES: Seek() called first |
542 | | // REQUIRES: use this function of DBImpl::column_family_memtables_ should be |
543 | | // under a DB mutex OR from a write thread |
544 | | virtual MemTable* GetMemTable() const override; |
545 | | |
546 | | // Returns column family handle for the selected column family |
547 | | // REQUIRES: use this function of DBImpl::column_family_memtables_ should be |
548 | | // under a DB mutex OR from a write thread |
549 | | virtual ColumnFamilyHandle* GetColumnFamilyHandle() override; |
550 | | |
551 | | // Cannot be called while another thread is calling Seek(). |
552 | | // REQUIRES: use this function of DBImpl::column_family_memtables_ should be |
553 | | // under a DB mutex OR from a write thread |
554 | 61.0M | virtual ColumnFamilyData* current() override { return current_; } |
555 | | |
556 | | private: |
557 | | ColumnFamilySet* column_family_set_; |
558 | | ColumnFamilyData* current_; |
559 | | ColumnFamilyHandleInternal handle_; |
560 | | }; |
561 | | |
562 | | extern uint32_t GetColumnFamilyID(ColumnFamilyHandle* column_family); |
563 | | |
564 | | extern const Comparator* GetColumnFamilyUserComparator( |
565 | | ColumnFamilyHandle* column_family); |
566 | | |
567 | | } // namespace rocksdb |
568 | | |
569 | | #endif // YB_ROCKSDB_DB_COLUMN_FAMILY_H |