Repository navigation
Expand file tree
/
Copy pathcompaction.cpp
More file actions
144 lines (124 loc) · 5.39 KB
/
Copy pathcompaction.cpp
File metadata and controls
144 lines (124 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "compaction.h"
#include "log.h"
#include "mem_table.h"
#include "ss_table.h"
#include <queue>
#include <memory>
#include <iostream>
#include <filesystem>
// Helper to track an entry and which SSTable it belongs to
struct MergeEntry {
std::string key;
std::string value;
size_t table_index;
// Custom comparison!
bool operator>(const MergeEntry& other) const {
if (key != other.key) {
return key > other.key;
}
// If keys are equal, the LOWER table_index is more recent (tables are
// ordered newest first), so it must pop first from the min-heap
return table_index > other.table_index;
}
};
// Old implementation - loads everything into RAM
std::map<std::string, std::string> compaction::merge(const std::vector<std::map<std::string, std::string>>& sstables) {
std::map<std::string, std::string> merged_data;
// min_heap to get smallest key across all SSTables
std::priority_queue<MergeEntry, std::vector<MergeEntry>, std::greater<MergeEntry>> min_heap;
// A vector to track current iterators for each SSTable
std::vector<std::map<std::string, std::string>::const_iterator> iterators;
// Building the heap with the first entry from each SSTable
for (size_t i = 0; i < sstables.size(); ++i) {
if (!sstables[i].empty()) {
auto it = sstables[i].begin();
min_heap.push({it->first, it->second, i});
iterators.push_back(it);
} else {
// NEED THIS or else INDEXING WILL BE OFF
iterators.push_back(sstables[i].end());
}
}
// We need to track the last key added to avoid duplicates
std::string last_key = "";
// K-way merge process: only ever have n members in the heap, one from each SSTable
// Pop the smallest, push the next from that SSTable
while (!min_heap.empty()) {
// Get the smallest entry from the heap
MergeEntry current = min_heap.top();
min_heap.pop();
// If this key is different from the last added key, add it
// Very cool trick: we only add the most recent since the min_heap orders by table_index
// So we only need to check if the key is different from last_key
if (current.key != last_key) {
if (current.value != mem_table::tombstone) {
merged_data[current.key] = current.value;
}
last_key = current.key;
}
// Move current ss_table iterator forward
auto& it = iterators[current.table_index];
++it;
// If not the end, push the next entry from this SSTable to min_heap
if (it != sstables[current.table_index].end()) {
min_heap.push({it->first, it->second, current.table_index});
}
}
return merged_data;
}
// New streaming merge - only keeps iterators in memory, not all data!
std::optional<size_t> compaction::streaming_merge(const std::vector<std::string>& sstable_files,
const std::string& output_file) {
try {
// Writes the block-based output; deletes the partial file if we
// throw or return before finish() succeeds
sstable_builder builder(output_file);
// Create iterators for each input SSTable (newest first). Opening or
// reading a corrupt input throws, which aborts the whole compaction:
// treating it as empty would delete its data with the inputs.
std::vector<std::unique_ptr<SSTableIterator>> iterators;
for (const auto& file : sstable_files) {
auto it = std::make_unique<SSTableIterator>(file);
if (it->valid()) {
iterators.push_back(std::move(it));
}
}
// Min heap to track which iterator has the smallest current key
std::priority_queue<MergeEntry, std::vector<MergeEntry>, std::greater<MergeEntry>> min_heap;
// Initialize heap with first entry from each iterator
for (size_t i = 0; i < iterators.size(); ++i) {
min_heap.push({iterators[i]->key(), iterators[i]->value(), i});
}
std::string last_key;
bool have_last = false;
// K-way merge: process entries in sorted order
while (!min_heap.empty()) {
MergeEntry current = min_heap.top();
min_heap.pop();
// Only write if this is a new key (skip duplicates, take most recent)
if (!have_last || current.key != last_key) {
// Skip tombstones
if (current.value != mem_table::tombstone) {
builder.add(slice(current.key), slice(current.value));
}
last_key = current.key;
have_last = true;
}
// Advance the iterator that just gave us this entry
size_t iter_idx = current.table_index;
if (iterators[iter_idx]->next()) {
min_heap.push({iterators[iter_idx]->key(), iterators[iter_idx]->value(), iter_idx});
}
}
if (!builder.finish()) {
smalldb_log::error("Compaction failed (write error): ", output_file);
return std::nullopt;
}
return builder.num_entries();
} catch (const std::exception& e) {
smalldb_log::error("Compaction failed (", e.what(), "): ", output_file);
std::error_code ec;
std::filesystem::remove(output_file, ec);
return std::nullopt;
}
}