Skip to content

Commit c281cf1

Browse files
authored
fil-422 unsorted hamt (#480)
Signed-off-by: turuslan <[email protected]>
1 parent 13c170e commit c281cf1

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

core/storage/hamt/hamt.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ OUTCOME_CPP_DEFINE_CATEGORY(fc::storage::hamt, HamtError, e) {
2626
namespace fc::storage::hamt {
2727
using common::which;
2828

29+
inline auto leafFind(Node::Leaf &leaf, BytesIn key) {
30+
return std::find_if(
31+
leaf.begin(), leaf.end(), [&](auto &p) { return p.first == key; });
32+
}
33+
34+
inline void leafInsert(Node::Leaf &leaf, Node::Leaf::value_type pair) {
35+
leaf.emplace(
36+
std::lower_bound(leaf.begin(),
37+
leaf.end(),
38+
pair,
39+
[&](auto &l, auto &r) { return l.first < r.first; }),
40+
std::move(pair));
41+
}
42+
2943
CBOR2_ENCODE(Node) {
3044
Bits bits;
3145
auto l_items{s.list()};
@@ -93,7 +107,7 @@ namespace fc::storage::hamt {
93107
for (size_t j = 0; j < n_leaf; ++j) {
94108
auto l_pair{l_leaf.list()};
95109
auto key{l_pair.get<Bytes>()};
96-
leaf.emplace(std::move(key), l_pair.raw());
110+
leaf.emplace_back(std::move(key), l_pair.raw());
97111
}
98112
v.items[j] = std::move(leaf);
99113
}
@@ -140,7 +154,7 @@ namespace fc::storage::hamt {
140154
node = boost::get<Node::Ptr>(item);
141155
} else {
142156
auto &leaf = boost::get<Node::Leaf>(item);
143-
auto it{leaf.find(key)};
157+
auto it{leafFind(leaf, key)};
144158
if (it == leaf.end()) {
145159
return HamtError::kNotFound;
146160
}
@@ -219,10 +233,10 @@ namespace fc::storage::hamt {
219233
*boost::get<Node::Ptr>(item), consumeIndex(indices), key, value);
220234
}
221235
auto &leaf = boost::get<Node::Leaf>(item);
222-
if (auto it2{leaf.find(key)}; it2 != leaf.end()) {
236+
if (auto it2{leafFind(leaf, key)}; it2 != leaf.end()) {
223237
copy(it2->second, value);
224238
} else if (leaf.size() < kLeafMax) {
225-
leaf.emplace(copy(key), copy(value));
239+
leafInsert(leaf, {copy(key), copy(value)});
226240
} else {
227241
auto child = std::make_shared<Node>();
228242
child->v3 = v3();
@@ -255,13 +269,14 @@ namespace fc::storage::hamt {
255269
OUTCOME_TRY(cleanShard(item));
256270
} else {
257271
auto &leaf = boost::get<Node::Leaf>(item);
258-
if (leaf.find(key) == leaf.end()) {
272+
auto it2{leafFind(leaf, key)};
273+
if (it2 == leaf.end()) {
259274
return HamtError::kNotFound;
260275
}
261276
if (leaf.size() == 1) {
262277
node.items.erase(index);
263278
} else {
264-
leaf.erase(leaf.find(key));
279+
leaf.erase(it2);
265280
}
266281
}
267282
return outcome::success();
@@ -281,7 +296,7 @@ namespace fc::storage::hamt {
281296
return outcome::success();
282297
}
283298
for (auto &pair : boost::get<Node::Leaf>(item2.second)) {
284-
leaf.emplace(pair);
299+
leafInsert(leaf, pair);
285300
if (leaf.size() > kLeafMax) {
286301
return outcome::success();
287302
}

core/storage/hamt/hamt.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace fc::storage::hamt {
5757
/** Hamt node representation */
5858
struct Node {
5959
using Ptr = std::shared_ptr<Node>;
60-
using Leaf = std::map<Bytes, Bytes, BytesLess>;
60+
using Leaf = std::vector<std::pair<Bytes, Bytes>>;
6161
using Item = boost::variant<CID, Ptr, Leaf>;
6262

6363
std::map<size_t, Item> items;

test/core/test_vectors/test.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,6 @@ auto search() {
245245
const auto &path{item.path()};
246246
if (item.status().type() == boost::filesystem::file_type::regular_file
247247
&& path.extension() == ".json") {
248-
// Skip tests that fail in Fuhon
249-
static std::vector<boost::filesystem::path> fail_in_fuhon{
250-
// TODO (m.tagirov) FIL-422 test-vectors hamt have incorrect order
251-
// Fuhon implementation loads and reorders hamt
252-
// while Lotus implementation uses lazy approach and keeps initial incorrect order
253-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storagepower/CreateMiner/Ok/ext-0004-fil_1_storagepower-CreateMiner-Ok-6.json",
254-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storagepower/CreateMiner/Ok/ext-0004-fil_1_storagepower-CreateMiner-Ok-10.json",
255-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storagepower/CreateMiner/Ok/ext-0001-fil_1_storagepower-CreateMiner-Ok-6.json",
256-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storageminer/PreCommitSector/Ok/ext-0001-fil_1_storageminer-PreCommitSector-Ok-1.json",
257-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storageminer/PreCommitSector/Ok/ext-0001-fil_1_storageminer-PreCommitSector-Ok-5.json",
258-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storageminer/PreCommitSector/Ok/ext-0001-fil_1_storageminer-PreCommitSector-Ok-6.json",
259-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storageminer/PreCommitSector/Ok/ext-0001-fil_1_storageminer-PreCommitSector-Ok-8.json",
260-
kCorpusRoot / "extracted/0001-initial-extraction/fil_1_storageminer/PreCommitSector/Ok/ext-0001-fil_1_storageminer-PreCommitSector-Ok-9.json",
261-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storageminer/PreCommitSector/Ok/ext-0004-fil_1_storageminer-PreCommitSector-Ok-4.json",
262-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storageminer/PreCommitSector/Ok/ext-0004-fil_1_storageminer-PreCommitSector-Ok-5.json",
263-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storageminer/PreCommitSector/Ok/ext-0004-fil_1_storageminer-PreCommitSector-Ok-7.json",
264-
kCorpusRoot / "extracted/0004-coverage-boost/fil_1_storageminer/PreCommitSector/Ok/ext-0004-fil_1_storageminer-PreCommitSector-Ok-9.json",
265-
};
266-
267-
if (std::find(fail_in_fuhon.cbegin(), fail_in_fuhon.cend(), path)
268-
!= fail_in_fuhon.cend()) {
269-
continue;
270-
}
271-
272248
// ignore broken/incorrect vectors that starts with "x--"
273249
if (boost::algorithm::starts_with(path.filename().string(), "x--")) {
274250
continue;

0 commit comments

Comments
 (0)