OnDiskHashTable.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. //===--- OnDiskHashTable.h - On-Disk Hash Table Implementation --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Defines facilities for reading and writing on-disk hash tables.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_ONDISKHASHTABLE_H
  14. #define LLVM_SUPPORT_ONDISKHASHTABLE_H
  15. #include "llvm/Support/Alignment.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/EndianStream.h"
  19. #include "llvm/Support/Host.h"
  20. #include "llvm/Support/MathExtras.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <cassert>
  23. #include <cstdlib>
  24. namespace llvm {
  25. /// Generates an on disk hash table.
  26. ///
  27. /// This needs an \c Info that handles storing values into the hash table's
  28. /// payload and computes the hash for a given key. This should provide the
  29. /// following interface:
  30. ///
  31. /// \code
  32. /// class ExampleInfo {
  33. /// public:
  34. /// typedef ExampleKey key_type; // Must be copy constructible
  35. /// typedef ExampleKey &key_type_ref;
  36. /// typedef ExampleData data_type; // Must be copy constructible
  37. /// typedef ExampleData &data_type_ref;
  38. /// typedef uint32_t hash_value_type; // The type the hash function returns.
  39. /// typedef uint32_t offset_type; // The type for offsets into the table.
  40. ///
  41. /// /// Calculate the hash for Key
  42. /// static hash_value_type ComputeHash(key_type_ref Key);
  43. /// /// Return the lengths, in bytes, of the given Key/Data pair.
  44. /// static std::pair<offset_type, offset_type>
  45. /// EmitKeyDataLength(raw_ostream &Out, key_type_ref Key, data_type_ref Data);
  46. /// /// Write Key to Out. KeyLen is the length from EmitKeyDataLength.
  47. /// static void EmitKey(raw_ostream &Out, key_type_ref Key,
  48. /// offset_type KeyLen);
  49. /// /// Write Data to Out. DataLen is the length from EmitKeyDataLength.
  50. /// static void EmitData(raw_ostream &Out, key_type_ref Key,
  51. /// data_type_ref Data, offset_type DataLen);
  52. /// /// Determine if two keys are equal. Optional, only needed by contains.
  53. /// static bool EqualKey(key_type_ref Key1, key_type_ref Key2);
  54. /// };
  55. /// \endcode
  56. template <typename Info> class OnDiskChainedHashTableGenerator {
  57. /// A single item in the hash table.
  58. class Item {
  59. public:
  60. typename Info::key_type Key;
  61. typename Info::data_type Data;
  62. Item *Next;
  63. const typename Info::hash_value_type Hash;
  64. Item(typename Info::key_type_ref Key, typename Info::data_type_ref Data,
  65. Info &InfoObj)
  66. : Key(Key), Data(Data), Next(nullptr), Hash(InfoObj.ComputeHash(Key)) {}
  67. };
  68. typedef typename Info::offset_type offset_type;
  69. offset_type NumBuckets;
  70. offset_type NumEntries;
  71. llvm::SpecificBumpPtrAllocator<Item> BA;
  72. /// A linked list of values in a particular hash bucket.
  73. struct Bucket {
  74. offset_type Off;
  75. unsigned Length;
  76. Item *Head;
  77. };
  78. Bucket *Buckets;
  79. private:
  80. /// Insert an item into the appropriate hash bucket.
  81. void insert(Bucket *Buckets, size_t Size, Item *E) {
  82. Bucket &B = Buckets[E->Hash & (Size - 1)];
  83. E->Next = B.Head;
  84. ++B.Length;
  85. B.Head = E;
  86. }
  87. /// Resize the hash table, moving the old entries into the new buckets.
  88. void resize(size_t NewSize) {
  89. Bucket *NewBuckets = static_cast<Bucket *>(
  90. safe_calloc(NewSize, sizeof(Bucket)));
  91. // Populate NewBuckets with the old entries.
  92. for (size_t I = 0; I < NumBuckets; ++I)
  93. for (Item *E = Buckets[I].Head; E;) {
  94. Item *N = E->Next;
  95. E->Next = nullptr;
  96. insert(NewBuckets, NewSize, E);
  97. E = N;
  98. }
  99. free(Buckets);
  100. NumBuckets = NewSize;
  101. Buckets = NewBuckets;
  102. }
  103. public:
  104. /// Insert an entry into the table.
  105. void insert(typename Info::key_type_ref Key,
  106. typename Info::data_type_ref Data) {
  107. Info InfoObj;
  108. insert(Key, Data, InfoObj);
  109. }
  110. /// Insert an entry into the table.
  111. ///
  112. /// Uses the provided Info instead of a stack allocated one.
  113. void insert(typename Info::key_type_ref Key,
  114. typename Info::data_type_ref Data, Info &InfoObj) {
  115. ++NumEntries;
  116. if (4 * NumEntries >= 3 * NumBuckets)
  117. resize(NumBuckets * 2);
  118. insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
  119. }
  120. /// Determine whether an entry has been inserted.
  121. bool contains(typename Info::key_type_ref Key, Info &InfoObj) {
  122. unsigned Hash = InfoObj.ComputeHash(Key);
  123. for (Item *I = Buckets[Hash & (NumBuckets - 1)].Head; I; I = I->Next)
  124. if (I->Hash == Hash && InfoObj.EqualKey(I->Key, Key))
  125. return true;
  126. return false;
  127. }
  128. /// Emit the table to Out, which must not be at offset 0.
  129. offset_type Emit(raw_ostream &Out) {
  130. Info InfoObj;
  131. return Emit(Out, InfoObj);
  132. }
  133. /// Emit the table to Out, which must not be at offset 0.
  134. ///
  135. /// Uses the provided Info instead of a stack allocated one.
  136. offset_type Emit(raw_ostream &Out, Info &InfoObj) {
  137. using namespace llvm::support;
  138. endian::Writer LE(Out, little);
  139. // Now we're done adding entries, resize the bucket list if it's
  140. // significantly too large. (This only happens if the number of
  141. // entries is small and we're within our initial allocation of
  142. // 64 buckets.) We aim for an occupancy ratio in [3/8, 3/4).
  143. //
  144. // As a special case, if there are two or fewer entries, just
  145. // form a single bucket. A linear scan is fine in that case, and
  146. // this is very common in C++ class lookup tables. This also
  147. // guarantees we produce at least one bucket for an empty table.
  148. //
  149. // FIXME: Try computing a perfect hash function at this point.
  150. unsigned TargetNumBuckets =
  151. NumEntries <= 2 ? 1 : NextPowerOf2(NumEntries * 4 / 3);
  152. if (TargetNumBuckets != NumBuckets)
  153. resize(TargetNumBuckets);
  154. // Emit the payload of the table.
  155. for (offset_type I = 0; I < NumBuckets; ++I) {
  156. Bucket &B = Buckets[I];
  157. if (!B.Head)
  158. continue;
  159. // Store the offset for the data of this bucket.
  160. B.Off = Out.tell();
  161. assert(B.Off && "Cannot write a bucket at offset 0. Please add padding.");
  162. // Write out the number of items in the bucket.
  163. LE.write<uint16_t>(B.Length);
  164. assert(B.Length != 0 && "Bucket has a head but zero length?");
  165. // Write out the entries in the bucket.
  166. for (Item *I = B.Head; I; I = I->Next) {
  167. LE.write<typename Info::hash_value_type>(I->Hash);
  168. const std::pair<offset_type, offset_type> &Len =
  169. InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
  170. #ifdef NDEBUG
  171. InfoObj.EmitKey(Out, I->Key, Len.first);
  172. InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
  173. #else
  174. // In asserts mode, check that the users length matches the data they
  175. // wrote.
  176. uint64_t KeyStart = Out.tell();
  177. InfoObj.EmitKey(Out, I->Key, Len.first);
  178. uint64_t DataStart = Out.tell();
  179. InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
  180. uint64_t End = Out.tell();
  181. assert(offset_type(DataStart - KeyStart) == Len.first &&
  182. "key length does not match bytes written");
  183. assert(offset_type(End - DataStart) == Len.second &&
  184. "data length does not match bytes written");
  185. #endif
  186. }
  187. }
  188. // Pad with zeros so that we can start the hashtable at an aligned address.
  189. offset_type TableOff = Out.tell();
  190. uint64_t N = offsetToAlignment(TableOff, Align(alignof(offset_type)));
  191. TableOff += N;
  192. while (N--)
  193. LE.write<uint8_t>(0);
  194. // Emit the hashtable itself.
  195. LE.write<offset_type>(NumBuckets);
  196. LE.write<offset_type>(NumEntries);
  197. for (offset_type I = 0; I < NumBuckets; ++I)
  198. LE.write<offset_type>(Buckets[I].Off);
  199. return TableOff;
  200. }
  201. OnDiskChainedHashTableGenerator() {
  202. NumEntries = 0;
  203. NumBuckets = 64;
  204. // Note that we do not need to run the constructors of the individual
  205. // Bucket objects since 'calloc' returns bytes that are all 0.
  206. Buckets = static_cast<Bucket *>(safe_calloc(NumBuckets, sizeof(Bucket)));
  207. }
  208. ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
  209. };
  210. /// Provides lookup on an on disk hash table.
  211. ///
  212. /// This needs an \c Info that handles reading values from the hash table's
  213. /// payload and computes the hash for a given key. This should provide the
  214. /// following interface:
  215. ///
  216. /// \code
  217. /// class ExampleLookupInfo {
  218. /// public:
  219. /// typedef ExampleData data_type;
  220. /// typedef ExampleInternalKey internal_key_type; // The stored key type.
  221. /// typedef ExampleKey external_key_type; // The type to pass to find().
  222. /// typedef uint32_t hash_value_type; // The type the hash function returns.
  223. /// typedef uint32_t offset_type; // The type for offsets into the table.
  224. ///
  225. /// /// Compare two keys for equality.
  226. /// static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
  227. /// /// Calculate the hash for the given key.
  228. /// static hash_value_type ComputeHash(internal_key_type &IKey);
  229. /// /// Translate from the semantic type of a key in the hash table to the
  230. /// /// type that is actually stored and used for hashing and comparisons.
  231. /// /// The internal and external types are often the same, in which case this
  232. /// /// can simply return the passed in value.
  233. /// static const internal_key_type &GetInternalKey(external_key_type &EKey);
  234. /// /// Read the key and data length from Buffer, leaving it pointing at the
  235. /// /// following byte.
  236. /// static std::pair<offset_type, offset_type>
  237. /// ReadKeyDataLength(const unsigned char *&Buffer);
  238. /// /// Read the key from Buffer, given the KeyLen as reported from
  239. /// /// ReadKeyDataLength.
  240. /// const internal_key_type &ReadKey(const unsigned char *Buffer,
  241. /// offset_type KeyLen);
  242. /// /// Read the data for Key from Buffer, given the DataLen as reported from
  243. /// /// ReadKeyDataLength.
  244. /// data_type ReadData(StringRef Key, const unsigned char *Buffer,
  245. /// offset_type DataLen);
  246. /// };
  247. /// \endcode
  248. template <typename Info> class OnDiskChainedHashTable {
  249. const typename Info::offset_type NumBuckets;
  250. const typename Info::offset_type NumEntries;
  251. const unsigned char *const Buckets;
  252. const unsigned char *const Base;
  253. Info InfoObj;
  254. public:
  255. typedef Info InfoType;
  256. typedef typename Info::internal_key_type internal_key_type;
  257. typedef typename Info::external_key_type external_key_type;
  258. typedef typename Info::data_type data_type;
  259. typedef typename Info::hash_value_type hash_value_type;
  260. typedef typename Info::offset_type offset_type;
  261. OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
  262. const unsigned char *Buckets,
  263. const unsigned char *Base,
  264. const Info &InfoObj = Info())
  265. : NumBuckets(NumBuckets), NumEntries(NumEntries), Buckets(Buckets),
  266. Base(Base), InfoObj(InfoObj) {
  267. assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
  268. "'buckets' must have a 4-byte alignment");
  269. }
  270. /// Read the number of buckets and the number of entries from a hash table
  271. /// produced by OnDiskHashTableGenerator::Emit, and advance the Buckets
  272. /// pointer past them.
  273. static std::pair<offset_type, offset_type>
  274. readNumBucketsAndEntries(const unsigned char *&Buckets) {
  275. assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
  276. "buckets should be 4-byte aligned.");
  277. using namespace llvm::support;
  278. offset_type NumBuckets =
  279. endian::readNext<offset_type, little, aligned>(Buckets);
  280. offset_type NumEntries =
  281. endian::readNext<offset_type, little, aligned>(Buckets);
  282. return std::make_pair(NumBuckets, NumEntries);
  283. }
  284. offset_type getNumBuckets() const { return NumBuckets; }
  285. offset_type getNumEntries() const { return NumEntries; }
  286. const unsigned char *getBase() const { return Base; }
  287. const unsigned char *getBuckets() const { return Buckets; }
  288. bool isEmpty() const { return NumEntries == 0; }
  289. class iterator {
  290. internal_key_type Key;
  291. const unsigned char *const Data;
  292. const offset_type Len;
  293. Info *InfoObj;
  294. public:
  295. iterator() : Key(), Data(nullptr), Len(0), InfoObj(nullptr) {}
  296. iterator(const internal_key_type K, const unsigned char *D, offset_type L,
  297. Info *InfoObj)
  298. : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
  299. data_type operator*() const { return InfoObj->ReadData(Key, Data, Len); }
  300. const unsigned char *getDataPtr() const { return Data; }
  301. offset_type getDataLen() const { return Len; }
  302. bool operator==(const iterator &X) const { return X.Data == Data; }
  303. bool operator!=(const iterator &X) const { return X.Data != Data; }
  304. };
  305. /// Look up the stored data for a particular key.
  306. iterator find(const external_key_type &EKey, Info *InfoPtr = nullptr) {
  307. const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
  308. hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
  309. return find_hashed(IKey, KeyHash, InfoPtr);
  310. }
  311. /// Look up the stored data for a particular key with a known hash.
  312. iterator find_hashed(const internal_key_type &IKey, hash_value_type KeyHash,
  313. Info *InfoPtr = nullptr) {
  314. using namespace llvm::support;
  315. if (!InfoPtr)
  316. InfoPtr = &InfoObj;
  317. // Each bucket is just an offset into the hash table file.
  318. offset_type Idx = KeyHash & (NumBuckets - 1);
  319. const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
  320. offset_type Offset = endian::readNext<offset_type, little, aligned>(Bucket);
  321. if (Offset == 0)
  322. return iterator(); // Empty bucket.
  323. const unsigned char *Items = Base + Offset;
  324. // 'Items' starts with a 16-bit unsigned integer representing the
  325. // number of items in this bucket.
  326. unsigned Len = endian::readNext<uint16_t, little, unaligned>(Items);
  327. for (unsigned i = 0; i < Len; ++i) {
  328. // Read the hash.
  329. hash_value_type ItemHash =
  330. endian::readNext<hash_value_type, little, unaligned>(Items);
  331. // Determine the length of the key and the data.
  332. const std::pair<offset_type, offset_type> &L =
  333. Info::ReadKeyDataLength(Items);
  334. offset_type ItemLen = L.first + L.second;
  335. // Compare the hashes. If they are not the same, skip the entry entirely.
  336. if (ItemHash != KeyHash) {
  337. Items += ItemLen;
  338. continue;
  339. }
  340. // Read the key.
  341. const internal_key_type &X =
  342. InfoPtr->ReadKey((const unsigned char *const)Items, L.first);
  343. // If the key doesn't match just skip reading the value.
  344. if (!InfoPtr->EqualKey(X, IKey)) {
  345. Items += ItemLen;
  346. continue;
  347. }
  348. // The key matches!
  349. return iterator(X, Items + L.first, L.second, InfoPtr);
  350. }
  351. return iterator();
  352. }
  353. iterator end() const { return iterator(); }
  354. Info &getInfoObj() { return InfoObj; }
  355. /// Create the hash table.
  356. ///
  357. /// \param Buckets is the beginning of the hash table itself, which follows
  358. /// the payload of entire structure. This is the value returned by
  359. /// OnDiskHashTableGenerator::Emit.
  360. ///
  361. /// \param Base is the point from which all offsets into the structure are
  362. /// based. This is offset 0 in the stream that was used when Emitting the
  363. /// table.
  364. static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
  365. const unsigned char *const Base,
  366. const Info &InfoObj = Info()) {
  367. assert(Buckets > Base);
  368. auto NumBucketsAndEntries = readNumBucketsAndEntries(Buckets);
  369. return new OnDiskChainedHashTable<Info>(NumBucketsAndEntries.first,
  370. NumBucketsAndEntries.second,
  371. Buckets, Base, InfoObj);
  372. }
  373. };
  374. /// Provides lookup and iteration over an on disk hash table.
  375. ///
  376. /// \copydetails llvm::OnDiskChainedHashTable
  377. template <typename Info>
  378. class OnDiskIterableChainedHashTable : public OnDiskChainedHashTable<Info> {
  379. const unsigned char *Payload;
  380. public:
  381. typedef OnDiskChainedHashTable<Info> base_type;
  382. typedef typename base_type::internal_key_type internal_key_type;
  383. typedef typename base_type::external_key_type external_key_type;
  384. typedef typename base_type::data_type data_type;
  385. typedef typename base_type::hash_value_type hash_value_type;
  386. typedef typename base_type::offset_type offset_type;
  387. private:
  388. /// Iterates over all of the keys in the table.
  389. class iterator_base {
  390. const unsigned char *Ptr;
  391. offset_type NumItemsInBucketLeft;
  392. offset_type NumEntriesLeft;
  393. public:
  394. typedef external_key_type value_type;
  395. iterator_base(const unsigned char *const Ptr, offset_type NumEntries)
  396. : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries) {}
  397. iterator_base()
  398. : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0) {}
  399. friend bool operator==(const iterator_base &X, const iterator_base &Y) {
  400. return X.NumEntriesLeft == Y.NumEntriesLeft;
  401. }
  402. friend bool operator!=(const iterator_base &X, const iterator_base &Y) {
  403. return X.NumEntriesLeft != Y.NumEntriesLeft;
  404. }
  405. /// Move to the next item.
  406. void advance() {
  407. using namespace llvm::support;
  408. if (!NumItemsInBucketLeft) {
  409. // 'Items' starts with a 16-bit unsigned integer representing the
  410. // number of items in this bucket.
  411. NumItemsInBucketLeft =
  412. endian::readNext<uint16_t, little, unaligned>(Ptr);
  413. }
  414. Ptr += sizeof(hash_value_type); // Skip the hash.
  415. // Determine the length of the key and the data.
  416. const std::pair<offset_type, offset_type> &L =
  417. Info::ReadKeyDataLength(Ptr);
  418. Ptr += L.first + L.second;
  419. assert(NumItemsInBucketLeft);
  420. --NumItemsInBucketLeft;
  421. assert(NumEntriesLeft);
  422. --NumEntriesLeft;
  423. }
  424. /// Get the start of the item as written by the trait (after the hash and
  425. /// immediately before the key and value length).
  426. const unsigned char *getItem() const {
  427. return Ptr + (NumItemsInBucketLeft ? 0 : 2) + sizeof(hash_value_type);
  428. }
  429. };
  430. public:
  431. OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
  432. const unsigned char *Buckets,
  433. const unsigned char *Payload,
  434. const unsigned char *Base,
  435. const Info &InfoObj = Info())
  436. : base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
  437. Payload(Payload) {}
  438. /// Iterates over all of the keys in the table.
  439. class key_iterator : public iterator_base {
  440. Info *InfoObj;
  441. public:
  442. typedef external_key_type value_type;
  443. key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
  444. Info *InfoObj)
  445. : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
  446. key_iterator() : iterator_base(), InfoObj() {}
  447. key_iterator &operator++() {
  448. this->advance();
  449. return *this;
  450. }
  451. key_iterator operator++(int) { // Postincrement
  452. key_iterator tmp = *this;
  453. ++*this;
  454. return tmp;
  455. }
  456. internal_key_type getInternalKey() const {
  457. auto *LocalPtr = this->getItem();
  458. // Determine the length of the key and the data.
  459. auto L = Info::ReadKeyDataLength(LocalPtr);
  460. // Read the key.
  461. return InfoObj->ReadKey(LocalPtr, L.first);
  462. }
  463. value_type operator*() const {
  464. return InfoObj->GetExternalKey(getInternalKey());
  465. }
  466. };
  467. key_iterator key_begin() {
  468. return key_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
  469. }
  470. key_iterator key_end() { return key_iterator(); }
  471. iterator_range<key_iterator> keys() {
  472. return make_range(key_begin(), key_end());
  473. }
  474. /// Iterates over all the entries in the table, returning the data.
  475. class data_iterator : public iterator_base {
  476. Info *InfoObj;
  477. public:
  478. typedef data_type value_type;
  479. data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
  480. Info *InfoObj)
  481. : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
  482. data_iterator() : iterator_base(), InfoObj() {}
  483. data_iterator &operator++() { // Preincrement
  484. this->advance();
  485. return *this;
  486. }
  487. data_iterator operator++(int) { // Postincrement
  488. data_iterator tmp = *this;
  489. ++*this;
  490. return tmp;
  491. }
  492. value_type operator*() const {
  493. auto *LocalPtr = this->getItem();
  494. // Determine the length of the key and the data.
  495. auto L = Info::ReadKeyDataLength(LocalPtr);
  496. // Read the key.
  497. const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
  498. return InfoObj->ReadData(Key, LocalPtr + L.first, L.second);
  499. }
  500. };
  501. data_iterator data_begin() {
  502. return data_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
  503. }
  504. data_iterator data_end() { return data_iterator(); }
  505. iterator_range<data_iterator> data() {
  506. return make_range(data_begin(), data_end());
  507. }
  508. /// Create the hash table.
  509. ///
  510. /// \param Buckets is the beginning of the hash table itself, which follows
  511. /// the payload of entire structure. This is the value returned by
  512. /// OnDiskHashTableGenerator::Emit.
  513. ///
  514. /// \param Payload is the beginning of the data contained in the table. This
  515. /// is Base plus any padding or header data that was stored, ie, the offset
  516. /// that the stream was at when calling Emit.
  517. ///
  518. /// \param Base is the point from which all offsets into the structure are
  519. /// based. This is offset 0 in the stream that was used when Emitting the
  520. /// table.
  521. static OnDiskIterableChainedHashTable *
  522. Create(const unsigned char *Buckets, const unsigned char *const Payload,
  523. const unsigned char *const Base, const Info &InfoObj = Info()) {
  524. assert(Buckets > Base);
  525. auto NumBucketsAndEntries =
  526. OnDiskIterableChainedHashTable<Info>::readNumBucketsAndEntries(Buckets);
  527. return new OnDiskIterableChainedHashTable<Info>(
  528. NumBucketsAndEntries.first, NumBucketsAndEntries.second,
  529. Buckets, Payload, Base, InfoObj);
  530. }
  531. };
  532. } // end namespace llvm
  533. #endif