1 //===--- OnDiskHashTable.h - On-Disk Hash Table Implementation --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines facilities for reading and writing on-disk hash tables.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_ONDISKHASHTABLE_H
15 #define LLVM_SUPPORT_ONDISKHASHTABLE_H
17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/EndianStream.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/raw_ostream.h"
29 /// \brief Generates an on disk hash table.
31 /// This needs an \c Info that handles storing values into the hash table's
32 /// payload and computes the hash for a given key. This should provide the
33 /// following interface:
36 /// class ExampleInfo {
38 /// typedef ExampleKey key_type; // Must be copy constructible
39 /// typedef ExampleKey &key_type_ref;
40 /// typedef ExampleData data_type; // Must be copy constructible
41 /// typedef ExampleData &data_type_ref;
42 /// typedef uint32_t hash_value_type; // The type the hash function returns.
43 /// typedef uint32_t offset_type; // The type for offsets into the table.
45 /// /// Calculate the hash for Key
46 /// static hash_value_type ComputeHash(key_type_ref Key);
47 /// /// Return the lengths, in bytes, of the given Key/Data pair.
48 /// static std::pair<offset_type, offset_type>
49 /// EmitKeyDataLength(raw_ostream &Out, key_type_ref Key, data_type_ref Data);
50 /// /// Write Key to Out. KeyLen is the length from EmitKeyDataLength.
51 /// static void EmitKey(raw_ostream &Out, key_type_ref Key,
52 /// offset_type KeyLen);
53 /// /// Write Data to Out. DataLen is the length from EmitKeyDataLength.
54 /// static void EmitData(raw_ostream &Out, key_type_ref Key,
55 /// data_type_ref Data, offset_type DataLen);
58 template <typename Info> class OnDiskChainedHashTableGenerator {
59 /// \brief A single item in the hash table.
62 typename Info::key_type Key;
63 typename Info::data_type Data;
65 const typename Info::hash_value_type Hash;
67 Item(typename Info::key_type_ref Key, typename Info::data_type_ref Data,
69 : Key(Key), Data(Data), Next(nullptr), Hash(InfoObj.ComputeHash(Key)) {}
72 typedef typename Info::offset_type offset_type;
73 offset_type NumBuckets;
74 offset_type NumEntries;
75 llvm::SpecificBumpPtrAllocator<Item> BA;
77 /// \brief A linked list of values in a particular hash bucket.
87 /// \brief Insert an item into the appropriate hash bucket.
88 void insert(Bucket *Buckets, size_t Size, Item *E) {
89 Bucket &B = Buckets[E->Hash & (Size - 1)];
95 /// \brief Resize the hash table, moving the old entries into the new buckets.
96 void resize(size_t NewSize) {
97 Bucket *NewBuckets = (Bucket *)std::calloc(NewSize, sizeof(Bucket));
98 // Populate NewBuckets with the old entries.
99 for (size_t I = 0; I < NumBuckets; ++I)
100 for (Item *E = Buckets[I].Head; E;) {
103 insert(NewBuckets, NewSize, E);
108 NumBuckets = NewSize;
109 Buckets = NewBuckets;
113 /// \brief Insert an entry into the table.
114 void insert(typename Info::key_type_ref Key,
115 typename Info::data_type_ref Data) {
117 insert(Key, Data, InfoObj);
120 /// \brief Insert an entry into the table.
122 /// Uses the provided Info instead of a stack allocated one.
123 void insert(typename Info::key_type_ref Key,
124 typename Info::data_type_ref Data, Info &InfoObj) {
127 if (4 * NumEntries >= 3 * NumBuckets)
128 resize(NumBuckets * 2);
129 insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
132 /// \brief Emit the table to Out, which must not be at offset 0.
133 offset_type Emit(raw_ostream &Out) {
135 return Emit(Out, InfoObj);
138 /// \brief Emit the table to Out, which must not be at offset 0.
140 /// Uses the provided Info instead of a stack allocated one.
141 offset_type Emit(raw_ostream &Out, Info &InfoObj) {
142 using namespace llvm::support;
143 endian::Writer<little> LE(Out);
145 // Emit the payload of the table.
146 for (offset_type I = 0; I < NumBuckets; ++I) {
147 Bucket &B = Buckets[I];
151 // Store the offset for the data of this bucket.
153 assert(B.Off && "Cannot write a bucket at offset 0. Please add padding.");
155 // Write out the number of items in the bucket.
156 LE.write<uint16_t>(B.Length);
157 assert(B.Length != 0 && "Bucket has a head but zero length?");
159 // Write out the entries in the bucket.
160 for (Item *I = B.Head; I; I = I->Next) {
161 LE.write<typename Info::hash_value_type>(I->Hash);
162 const std::pair<offset_type, offset_type> &Len =
163 InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
164 InfoObj.EmitKey(Out, I->Key, Len.first);
165 InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
169 // Pad with zeros so that we can start the hashtable at an aligned address.
170 offset_type TableOff = Out.tell();
171 uint64_t N = llvm::OffsetToAlignment(TableOff, alignOf<offset_type>());
174 LE.write<uint8_t>(0);
176 // Emit the hashtable itself.
177 LE.write<offset_type>(NumBuckets);
178 LE.write<offset_type>(NumEntries);
179 for (offset_type I = 0; I < NumBuckets; ++I)
180 LE.write<offset_type>(Buckets[I].Off);
185 OnDiskChainedHashTableGenerator() {
188 // Note that we do not need to run the constructors of the individual
189 // Bucket objects since 'calloc' returns bytes that are all 0.
190 Buckets = (Bucket *)std::calloc(NumBuckets, sizeof(Bucket));
193 ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
196 /// \brief Provides lookup on an on disk hash table.
198 /// This needs an \c Info that handles reading values from the hash table's
199 /// payload and computes the hash for a given key. This should provide the
200 /// following interface:
203 /// class ExampleLookupInfo {
205 /// typedef ExampleData data_type;
206 /// typedef ExampleInternalKey internal_key_type; // The stored key type.
207 /// typedef ExampleKey external_key_type; // The type to pass to find().
208 /// typedef uint32_t hash_value_type; // The type the hash function returns.
209 /// typedef uint32_t offset_type; // The type for offsets into the table.
211 /// /// Compare two keys for equality.
212 /// static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
213 /// /// Calculate the hash for the given key.
214 /// static hash_value_type ComputeHash(internal_key_type &IKey);
215 /// /// Translate from the semantic type of a key in the hash table to the
216 /// /// type that is actually stored and used for hashing and comparisons.
217 /// /// The internal and external types are often the same, in which case this
218 /// /// can simply return the passed in value.
219 /// static const internal_key_type &GetInternalKey(external_key_type &EKey);
220 /// /// Read the key and data length from Buffer, leaving it pointing at the
221 /// /// following byte.
222 /// static std::pair<offset_type, offset_type>
223 /// ReadKeyDataLength(const unsigned char *&Buffer);
224 /// /// Read the key from Buffer, given the KeyLen as reported from
225 /// /// ReadKeyDataLength.
226 /// const internal_key_type &ReadKey(const unsigned char *Buffer,
227 /// offset_type KeyLen);
228 /// /// Read the data for Key from Buffer, given the DataLen as reported from
229 /// /// ReadKeyDataLength.
230 /// data_type ReadData(StringRef Key, const unsigned char *Buffer,
231 /// offset_type DataLen);
234 template <typename Info> class OnDiskChainedHashTable {
235 const typename Info::offset_type NumBuckets;
236 const typename Info::offset_type NumEntries;
237 const unsigned char *const Buckets;
238 const unsigned char *const Base;
242 typedef typename Info::internal_key_type internal_key_type;
243 typedef typename Info::external_key_type external_key_type;
244 typedef typename Info::data_type data_type;
245 typedef typename Info::hash_value_type hash_value_type;
246 typedef typename Info::offset_type offset_type;
248 OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
249 const unsigned char *Buckets,
250 const unsigned char *Base,
251 const Info &InfoObj = Info())
252 : NumBuckets(NumBuckets), NumEntries(NumEntries), Buckets(Buckets),
253 Base(Base), InfoObj(InfoObj) {
254 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
255 "'buckets' must have a 4-byte alignment");
258 offset_type getNumBuckets() const { return NumBuckets; }
259 offset_type getNumEntries() const { return NumEntries; }
260 const unsigned char *getBase() const { return Base; }
261 const unsigned char *getBuckets() const { return Buckets; }
263 bool isEmpty() const { return NumEntries == 0; }
266 internal_key_type Key;
267 const unsigned char *const Data;
268 const offset_type Len;
272 iterator() : Data(nullptr), Len(0) {}
273 iterator(const internal_key_type K, const unsigned char *D, offset_type L,
275 : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
277 data_type operator*() const { return InfoObj->ReadData(Key, Data, Len); }
278 bool operator==(const iterator &X) const { return X.Data == Data; }
279 bool operator!=(const iterator &X) const { return X.Data != Data; }
282 /// \brief Look up the stored data for a particular key.
283 iterator find(const external_key_type &EKey, Info *InfoPtr = 0) {
287 using namespace llvm::support;
288 const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
289 hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
291 // Each bucket is just an offset into the hash table file.
292 offset_type Idx = KeyHash & (NumBuckets - 1);
293 const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
295 offset_type Offset = endian::readNext<offset_type, little, aligned>(Bucket);
297 return iterator(); // Empty bucket.
298 const unsigned char *Items = Base + Offset;
300 // 'Items' starts with a 16-bit unsigned integer representing the
301 // number of items in this bucket.
302 unsigned Len = endian::readNext<uint16_t, little, unaligned>(Items);
304 for (unsigned i = 0; i < Len; ++i) {
306 hash_value_type ItemHash =
307 endian::readNext<hash_value_type, little, unaligned>(Items);
309 // Determine the length of the key and the data.
310 const std::pair<offset_type, offset_type> &L =
311 Info::ReadKeyDataLength(Items);
312 offset_type ItemLen = L.first + L.second;
314 // Compare the hashes. If they are not the same, skip the entry entirely.
315 if (ItemHash != KeyHash) {
321 const internal_key_type &X =
322 InfoPtr->ReadKey((const unsigned char *const)Items, L.first);
324 // If the key doesn't match just skip reading the value.
325 if (!InfoPtr->EqualKey(X, IKey)) {
331 return iterator(X, Items + L.first, L.second, InfoPtr);
337 iterator end() const { return iterator(); }
339 Info &getInfoObj() { return InfoObj; }
341 /// \brief Create the hash table.
343 /// \param Buckets is the beginning of the hash table itself, which follows
344 /// the payload of entire structure. This is the value returned by
345 /// OnDiskHashTableGenerator::Emit.
347 /// \param Base is the point from which all offsets into the structure are
348 /// based. This is offset 0 in the stream that was used when Emitting the
350 static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
351 const unsigned char *const Base,
352 const Info &InfoObj = Info()) {
353 using namespace llvm::support;
354 assert(Buckets > Base);
355 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
356 "buckets should be 4-byte aligned.");
358 offset_type NumBuckets =
359 endian::readNext<offset_type, little, aligned>(Buckets);
360 offset_type NumEntries =
361 endian::readNext<offset_type, little, aligned>(Buckets);
362 return new OnDiskChainedHashTable<Info>(NumBuckets, NumEntries, Buckets,
367 /// \brief Provides lookup and iteration over an on disk hash table.
369 /// \copydetails llvm::OnDiskChainedHashTable
370 template <typename Info>
371 class OnDiskIterableChainedHashTable : public OnDiskChainedHashTable<Info> {
372 const unsigned char *Payload;
375 typedef OnDiskChainedHashTable<Info> base_type;
376 typedef typename base_type::internal_key_type internal_key_type;
377 typedef typename base_type::external_key_type external_key_type;
378 typedef typename base_type::data_type data_type;
379 typedef typename base_type::hash_value_type hash_value_type;
380 typedef typename base_type::offset_type offset_type;
382 OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
383 const unsigned char *Buckets,
384 const unsigned char *Payload,
385 const unsigned char *Base,
386 const Info &InfoObj = Info())
387 : base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
390 /// \brief Iterates over all of the keys in the table.
392 const unsigned char *Ptr;
393 offset_type NumItemsInBucketLeft;
394 offset_type NumEntriesLeft;
398 typedef external_key_type value_type;
400 key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
402 : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
405 : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
408 friend bool operator==(const key_iterator &X, const key_iterator &Y) {
409 return X.NumEntriesLeft == Y.NumEntriesLeft;
411 friend bool operator!=(const key_iterator &X, const key_iterator &Y) {
412 return X.NumEntriesLeft != Y.NumEntriesLeft;
415 key_iterator &operator++() { // Preincrement
416 using namespace llvm::support;
417 if (!NumItemsInBucketLeft) {
418 // 'Items' starts with a 16-bit unsigned integer representing the
419 // number of items in this bucket.
420 NumItemsInBucketLeft =
421 endian::readNext<uint16_t, little, unaligned>(Ptr);
423 Ptr += sizeof(hash_value_type); // Skip the hash.
424 // Determine the length of the key and the data.
425 const std::pair<offset_type, offset_type> &L =
426 Info::ReadKeyDataLength(Ptr);
427 Ptr += L.first + L.second;
428 assert(NumItemsInBucketLeft);
429 --NumItemsInBucketLeft;
430 assert(NumEntriesLeft);
434 key_iterator operator++(int) { // Postincrement
435 key_iterator tmp = *this; ++*this; return tmp;
438 value_type operator*() const {
439 const unsigned char *LocalPtr = Ptr;
440 if (!NumItemsInBucketLeft)
441 LocalPtr += 2; // number of items in bucket
442 LocalPtr += sizeof(hash_value_type); // Skip the hash.
444 // Determine the length of the key and the data.
445 const std::pair<offset_type, offset_type> &L =
446 Info::ReadKeyDataLength(LocalPtr);
449 const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
450 return InfoObj->GetExternalKey(Key);
454 key_iterator key_begin() {
455 return key_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
457 key_iterator key_end() { return key_iterator(); }
459 iterator_range<key_iterator> keys() {
460 return make_range(key_begin(), key_end());
463 /// \brief Iterates over all the entries in the table, returning the data.
464 class data_iterator {
465 const unsigned char *Ptr;
466 offset_type NumItemsInBucketLeft;
467 offset_type NumEntriesLeft;
471 typedef data_type value_type;
473 data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
475 : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
478 : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
481 bool operator==(const data_iterator &X) const {
482 return X.NumEntriesLeft == NumEntriesLeft;
484 bool operator!=(const data_iterator &X) const {
485 return X.NumEntriesLeft != NumEntriesLeft;
488 data_iterator &operator++() { // Preincrement
489 using namespace llvm::support;
490 if (!NumItemsInBucketLeft) {
491 // 'Items' starts with a 16-bit unsigned integer representing the
492 // number of items in this bucket.
493 NumItemsInBucketLeft =
494 endian::readNext<uint16_t, little, unaligned>(Ptr);
496 Ptr += sizeof(hash_value_type); // Skip the hash.
497 // Determine the length of the key and the data.
498 const std::pair<offset_type, offset_type> &L =
499 Info::ReadKeyDataLength(Ptr);
500 Ptr += L.first + L.second;
501 assert(NumItemsInBucketLeft);
502 --NumItemsInBucketLeft;
503 assert(NumEntriesLeft);
507 data_iterator operator++(int) { // Postincrement
508 data_iterator tmp = *this; ++*this; return tmp;
511 value_type operator*() const {
512 const unsigned char *LocalPtr = Ptr;
513 if (!NumItemsInBucketLeft)
514 LocalPtr += 2; // number of items in bucket
515 LocalPtr += sizeof(hash_value_type); // Skip the hash.
517 // Determine the length of the key and the data.
518 const std::pair<offset_type, offset_type> &L =
519 Info::ReadKeyDataLength(LocalPtr);
522 const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
523 return InfoObj->ReadData(Key, LocalPtr + L.first, L.second);
527 data_iterator data_begin() {
528 return data_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
530 data_iterator data_end() { return data_iterator(); }
532 iterator_range<data_iterator> data() {
533 return make_range(data_begin(), data_end());
536 /// \brief Create the hash table.
538 /// \param Buckets is the beginning of the hash table itself, which follows
539 /// the payload of entire structure. This is the value returned by
540 /// OnDiskHashTableGenerator::Emit.
542 /// \param Payload is the beginning of the data contained in the table. This
543 /// is Base plus any padding or header data that was stored, ie, the offset
544 /// that the stream was at when calling Emit.
546 /// \param Base is the point from which all offsets into the structure are
547 /// based. This is offset 0 in the stream that was used when Emitting the
549 static OnDiskIterableChainedHashTable *
550 Create(const unsigned char *Buckets, const unsigned char *const Payload,
551 const unsigned char *const Base, const Info &InfoObj = Info()) {
552 using namespace llvm::support;
553 assert(Buckets > Base);
554 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
555 "buckets should be 4-byte aligned.");
557 offset_type NumBuckets =
558 endian::readNext<offset_type, little, aligned>(Buckets);
559 offset_type NumEntries =
560 endian::readNext<offset_type, little, aligned>(Buckets);
561 return new OnDiskIterableChainedHashTable<Info>(
562 NumBuckets, NumEntries, Buckets, Payload, Base, InfoObj);
566 } // end namespace llvm