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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/CAS/OnDiskTrieRawHashMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Testing/Support/Error.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h"
#if LLVM_ENABLE_ONDISK_CAS
using namespace llvm;
using namespace llvm::cas;
namespace {
struct OnDiskTrieRawHashMapTestFixture
: public ::testing::TestWithParam<size_t> {
static constexpr size_t MB = 1024u * 1024u;
static constexpr size_t DataSize = 8; // Multiple of 8B.
std::optional<unittest::TempDir> Temp;
size_t NumHashBytes;
void SetUp() override {
Temp.emplace("trie-raw-hash-map", /*Unique=*/true);
NumHashBytes = GetParam();
}
void TearDown() override { Temp.reset(); }
Expected<OnDiskTrieRawHashMap> createTrie() {
size_t NumHashBits = NumHashBytes * 8;
return OnDiskTrieRawHashMap::create(
Temp->path((Twine(NumHashBytes) + "B").str()), "index",
/*NumHashBits=*/NumHashBits, DataSize, /*MaxFileSize=*/MB,
/*NewInitialFileSize=*/std::nullopt);
}
};
// Create tries with various sizes of hash and with data.
TEST_P(OnDiskTrieRawHashMapTestFixture, General) {
std::optional<OnDiskTrieRawHashMap> Trie1;
ASSERT_THAT_ERROR(createTrie().moveInto(Trie1), Succeeded());
std::optional<OnDiskTrieRawHashMap> Trie2;
ASSERT_THAT_ERROR(createTrie().moveInto(Trie2), Succeeded());
uint8_t Hash0Bytes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t Hash1Bytes[8] = {1, 0, 0, 0, 0, 0, 0, 0};
auto Hash0 = ArrayRef(Hash0Bytes).take_front(NumHashBytes);
auto Hash1 = ArrayRef(Hash1Bytes).take_front(NumHashBytes);
constexpr StringLiteral Data0v1Bytes = "data0.v1";
constexpr StringLiteral Data0v2Bytes = "data0.v2";
constexpr StringLiteral Data1Bytes = "data1...";
static_assert(Data0v1Bytes.size() == DataSize, "math error");
static_assert(Data0v2Bytes.size() == DataSize, "math error");
static_assert(Data1Bytes.size() == DataSize, "math error");
ArrayRef<char> Data0v1 = ArrayRef(Data0v1Bytes.data(), Data0v1Bytes.size());
ArrayRef<char> Data0v2 = ArrayRef(Data0v2Bytes.data(), Data0v2Bytes.size());
ArrayRef<char> Data1 = ArrayRef(Data1Bytes.data(), Data1Bytes.size());
// Lookup when trie is empty.
EXPECT_FALSE(Trie1->find(Hash0));
// Insert.
std::optional<FileOffset> Offset;
std::optional<MutableArrayRef<char>> Data;
{
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
ASSERT_THAT_ERROR(Trie1->insert({Hash0, Data0v1}).moveInto(Insertion),
Succeeded());
EXPECT_EQ(Hash0, (*Insertion)->Hash);
EXPECT_EQ(Data0v1, (*Insertion)->Data);
EXPECT_TRUE(isAddrAligned(Align(8), (*Insertion)->Data.data()));
Offset = Insertion->getOffset();
Data = (*Insertion)->Data;
}
// Find.
{
auto Lookup = Trie1->find(Hash0);
ASSERT_TRUE(Lookup);
EXPECT_EQ(Hash0, Lookup->Hash);
EXPECT_EQ(Data0v1, Lookup->Data);
EXPECT_EQ(Offset->get(), Lookup.getOffset().get());
}
// Find in a different instance of the same on-disk trie that existed
// before the insertion.
{
auto Lookup = Trie2->find(Hash0);
ASSERT_TRUE(Lookup);
EXPECT_EQ(Hash0, Lookup->Hash);
EXPECT_EQ(Data0v1, Lookup->Data);
EXPECT_EQ(Offset->get(), Lookup.getOffset().get());
}
// Create a new instance and check that too.
Trie2.reset();
ASSERT_THAT_ERROR(createTrie().moveInto(Trie2), Succeeded());
{
auto Lookup = Trie2->find(Hash0);
ASSERT_TRUE(Lookup);
EXPECT_EQ(Hash0, Lookup->Hash);
EXPECT_EQ(Data0v1, Lookup->Data);
EXPECT_EQ(Offset->get(), Lookup.getOffset().get());
}
// Change the data.
llvm::copy(Data0v2, Data->data());
{
auto Lookup = Trie2->find(Hash0);
ASSERT_TRUE(Lookup);
EXPECT_EQ(Hash0, Lookup->Hash);
EXPECT_EQ(Data0v2, Lookup->Data);
EXPECT_EQ(Offset->get(), Lookup.getOffset().get());
}
// Find different hash.
EXPECT_FALSE(Trie1->find(Hash1));
EXPECT_FALSE(Trie2->find(Hash1));
// Recover from an offset.
{
OnDiskTrieRawHashMap::const_pointer Recovered;
ASSERT_THAT_ERROR(Trie1->recoverFromFileOffset(*Offset).moveInto(Recovered),
Succeeded());
ASSERT_TRUE(Recovered);
EXPECT_EQ(Offset->get(), Recovered.getOffset().get());
EXPECT_EQ(Hash0, Recovered->Hash);
EXPECT_EQ(Data0v2, Recovered->Data);
}
// Recover from a bad offset.
{
FileOffset BadOffset(1);
OnDiskTrieRawHashMap::const_pointer Recovered;
ASSERT_THAT_ERROR(
Trie1->recoverFromFileOffset(BadOffset).moveInto(Recovered), Failed());
}
// Insert another thing.
{
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
ASSERT_THAT_ERROR(Trie1->insert({Hash1, Data1}).moveInto(Insertion),
Succeeded());
EXPECT_EQ(Hash1, (*Insertion)->Hash);
EXPECT_EQ(Data1, (*Insertion)->Data);
EXPECT_TRUE(isAddrAligned(Align(8), (*Insertion)->Data.data()));
EXPECT_NE(Offset->get(), Insertion->getOffset().get());
}
// Validate.
{
auto RecordVerify =
[&](FileOffset Offset,
OnDiskTrieRawHashMap::ConstValueProxy Proxy) -> Error {
if (Proxy.Hash.size() != NumHashBytes)
return createStringError("wrong hash size");
if (Proxy.Data.size() != DataSize)
return createStringError("wrong data size");
return Error::success();
};
ASSERT_THAT_ERROR(Trie1->validate(RecordVerify), Succeeded());
ASSERT_THAT_ERROR(Trie2->validate(RecordVerify), Succeeded());
}
// Size and capacity.
{
EXPECT_EQ(Trie1->capacity(), MB);
EXPECT_EQ(Trie2->capacity(), MB);
EXPECT_LE(Trie1->size(), MB);
EXPECT_LE(Trie2->size(), MB);
}
}
INSTANTIATE_TEST_SUITE_P(OnDiskTrieRawHashMapTest,
OnDiskTrieRawHashMapTestFixture,
::testing::Values(1, 2, 4, 8));
TEST(OnDiskTrieRawHashMapTest, OutOfSpace) {
unittest::TempDir Temp("trie-raw-hash-map", /*Unique=*/true);
std::optional<OnDiskTrieRawHashMap> Trie;
// Too small to create header.
ASSERT_THAT_ERROR(OnDiskTrieRawHashMap::create(
Temp.path("NoSpace1").str(), "index",
/*NumHashBits=*/8, /*DataSize=*/8, /*MaxFileSize=*/8,
/*NewInitialFileSize=*/std::nullopt)
.moveInto(Trie),
Failed());
// Just enough for root node but not enough for any insertion.
ASSERT_THAT_ERROR(OnDiskTrieRawHashMap::create(
Temp.path("NoSpace2").str(), "index",
/*NumHashBits=*/8, /*DataSize=*/8, /*MaxFileSize=*/118,
/*NewInitialFileSize=*/std::nullopt,
/*NewTableNumRootBits=*/1, /*NewTableNumSubtrieBits=*/1)
.moveInto(Trie),
Succeeded());
uint8_t Hash0Bytes[1] = {0};
auto Hash0 = ArrayRef(Hash0Bytes);
constexpr StringLiteral Data0v1Bytes = "data0.v1";
ArrayRef<char> Data0v1 = ArrayRef(Data0v1Bytes.data(), Data0v1Bytes.size());
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
ASSERT_THAT_ERROR(Trie->insert({Hash0, Data0v1}).moveInto(Insertion),
Failed());
}
} // namespace
#endif // LLVM_ENABLE_ONDISK_CAS
|