initial try

This commit is contained in:
2024-04-29 16:01:50 +00:00
parent 80f4fa4529
commit 0e9a7b2372
2 changed files with 109 additions and 36 deletions

View File

@ -777,7 +777,7 @@ TEST(RemoveTest, RM_1) {
TEST(RemoveTest, RM_2) {
const unsigned int RndSeed = testing::GTEST_FLAG(random_seed);
std::mt19937 rnd(RndSeed);
std::mt19937 rnd(1);
const int str_len = 800;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
@ -785,8 +785,8 @@ TEST(RemoveTest, RM_2) {
const std::string db_file_name = "/tmp/bpt16.db";
remove(db_file_name.c_str());
std::vector<std::pair<KeyType, int>> entries;
const int max_keys = 25;
const int keys_num_to_remove = 20;
const int max_keys = 700;
const int keys_num_to_remove = 699;
for (int i = 1; i <= max_keys; i++) {
KeyType key;
for (size_t j = 0; j < str_len; j++) key.data[j] = 'a' + rnd() % 26;
@ -864,34 +864,31 @@ TEST(RemoveTest, RM_2) {
}
delete bpm;
delete dm;
// dm = new DiskManager(db_file_name.c_str());
// bpm = new BufferPoolManager(20, 3, dm);
// {
// BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
// ASSERT_EQ(bpt.Size(), entries.size());
// for (int i = 0; i < entries.size(); i++) {
// ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
// }
// sort(entries.begin(), entries.end());
// for (int i = 0; i < entries.size(); i++) {
// ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
// }
// auto it_std = entries.begin();
// auto it_bpt_tmp = bpt.lower_bound_const(entries[6].first);
// ASSERT_EQ(it_bpt_tmp.GetValue(), entries[6].second);
// ASSERT_EQ(it_bpt_tmp.GetKey(), entries[6].first);
// auto it_bpt = bpt.lower_bound_const(entries[0].first);
// for (int i = 0; i < entries.size(); i++) {
// fprintf(stderr, "i=%d checking key[%d]=%s value[%d]=%d\n", i, i, it_std->first.data, i, it_std->second);
// ASSERT_TRUE(!(it_bpt == bpt.end_const()));
// ASSERT_EQ(it_bpt.GetKey(), it_std->first);
// ASSERT_EQ(it_bpt.GetValue(), it_std->second);
// ++it_bpt;
// it_std++;
// }
// ASSERT_TRUE(it_bpt == bpt.end_const());
// ASSERT_EQ(bpt.Size(), entries.size());
// }
// delete bpm;
// delete dm;
dm = new DiskManager(db_file_name.c_str());
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), entries.size());
for (int i = 0; i < entries.size(); i++) {
ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
}
sort(entries.begin(), entries.end());
for (int i = 0; i < entries.size(); i++) {
ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
}
auto it_std = entries.begin();
auto it_bpt = bpt.lower_bound_const(entries[0].first);
for (int i = 0; i < entries.size(); i++) {
fprintf(stderr, "i=%d checking key[%d]=%s value[%d]=%d\n", i, i, it_std->first.data, i, it_std->second);
ASSERT_TRUE(!(it_bpt == bpt.end_const()));
ASSERT_EQ(it_bpt.GetKey(), it_std->first);
ASSERT_EQ(it_bpt.GetValue(), it_std->second);
++it_bpt;
it_std++;
}
ASSERT_TRUE(it_bpt == bpt.end_const());
ASSERT_EQ(bpt.Size(), entries.size());
}
delete bpm;
delete dm;
}

View File

@ -1,5 +1,81 @@
#include "bpt/disk_manager.h"
int main()
{
#include <cstdint>
#include <cstdio>
#include <ios>
#include <iostream>
#include <string>
#include "bpt/bpt.hpp"
#include "bpt/buffer_pool_manager.h"
typedef uint64_t hash_t;
inline hash_t Hash(std::string str) noexcept {
constexpr static char salt1[10] = "mL;]-=eT";
constexpr static char salt2[10] = "9B<mF_me";
constexpr static char inner_salt[17] = "si9aW@zl#2$3%4^!";
/* Reference: http://xorshift.di.unimi.it/splitmix64.c */
str = salt1 + str + salt2;
hash_t ret = 0;
int i = 0;
for (; i + 8 <= str.length(); i += 8) {
ret ^= *reinterpret_cast<const hash_t *>(str.c_str() + i);
ret ^= *reinterpret_cast<const hash_t *>(inner_salt + (i & 15));
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
for (; i < str.length(); ++i) {
ret ^= str[i];
ret ^= inner_salt[i & 15];
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
return ret;
}
typedef std::pair<hash_t, int> ValType;
const int kIntMin = std::numeric_limits<int>::min();
int main() {
DiskManager *disk_manager = new DiskManager("data.db");
BufferPoolManager *buffer_pool_manager = new BufferPoolManager(100, 10, disk_manager);
{
BPlusTreeIndexer<ValType, std::less<ValType>> bpt(buffer_pool_manager);
int n;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::string op, index;
int val;
std::cin >> n;
while (n-- > 0) {
std::cin >> op;
if (op[0] == 'i') {
std::cin >> index >> val;
hash_t hsh = Hash(index);
bpt.Put({hsh, val}, 1);
} else if (op[0] == 'd') {
std::cin >> index >> val;
hash_t hsh = Hash(index);
bpt.Remove({hsh, val});
} else if (op[0] == 'f') {
std::cin >> index;
hash_t hsh = Hash(index);
ValType marker = {hsh, kIntMin};
auto it = bpt.lower_bound_const(marker);
bool has_value = false;
while (true) {
if (it == bpt.end_const()) break;
if (it.GetKey().first != hsh) break;
has_value = true;
std::cout << it.GetKey().second << ' ';
++it;
}
if (!has_value) std::cout << "null";
std::cout << '\n';
} else
throw std::runtime_error("Invalid operation");
}
}
delete buffer_pool_manager;
delete disk_manager;
return 0;
}