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

support Ser && Deser #40

Open
wants to merge 1 commit into
base: 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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ all: $(TEST)
clean:
rm -f $(TEST) */*.o

test: example/test.o $(LIBOBJECTS)
test: example/test.o $(LIBOBJECTS)
$(CC) example/test.o $(LIBOBJECTS) $(LDFLAGS) -o $@

test1: example/test1.o $(LIBOBJECTS)
$(CC) example/test1.o $(LIBOBJECTS) $(LDFLAGS) -o $@


%.o: %.cc ${HEADERS} Makefile
$(CC) $(CFLAGS) $< -o $@

Expand Down
75 changes: 75 additions & 0 deletions example/test1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "cuckoofilter.h"

#include <assert.h>
#include <math.h>

#include <iostream>
#include <fstream>
#include <vector>

using cuckoofilter::CuckooFilter;

template<typename T>
void fpr(T& t, size_t total_items) {
// Check non-existing items, a few false positives expected
size_t total_queries = 0;
size_t false_queries = 0;
for (size_t i = total_items; i < 2 * total_items; i++) {
if (t.Contain(i) == cuckoofilter::Ok) {
false_queries++;
}
total_queries++;
}

// Output the measured false positive rate
std::cout << "false positive rate is "
<< 100.0 * false_queries / total_queries << "%\n";
}

int main(int argc, char **argv) {
size_t total_items = 1000000;

// Create a cuckoo filter where each item is of type size_t and
// use 12 bits for each item:
// CuckooFilter<size_t, 12> filter(total_items);
// To enable semi-sorting, define the storage of cuckoo filter to be
// PackedTable, accepting keys of size_t type and making 13 bits
// for each key:
CuckooFilter<size_t, 13, cuckoofilter::PackedTable> filter(total_items);
// CuckooFilter<size_t, 12> filter(total_items);
using FilterType = decltype(filter);

// Insert items to this cuckoo filter
size_t num_inserted = 0;
for (size_t i = 0; i < total_items; i++, num_inserted++) {
if (filter.Add(i) != cuckoofilter::Ok) {
break;
}
}

std::cout << "actual num_inserted: " << num_inserted << std::endl;
num_inserted = 1000000;
std::cout << "num_inserted: " << num_inserted << std::endl;

// Check if previously inserted items are in the filter, expected
// true for all items
for (size_t i = 0; i < num_inserted; i++) {
if(filter.Contain(i) != cuckoofilter::Ok) {
std::cout << "I: " << i << " not ok" << std::endl;
break;
}
}

std::ofstream os("filter.meta", std::ios_base::binary);
filter.Serialize(os);
os.close();
fpr(filter, total_items);

FilterType filter1(total_items);
std::ifstream handler("filter.meta", std::ios_base::binary);
filter1.Deserialize(handler);
handler.close();
fpr(filter1, total_items);

return 0;
}
17 changes: 17 additions & 0 deletions src/cuckoofilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ class CuckooFilter {

~CuckooFilter() { delete table_; }

void Serialize(std::ofstream& handler) {
uint64_t bytes = sizeof(VictimCache);
std::cout << "Write from file: with size: " << bytes << std::endl;
handler.write(reinterpret_cast<char*>(&victim_), bytes);
table_->Serialize(handler);
hasher_.Serialize(handler);
}

void Deserialize(std::ifstream& handler) {
char* buffer = reinterpret_cast<char*>(&victim_);
uint64_t length = sizeof(VictimCache);
std::cout << "Read from file: with size: " << length << std::endl;
handler.read(buffer, length);
table_->Deserialize(handler);
hasher_.Deserialize(handler);
}

// Add an item to the filter.
Status Add(const ItemType &item);

Expand Down
48 changes: 48 additions & 0 deletions src/hashutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>

#include <string>

Expand Down Expand Up @@ -64,6 +66,27 @@ class TwoIndependentMultiplyShift {
uint64_t operator()(uint64_t key) const {
return (add_ + multiply_ * static_cast<decltype(multiply_)>(key)) >> 64;
}

void Serialize(std::ofstream& handler) {
uint64_t bytes = sizeof(multiply_);
handler.write(reinterpret_cast<char*>(&multiply_), bytes);
std::cout << "Write multiply_ to file: total bytes: " << bytes << std::endl;
bytes = sizeof(add_);
handler.write(reinterpret_cast<char*>(&add_), bytes);
std::cout << "Write add_ to file: total bytes: " << bytes << std::endl;
}

void Deserialize(std::ifstream& handler) {
char* buffer = reinterpret_cast<char*>(&multiply_);
uint64_t length = sizeof(multiply_);
std::cout << "Read multiply_ from file: with size: " << length << std::endl;
handler.read(buffer, length);
buffer = reinterpret_cast<char*>(&add_);
length = sizeof(add_);
std::cout << "Read add_ from file: with size: " << length << std::endl;
handler.read(buffer, length);
}

};

// See Patrascu and Thorup's "The Power of Simple Tabulation Hashing"
Expand All @@ -87,6 +110,31 @@ class SimpleTabulation {
}
return result;
}
void Serialize(std::ofstream& handler) {
int row = sizeof(uint64_t);
int col = (1 << CHAR_BIT);
uint64_t bytes = sizeof(uint64_t);
uint64_t total_bytes = row * col * bytes;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
handler.write(reinterpret_cast<char*>(&tables_[i][j]), bytes);
}
}
std::cout << "Write table_ to file: total bytes: " << total_bytes << std::endl;
}

void Deserialize(std::ifstream& handler) {
int row = sizeof(uint64_t);
int col = (1 << CHAR_BIT);
uint64_t bytes = sizeof(uint64_t);
uint64_t total_bytes = row * col * bytes;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
handler.read(reinterpret_cast<char*>(&tables_[i][j]), bytes);
}
}
std::cout << "Read table_ to file: total bytes: " << total_bytes << std::endl;
}
};
}

Expand Down
13 changes: 13 additions & 0 deletions src/packedtable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CUCKOO_FILTER_PACKED_TABLE_H_
#define CUCKOO_FILTER_PACKED_TABLE_H_

#include <iostream>
#include <fstream>
#include <sstream>
#include <utility>

Expand Down Expand Up @@ -426,6 +428,17 @@ class PackedTable {
return false;
}

void Serialize(std::ofstream& handler) {
std::cout << "Write to file: "<< "total bytes: " << len_ << std::endl;
handler.write(buckets_, len_);
}

void Deserialize(std::ifstream& handler) {
std::cout << "Read from file: with size: " << len_ << std::endl;
handler.read(buckets_, len_);
}


// inline size_t NumTagsInBucket(const size_t i) {
// size_t num = 0;
// for (size_t j = 0; j < tags_per_bucket; j++ ){
Expand Down
15 changes: 15 additions & 0 deletions src/singletable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <assert.h>

#include <fstream>
#include <iostream>
#include <sstream>

#include "bitsutil.h"
Expand Down Expand Up @@ -53,6 +55,19 @@ class SingleTable {
return kTagsPerBucket * num_buckets_;
}

void Serialize(std::ofstream& handler) {
uint64_t bytes = kBytesPerBucket * (num_buckets_ + kPaddingBuckets);
std::cout << "Write to file: "<< "total bytes: " << bytes << std::endl;
handler.write(reinterpret_cast<char*>(buckets_), bytes);
}

void Deserialize(std::ifstream& handler) {
char* buffer = reinterpret_cast<char*>(buckets_);
uint64_t length = kBytesPerBucket * (num_buckets_ + kPaddingBuckets);
std::cout << "Read from file: with size: " << length << std::endl;
handler.read(buffer, length);
}

std::string Info() const {
std::stringstream ss;
ss << "SingleHashtable with tag size: " << bits_per_tag << " bits \n";
Expand Down