@@ -185,9 +185,8 @@ ColumnFamilyData::ColumnFamilyData(const std::string& dbname, uint32_t id,
185
185
dropped_(false ),
186
186
internal_comparator_(options.comparator),
187
187
internal_filter_policy_(options.filter_policy),
188
- options_(SanitizeOptions(&internal_comparator_, &internal_filter_policy_,
189
- options)),
190
- full_options_(*db_options, options_),
188
+ options_(*db_options, SanitizeOptions(&internal_comparator_,
189
+ &internal_filter_policy_, options)),
191
190
mem_(nullptr ),
192
191
imm_(options.min_write_buffer_number_to_merge),
193
192
super_version_(nullptr ),
@@ -205,18 +204,19 @@ ColumnFamilyData::ColumnFamilyData(const std::string& dbname, uint32_t id,
205
204
internal_stats_.reset (new InternalStats (options.num_levels , db_options->env ,
206
205
db_options->statistics .get ()));
207
206
table_cache_.reset (
208
- new TableCache (dbname, &full_options_ , storage_options, table_cache));
207
+ new TableCache (dbname, &options_ , storage_options, table_cache));
209
208
if (options_.compaction_style == kCompactionStyleUniversal ) {
210
- compaction_picker_.reset (new UniversalCompactionPicker (
211
- &options_, &internal_comparator_, db_options-> info_log . get () ));
209
+ compaction_picker_.reset (
210
+ new UniversalCompactionPicker ( &options_, &internal_comparator_));
212
211
} else {
213
- compaction_picker_.reset (new LevelCompactionPicker (
214
- &options_, &internal_comparator_, db_options-> info_log . get () ));
212
+ compaction_picker_.reset (
213
+ new LevelCompactionPicker ( &options_, &internal_comparator_));
215
214
}
216
215
217
- Log (full_options_ .info_log , " Options for column family \" %s\" :\n " ,
216
+ Log (options_ .info_log , " Options for column family \" %s\" :\n " ,
218
217
name.c_str ());
219
- options_.Dump (full_options_.info_log .get ());
218
+ const ColumnFamilyOptions* cf_options = &options_;
219
+ cf_options->Dump (options_.info_log .get ());
220
220
}
221
221
}
222
222
@@ -232,14 +232,27 @@ ColumnFamilyData::~ColumnFamilyData() {
232
232
// it's nullptr for dummy CFD
233
233
if (column_family_set_ != nullptr ) {
234
234
// remove from column_family_set
235
- column_family_set_->DropColumnFamily (this );
235
+ column_family_set_->RemoveColumnFamily (this );
236
236
}
237
237
238
238
if (current_ != nullptr ) {
239
239
current_->Unref ();
240
240
}
241
241
242
- DeleteSuperVersion ();
242
+ if (super_version_ != nullptr ) {
243
+ // Release SuperVersion reference kept in ThreadLocalPtr.
244
+ // This must be done outside of mutex_ since unref handler can lock mutex.
245
+ super_version_->db_mutex ->Unlock ();
246
+ local_sv_.reset ();
247
+ super_version_->db_mutex ->Lock ();
248
+
249
+ bool is_last_reference __attribute__ ((unused));
250
+ is_last_reference = super_version_->Unref ();
251
+ assert (is_last_reference);
252
+ super_version_->Cleanup ();
253
+ delete super_version_;
254
+ super_version_ = nullptr ;
255
+ }
243
256
244
257
if (dummy_versions_ != nullptr ) {
245
258
// List must be empty
@@ -257,10 +270,6 @@ ColumnFamilyData::~ColumnFamilyData() {
257
270
}
258
271
}
259
272
260
- InternalStats* ColumnFamilyData::internal_stats () {
261
- return internal_stats_.get ();
262
- }
263
-
264
273
void ColumnFamilyData::SetCurrent (Version* current) {
265
274
current_ = current;
266
275
need_slowdown_for_num_level0_files_ =
@@ -320,23 +329,6 @@ void ColumnFamilyData::ResetThreadLocalSuperVersions() {
320
329
}
321
330
}
322
331
323
- void ColumnFamilyData::DeleteSuperVersion () {
324
- if (super_version_ != nullptr ) {
325
- // Release SuperVersion reference kept in ThreadLocalPtr.
326
- // This must be done outside of mutex_ since unref handler can lock mutex.
327
- super_version_->db_mutex ->Unlock ();
328
- local_sv_.reset ();
329
- super_version_->db_mutex ->Lock ();
330
-
331
- bool is_last_reference __attribute__ ((unused));
332
- is_last_reference = super_version_->Unref ();
333
- assert (is_last_reference);
334
- super_version_->Cleanup ();
335
- delete super_version_;
336
- super_version_ = nullptr ;
337
- }
338
- }
339
-
340
332
ColumnFamilySet::ColumnFamilySet (const std::string& dbname,
341
333
const DBOptions* db_options,
342
334
const EnvOptions& storage_options,
@@ -345,6 +337,7 @@ ColumnFamilySet::ColumnFamilySet(const std::string& dbname,
345
337
dummy_cfd_ (new ColumnFamilyData(dbname, 0 , " " , nullptr , nullptr ,
346
338
ColumnFamilyOptions (), db_options,
347
339
storage_options_, nullptr)),
340
+ default_cfd_cache_(nullptr ),
348
341
db_name_(dbname),
349
342
db_options_(db_options),
350
343
storage_options_(storage_options),
@@ -367,10 +360,8 @@ ColumnFamilySet::~ColumnFamilySet() {
367
360
}
368
361
369
362
ColumnFamilyData* ColumnFamilySet::GetDefault () const {
370
- auto cfd = GetColumnFamily (0 );
371
- // default column family should always exist
372
- assert (cfd != nullptr );
373
- return cfd;
363
+ assert (default_cfd_cache_ != nullptr );
364
+ return default_cfd_cache_;
374
365
}
375
366
376
367
ColumnFamilyData* ColumnFamilySet::GetColumnFamily (uint32_t id) const {
@@ -385,24 +376,13 @@ ColumnFamilyData* ColumnFamilySet::GetColumnFamily(uint32_t id) const {
385
376
ColumnFamilyData* ColumnFamilySet::GetColumnFamily (const std::string& name)
386
377
const {
387
378
auto cfd_iter = column_families_.find (name);
388
- if (cfd_iter == column_families_.end ()) {
379
+ if (cfd_iter != column_families_.end ()) {
380
+ auto cfd = GetColumnFamily (cfd_iter->second );
381
+ assert (cfd != nullptr );
382
+ return cfd;
383
+ } else {
389
384
return nullptr ;
390
385
}
391
- return GetColumnFamily (cfd_iter->second );
392
- }
393
-
394
- bool ColumnFamilySet::Exists (uint32_t id) {
395
- return column_family_data_.find (id) != column_family_data_.end ();
396
- }
397
-
398
- bool ColumnFamilySet::Exists (const std::string& name) {
399
- return column_families_.find (name) != column_families_.end ();
400
- }
401
-
402
- uint32_t ColumnFamilySet::GetID (const std::string& name) {
403
- auto cfd_iter = column_families_.find (name);
404
- assert (cfd_iter != column_families_.end ());
405
- return cfd_iter->second ;
406
386
}
407
387
408
388
uint32_t ColumnFamilySet::GetNextColumnFamilyID () {
@@ -434,11 +414,22 @@ ColumnFamilyData* ColumnFamilySet::CreateColumnFamily(
434
414
new_cfd->prev_ = prev;
435
415
prev->next_ = new_cfd;
436
416
dummy_cfd_->prev_ = new_cfd;
417
+ if (id == 0 ) {
418
+ default_cfd_cache_ = new_cfd;
419
+ }
437
420
return new_cfd;
438
421
}
439
422
423
+ void ColumnFamilySet::Lock () {
424
+ // spin lock
425
+ while (spin_lock_.test_and_set (std::memory_order_acquire)) {
426
+ }
427
+ }
428
+
429
+ void ColumnFamilySet::Unlock () { spin_lock_.clear (std::memory_order_release); }
430
+
440
431
// under a DB mutex
441
- void ColumnFamilySet::DropColumnFamily (ColumnFamilyData* cfd) {
432
+ void ColumnFamilySet::RemoveColumnFamily (ColumnFamilyData* cfd) {
442
433
auto cfd_iter = column_family_data_.find (cfd->GetID ());
443
434
assert (cfd_iter != column_family_data_.end ());
444
435
Lock ();
@@ -447,19 +438,16 @@ void ColumnFamilySet::DropColumnFamily(ColumnFamilyData* cfd) {
447
438
Unlock ();
448
439
}
449
440
450
- void ColumnFamilySet::Lock () {
451
- // spin lock
452
- while (spin_lock_.test_and_set (std::memory_order_acquire)) {
453
- }
454
- }
455
-
456
- void ColumnFamilySet::Unlock () { spin_lock_.clear (std::memory_order_release); }
457
-
458
441
bool ColumnFamilyMemTablesImpl::Seek (uint32_t column_family_id) {
459
- // maybe outside of db mutex, should lock
460
- column_family_set_->Lock ();
461
- current_ = column_family_set_->GetColumnFamily (column_family_id);
462
- column_family_set_->Unlock ();
442
+ if (column_family_id == 0 ) {
443
+ // optimization for common case
444
+ current_ = column_family_set_->GetDefault ();
445
+ } else {
446
+ // maybe outside of db mutex, should lock
447
+ column_family_set_->Lock ();
448
+ current_ = column_family_set_->GetColumnFamily (column_family_id);
449
+ column_family_set_->Unlock ();
450
+ }
463
451
handle_.SetCFD (current_);
464
452
return current_ != nullptr ;
465
453
}
@@ -474,9 +462,9 @@ MemTable* ColumnFamilyMemTablesImpl::GetMemTable() const {
474
462
return current_->mem ();
475
463
}
476
464
477
- const Options* ColumnFamilyMemTablesImpl::GetFullOptions () const {
465
+ const Options* ColumnFamilyMemTablesImpl::GetOptions () const {
478
466
assert (current_ != nullptr );
479
- return current_->full_options ();
467
+ return current_->options ();
480
468
}
481
469
482
470
ColumnFamilyHandle* ColumnFamilyMemTablesImpl::GetColumnFamilyHandle () {
0 commit comments