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

RecSplit refactoring #2

Open
wants to merge 1 commit into
base: silkworm_master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions sux/function/RecSplit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,32 @@ template <size_t LEAF_SIZE, util::AllocType AT = util::AllocType::MALLOC> class
// For each bucket size, the Golomb-Rice parameter (upper 8 bits) and the number of bits to
// skip in the fixed part of the tree (lower 24 bits).
static constexpr array<uint32_t, MAX_BUCKET_SIZE> memo = fill_golomb_rice<LEAF_SIZE>();
static constexpr array<uint8_t, MAX_LEAF_SIZE> bij_midstop = fill_bij_midstop();

size_t bucket_size;
size_t nbuckets;
size_t keys_count;
RiceBitVector<AT> descriptors;
DoubleEF<AT> ef;
std::vector<hash128_t> key_hashes;

public:
RecSplit() {}

RecSplit(const size_t _keys_count, const size_t _bucket_size) {
this->bucket_size = _bucket_size;
this->keys_count = _keys_count;
key_hashes.reserve(this->keys_count);
}

void add_key(const string& key) {
key_hashes.push_back(first_hash(key.c_str(), key.size()));
}

void build() {
hash_gen(key_hashes.data());
key_hashes.clear();
}

/** Builds a RecSplit instance using a given list of keys and bucket size.
*
* **Warning**: duplicate keys will cause this method to never return.
Expand Down Expand Up @@ -448,7 +463,8 @@ template <size_t LEAF_SIZE, util::AllocType AT = util::AllocType::MALLOC> class
}

void recSplit(vector<uint64_t> &bucket, vector<uint64_t> &temp, size_t start, size_t end, typename RiceBitVector<AT>::Builder &builder, vector<uint32_t> &unary, const int level) {
const auto m = end - start;
static const array<uint8_t, MAX_LEAF_SIZE> bij_midstop = fill_bij_midstop();
const auto m = end - start;
assert(m > 1);
uint64_t x = start_seed[level];

Expand Down
8 changes: 4 additions & 4 deletions sux/util/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ template <typename T, AllocType AT = MALLOC> class Vector : public Expandable {
T *data = nullptr;

public:
Vector<T, AT>() = default;
Vector() = default;

explicit Vector<T, AT>(size_t length) { size(length); }
explicit Vector(size_t length) { size(length); }

explicit Vector<T, AT>(const T *data, size_t length) : Vector(length) { memcpy(this->data, data, length); }
explicit Vector(const T *data, size_t length) : Vector(length) { memcpy(this->data, data, length); }

~Vector<T, AT>() {
~Vector() {
if (data) {
if (AT == MALLOC) {
free(data);
Expand Down
30 changes: 30 additions & 0 deletions test/function/recsplit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,33 @@ TEST(recsplit_test, small_text_dump_and_load) {
recsplit_unit_test(rs_load, keys);
remove(filename);
}

TEST(recsplit_test, small_text_dump_and_load_one_by_one) {
vector<string> keys;
keys.push_back("a");
keys.push_back("b");
keys.push_back("c");
keys.push_back("d");

const char *filename = "test/test_dump";

RecSplit<8> rs_dump(keys.size(), 2);
for (size_t i = 0; i < keys.size(); i++) rs_dump.add_key(keys[i]);
rs_dump.build();

fstream fs;
fs.exceptions(fstream::failbit | fstream::badbit);
fs.open(filename, fstream::out | fstream::binary | fstream::trunc);
fs << rs_dump;
fs.close();

RecSplit<8> rs_load;
fs.open(filename, std::fstream::in | std::fstream::binary);
fs >> rs_load;
fs.close();

for (size_t i = 0; i < rs_dump.size(); i++) ASSERT_EQ(rs_dump(keys[i]), rs_load(keys[i]));

recsplit_unit_test(rs_load, keys);
remove(filename);
}