finish writing remove, ready to deeeeeeeeeeeeeeeeeeeeeeeeeebug

This commit is contained in:
2024-04-29 12:19:46 +00:00
parent 1632870656
commit bf76de6590
5 changed files with 465 additions and 36 deletions

View File

@ -21,6 +21,12 @@ class FixLengthString {
}
return false;
}
bool operator==(const FixLengthString<length> &that) const {
for (size_t i = 0; i < length; i++) {
if (data[i] != that.data[i]) return false;
}
return true;
}
};
} // namespace bpt_basic_test
TEST(BasicTest, Compile) { // This Test only test the compile of the code
@ -217,7 +223,7 @@ TEST(BasicTest, Split_in_Put_Simple_3) {
const int str_len = 16;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
remove("/tmp/bpt5.db");
DiskManager *dm = new DiskManager("/tmp/bpt5.db");
BufferPoolManager *bpm = new BufferPoolManager(20, 3, dm);
@ -261,7 +267,7 @@ TEST(HarderTest, Split_in_Put_Harder_1) {
const int str_len = 1360 - 4;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
remove("/tmp/bpt6.db");
DiskManager *dm = new DiskManager("/tmp/bpt6.db");
BufferPoolManager *bpm = new BufferPoolManager(20, 3, dm);
@ -280,10 +286,13 @@ TEST(HarderTest, Split_in_Put_Harder_1) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -291,9 +300,11 @@ TEST(HarderTest, Split_in_Put_Harder_1) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -305,7 +316,7 @@ TEST(HarderTest, Split_in_Put_Harder_2) {
const int str_len = 2030;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
remove("/tmp/bpt7.db");
DiskManager *dm = new DiskManager("/tmp/bpt7.db");
BufferPoolManager *bpm = new BufferPoolManager(20, 3, dm);
@ -324,10 +335,13 @@ TEST(HarderTest, Split_in_Put_Harder_2) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -335,9 +349,11 @@ TEST(HarderTest, Split_in_Put_Harder_2) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -349,7 +365,7 @@ TEST(HarderTest, Split_in_Put_Harder_3) {
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",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt8.db";
std::vector<KeyType> keys;
const int ops = 1000;
@ -370,10 +386,13 @@ TEST(HarderTest, Split_in_Put_Harder_3) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -381,9 +400,11 @@ TEST(HarderTest, Split_in_Put_Harder_3) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -395,7 +416,7 @@ TEST(HarderTest, Split_in_Put_Harder_4) {
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",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt9.db";
std::vector<KeyType> keys;
const int ops = 1000;
@ -416,10 +437,13 @@ TEST(HarderTest, Split_in_Put_Harder_4) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -427,9 +451,11 @@ TEST(HarderTest, Split_in_Put_Harder_4) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -441,7 +467,7 @@ TEST(HarderTest, Split_in_Put_Harder_5) {
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",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt10.db";
std::vector<KeyType> keys;
const int ops = 15 + rnd() % 20;
@ -462,10 +488,13 @@ TEST(HarderTest, Split_in_Put_Harder_5) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -473,9 +502,11 @@ TEST(HarderTest, Split_in_Put_Harder_5) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -487,7 +518,7 @@ TEST(HarderTest, Split_in_Put_Harder_6) {
const int str_len = 1000;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt11.db";
std::vector<KeyType> keys;
const int ops = 15 + rnd() % 20;
@ -508,10 +539,13 @@ TEST(HarderTest, Split_in_Put_Harder_6) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -519,9 +553,11 @@ TEST(HarderTest, Split_in_Put_Harder_6) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -533,7 +569,7 @@ TEST(HarderTest, Split_in_Put_Harder_7) {
const int str_len = 2000;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt12.db";
std::vector<KeyType> keys;
const int ops = 15 + rnd() % 20;
@ -554,10 +590,13 @@ TEST(HarderTest, Split_in_Put_Harder_7) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -565,9 +604,11 @@ TEST(HarderTest, Split_in_Put_Harder_7) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -579,7 +620,7 @@ TEST(HarderTest, Split_in_Put_Harder_8) {
const int str_len = 1300;
typedef bpt_basic_test::FixLengthString<str_len> KeyType;
// fprintf(stderr, "sizeof(std::pair<KeyType, default_numeric_index_t>)=%lu\n",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt13.db";
std::vector<KeyType> keys;
const int ops = 15 + rnd() % 20;
@ -600,10 +641,13 @@ TEST(HarderTest, Split_in_Put_Harder_8) {
for (int i = 1; i <= ops; i++) {
bpt.Put(keys[i - 1], i + 3);
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
ASSERT_EQ(bpt.Size(), i);
}
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -611,9 +655,11 @@ TEST(HarderTest, Split_in_Put_Harder_8) {
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
ASSERT_EQ(bpt.Size(), ops);
for (int i = 1; i <= ops; i++) {
ASSERT_EQ(bpt.Get(keys[i - 1]), i + 3);
}
ASSERT_EQ(bpt.Size(), ops);
}
delete bpm;
delete dm;
@ -623,7 +669,7 @@ TEST(HarderTest, Split_in_Put_Harder_9) {
std::vector<std::pair<int, int>> entries;
const int kNumberOfKeys = 100000;
const unsigned int RndSeed = testing::GTEST_FLAG(random_seed);
std::mt19937 rnd(RndSeed);
std::mt19937 rnd(1);
for (int i = 0; i < kNumberOfKeys; i++) {
entries.push_back({i + 3, rnd()});
}
@ -636,11 +682,14 @@ TEST(HarderTest, Split_in_Put_Harder_9) {
for (int i = 0; i < kNumberOfKeys; i++) {
bpt.Put(entries[i].first, entries[i].second);
ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
ASSERT_EQ(bpt.Size(), i + 1);
}
ASSERT_EQ(bpt.Size(), kNumberOfKeys);
std::shuffle(entries.begin(), entries.end(), rnd);
for (int i = 0; i < kNumberOfKeys; i++) {
ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
}
ASSERT_EQ(bpt.Size(), kNumberOfKeys);
}
delete bpm;
delete dm;
@ -649,9 +698,78 @@ TEST(HarderTest, Split_in_Put_Harder_9) {
std::shuffle(entries.begin(), entries.end(), rnd);
{
BPlusTreeIndexer<long long, std::less<long long>> bpt(bpm);
ASSERT_EQ(bpt.Size(), kNumberOfKeys);
for (int i = 0; i < kNumberOfKeys; i++) {
ASSERT_EQ(bpt.Get(entries[i].first), entries[i].second);
}
ASSERT_EQ(bpt.Size(), kNumberOfKeys);
}
delete bpm;
delete dm;
dm = new DiskManager("/tmp/bpt14.db");
bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<long long, std::less<long long>> bpt(bpm);
ASSERT_EQ(bpt.Size(), kNumberOfKeys);
sort(entries.begin(), entries.end());
auto it_std = entries.begin();
auto it_bpt = bpt.lower_bound_const(entries[0].first);
for (int i = 0; i < kNumberOfKeys; i++) {
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(), kNumberOfKeys);
}
delete bpm;
delete dm;
}
TEST(RemoveTest, RM_1) {
const unsigned int RndSeed = testing::GTEST_FLAG(random_seed);
std::mt19937 rnd(RndSeed);
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",
// sizeof(std::pair<KeyType, default_numeric_index_t>));
const std::string db_file_name = "/tmp/bpt15.db";
remove(db_file_name.c_str());
std::vector<KeyType> keys;
const int max_keys = 10;
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;
key.data[6] = '\0';
keys.push_back(key);
}
std::sort(keys.begin(), keys.end());
DiskManager *dm = new DiskManager(db_file_name.c_str());
BufferPoolManager *bpm = new BufferPoolManager(20, 3, dm);
{
BPlusTreeIndexer<KeyType, std::less<KeyType>> bpt(bpm);
bpt.Put(keys[1], 4);
bpt.Put(keys[0], 3);
bpt.Put(keys[2], 5);
bpt.Put(keys[3], 6);
bpt.Put(keys[4], 7);
bpt.Remove(keys[1]);
ASSERT_EQ(bpt.Size(), 4);
auto it = bpt.lower_bound_const(keys[0]);
ASSERT_EQ(it.GetKey(), keys[0]);
ASSERT_EQ(it.GetValue(), 3);
++it;
ASSERT_EQ(it.GetKey(), keys[2]);
ASSERT_EQ(it.GetValue(), 5);
++it;
ASSERT_EQ(it.GetKey(), keys[3]);
ASSERT_EQ(it.GetValue(), 6);
++it;
ASSERT_EQ(it.GetKey(), keys[4]);
ASSERT_EQ(it.GetValue(), 7);
++it;
ASSERT_TRUE(it == bpt.end_const());
}
delete bpm;
delete dm;

88
test/t1_std.cpp Normal file
View File

@ -0,0 +1,88 @@
// 此程序仅用于对拍
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include "bpt/disk_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;
}
std::unordered_map<hash_t, std::set<int>> mp;
int main() {
std::fstream f("data.txt");
hash_t key;
int value;
while (f >> key >> value) {
mp[key].insert(value);
}
int n;
std::cin >> n;
std::string op;
while (n-- > 0) {
std::cin >> op;
if (op == "insert") {
std::string key;
int value;
std::cin >> key >> value;
mp[Hash(key)].insert(value);
} else if (op == "delete") {
std::string key;
int value;
std::cin >> key >> value;
hash_t hsh = Hash(key);
mp[hsh].erase(value);
if (mp[hsh].empty()) mp.erase(hsh);
} else if (op == "find") {
std::string key;
int value;
std::cin >> key;
hash_t hsh = Hash(key);
if (mp.find(hsh) == mp.end()) {
std::cout << "null";
} else {
for (auto &x : mp[hsh]) {
std::cout << x << ' ';
}
}
std::cout << '\n';
} else {
std::cout << "Unknown operation\n";
}
}
f.close();
remove("data.txt");
f.open("data.txt", std::ios::out);
for (auto &x : mp) {
for (auto &y : x.second) {
f << x.first << ' ' << y << '\n';
}
}
return 0;
}