20
20
21
21
namespace leveldb {
22
22
23
- static const int kTargetFileSize = 2 * 1048576 ;
23
+ static int TargetFileSize (const Options* options) {
24
+ return options->max_file_size ;
25
+ }
24
26
25
27
// Maximum bytes of overlaps in grandparent (i.e., level+2) before we
26
28
// stop building a single file in a level->level+1 compaction.
27
- static const int64_t kMaxGrandParentOverlapBytes = 10 * kTargetFileSize ;
29
+ static int64_t MaxGrandParentOverlapBytes (const Options* options) {
30
+ return 10 * TargetFileSize (options);
31
+ }
28
32
29
33
// Maximum number of bytes in all compacted files. We avoid expanding
30
34
// the lower level file set of a compaction if it would make the
31
35
// total compaction cover more than this many bytes.
32
- static const int64_t kExpandedCompactionByteSizeLimit = 25 * kTargetFileSize ;
36
+ static int64_t ExpandedCompactionByteSizeLimit (const Options* options) {
37
+ return 25 * TargetFileSize (options);
38
+ }
33
39
34
- static double MaxBytesForLevel (int level) {
40
+ static double MaxBytesForLevel (const Options* options, int level) {
35
41
// Note: the result for level zero is not really used since we set
36
42
// the level-0 compaction threshold based on number of files.
37
- double result = 10 * 1048576.0 ; // Result for both level-0 and level-1
43
+
44
+ // Result for both level-0 and level-1
45
+ double result = 10 . * 1048576.0 ;
38
46
while (level > 1 ) {
39
47
result *= 10 ;
40
48
level--;
41
49
}
42
50
return result;
43
51
}
44
52
45
- static uint64_t MaxFileSizeForLevel (int level) {
46
- return kTargetFileSize ; // We could vary per level to reduce number of files?
53
+ static uint64_t MaxFileSizeForLevel (const Options* options, int level) {
54
+ // We could vary per level to reduce number of files?
55
+ return TargetFileSize (options);
47
56
}
48
57
49
58
static int64_t TotalFileSize (const std::vector<FileMetaData*>& files) {
@@ -508,7 +517,7 @@ int Version::PickLevelForMemTableOutput(
508
517
// Check that file does not overlap too many grandparent bytes.
509
518
GetOverlappingInputs (level + 2 , &start, &limit, &overlaps);
510
519
const int64_t sum = TotalFileSize (overlaps);
511
- if (sum > kMaxGrandParentOverlapBytes ) {
520
+ if (sum > MaxGrandParentOverlapBytes (vset_-> options_ ) ) {
512
521
break ;
513
522
}
514
523
}
@@ -1027,7 +1036,7 @@ bool VersionSet::ReuseManifest(const std::string& dscname,
1027
1036
manifest_type != kDescriptorFile ||
1028
1037
!env_->GetFileSize (dscname, &manifest_size).ok () ||
1029
1038
// Make new compacted MANIFEST if old one is too big
1030
- manifest_size >= kTargetFileSize ) {
1039
+ manifest_size >= TargetFileSize (options_) ) {
1031
1040
return false ;
1032
1041
}
1033
1042
@@ -1076,7 +1085,8 @@ void VersionSet::Finalize(Version* v) {
1076
1085
} else {
1077
1086
// Compute the ratio of current size to size limit.
1078
1087
const uint64_t level_bytes = TotalFileSize (v->files_ [level]);
1079
- score = static_cast <double >(level_bytes) / MaxBytesForLevel (level);
1088
+ score =
1089
+ static_cast <double >(level_bytes) / MaxBytesForLevel (options_, level);
1080
1090
}
1081
1091
1082
1092
if (score > best_score) {
@@ -1290,7 +1300,7 @@ Compaction* VersionSet::PickCompaction() {
1290
1300
level = current_->compaction_level_ ;
1291
1301
assert (level >= 0 );
1292
1302
assert (level+1 < config::kNumLevels );
1293
- c = new Compaction (level);
1303
+ c = new Compaction (options_, level);
1294
1304
1295
1305
// Pick the first file that comes after compact_pointer_[level]
1296
1306
for (size_t i = 0 ; i < current_->files_ [level].size (); i++) {
@@ -1307,7 +1317,7 @@ Compaction* VersionSet::PickCompaction() {
1307
1317
}
1308
1318
} else if (seek_compaction) {
1309
1319
level = current_->file_to_compact_level_ ;
1310
- c = new Compaction (level);
1320
+ c = new Compaction (options_, level);
1311
1321
c->inputs_ [0 ].push_back (current_->file_to_compact_ );
1312
1322
} else {
1313
1323
return NULL ;
@@ -1352,7 +1362,8 @@ void VersionSet::SetupOtherInputs(Compaction* c) {
1352
1362
const int64_t inputs1_size = TotalFileSize (c->inputs_ [1 ]);
1353
1363
const int64_t expanded0_size = TotalFileSize (expanded0);
1354
1364
if (expanded0.size () > c->inputs_ [0 ].size () &&
1355
- inputs1_size + expanded0_size < kExpandedCompactionByteSizeLimit ) {
1365
+ inputs1_size + expanded0_size <
1366
+ ExpandedCompactionByteSizeLimit (options_)) {
1356
1367
InternalKey new_start, new_limit;
1357
1368
GetRange (expanded0, &new_start, &new_limit);
1358
1369
std::vector<FileMetaData*> expanded1;
@@ -1414,7 +1425,7 @@ Compaction* VersionSet::CompactRange(
1414
1425
// and we must not pick one file and drop another older file if the
1415
1426
// two files overlap.
1416
1427
if (level > 0 ) {
1417
- const uint64_t limit = MaxFileSizeForLevel (level);
1428
+ const uint64_t limit = MaxFileSizeForLevel (options_, level);
1418
1429
uint64_t total = 0 ;
1419
1430
for (size_t i = 0 ; i < inputs.size (); i++) {
1420
1431
uint64_t s = inputs[i]->file_size ;
@@ -1426,17 +1437,17 @@ Compaction* VersionSet::CompactRange(
1426
1437
}
1427
1438
}
1428
1439
1429
- Compaction* c = new Compaction (level);
1440
+ Compaction* c = new Compaction (options_, level);
1430
1441
c->input_version_ = current_;
1431
1442
c->input_version_ ->Ref ();
1432
1443
c->inputs_ [0 ] = inputs;
1433
1444
SetupOtherInputs (c);
1434
1445
return c;
1435
1446
}
1436
1447
1437
- Compaction::Compaction (int level)
1448
+ Compaction::Compaction (const Options* options, int level)
1438
1449
: level_(level),
1439
- max_output_file_size_(MaxFileSizeForLevel(level)),
1450
+ max_output_file_size_(MaxFileSizeForLevel(options, level)),
1440
1451
input_version_(NULL ),
1441
1452
grandparent_index_(0 ),
1442
1453
seen_key_(false ),
@@ -1453,12 +1464,13 @@ Compaction::~Compaction() {
1453
1464
}
1454
1465
1455
1466
bool Compaction::IsTrivialMove () const {
1467
+ const VersionSet* vset = input_version_->vset_ ;
1456
1468
// Avoid a move if there is lots of overlapping grandparent data.
1457
1469
// Otherwise, the move could create a parent file that will require
1458
1470
// a very expensive merge later on.
1459
- return (num_input_files (0 ) == 1 &&
1460
- num_input_files ( 1 ) == 0 &&
1461
- TotalFileSize (grandparents_) <= kMaxGrandParentOverlapBytes );
1471
+ return (num_input_files (0 ) == 1 && num_input_files ( 1 ) == 0 &&
1472
+ TotalFileSize (grandparents_) <=
1473
+ MaxGrandParentOverlapBytes (vset-> options_ ) );
1462
1474
}
1463
1475
1464
1476
void Compaction::AddInputDeletions (VersionEdit* edit) {
@@ -1491,8 +1503,9 @@ bool Compaction::IsBaseLevelForKey(const Slice& user_key) {
1491
1503
}
1492
1504
1493
1505
bool Compaction::ShouldStopBefore (const Slice& internal_key) {
1506
+ const VersionSet* vset = input_version_->vset_ ;
1494
1507
// Scan to find earliest grandparent file that contains key.
1495
- const InternalKeyComparator* icmp = &input_version_-> vset_ ->icmp_ ;
1508
+ const InternalKeyComparator* icmp = &vset ->icmp_ ;
1496
1509
while (grandparent_index_ < grandparents_.size () &&
1497
1510
icmp->Compare (internal_key,
1498
1511
grandparents_[grandparent_index_]->largest .Encode ()) > 0 ) {
@@ -1503,7 +1516,7 @@ bool Compaction::ShouldStopBefore(const Slice& internal_key) {
1503
1516
}
1504
1517
seen_key_ = true ;
1505
1518
1506
- if (overlapped_bytes_ > kMaxGrandParentOverlapBytes ) {
1519
+ if (overlapped_bytes_ > MaxGrandParentOverlapBytes (vset-> options_ ) ) {
1507
1520
// Too much overlap for current output; start new output
1508
1521
overlapped_bytes_ = 0 ;
1509
1522
return true ;
0 commit comments