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.
90 /// \brief Insert an item into the appropriate hash bucket.
91 void insert(Bucket *Buckets, size_t Size, Item *E) {
92 Bucket &B = Buckets[E->Hash & (Size - 1)];
98 /// \brief Resize the hash table, moving the old entries into the new buckets.
99 void resize(size_t NewSize) {
100 Bucket *NewBuckets = (Bucket *)std::calloc(NewSize, sizeof(Bucket));
101 // Populate NewBuckets with the old entries.
102 for (size_t I = 0; I < NumBuckets; ++I)
103 for (Item *E = Buckets[I].Head; E;) {
106 insert(NewBuckets, NewSize, E);
111 NumBuckets = NewSize;
112 Buckets = NewBuckets;
116 /// \brief Insert an entry into the table.
117 void insert(typename Info::key_type_ref Key,
118 typename Info::data_type_ref Data) {
120 insert(Key, Data, InfoObj);
123 /// \brief Insert an entry into the table.
125 /// Uses the provided Info instead of a stack allocated one.
126 void insert(typename Info::key_type_ref Key,
127 typename Info::data_type_ref Data, Info &InfoObj) {
130 if (4 * NumEntries >= 3 * NumBuckets)
131 resize(NumBuckets * 2);
132 insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
135 /// \brief Emit the table to Out, which must not be at offset 0.
136 offset_type Emit(raw_ostream &Out) {
138 return Emit(Out, InfoObj);
141 /// \brief Emit the table to Out, which must not be at offset 0.
143 /// Uses the provided Info instead of a stack allocated one.
144 offset_type Emit(raw_ostream &Out, Info &InfoObj) {
145 using namespace llvm::support;
146 endian::Writer<little> LE(Out);
148 // Emit the payload of the table.
149 for (offset_type I = 0; I < NumBuckets; ++I) {
150 Bucket &B = Buckets[I];
154 // Store the offset for the data of this bucket.
156 assert(B.Off && "Cannot write a bucket at offset 0. Please add padding.");
158 // Write out the number of items in the bucket.
159 LE.write<uint16_t>(B.Length);
160 assert(B.Length != 0 && "Bucket has a head but zero length?");
162 // Write out the entries in the bucket.
163 for (Item *I = B.Head; I; I = I->Next) {
164 LE.write<typename Info::hash_value_type>(I->Hash);
165 const std::pair<offset_type, offset_type> &Len =
166 InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
167 InfoObj.EmitKey(Out, I->Key, Len.first);
168 InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
172 // Pad with zeros so that we can start the hashtable at an aligned address.
173 offset_type TableOff = Out.tell();
174 uint64_t N = llvm::OffsetToAlignment(TableOff, alignOf<offset_type>());
177 LE.write<uint8_t>(0);
179 // Emit the hashtable itself.
180 LE.write<offset_type>(NumBuckets);
181 LE.write<offset_type>(NumEntries);
182 for (offset_type I = 0; I < NumBuckets; ++I)
183 LE.write<offset_type>(Buckets[I].Off);
188 OnDiskChainedHashTableGenerator() {
191 // Note that we do not need to run the constructors of the individual
192 // Bucket objects since 'calloc' returns bytes that are all 0.
193 Buckets = (Bucket *)std::calloc(NumBuckets, sizeof(Bucket));
196 ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
199 /// \brief Provides lookup on an on disk hash table.
201 /// This needs an \c Info that handles reading values from the hash table's
202 /// payload and computes the hash for a given key. This should provide the
203 /// following interface:
206 /// class ExampleLookupInfo {
208 /// typedef ExampleData data_type;
209 /// typedef ExampleInternalKey internal_key_type; // The stored key type.
210 /// typedef ExampleKey external_key_type; // The type to pass to find().
211 /// typedef uint32_t hash_value_type; // The type the hash function returns.
212 /// typedef uint32_t offset_type; // The type for offsets into the table.
214 /// /// Compare two keys for equality.
215 /// static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
216 /// /// Calculate the hash for the given key.
217 /// static hash_value_type ComputeHash(internal_key_type &IKey);
218 /// /// Translate from the semantic type of a key in the hash table to the
219 /// /// type that is actually stored and used for hashing and comparisons.
220 /// /// The internal and external types are often the same, in which case this
221 /// /// can simply return the passed in value.
222 /// static const internal_key_type &GetInternalKey(external_key_type &EKey);
223 /// /// Read the key and data length from Buffer, leaving it pointing at the
224 /// /// following byte.
225 /// static std::pair<offset_type, offset_type>
226 /// ReadKeyDataLength(const unsigned char *&Buffer);
227 /// /// Read the key from Buffer, given the KeyLen as reported from
228 /// /// ReadKeyDataLength.
229 /// const internal_key_type &ReadKey(const unsigned char *Buffer,
230 /// offset_type KeyLen);
231 /// /// Read the data for Key from Buffer, given the DataLen as reported from
232 /// /// ReadKeyDataLength.
233 /// data_type ReadData(StringRef Key, const unsigned char *Buffer,
234 /// offset_type DataLen);
237 template <typename Info> class OnDiskChainedHashTable {
238 const typename Info::offset_type NumBuckets;
239 const typename Info::offset_type NumEntries;
240 const unsigned char *const Buckets;
241 const unsigned char *const Base;
245 typedef typename Info::internal_key_type internal_key_type;
246 typedef typename Info::external_key_type external_key_type;
247 typedef typename Info::data_type data_type;
248 typedef typename Info::hash_value_type hash_value_type;
249 typedef typename Info::offset_type offset_type;
251 OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
252 const unsigned char *Buckets,
253 const unsigned char *Base,
254 const Info &InfoObj = Info())
255 : NumBuckets(NumBuckets), NumEntries(NumEntries), Buckets(Buckets),
256 Base(Base), InfoObj(InfoObj) {
257 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
258 "'buckets' must have a 4-byte alignment");
261 offset_type getNumBuckets() const { return NumBuckets; }
262 offset_type getNumEntries() const { return NumEntries; }
263 const unsigned char *getBase() const { return Base; }
264 const unsigned char *getBuckets() const { return Buckets; }
266 bool isEmpty() const { return NumEntries == 0; }
269 internal_key_type Key;
270 const unsigned char *const Data;
271 const offset_type Len;
275 iterator() : Data(nullptr), Len(0) {}
276 iterator(const internal_key_type K, const unsigned char *D, offset_type L,
278 : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
280 data_type operator*() const { return InfoObj->ReadData(Key, Data, Len); }
281 bool operator==(const iterator &X) const { return X.Data == Data; }
282 bool operator!=(const iterator &X) const { return X.Data != Data; }
285 /// \brief Look up the stored data for a particular key.
286 iterator find(const external_key_type &EKey, Info *InfoPtr = 0) {
290 using namespace llvm::support;
291 const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
292 hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
294 // Each bucket is just an offset into the hash table file.
295 offset_type Idx = KeyHash & (NumBuckets - 1);
296 const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
298 offset_type Offset = endian::readNext<offset_type, little, aligned>(Bucket);
300 return iterator(); // Empty bucket.
301 const unsigned char *Items = Base + Offset;
303 // 'Items' starts with a 16-bit unsigned integer representing the
304 // number of items in this bucket.
305 unsigned Len = endian::readNext<uint16_t, little, unaligned>(Items);
307 for (unsigned i = 0; i < Len; ++i) {
309 hash_value_type ItemHash =
310 endian::readNext<hash_value_type, little, unaligned>(Items);
312 // Determine the length of the key and the data.
313 const std::pair<offset_type, offset_type> &L =
314 Info::ReadKeyDataLength(Items);
315 offset_type ItemLen = L.first + L.second;
317 // Compare the hashes. If they are not the same, skip the entry entirely.
318 if (ItemHash != KeyHash) {
324 const internal_key_type &X =
325 InfoPtr->ReadKey((const unsigned char *const)Items, L.first);
327 // If the key doesn't match just skip reading the value.
328 if (!InfoPtr->EqualKey(X, IKey)) {
334 return iterator(X, Items + L.first, L.second, InfoPtr);
340 iterator end() const { return iterator(); }
342 Info &getInfoObj() { return InfoObj; }
344 /// \brief Create the hash table.
346 /// \param Buckets is the beginning of the hash table itself, which follows
347 /// the payload of entire structure. This is the value returned by
348 /// OnDiskHashTableGenerator::Emit.
350 /// \param Base is the point from which all offsets into the structure are
351 /// based. This is offset 0 in the stream that was used when Emitting the
353 static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
354 const unsigned char *const Base,
355 const Info &InfoObj = Info()) {
356 using namespace llvm::support;
357 assert(Buckets > Base);
358 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
359 "buckets should be 4-byte aligned.");
361 offset_type NumBuckets =
362 endian::readNext<offset_type, little, aligned>(Buckets);
363 offset_type NumEntries =
364 endian::readNext<offset_type, little, aligned>(Buckets);
365 return new OnDiskChainedHashTable<Info>(NumBuckets, NumEntries, Buckets,
370 /// \brief Provides lookup and iteration over an on disk hash table.
372 /// \copydetails llvm::OnDiskChainedHashTable
373 template <typename Info>
374 class OnDiskIterableChainedHashTable : public OnDiskChainedHashTable<Info> {
375 const unsigned char *Payload;
378 typedef OnDiskChainedHashTable<Info> base_type;
379 typedef typename base_type::internal_key_type internal_key_type;
380 typedef typename base_type::external_key_type external_key_type;
381 typedef typename base_type::data_type data_type;
382 typedef typename base_type::hash_value_type hash_value_type;
383 typedef typename base_type::offset_type offset_type;
385 OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
386 const unsigned char *Buckets,
387 const unsigned char *Payload,
388 const unsigned char *Base,
389 const Info &InfoObj = Info())
390 : base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
393 /// \brief Iterates over all of the keys in the table.
395 const unsigned char *Ptr;
396 offset_type NumItemsInBucketLeft;
397 offset_type NumEntriesLeft;
401 typedef external_key_type value_type;
403 key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
405 : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
408 : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
411 friend bool operator==(const key_iterator &X, const key_iterator &Y) {
412 return X.NumEntriesLeft == Y.NumEntriesLeft;
414 friend bool operator!=(const key_iterator &X, const key_iterator &Y) {
415 return X.NumEntriesLeft != Y.NumEntriesLeft;
418 key_iterator &operator++() { // Preincrement
419 using namespace llvm::support;
420 if (!NumItemsInBucketLeft) {
421 // 'Items' starts with a 16-bit unsigned integer representing the
422 // number of items in this bucket.
423 NumItemsInBucketLeft =
424 endian::readNext<uint16_t, little, unaligned>(Ptr);
426 Ptr += sizeof(hash_value_type); // Skip the hash.
427 // Determine the length of the key and the data.
428 const std::pair<offset_type, offset_type> &L =
429 Info::ReadKeyDataLength(Ptr);
430 Ptr += L.first + L.second;
431 assert(NumItemsInBucketLeft);
432 --NumItemsInBucketLeft;
433 assert(NumEntriesLeft);
437 key_iterator operator++(int) { // Postincrement
438 key_iterator tmp = *this; ++*this; return tmp;
441 value_type operator*() const {
442 const unsigned char *LocalPtr = Ptr;
443 if (!NumItemsInBucketLeft)
444 LocalPtr += 2; // number of items in bucket
445 LocalPtr += sizeof(hash_value_type); // Skip the hash.
447 // Determine the length of the key and the data.
448 const std::pair<offset_type, offset_type> &L =
449 Info::ReadKeyDataLength(LocalPtr);
452 const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
453 return InfoObj->GetExternalKey(Key);
457 key_iterator key_begin() {
458 return key_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
460 key_iterator key_end() { return key_iterator(); }
462 iterator_range<key_iterator> keys() {
463 return make_range(key_begin(), key_end());
466 /// \brief Iterates over all the entries in the table, returning the data.
467 class data_iterator {
468 const unsigned char *Ptr;
469 offset_type NumItemsInBucketLeft;
470 offset_type NumEntriesLeft;
474 typedef data_type value_type;
476 data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
478 : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
481 : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
484 bool operator==(const data_iterator &X) const {
485 return X.NumEntriesLeft == NumEntriesLeft;
487 bool operator!=(const data_iterator &X) const {
488 return X.NumEntriesLeft != NumEntriesLeft;
491 data_iterator &operator++() { // Preincrement
492 using namespace llvm::support;
493 if (!NumItemsInBucketLeft) {
494 // 'Items' starts with a 16-bit unsigned integer representing the
495 // number of items in this bucket.
496 NumItemsInBucketLeft =
497 endian::readNext<uint16_t, little, unaligned>(Ptr);
499 Ptr += sizeof(hash_value_type); // Skip the hash.
500 // Determine the length of the key and the data.
501 const std::pair<offset_type, offset_type> &L =
502 Info::ReadKeyDataLength(Ptr);
503 Ptr += L.first + L.second;
504 assert(NumItemsInBucketLeft);
505 --NumItemsInBucketLeft;
506 assert(NumEntriesLeft);
510 data_iterator operator++(int) { // Postincrement
511 data_iterator tmp = *this; ++*this; return tmp;
514 value_type operator*() const {
515 const unsigned char *LocalPtr = Ptr;
516 if (!NumItemsInBucketLeft)
517 LocalPtr += 2; // number of items in bucket
518 LocalPtr += sizeof(hash_value_type); // Skip the hash.
520 // Determine the length of the key and the data.
521 const std::pair<offset_type, offset_type> &L =
522 Info::ReadKeyDataLength(LocalPtr);
525 const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
526 return InfoObj->ReadData(Key, LocalPtr + L.first, L.second);
530 data_iterator data_begin() {
531 return data_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
533 data_iterator data_end() { return data_iterator(); }
535 iterator_range<data_iterator> data() {
536 return make_range(data_begin(), data_end());
539 /// \brief Create the hash table.
541 /// \param Buckets is the beginning of the hash table itself, which follows
542 /// the payload of entire structure. This is the value returned by
543 /// OnDiskHashTableGenerator::Emit.
545 /// \param Payload is the beginning of the data contained in the table. This
546 /// is Base plus any padding or header data that was stored, ie, the offset
547 /// that the stream was at when calling Emit.
549 /// \param Base is the point from which all offsets into the structure are
550 /// based. This is offset 0 in the stream that was used when Emitting the
552 static OnDiskIterableChainedHashTable *
553 Create(const unsigned char *Buckets, const unsigned char *const Payload,
554 const unsigned char *const Base, const Info &InfoObj = Info()) {
555 using namespace llvm::support;
556 assert(Buckets > Base);
557 assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
558 "buckets should be 4-byte aligned.");
560 offset_type NumBuckets =
561 endian::readNext<offset_type, little, aligned>(Buckets);
562 offset_type NumEntries =
563 endian::readNext<offset_type, little, aligned>(Buckets);
564 return new OnDiskIterableChainedHashTable<Info>(
565 NumBuckets, NumEntries, Buckets, Payload, Base, InfoObj);
569 } // end namespace llvm