Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add intrusive stringset DO NOT REVIEW #4705

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cxx_test(interpreter_test dfly_core LABELS DFLY)

cxx_test(string_set_test dfly_core LABELS DFLY)
cxx_test(string_map_test dfly_core LABELS DFLY)
cxx_test(intrusive_string_set_test dfly_core LABELS DFLY)
cxx_test(sorted_map_test dfly_core redis_test_lib LABELS DFLY)
cxx_test(bptree_set_test dfly_core LABELS DFLY)
cxx_test(score_map_test dfly_core LABELS DFLY)
Expand Down
181 changes: 181 additions & 0 deletions src/core/intrusive_string_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#pragma once

#include <cassert>
#include <cstring>
#include <memory>
#include <string_view>
#include <vector>

#include "base/hash.h"

namespace dfly {

class ISLEntry {
friend class IntrusiveStringList;

public:
ISLEntry() = default;

ISLEntry(char* data) {
data_ = data;
}

operator bool() const {
return data_;
}

std::string_view Key() const {
return {GetKeyData(), GetKeySize()};
}

private:
static ISLEntry Create(std::string_view key) {
char* next = nullptr;
uint32_t key_size = key.size();

auto size = sizeof(next) + sizeof(key_size) + key_size;

char* data = (char*)malloc(size);

std::memcpy(data, &next, sizeof(next));

auto* key_size_pos = data + sizeof(next);
std::memcpy(key_size_pos, &key_size, sizeof(key_size));

auto* key_pos = key_size_pos + sizeof(key_size);
std::memcpy(key_pos, key.data(), key_size);

return ISLEntry(data);
}

static void Destroy(ISLEntry entry) {
free(entry.data_);
}

// TODO make private
ISLEntry Next() const {
ISLEntry next;
std::memcpy(&next.data_, data_, sizeof(next));
return next;
}

// TODO make private
void SetNext(ISLEntry next) {
std::memcpy(data_, &next, sizeof(next));
next.data_ = nullptr;
}

const char* GetKeyData() const {
return data_ + sizeof(ISLEntry*) + sizeof(uint32_t);
}

uint32_t GetKeySize() const {
uint32_t size = 0;
std::memcpy(&size, data_ + sizeof(ISLEntry*), sizeof(size));
return size;
}

// TODO consider use SDS strings or other approach
// TODO add optimization for big keys
// memory daya layout [ISLEntry*, key_size, key]
char* data_ = nullptr;
};

class IntrusiveStringList {
public:
~IntrusiveStringList() {
while (start_) {
auto next = start_.Next();
ISLEntry::Destroy(start_);
start_ = next;
}
}

ISLEntry Emplace(std::string_view key) {
auto e = ISLEntry::Create(key);
e.SetNext(start_);
start_ = e;
return start_;
}

ISLEntry Find(std::string_view str) {
auto it = start_;
for (; it && it.Key() != str; it = it.Next())
;
return it;
}

private:
ISLEntry start_;
};

class IntrusiveStringSet {
public:
// TODO add TTL processing
ISLEntry Add(std::string_view str, uint32_t ttl_sec = UINT32_MAX) {
if (size_ >= entries_.size()) {
Grow();
}
auto bucket_id = BucketId(Hash(str));
auto& bucket = entries_[bucket_id];

if (auto existed_item = bucket.Find(str); existed_item) {
// TODO consider common implementation for key value pair
return ISLEntry();
}

return bucket.Emplace(str);
}

ISLEntry Find(std::string_view member) {
auto bucket_id = BucketId(Hash(member));
return entries_[bucket_id].Find(member);
}

// Returns the number of elements in the map. Note that it might be that some of these elements
// have expired and can't be accessed.
size_t UpperBoundSize() const {
return size_;
}

bool Empty() const {
return size_ == 0;
}

private:
std::uint32_t Capacity() const {
return 1 << capacity_log_;
}

void Grow() {
++capacity_log_;
entries_.resize(Capacity());

// TODO rehashing
}

uint32_t BucketId(uint64_t hash) const {
assert(capacity_log_ > 0);
return hash >> (64 - capacity_log_);
}

uint64_t Hash(std::string_view str) const {
constexpr XXH64_hash_t kHashSeed = 24061983;
return XXH3_64bits_withSeed(str.data(), str.size(), kHashSeed);
}

private:
static constexpr size_t kMinSizeShift = 2;
std::uint32_t capacity_log_ = 1;
std::uint32_t size_ = 0; // number of elements in the set.

static_assert(sizeof(IntrusiveStringList) == sizeof(void*),
"IntrusiveStringList should be just a pointer");
std::vector<IntrusiveStringList> entries_;
};

} // namespace dfly
46 changes: 46 additions & 0 deletions src/core/intrusive_string_set_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2022, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#include "core/intrusive_string_set.h"

#include "base/gtest.h"

namespace dfly {

using namespace std;

class IntrusiveStringSetTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
}

static void TearDownTestSuite() {
}

void SetUp() override {
}

void TearDown() override {
}
};

TEST_F(IntrusiveStringSetTest, IntrusiceStringListTest) {
IntrusiveStringList isl;
ISLEntry test = isl.Emplace("0123456789");

EXPECT_EQ(test.Key(), "0123456789"sv);

test = isl.Emplace("123456789");

EXPECT_EQ(test.Key(), "123456789"sv);

test = isl.Emplace("23456789");

EXPECT_EQ(isl.Find("0123456789").Key(), "0123456789"sv);
EXPECT_EQ(isl.Find("23456789").Key(), "23456789"sv);
EXPECT_EQ(isl.Find("123456789").Key(), "123456789"sv);
EXPECT_EQ(isl.Find("test"), ISLEntry());
}

} // namespace dfly
Loading