Skip to content

Commit

Permalink
bloom_filter component unittests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
vinland-avalon committed Dec 24, 2022
1 parent 6d48d9e commit a53ce3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
11 changes: 6 additions & 5 deletions include/bloom_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@

class BloomFilter {
public:
static void CalculateFilterTable(MemTable &immutable_memtbale, std::string *dst) {
static void CalculateFilterTable(std::vector<std::string> &keys, std::string *dst) {
dst->resize(sizeof(size_t), 0);

char *array = &(*dst)[0];
int char_num = dst->size() / sizeof(char);

std::hash<std::string> hash_algorithm_;
size_t hash_table;
for (immutable_memtbale.ReachBegin(); immutable_memtbale.Curr(); immutable_memtbale.Next()) {
size_t hash_code = hash_algorithm_(immutable_memtbale.Curr()->GetKey());
for (auto &key : keys) {
size_t hash_code = hash_algorithm_(key);
char *hash_code_byte = (char *)&hash_code;
for (int i = 0; i < char_num; i++) {
array[i] |= hash_code_byte[i];
Expand All @@ -49,11 +50,12 @@ class BloomFilter {
char *array = &(filter)[0];
int char_num = filter.size() / sizeof(char);

std::hash<std::string> hash_algorithm_;
size_t hash_code = hash_algorithm_(key);
char *hash_code_byte = (char *)&hash_code;

for (int i = 0; i < char_num; i++) {
if ((array[i] ^ hash_code_byte[i]) != hash_code_byte[i]) {
if ((array[i] & hash_code_byte[i]) != hash_code_byte[i]) {
return false;
}
}
Expand All @@ -62,7 +64,6 @@ class BloomFilter {
}

private:
static std::hash<std::string> hash_algorithm_;
};

#endif
39 changes: 25 additions & 14 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,46 @@
* Copyright (c) 2022 by BohanWu [email protected], All Rights Reserved.
*/

#include "utils_for_time_operation.h"
#include "bloom_filter.h"
#include "lsm_kv_store.h"
#include "spdlog/spdlog.h"
#include "utils_for_file_operation.h"
#include "utils_for_time_operation.h"
#include <gtest/gtest.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include "spdlog/spdlog.h"
#include "lsm_kv_store.h"
using json = nlohmann::json;

TEST(FooTest, test0) {
EXPECT_EQ(0, 0);
EXPECT_EQ("a", "a");
}

TEST(UtilsForFileOperation, test){
TEST(UtilsForFileOperation, test) {
EXPECT_EQ(IsSolidDirectory("."), true);
EXPECT_EQ(IsSolidDirectory("invalid_dir"), false);
EXPECT_GT(GetFilenamesInDirectory(".").size(), 0);
EXPECT_EQ(IsFileExisting("test.cpp"), true);
EXPECT_EQ(IsFileExisting("invalid_file"), false);
}

TEST(PreWork, ClearDataDirectory){
TEST(PreWork, ClearDataDirectory) {
DeleteFilesInDir("./data");
}

TEST(WholeWorkingFlow, SetKey1){
TEST(BloomFilter, test) {
std::string bloom_table;
std::vector<std::string> keys;
std::string key1 = "key1";
std::string key2 = "key2";
keys.push_back(key1);
BloomFilter::CalculateFilterTable(keys, &bloom_table);
spdlog::info("[Test-Variable] BloomFilter bloom_table: {}", bloom_table);
EXPECT_EQ(BloomFilter::KeyMayExist(key1, bloom_table), true);
EXPECT_EQ(BloomFilter::KeyMayExist(key2, bloom_table), false);
}

TEST(WholeWorkingFlow, SetKey1) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
store->Set("key1", "300");
spdlog::info("[Test-Result]: try to get key1's value: {}", store->Get("key1"));
Expand All @@ -44,7 +57,7 @@ TEST(WholeWorkingFlow, SetKey1){
EXPECT_EQ(store->Get("key2"), "");
}

TEST(WholeWorkingFlow, NotSetKey1ButGetItFromSSD){
TEST(WholeWorkingFlow, NotSetKey1ButGetItFromSSD) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
// store->Set("key1", "300");
spdlog::info("[Test-Result]: try to get key1's value: {}", store->Get("key1"));
Expand All @@ -53,7 +66,7 @@ TEST(WholeWorkingFlow, NotSetKey1ButGetItFromSSD){
EXPECT_EQ(store->Get("key2"), "");
}

TEST(WholeWorkingFlow, AgainSetKey1){
TEST(WholeWorkingFlow, AgainSetKey1) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
store->Set("key1", "400");
spdlog::info("[Test-Result]: try to get key1's value: {}", store->Get("key1"));
Expand All @@ -62,7 +75,7 @@ TEST(WholeWorkingFlow, AgainSetKey1){
EXPECT_EQ(store->Get("key2"), "");
}

TEST(WholeWorkingFlow, AgainNotSetKey1ButGetItFromSSD){
TEST(WholeWorkingFlow, AgainNotSetKey1ButGetItFromSSD) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
// store->Set("key1", "400");
spdlog::info("[Test-Result]: try to get key1's value: {}", store->Get("key1"));
Expand All @@ -71,7 +84,7 @@ TEST(WholeWorkingFlow, AgainNotSetKey1ButGetItFromSSD){
EXPECT_EQ(store->Get("key2"), "");
}

TEST(WholeWorkingFlow, RemoveKey1){
TEST(WholeWorkingFlow, RemoveKey1) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
store->Remove("key1");
spdlog::info("[Test-Result]: try to get key1's value: {}", store->Get("key1"));
Expand All @@ -80,7 +93,7 @@ TEST(WholeWorkingFlow, RemoveKey1){
EXPECT_EQ(store->Get("key2"), "");
}

TEST(WholeWorkingFlow, NotRemoveKey1ButGetItFromSSD){
TEST(WholeWorkingFlow, NotRemoveKey1ButGetItFromSSD) {
auto store = std::shared_ptr<LsmKvStore<SkipListMemTable>>(new LsmKvStore<SkipListMemTable>("./data", 2, 1));
// store->Remove("key1");
store->Get("key1");
Expand All @@ -89,5 +102,3 @@ TEST(WholeWorkingFlow, NotRemoveKey1ButGetItFromSSD){
EXPECT_EQ(store->Get("key1"), "");
EXPECT_EQ(store->Get("key2"), "");
}


0 comments on commit a53ce3f

Please sign in to comment.