]> git.sesse.net Git - plocate/blob - db.h
Switch trigram lookup from binary search to a hash table.
[plocate] / db.h
1 #ifndef DB_H
2 #define DB_H 1
3
4 #include <stdint.h>
5
6 struct Header {
7         char magic[8]; // "\0plocate";
8         uint32_t version;  // 0.
9         uint32_t hashtable_size;
10         uint32_t extra_ht_slots;
11         uint64_t hash_table_offset_bytes;
12         uint64_t filename_index_offset_bytes;
13 }; 
14
15 struct Trigram {
16         uint32_t trgm;
17         uint32_t num_docids;
18         uint64_t offset;
19
20         bool operator==(const Trigram &other) const
21         {
22                 return trgm == other.trgm;
23         }
24         bool operator<(const Trigram &other) const
25         {
26                 return trgm < other.trgm;
27         }
28 };
29
30 inline uint32_t hash_trigram(uint32_t trgm, uint32_t ht_size)
31 {
32         // CRC-like computation.
33         uint32_t crc = trgm;
34         for (int i = 0; i < 32; i++) {
35                 bool bit = crc & 0x80000000;
36                 crc <<= 1;
37                 if (bit) {
38                         crc ^= 0x1edc6f41;
39                 }
40         }
41         return crc % ht_size;
42 }
43
44 #endif  // !defined(DB_H)