Skip to content

Changes marshal dump/load format to be size of in-memory bitmap #22

Merged
merged 1 commit into from Dec 5, 2012
View
1 .gitignore
@@ -7,3 +7,4 @@ ext/Makefile
lib/cbloomfilter.so
tmp
Gemfile.lock
+pkg
View
14 ext/cbloomfilter/cbloomfilter.c
@@ -361,13 +361,11 @@ static VALUE bf_bitmap(VALUE self) {
struct BloomFilter *bf;
Data_Get_Struct(self, struct BloomFilter, bf);
- VALUE str = rb_str_new(0, bf->m);
+ VALUE str = rb_str_new(0, bf->bytes);
unsigned char* ptr = (unsigned char *) RSTRING_PTR(str);
- int i;
- for (i = 0; i < bf->m; i++)
- *ptr++ = bucket_get(bf, i);
-
+ memcpy(ptr, bf->ptr, bf->bytes);
+
return str;
}
@@ -376,11 +374,7 @@ static VALUE bf_load(VALUE self, VALUE bitmap) {
Data_Get_Struct(self, struct BloomFilter, bf);
unsigned char* ptr = (unsigned char *) RSTRING_PTR(bitmap);
- int i;
- for (i = 0; i < bf->m; i++) {
- if (*ptr++)
- bucket_set(bf, i);
- }
+ memcpy(bf->ptr, ptr, bf->bytes);
return Qnil;
}
View
11 spec/native_spec.rb
@@ -140,5 +140,16 @@
bf.send(:same_parameters?, bf2).should be_true
end
+
+ it "should serialize to a file size proporational its bucket size" do
+ fs_size = 0
+ 8.times do |i|
+ bf = Native.new(size: 10_000, bucket: i+1)
+ bf.save('bf.out')
+ prev_size, fs_size = fs_size, File.size('bf.out')
+ prev_size.should < fs_size
+ end
+ end
+
end
end
Something went wrong with that request. Please try again.