diff --git a/backend/include/key2index.hpp b/backend/include/key2index.hpp index 50927a2..322a66f 100644 --- a/backend/include/key2index.hpp +++ b/backend/include/key2index.hpp @@ -10,15 +10,26 @@ class String2Index { private: static const int kBucketSize = 262142; static const int kPageSize = 4096; + constexpr static char salt1[10] = "mL;]-=eT"; + constexpr static char salt2[10] = "9B= 1, "kNodesPerBlock error"); - static_assert(sizeof(Block) == kPageSize - 4, "Block Size error"); + static_assert(sizeof(Block) <= kPageSize - 4, "Block Size error"); DriveArray mem; int *hash_table = nullptr; std::string file_name; - inline size_t Hash(std::string str) noexcept { - const static std::string salt1 = "mL;]-=eT"; - const static std::string salt2 = "9Btot; ++i) { - if (blk_ptr->data[i].str == str && blk_ptr->data[i].val == val) { + if (blk_ptr->data[i].main_hash == str_main_hash && + blk_ptr->data[i].sub_hash == str_sub_hash && + blk_ptr->data[i].val == val) { int headidx = hash_table[hash_val % kBucketSize]; if (headidx == idx) { blk_ptr->data[i] = blk_ptr->data[--blk_ptr->tot]; @@ -137,10 +144,13 @@ class String2Index { size_t hash_val = Hash(str); int idx = hash_table[hash_val % kBucketSize]; Block *blk_ptr = new Block; + size_t str_main_hash = Hash(str), + str_sub_hash = Hash(sub_salt1 + str + sub_salt2); while (idx != 0) { mem.read(*blk_ptr, idx); for (int i = 0; i < blk_ptr->tot; ++i) { - if (blk_ptr->data[i].str == str) { + if (blk_ptr->data[i].main_hash == str_main_hash && + blk_ptr->data[i].sub_hash == str_sub_hash) { ret.push_back(blk_ptr->data[i].val); } }